Commit e934ec9b authored by Florent Chehab's avatar Florent Chehab

City added

parent e1c5fa17
Pipeline #22046 passed with stage
in 1 minute and 11 seconds
......@@ -2,9 +2,10 @@ from django.contrib import admin
from reversion_compare.admin import CompareVersionAdmin
from rex.models.university import University
from rex.models.localization import Country, Region
from rex.models.localization import Country, Region, City
admin.site.register(University, CompareVersionAdmin)
admin.site.register(Country)
admin.site.register(Region)
admin.site.register(City)
# Generated by Django 2.0.3 on 2018-04-25 08:21
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('rex', '0002_auto_20180424_2119'),
]
operations = [
migrations.CreateModel(
name='City',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=200)),
('local_name', models.CharField(max_length=200, null=True)),
('area', models.CharField(max_length=200, null=True)),
('country', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='rex.Country')),
],
),
]
from .localization import Country, CountrySerializer, CountryViewSet # noqa: F401
from .localization import Region, RegionSerializer, RegionViewSet # noqa: F401
from .localization import City, CitySerializer, CityViewSet # noqa: F401
......@@ -41,6 +41,15 @@ class Country(models.Model):
return regions[0:3]
class City(models.Model):
name = models.CharField(max_length=200)
local_name = models.CharField(max_length=200, null=True)
# We add an area to distinguish similarly named cities
# in a country
area = models.CharField(max_length=200, null=True)
country = models.ForeignKey(Country, on_delete=models.PROTECT)
"""
API RELATED STUFF BELLOW
"""
......@@ -111,3 +120,24 @@ class RegionViewSet(viewsets.ModelViewSet):
permission_classes = (permissions.DjangoModelPermissions,)
queryset = Region.objects.all() # noqa: E1101
serializer_class = RegionSerializer
class CitySerializer(serializers.ModelSerializer):
country_url = serializers.SerializerMethodField()
def get_country_url(self, obj):
return serializers.HyperlinkedRelatedField(
view_name='country-detail',
read_only=True) \
.get_url(obj.country, view_name='region-detail',
request=self.context['request'], format=None)
class Meta:
model = City
fields = ('name', 'local_name', 'area', 'country', 'country_url')
class CityViewSet(viewsets.ModelViewSet):
permission_classes = (permissions.DjangoModelPermissions,)
queryset = City.objects.all() # noqa: E1101
serializer_class = CitySerializer
from django.conf.urls import url, include
from rex import views
from rest_framework import routers
from rex.models.localization import CountryViewSet, RegionViewSet
from rex.models.localization import CountryViewSet, RegionViewSet, CityViewSet
from rest_framework.documentation import include_docs_urls
......@@ -14,5 +14,6 @@ urlpatterns = [
router = routers.DefaultRouter()
router.register(r'country', CountryViewSet)
router.register(r'region', RegionViewSet)
router.register(r'city', CityViewSet)
urlpatterns += [url(r'^api/', include(router.urls))]
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment