diff --git a/events/forms.py b/events/forms.py
index af7af1d..f46f8bf 100644
--- a/events/forms.py
+++ b/events/forms.py
@@ -4,29 +4,13 @@ from django.forms.widgets import TextInput, Media
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User
+from .models.locale import Country, SPR, City
from .models.profiles import Team, UserProfile
from .models.events import Event, EventComment ,CommonEvent, Place, EventPhoto
from datetime import time
from time import strptime, strftime
-class LookupMedia(Media):
- def render(self):
- return mark_safe('''''')
-
class Lookup(TextInput):
input_type = 'text'
template_name = 'forms/widgets/lookup.html'
@@ -34,7 +18,7 @@ class Lookup(TextInput):
checked_attribute = {'selected': True}
option_inherits_attrs = False
- def __init__(self, source='#', key="id", label="name", attrs=None):
+ def __init__(self, source, key="id", label='__str__', attrs=None):
super().__init__(attrs)
self.source = source
self.key = key
@@ -42,14 +26,18 @@ class Lookup(TextInput):
def get_context(self, name, value, attrs):
context = super().get_context(name, value, attrs)
- context['widget']['source'] = self.source
- context['widget']['key'] = self.key
- context['widget']['label'] = self.label
return context
def format_value(self, value):
if value is not None:
- return mark_safe('' % (value, _('No change')))
+ lookup_query = {self.key: value}
+ lookup_object = self.source.objects.get(**lookup_query)
+ lookup_field = getattr(lookup_object, self.label)
+ if callable(lookup_field):
+ lookup_value = lookup_field()
+ else:
+ lookup_value = lookup_field
+ return mark_safe('' % (value, lookup_value))
else:
return mark_safe('')
@@ -152,7 +140,7 @@ class TeamForm(forms.ModelForm):
model = Team
fields = ['name', 'description', 'category', 'city', 'web_url', 'tz']
widgets = {
- 'city': Lookup(source='/api/cities/', label='name'),
+ 'city': Lookup(source=City),
}
raw_id_fields = ('city')
def __init__(self, *args, **kwargs):
@@ -164,7 +152,7 @@ class NewTeamForm(forms.ModelForm):
model = Team
fields = ['name', 'description', 'category', 'city', 'web_url', 'tz']
widgets = {
- 'city': Lookup(source='/api/cities/', label='name'),
+ 'city': Lookup(source=City),
}
raw_id_fields = ('city')
def __init__(self, *args, **kwargs):
@@ -179,7 +167,7 @@ class TeamEventForm(forms.ModelForm):
model = Event
fields = ['name', 'start_time', 'end_time', 'summary', 'place', 'web_url', 'announce_url', 'tags']
widgets = {
- 'place': Lookup(source='/api/places/', label='name'),
+ 'place': Lookup(source=Place),
'start_time': DateTimeWidget,
'end_time': DateTimeWidget
}
@@ -211,7 +199,7 @@ class NewPlaceForm(forms.ModelForm):
model = Place
fields = ['name', 'address', 'city', 'longitude', 'latitude', 'place_url', 'tz']
widgets = {
- 'city': Lookup(source='/api/cities/', label='name'),
+ 'city': Lookup(source=City),
}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@@ -244,11 +232,11 @@ class SendNotificationsForm(forms.ModelForm):
}
class SearchForm(forms.Form):
- city = forms.IntegerField(required=False, widget=Lookup(source='/api/cities/', label='name'))
+ city = forms.IntegerField(required=False, widget=Lookup(source=City, label='name'))
distance = forms.IntegerField(label=_("Distance(km)"), required=True)
class Meta:
widgets ={
- 'city': Lookup(source='/api/cities/', label='name'),
+ 'city': Lookup(source=City, label='name'),
}
class NewCommonEventForm(forms.ModelForm):
@@ -272,10 +260,10 @@ class NewCommonEventForm(forms.ModelForm):
'tags',
]
widgets ={
- 'country': Lookup(source='/api/countries/', label='name'),
- 'spr': Lookup(source='/api/spr/', label='name'),
- 'city': Lookup(source='/api/cities/', label='name'),
- 'place': Lookup(source='/api/places/', label='name'),
+ 'country': Lookup(source=Country, label='name'),
+ 'spr': Lookup(source=SPR, label='name'),
+ 'city': Lookup(source=City, label='name'),
+ 'place': Lookup(source=Place, label='name'),
'start_time': DateTimeWidget,
'end_time': DateTimeWidget
}
diff --git a/events/models/locale.py b/events/models/locale.py
index 561bd37..7e48fa0 100644
--- a/events/models/locale.py
+++ b/events/models/locale.py
@@ -44,12 +44,14 @@ class Country(models.Model):
return 'no_country'
class CountrySerializer(serializers.ModelSerializer):
+ display = serializers.CharField(source='__str__', read_only=True)
class Meta:
model = Country
fields = (
'id',
'name',
- 'code'
+ 'code',
+ 'display',
)
class SPR(models.Model):
@@ -94,6 +96,13 @@ class City(models.Model):
longitude = models.FloatField(help_text=_('Longitude in Degrees East'), null=True, blank=True)
latitude = models.FloatField(help_text=_('Latitude in Degrees North'), null=True, blank=True)
+ @property
+ def short_name(self):
+ if self.spr.country.name == 'United States':
+ return u'%s, %s' % (self.name, self.spr.name)
+ else:
+ return u'%s, %s' % (self.name, self.spr.country.name)
+
def __str__(self):
return u'%s, %s, %s' % (self.name, self.spr.name, self.spr.country.name)
@@ -112,6 +121,7 @@ class CitySerializer(serializers.ModelSerializer):
fields = (
'id',
'name',
+ 'short_name',
'spr',
'tz',
'slug',
diff --git a/events/templates/forms/widgets/lookup.html b/events/templates/forms/widgets/lookup.html
index 35c8aea..2cdffa1 100644
--- a/events/templates/forms/widgets/lookup.html
+++ b/events/templates/forms/widgets/lookup.html
@@ -1,4 +1,6 @@
-