general: allow a custom label on events (#11112)

This commit is contained in:
Frédéric Péters 2016-06-18 13:28:29 +02:00
parent e19bc3d1ab
commit 308bd37ecd
5 changed files with 37 additions and 2 deletions

View File

@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('agendas', '0004_booking_cancellation_datetime'),
]
operations = [
migrations.AddField(
model_name='event',
name='label',
field=models.CharField(help_text='Optional label to identify this date.', max_length=50, null=True, blank=True, verbose_name='Label'),
),
]

View File

@ -17,7 +17,9 @@
from django.core.urlresolvers import reverse
from django.db import models
from django.db.models.expressions import RawSQL
from django.utils.formats import date_format
from django.utils.text import slugify
from django.utils.timezone import localtime
from django.utils.translation import ugettext_lazy as _
from jsonfield import JSONField
@ -62,12 +64,18 @@ class Event(models.Model):
agenda = models.ForeignKey(Agenda)
start_datetime = models.DateTimeField(_('Date/time'))
places = models.PositiveIntegerField(_('Places'))
label = models.CharField(_('Label'), max_length=50, null=True, blank=True,
help_text=_('Optional label to identify this date.'))
objects = EventManager()
class Meta:
ordering = ['agenda', 'start_datetime']
def __unicode__(self):
if self.label:
return self.label
return date_format(localtime(self.start_datetime), format='DATETIME_FORMAT')
class Booking(models.Model):
event = models.ForeignKey(Event)

View File

@ -15,7 +15,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from django.db.models import F
from django.utils.formats import date_format
from django.utils.timezone import localtime, now
from rest_framework import permissions, serializers, status
@ -31,7 +30,7 @@ class Datetimes(GenericAPIView):
min_datetime = now()
response = {'data': [{
'id': x.id,
'text': date_format(localtime(x.start_datetime), format='DATETIME_FORMAT')}
'text': unicode(x)}
for x in Event.objects.filter(agenda=pk).filter(
places__gt=F('booked_places'),
start_datetime__gte=min_datetime)

View File

@ -24,6 +24,7 @@
<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 %}">
{% if event.label %}{{event.label}} / {% endif %}
{% blocktrans with start=event.start_datetime places=event.places booked_places=event.booked_places %}
{{ start }} ({{ places }} places, {{ booked_places }} booked places)
{% endblocktrans %}</a>

View File

@ -61,6 +61,14 @@ def test_datetime_api_fr(app, some_data):
assert resp.json['data'][0]['text'].endswith(' 17:00')
assert 'data' in resp.json
def test_datetime_api_label(app, some_data):
agenda_id = Agenda.objects.filter(label=u'Foo bar2')[0].id
event = Event.objects.filter(agenda=agenda_id)[0]
event.label = 'Hello world'
event.save()
resp = app.get('/api/agenda/%s/datetimes/' % agenda_id)
assert 'Hello world' in [x['text'] for x in resp.json['data']]
def test_booking_api(app, some_data, user):
agenda_id = Agenda.objects.filter(label=u'Foo bar')[0].id
event = Event.objects.filter(agenda_id=agenda_id)[0]