pretixdroid: add status endpoint (#351)

This commit is contained in:
Raphael Michel
2016-12-08 22:38:17 +01:00
committed by GitHub
parent fb19891473
commit 8e4eb52386
4 changed files with 167 additions and 3 deletions

View File

@@ -9,4 +9,6 @@ urlpatterns = [
name='api.redeem'),
url(r'^pretixdroid/api/(?P<organizer>[^/]+)/(?P<event>[^/]+)/search/', views.ApiSearchView.as_view(),
name='api.search'),
url(r'^pretixdroid/api/(?P<organizer>[^/]+)/(?P<event>[^/]+)/status/', views.ApiStatusView.as_view(),
name='api.status'),
]

View File

@@ -3,7 +3,7 @@ import logging
import string
from django.db import transaction
from django.db.models import Q
from django.db.models import Count, Q
from django.http import (
HttpResponseForbidden, HttpResponseNotFound, JsonResponse,
)
@@ -133,3 +133,66 @@ class ApiSearchView(ApiView):
response['results'] = []
return JsonResponse(response)
class ApiStatusView(ApiView):
def get(self, request, **kwargs):
response = {
'version': API_VERSION,
'checkins': Checkin.objects.filter(
position__order__event=self.event
).count(),
'total': OrderPosition.objects.filter(
order__event=self.event, order__status=Order.STATUS_PAID
).count()
}
op_by_item = {
p['item']: p['cnt']
for p in OrderPosition.objects.filter(
order__event=self.event,
order__status=Order.STATUS_PAID
).order_by().values('item').annotate(cnt=Count('id'))
}
op_by_variation = {
p['variation']: p['cnt']
for p in OrderPosition.objects.filter(
order__event=self.event,
order__status=Order.STATUS_PAID
).order_by().values('variation').annotate(cnt=Count('id'))
}
c_by_item = {
p['position__item']: p['cnt']
for p in Checkin.objects.filter(
position__order__event=self.event,
position__order__status=Order.STATUS_PAID
).order_by().values('position__item').annotate(cnt=Count('id'))
}
c_by_variation = {
p['position__variation']: p['cnt']
for p in Checkin.objects.filter(
position__order__event=self.event,
position__order__status=Order.STATUS_PAID
).order_by().values('position__variation').annotate(cnt=Count('id'))
}
response['items'] = []
for item in self.event.items.prefetch_related('variations'):
i = {
'id': item.pk,
'name': str(item),
'admission': item.admission,
'checkins': c_by_item.get(item.pk, 0),
'total': op_by_item.get(item.pk, 0),
'variations': []
}
for var in item.variations.all():
i['variations'].append({
'id': var.pk,
'name': str(var),
'checkins': c_by_variation.get(var.pk, 0),
'total': op_by_variation.get(var.pk, 0),
})
response['items'].append(i)
return JsonResponse(response)

View File

@@ -5,7 +5,7 @@ import pytest
from django.utils.timezone import now
from pretix.base.models import (
Event, EventPermission, Item, ItemVariation, Order, OrderPosition,
Checkin, Event, EventPermission, Item, ItemVariation, Order, OrderPosition,
Organizer, User,
)
@@ -111,7 +111,44 @@ def test_search(client, env):
env[0].settings.set('pretixdroid_key', 'abcdefg')
resp = client.get('/pretixdroid/api/%s/%s/search/?key=%s&query=%s' % (
env[0].organizer.slug, env[0].slug, 'abcdefg', '567891'))
print(resp.content)
jdata = json.loads(resp.content.decode("utf-8"))
assert len(jdata['results']) == 1
assert jdata['results'][0]['secret'] == '5678910'
@pytest.mark.django_db
def test_status(client, env):
env[0].settings.set('pretixdroid_key', 'abcdefg')
Checkin.objects.create(position=env[3])
resp = client.get('/pretixdroid/api/%s/%s/status/?key=%s' % (
env[0].organizer.slug, env[0].slug, 'abcdefg'))
jdata = json.loads(resp.content.decode("utf-8"))
assert jdata['checkins'] == 1
assert jdata['total'] == 2
assert jdata['items'] == [
{'name': 'T-Shirt',
'id': 1,
'checkins': 1,
'admission': False,
'total': 1,
'variations': [
{'name': 'Red',
'id': 1,
'checkins': 1,
'total': 1
},
{'name': 'Blue',
'id': 2,
'checkins': 0,
'total': 0
}
]
},
{'name': 'Ticket',
'id': 2,
'checkins': 0,
'admission': False,
'total': 1,
'variations': []
}
]