diff --git a/events/forms.py b/events/forms.py index 4f0be34..ba4f2f1 100644 --- a/events/forms.py +++ b/events/forms.py @@ -1,7 +1,47 @@ -from django.forms import ModelForm +from django.utils.safestring import mark_safe +from django.forms import ModelForm, Field +from django.forms.widgets import Select, Media from .models.profiles import Team from .models.events import Event +class LookupMedia(Media): + def render(self): + return mark_safe('''''') + +class Lookup(Select): + input_type = 'select' + template_name = 'forms/widgets/lookup.html' + add_id_index = False + checked_attribute = {'selected': True} + option_inherits_attrs = False + + def __init__(self, source='#', key="id", label="name", attrs=None): + super().__init__(attrs) + self.source = source + self.key = key + self.label = label + self.name = 'place' + + 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 + class TeamForm(ModelForm): class Meta: model = Team @@ -11,14 +51,28 @@ class NewTeamForm(ModelForm): class Meta: model = Team fields = ['name', 'country', 'spr', 'city', 'web_url', 'tz'] + widgets = { + 'country': Lookup(source='/api/country/', label='name'), + 'spr': Lookup(source='/api/spr/', label='name'), + 'city': Lookup(source='/api/cities/', label='name'), + } class TeamEventForm(ModelForm): class Meta: model = Event fields = ['name', 'start_time', 'end_time', 'summary', 'place', 'web_url', 'announce_url', 'tags'] + widgets = { + 'country': Lookup(source='/api/country/', label='name'), + 'spr': Lookup(source='/api/spr/', label='name'), + 'city': Lookup(source='/api/cities/', label='name'), + } class NewTeamEventForm(ModelForm): class Meta: model = Event fields = ['name', 'start_time', 'end_time', 'summary', 'place'] + widgets = { + 'place': Lookup(source='/api/places/', label='name'), + } + diff --git a/events/models/events.py b/events/models/events.py index 89b9a67..d10ae73 100644 --- a/events/models/events.py +++ b/events/models/events.py @@ -4,6 +4,8 @@ from django.contrib.auth.models import User, Group from django.utils.translation import ugettext_lazy as _ from django.shortcuts import reverse +from rest_framework import serializers + from .locale import * from .profiles import * from .search import * diff --git a/events/models/locale.py b/events/models/locale.py index 889bcd2..b873e8b 100644 --- a/events/models/locale.py +++ b/events/models/locale.py @@ -1,6 +1,8 @@ from django.db import models from django.utils.translation import ugettext_lazy as _ +from rest_framework import serializers + import pytz class Language(models.Model): @@ -41,6 +43,15 @@ class Country(models.Model): else: return 'no_country' +class CountrySerializer(serializers.ModelSerializer): + class Meta: + model = Country + fields = ( + 'id', + 'name', + 'code' + ) + class SPR(models.Model): name = models.CharField(_("Name"), max_length=100) code = models.CharField(_("Admin Code"), max_length=8) @@ -59,6 +70,17 @@ class SPR(models.Model): else: return 'no_spr' +class SPRSerializer(serializers.ModelSerializer): + class Meta: + model = SPR + fields = ( + 'id', + 'name', + 'code', + 'country', + 'slug' + ) + class City(models.Model): class Meta: ordering = ('name',) @@ -79,4 +101,14 @@ class City(models.Model): return 'no_city' +class CitySerializer(serializers.ModelSerializer): + class Meta: + model = City + fields = ( + 'id', + 'name', + 'spr', + 'tz', + 'slug' + ) diff --git a/events/templates/forms/widgets/lookup.html b/events/templates/forms/widgets/lookup.html new file mode 100644 index 0000000..14e719d --- /dev/null +++ b/events/templates/forms/widgets/lookup.html @@ -0,0 +1,5 @@ + + + diff --git a/events/views.py b/events/views.py index fbe402c..e13dd9a 100644 --- a/events/views.py +++ b/events/views.py @@ -1,8 +1,12 @@ from django.shortcuts import render from django.http import HttpResponse, JsonResponse +from rest_framework.decorators import api_view, throttle_classes +from rest_framework.response import Response + from .models.search import Searchable, SearchableSerializer -from .models.events import Event +from .models.events import Event, Place, PlaceSerializer +from .models.locale import Country ,CountrySerializer, SPR, SPRSerializer, City, CitySerializer import simplejson @@ -18,3 +22,52 @@ def events_list(request, *args, **kwargs): 'events_list': events, } return render(request, 'events/event_list.html', context) + +@api_view(['GET']) +def places_list(request, *args, **kwargs): + places = Place.objects.all() + if "q" in request.GET: + match = request.GET.get("q", "") + places = Place.objects.filter(name__icontains=match) + else: + places = Place.objects.all() + serializer = PlaceSerializer(places, many=True) + return Response(serializer.data) + +@api_view(['GET']) +def country_list(request, *args, **kwargs): + if "q" in request.GET: + match = request.GET.get("q", "") + countries = Country.objects.filter(name__icontains=match) + else: + countries = Country.objects.all() + serializer = CountrySerializer(countries, many=True) + return Response(serializer.data) + +@api_view(['GET']) +def spr_list(request, *args, **kwargs): + if "q" in request.GET: + match = request.GET.get("q", "") + sprs = SPR.objects.filter(name__icontains=match) + else: + sprs = SPR.objects.all() + if "country" in request.GET and request.GET.get("country") is not "": + sprs = sprs.filter(country=request.GET.get("country")) + + serializer = SPRSerializer(sprs, many=True) + return Response(serializer.data) + +@api_view(['GET']) +def city_list(request, *args, **kwargs): + if "q" in request.GET: + match = request.GET.get("q", "") + cities = City.objects.filter(name__icontains=match) + else: + cities = City.objects.all() + + if "spr" in request.GET and request.GET.get("spr") is not "": + cities = cities.filter(spr=request.GET.get("spr")) + + serializer = CitySerializer(cities, many=True) + return Response(serializer.data) + diff --git a/get_together/settings.py b/get_together/settings.py index 5d95b87..75123df 100644 --- a/get_together/settings.py +++ b/get_together/settings.py @@ -149,3 +149,4 @@ USE_TZ = True # https://docs.djangoproject.com/en/2.0/howto/static-files/ STATIC_URL = '/static/' + diff --git a/get_together/templates/get_together/base.html b/get_together/templates/get_together/base.html index fc1c8ac..a825d47 100644 --- a/get_together/templates/get_together/base.html +++ b/get_together/templates/get_together/base.html @@ -10,7 +10,7 @@ - +