Skip to content

Commit

Permalink
Merge pull request #176 from FJNR-inc/develop
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
RignonNoel authored Sep 12, 2019
2 parents db4e38a + 78504ea commit b62682a
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 5 deletions.
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ pycodestyle==2.5.0
coveralls==1.8.2
responses==0.10.6
six==1.12.0
awscli==1.16.234
awscli==1.16.237
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Pillow==6.1.0
django-simple-history==2.7.3
djangorestframework-filters==0.11.1
python-decouple==3.1
django-storages==1.7.1
django-storages==1.7.2
dj_database_url==0.5.0
zappa==0.47.0
psycopg2==2.7.5
Expand Down
51 changes: 50 additions & 1 deletion retirement/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from import_export.widgets import (ForeignKeyWidget, ManyToManyWidget,
DateTimeWidget)

from store.models import OrderLine
from store.models import OrderLine, OrderLineBaseProduct, OptionProduct
from .models import Reservation, Retreat

User = get_user_model()
Expand Down Expand Up @@ -263,3 +263,52 @@ class Meta:
'city',
'phone'
)


class OptionProductResource(resources.ModelResource):

last_name = fields.Field(
column_name='last_name',
attribute='order_line',
widget=ForeignKeyWidget(User, 'order__user__last_name'),
)

first_name = fields.Field(
column_name='first_name',
attribute='order_line',
widget=ForeignKeyWidget(User, 'order__user__first_name'),
)

email = fields.Field(
column_name='email',
attribute='order_line',
widget=ForeignKeyWidget(User, 'order__user__email'),
)

option_name = fields.Field(
column_name='name',
attribute='option',
widget=ForeignKeyWidget(OptionProduct, 'name')
)

quantity = fields.Field(
column_name='quantity',
attribute='quantity'
)

class Meta:
model = OrderLineBaseProduct
fields = (
'last_name',
'first_name',
'email',
'option_name',
'quantity'
)
export_order = (
'last_name',
'first_name',
'email',
'option_name',
'quantity'
)
37 changes: 35 additions & 2 deletions retirement/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
from blitz_api.models import ExportMedia
from blitz_api.serializers import ExportMediaSerializer
from store.exceptions import PaymentAPIError
from store.models import Refund
from store.models import Refund, OptionProduct, OrderLineBaseProduct
from store.services import refund_amount, PAYSAFE_EXCEPTION

from . import permissions, serializers
from .models import (Picture, Reservation, Retreat, WaitQueue,
WaitQueueNotification, RetreatInvitation)
from .resources import (ReservationResource, RetreatResource,
WaitQueueNotificationResource, WaitQueueResource,
RetreatReservationResource)
RetreatReservationResource, OptionProductResource)
from .services import (send_retreat_7_days_email,
send_post_retreat_email, )

Expand Down Expand Up @@ -216,6 +216,39 @@ def export_participation(self, request, pk=None):

return response

@action(detail=True, permission_classes=[IsAdminUser])
def export_options(self, request, pk=None):

retreat: Retreat = self.get_object()
# Order queryset by ascending id, thus by descending age too
queryset = OrderLineBaseProduct.objects.filter(
order_line__object_id=retreat.id,
order_line__content_type__model='retreat')
# Build dataset using paginated queryset
dataset = OptionProductResource().export(queryset)

date_file = LOCAL_TIMEZONE.localize(datetime.now()) \
.strftime("%Y%m%d-%H%M%S")
filename = f'export-option-{retreat.name}-{date_file}.xls'

new_exprt = ExportMedia.objects.create()
content = ContentFile(dataset.xls)
new_exprt.file.save(filename, content)

export_url = ExportMediaSerializer(
new_exprt,
context={'request': request}
).data.get('file')

response = Response(
status=status.HTTP_200_OK,
data={
'file_url': export_url
}
)

return response


class PictureViewSet(viewsets.ModelViewSet):
"""
Expand Down

0 comments on commit b62682a

Please sign in to comment.