Add checkbox for "is online" on events

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
This commit is contained in:
Magnus Hagander
2013-01-30 14:07:48 +01:00
parent 3818de50c5
commit 5055c330c7
3 changed files with 44 additions and 3 deletions

View 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)

View 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', )

View 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)