from django.contrib import admin
+from django import forms
from util.admin import PgwebAdmin
from models import *
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)
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', )
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")
@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)