Add checkbox for "is online" on events
authorMagnus Hagander <magnus@hagander.net>
Wed, 30 Jan 2013 13:07:48 +0000 (14:07 +0100)
committerMagnus Hagander <magnus@hagander.net>
Wed, 30 Jan 2013 13:07:48 +0000 (14:07 +0100)
This gets rid of the annoying requirement to specify things like
"online, United States" for events that are online.

This requires some SQL to run on existing installations:
ALTER TABLE events_event ADD COLUMN isonline boolean NOT NULL DEFAULT 'f';
ALTER TABLE events_event ALTER COLUMN isonline DROP DEFAULT;
ALTER TABLE events_event ALTER COLUMN country_id DROP NOT NULL;

Fixes #166

pgweb/events/admin.py
pgweb/events/forms.py
pgweb/events/models.py

index d876cce299bbee78c6388e6f5ad9a5d09db07a01..e7a722f8c9c4983116f44c8c3ae6058782cc2221 100644 (file)
@@ -1,4 +1,5 @@
 from django.contrib import admin
+from django import forms
 
 from util.admin import PgwebAdmin
 from models import *
@@ -11,10 +12,27 @@ def approve_event(modeladmin, request, queryset):
                e.save()
 approve_event.short_description = 'Approve event'
 
+class EventAdminForm(forms.ModelForm):
+       class Meta:
+               model = Event
+
+       def clean(self):
+               cleaned_data = super(EventAdminForm, self).clean()
+               if not cleaned_data.get('isonline'):
+                       if not cleaned_data.get('city'):
+                               self._errors['city'] = self.error_class(['City must be specified for non-online events'])
+                               del cleaned_data['city']
+                       if not cleaned_data.get('country'):
+                               self._errors['country'] = self.error_class(['Country must be specified for non-online events'])
+                               del cleaned_data['country']
+               return cleaned_data
+
 class EventAdmin(PgwebAdmin):
        list_display = ('title', 'org', 'startdate', 'training', 'approved',)
        list_filter = ('approved','training',)
        search_fields = ('summary', 'details', 'title', )
        actions = [approve_event, ]
+       form = EventAdminForm
+
 
 admin.site.register(Event, EventAdmin)
index 02b5c30dd7894e28625ffa09412227740f1d07db..1e4dbfcdd36aac172e6b493899169eaf404587a5 100644 (file)
@@ -4,10 +4,30 @@ from pgweb.core.models import Organisation
 from models import Event
 
 class EventForm(forms.ModelForm):
+       toggle_fields = [{
+                       'name': 'isonline',
+                       'invert': True,
+                       'fields': ['city', 'state', 'country',]
+                       },
+                                        ]
        def __init__(self, *args, **kwargs):
                super(EventForm, self).__init__(*args, **kwargs)
        def filter_by_user(self, user):
                self.fields['org'].queryset = Organisation.objects.filter(managers=user, approved=True)
+
+       def clean(self):
+               cleaned_data = super(EventForm, self).clean()
+               if not cleaned_data.get('isonline'):
+                       # Non online events require city and country
+                       # (we don't require state, since many countries have no such thing)
+                       if not cleaned_data.get('city'):
+                               self._errors['city'] = self.error_class(['City must be specified for non-online events'])
+                               del cleaned_data['city']
+                       if not cleaned_data.get('country'):
+                               self._errors['country'] = self.error_class(['Country must be specified for non-online events'])
+                               del cleaned_data['country']
+               return cleaned_data
+
        class Meta:
                model = Event
                exclude = ('submitter', 'approved', )
index 127f72a938d21fc6c1824d72321631ae7cf1211c..d1b4daa6e1d663322a1cd90a788d5f1b61142f4d 100644 (file)
@@ -10,9 +10,10 @@ class Event(PgModel, models.Model):
 
        org = models.ForeignKey(Organisation, null=False, blank=False, verbose_name="Organisation", help_text="If no organisations are listed, please check the <a href=\"/account/orglist/\">organisation list</a> and contact the organisation manager or webmaster@postgresql.org if none are listed.")
        title = models.CharField(max_length=100, null=False, blank=False)
-       city = models.CharField(max_length=50, null=False, blank=False)
+       isonline = models.BooleanField(null=False, default=False, verbose_name="Online event")
+       city = models.CharField(max_length=50, null=False, blank=True)
        state = models.CharField(max_length=50, null=False, blank=True) 
-       country = models.ForeignKey(Country, null=False, blank=False)
+       country = models.ForeignKey(Country, null=True, blank=True)
        
        training = models.BooleanField(null=False, blank=False, default=False)
        startdate = models.DateField(null=False, blank=False, verbose_name="Start date")
@@ -59,7 +60,9 @@ class Event(PgModel, models.Model):
        
        @property
        def locationstring(self):
-               if self.state:
+               if self.isonline:
+                       return "online"
+               elif self.state:
                        return "%s, %s, %s" % (self.city, self.state, self.country)
                else:
                        return "%s, %s" % (self.city, self.country)