misc: display booked places in manager view

(this factors the booked_place annotation into a new manager for the Event
object)
This commit is contained in:
Frédéric Péters 2016-02-13 17:52:32 +01:00
parent b14ca48427
commit f7f7c2ef26
4 changed files with 28 additions and 7 deletions

View File

@ -16,6 +16,7 @@
from django.core.urlresolvers import reverse
from django.db import models
from django.db.models import Count
from django.utils.text import slugify
from django.utils.translation import ugettext_lazy as _
@ -48,11 +49,19 @@ class Agenda(models.Model):
return reverse('chrono-manager-agenda-view', kwargs={'pk': self.id})
class EventManager(models.Manager):
def get_queryset(self):
return super(EventManager, self).get_queryset().annotate(
booked_places=Count('booking'))
class Event(models.Model):
agenda = models.ForeignKey(Agenda)
start_datetime = models.DateTimeField(_('Date/time'))
places = models.PositiveIntegerField(_('Places'))
objects = EventManager()
class Meta:
ordering = ['agenda', 'start_datetime']

View File

@ -14,7 +14,7 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from django.db.models import F, Count
from django.db.models import F
from django.utils.translation import ugettext as _
from rest_framework import serializers, status
@ -29,8 +29,7 @@ class Datetimes(GenericAPIView):
response = {'data': [{
'id': x.id,
'text': x.start_datetime.strftime(_('%Y-%m-%d %H:%M'))}
for x in Event.objects.filter(agenda=pk).annotate(
booked_places=Count('booking')).filter(
for x in Event.objects.filter(agenda=pk).filter(
places__gt=F('booked_places'))
]}
return Response(response)
@ -46,7 +45,7 @@ class Fillslot(GenericAPIView):
serializer_class = SlotSerializer
def post(self, request, agenda_pk=None, event_pk=None, format=None):
event = Event.objects.filter(id=event_pk).annotate(booked_places=Count('booking'))[0]
event = Event.objects.filter(id=event_pk)[0]
if event.booked_places >= event.places:
return Response({'err': 1, 'reason': 'sold out'}, status.HTTP_400_BAD_REQUEST)
booking = Booking(event_id=event_pk, extra_data=request.data)

View File

@ -24,8 +24,8 @@
<ul class="objects-list single-links">
{% for event in object.event_set.all %}
<li><a rel="popup" href="{% url 'chrono-manager-event-edit' pk=event.id %}">
{% blocktrans with start=event.start_datetime places=event.places%}
{{ start }} ({{ places }} places)
{% blocktrans with start=event.start_datetime places=event.places booked_places=event.booked_places %}
{{ start }} ({{ places }} places, {{ booked_places }} booked places)
{% endblocktrans %}</a>
</li>
{% endfor %}

View File

@ -5,7 +5,7 @@ from webtest import TestApp
from chrono.wsgi import application
from chrono.agendas.models import Agenda, Event
from chrono.agendas.models import Agenda, Event, Booking
pytestmark = pytest.mark.django_db
@ -119,3 +119,16 @@ def test_edit_event(admin_user):
assert '/manage/events/1/' in resp.body
assert 'Feb. 16, 2016, 5 p.m.' in resp.body
assert '20 places' in resp.body
def test_booked_places(admin_user):
agenda = Agenda(label=u'Foo bar')
agenda.save()
event = Event(start_datetime=datetime.datetime(2016, 2, 15, 17, 0),
places=10, agenda=agenda)
event.save()
Booking(event=event).save()
Booking(event=event).save()
app = login(TestApp(application))
resp = app.get('/manage/agendas/1/', status=200)
assert '10 places' in resp.body
assert '2 booked places' in resp.body