Skip to content

Commit

Permalink
Merge pull request #2486 from FJNR-inc/develop
Browse files Browse the repository at this point in the history
new release
  • Loading branch information
MelanieFJNR authored Jul 1, 2024
2 parents 4c725db + 6aa534f commit b7db2f4
Show file tree
Hide file tree
Showing 4 changed files with 244 additions and 67 deletions.
2 changes: 1 addition & 1 deletion log_management/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def anonymize_data(cls, start_date=None, end_date=None, targetIds=None):
session_uuid_matching = {}
queryset = cls.objects.filter(id__in=targetIds) if targetIds else cls.objects.all()
if start_date and end_date:
queryset = queryset.objects.filter(
queryset = queryset.filter(
created__gte=start_date,
created__lte=end_date,
)
Expand Down
78 changes: 52 additions & 26 deletions store/exports.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,35 @@
LOCAL_TIMEZONE = pytz.timezone(settings.TIME_ZONE)


def _get_coupon_export_usage(coupon):
usages_per_order = {}
for line in OrderLine.objects.filter(coupon=coupon):
is_refunded = Refund.objects.filter(orderline=line).exists()
if is_refunded: continue
if line.order.id not in usages_per_order:
usages_per_order[line.order.id] = {
'date': line.order.transaction_date.
astimezone(LOCAL_TIMEZONE).
strftime("%Y-%m-%d %H:%M:%S"),
'user': line.order.user,
'amount_used': line.coupon_real_value,
'product_name': set(),
}
usages_per_order[line.order.id]['product_name'].add(
line.content_object.name)
else:
usages_per_order[line.order.id]['amount_used'] += \
line.coupon_real_value
usages_per_order[line.order.id]['product_name'].add(
line.content_object.name
)
display_usages = []
for key, value in usages_per_order.items():
value['product_name'] = ', '.join(list(value['product_name']))
display_usages.append(value)
return display_usages


@shared_task()
def generate_coupon_usage(admin_id, coupon_id):
"""
Expand All @@ -40,38 +69,35 @@ def generate_coupon_usage(admin_id, coupon_id):
'Numéro étudiant',
'Code programme académique',
'Valeur utilisée',
'Élément associé',
'Éléments associés',
'Date d\'utilisation',
]
writer.writerow(header)

coupon = Coupon.objects.get(pk=coupon_id)
usages = _get_coupon_export_usage(coupon)

for line in OrderLine.objects.filter(coupon=coupon):
is_refunded = Refund.objects.filter(orderline=line).exists()
if not is_refunded:
line_array = [None] * len(header)
user = line.order.user
line_array[0] = user.id
line_array[1] = user.email
university = user.university
line_array[2] = university.name if university else ''
academic_field = user.academic_field
line_array[3] = academic_field.name if academic_field else ''
academic_level = user.academic_level
line_array[4] = academic_level.name if academic_level else ''
line_array[5] = user.first_name
line_array[6] = user.last_name
line_array[7] = user.gender
line_array[8] = user.city
line_array[9] = user.student_number
line_array[10] = user.academic_program_code
line_array[11] = line.coupon_real_value
line_array[12] = line.content_object.name
line_array[13] = (line.order.transaction_date.
astimezone(LOCAL_TIMEZONE).
strftime("%Y-%m-%d %H:%M:%S"))
writer.writerow(line_array)
for usage in usages:
line_array = [None] * len(header)
user = usage['user']
line_array[0] = user.id
line_array[1] = user.email
university = user.university
line_array[2] = university.name if university else ''
academic_field = user.academic_field
line_array[3] = academic_field.name if academic_field else ''
academic_level = user.academic_level
line_array[4] = academic_level.name if academic_level else ''
line_array[5] = user.first_name
line_array[6] = user.last_name
line_array[7] = user.gender
line_array[8] = user.city
line_array[9] = user.student_number
line_array[10] = user.academic_program_code
line_array[11] = usage['amount_used']
line_array[12] = usage['product_name']
line_array[13] = usage['date']
writer.writerow(line_array)

date_file = LOCAL_TIMEZONE.localize(datetime.now()) \
.strftime("%Y%m%d")
Expand Down
93 changes: 54 additions & 39 deletions store/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
create_external_payment_profile,
create_external_card,
get_external_cards,
PAYSAFE_CARD_TYPE,)
PAYSAFE_CARD_TYPE, )

User = get_user_model()

Expand Down Expand Up @@ -79,7 +79,6 @@ def to_representation(self, instance):


class OrderLineBaseProductSerializer(serializers.ModelSerializer):

id = serializers.IntegerField()
quantity = serializers.IntegerField()
metadata = serializers.JSONField(required=False)
Expand Down Expand Up @@ -117,7 +116,7 @@ class SimpleBaseProductSerializer(serializers.HyperlinkedModelSerializer):
product_type = serializers.SerializerMethodField()

def get_product_type(self, obj):
return BaseProduct.objects.get_subclass(id=obj.id).\
return BaseProduct.objects.get_subclass(id=obj.id). \
__class__.__name__.lower()

class Meta:
Expand Down Expand Up @@ -486,7 +485,7 @@ def to_representation(self, instance: OrderLine):
option: BaseProduct
for option in options:
option_id = option.id
option_data = option.orderlinebaseproduct_set.\
option_data = option.orderlinebaseproduct_set. \
get(order_line_id=instance.id)

option_data = {
Expand Down Expand Up @@ -570,8 +569,8 @@ def create(self, validated_data):
else:
raise serializers.ValidationError({
'non_field_errors': [_(
"You don't have the permission to create "
"an order for another user."
"You don't have the permission to create "
"an order for another user."
)]
})
if 'bypass_payment' in validated_data.keys():
Expand Down Expand Up @@ -760,7 +759,7 @@ def create(self, validated_data):
'non_field_errors': [_(
"You already are registered "
"to this timeslot: ") + str(timeslot) + "."
]
]
})
if (timeslot.period.workplace and
timeslot.period.workplace.seats - reserved > 0):
Expand Down Expand Up @@ -1047,7 +1046,7 @@ def to_representation(self, instance):
data = super(OrderSerializer, self).to_representation(instance)
# TTC is in cents after serialization
data['total_cost_with_taxes'] = round(
data['total_cost_with_taxes']/100, 2)
data['total_cost_with_taxes'] / 100, 2)
data['taxes'] = data['total_cost_with_taxes'] - data['total_cost']
return data

Expand Down Expand Up @@ -1162,47 +1161,63 @@ def update(self, instance, validated_data):

return super(CouponSerializer, self).update(instance, validated_data)

def to_representation(self, instance):
data = super(CouponSerializer, self).to_representation(instance)
from workplace.serializers import TimeSlotSerializer
def get_coupon_usage(self, instance):
"""
Get coupon usage per order, listing elements for frontend
"""
from blitz_api.serializers import (
OrganizationSerializer,
ReservationUserSerializer,
)
usages_per_order = {}
for line in OrderLine.objects.filter(coupon=instance):
is_refunded = Refund.objects.filter(orderline=line).exists()
if is_refunded: continue
if line.order.id not in usages_per_order:
usages_per_order[line.order.id] = {
'date': line.order.transaction_date,
'user': ReservationUserSerializer(
line.order.user,
context={
'request': self.context['request'],
'view': self.context['view'],
},
).data,
'amount_used': line.coupon_real_value,
'user_university': OrganizationSerializer(
line.order.user.university,
context={
'request': self.context['request'],
'view': self.context['view'],
},
).data,
'product_name': set(),
}
usages_per_order[line.order.id]['product_name'].add(
line.content_object.name)
else:
usages_per_order[line.order.id]['amount_used'] += \
line.coupon_real_value
usages_per_order[line.order.id]['product_name'].add(
line.content_object.name
)
display_usages = []
for key, value in usages_per_order.items():
value['product_name'] = ', '.join(list(sorted(value['product_name'])))
display_usages.append(value)
return display_usages

def to_representation(self, instance):
data = super(CouponSerializer, self).to_representation(instance)
from workplace.serializers import TimeSlotSerializer
from blitz_api.serializers import OrganizationSerializer
from retirement.serializers import (
RetreatSerializer,
RetreatTypeSerializer,
)
action = self.context['view'].action
if action == 'retrieve' or action == 'list':

usages = list()
for line in OrderLine.objects.filter(coupon=instance):
is_refunded = Refund.objects.filter(orderline=line).exists()
usages.append(
{
'date': line.order.transaction_date,
'user': ReservationUserSerializer(
line.order.user,
context={
'request': self.context['request'],
'view': self.context['view'],
},
).data,
'amount_used': line.coupon_real_value,
'user_university': OrganizationSerializer(
line.order.user.university,
context={
'request': self.context['request'],
'view': self.context['view'],
},
).data,
'product_name': line.content_object.name,
'orderline_refunded': is_refunded,
}
)

data['usages'] = usages
data['usages'] = self.get_coupon_usage(instance)
data['applicable_retreats'] = RetreatSerializer(
instance.applicable_retreats,
many=True,
Expand Down
Loading

0 comments on commit b7db2f4

Please sign in to comment.