Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Admin Sales API #7322

Merged
merged 3 commits into from
Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions app/api/admin_sales/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ class Meta:
inflect = dasherize

id = fields.String()
identifier = fields.String()
name = fields.String()
created_at = fields.DateTime()
starts_at = fields.DateTime()
ends_at = fields.DateTime()
payment_currency = fields.String()
payment_country = fields.String()
sales = fields.Method('calc_sales')

@staticmethod
Expand All @@ -38,7 +41,7 @@ def calc_sales(obj):
Returns sales (dictionary with total sales and ticket count) for
placed, completed and pending orders
"""
return summary(obj.orders)
return summary(obj)


class AdminSalesByEventsList(ResourceList):
Expand All @@ -48,7 +51,7 @@ class AdminSalesByEventsList(ResourceList):
"""

def query(self, _):
return self.session.query(Event).outerjoin(Order).outerjoin(OrderTicket)
return Event.query

methods = ['GET']
decorators = (api.has_permission('is_admin'),)
Expand Down
2 changes: 1 addition & 1 deletion app/api/admin_sales/marketer.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def calc_sales(obj):
Returns sales (dictionary with total sales and ticket count) for
placed, completed and pending orders
"""
return summary(obj.orders)
return summary(obj)


class AdminSalesByMarketerList(ResourceList):
Expand Down
2 changes: 1 addition & 1 deletion app/api/admin_sales/organizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def calc_sales(obj):
Returns sales (dictionary with total sales and ticket count) for
placed, completed and pending orders
"""
return summary(obj.orders)
return summary(obj)


class AdminSalesByOrganizersList(ResourceList):
Expand Down
38 changes: 33 additions & 5 deletions app/api/admin_sales/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,50 @@
This module contains common sales calculations that are used throughout the
admin section
"""
from sqlalchemy import func

from app.models.order import Order
from app.models.ticket_holder import TicketHolder

def status_summary(orders, status):

def status_summary(sales_summary, tickets_summary, status):
"""
Groups orders by status and returns the total sales and ticket count as a
dictionary
"""
sales = 0
tickets = 0

for sale_status, sale in sales_summary:
if sale_status == status:
sales = sale

for ticket_status, ticket in tickets_summary:
if ticket_status == status:
tickets = ticket

return {
'sales_total': sum(o.amount for o in orders if o.status == status),
'ticket_count': sum(o.tickets_count for o in orders if o.status == status),
'sales_total': sales,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Black would make changes.

'ticket_count': tickets,
}


def summary(orders):
def summary(event):
"""
Returns sales as dictionary for all status codes
"""
sales_summary = (
Order.query.filter_by(event_id=event.id)
.with_entities(Order.status, func.sum(Order.amount))
.group_by(Order.status)
.all()
)
tickets_summary = (
TicketHolder.query.join(Order)
.filter(Order.event_id == event.id)
.with_entities(Order.status, func.count())
.group_by(Order.status)
.all()
)
status_codes = ['placed', 'completed', 'pending']
return {s: status_summary(orders, s) for s in status_codes}
return {s: status_summary(sales_summary, tickets_summary, s) for s in status_codes}