Commit e1c5fa17 authored by Florent Chehab's avatar Florent Chehab

File rename and code factorized

parent 03447ac6
Pipeline #22044 passed with stage
in 1 minute and 11 seconds
......@@ -2,7 +2,7 @@ from django.contrib import admin
from reversion_compare.admin import CompareVersionAdmin
from rex.models.university import University
from rex.models.country import Country, Region
from rex.models.localization import Country, Region
admin.site.register(University, CompareVersionAdmin)
......
from .country import country # noqa: F401
from .localization import localization # noqa: F401
from .university import university # noqa: F401
from .country import Country, CountrySerializer, CountryViewSet # noqa: F401
from .country import Region, RegionSerializer, RegionViewSet # noqa: F401
from .localization import Country, CountrySerializer, CountryViewSet # noqa: F401
from .localization import Region, RegionSerializer, RegionViewSet # noqa: F401
......@@ -10,37 +10,58 @@ class Region(models.Model):
parent = models.ForeignKey(
'self', on_delete=models.SET_NULL, null=True)
def r_type(self):
p = 0
region = self
while region.parent is not None:
p += 1
region = region.parent
if p is 0:
return "main"
elif p is 1:
return "sub"
elif p is 2:
return "inter"
else:
raise Exception("Impossible case found")
class Country(models.Model):
name = models.CharField(max_length=200)
iso_alpha3_code = models.CharField(primary_key=True, max_length=3)
region = models.ForeignKey(Region, on_delete=models.PROTECT, null=True)
def get_regions(self):
regions = [None] * 3
regions.append(self.region)
while regions[-1] is not None and regions[-1].parent is not None:
regions.append(regions[-1].parent)
regions = regions[::-1]
return regions[0:3]
"""
API RELATED STUFF BELLOW
"""
def get_regions(country):
regions = [None] * 3
regions.append(country.region)
while regions[-1] is not None and regions[-1].parent is not None:
regions.append(regions[-1].parent)
regions = regions[::-1]
return regions[0:3]
class CountrySerializer(serializers.ModelSerializer):
regions = serializers.SerializerMethodField()
regions_url = serializers.SerializerMethodField()
def get_regions(self, obj):
return [r.un_code if r is not None else None
for r in get_regions(obj)]
def get_regions_url(self, obj):
return [self.get_region_url(r) for r in get_regions(obj)]
regions = obj.get_regions()
def get_info(i):
r = regions[i]
r_code = r.un_code if r is not None else None
return {"code": r_code,
"url": self.get_region_url(regions[i])}
res = {}
res["main"] = get_info(0)
res["sub"] = get_info(1)
res["inter"] = get_info(2)
return res
def get_region_url(self, region):
if region is None:
......@@ -55,20 +76,10 @@ class CountrySerializer(serializers.ModelSerializer):
class Meta:
model = Country
fields = ('name', 'iso_alpha3_code',
'region', 'regions', 'regions_url')
'region', 'regions')
class CountryViewSet(viewsets.ModelViewSet):
"""
retrieve:
Retourne un pays.
list:
Retourne une liste de pays.
create:
Créée un nouveau pays.
"""
permission_classes = (permissions.DjangoModelPermissions,)
queryset = Country.objects.all() # noqa: E1101
serializer_class = CountrySerializer
......@@ -76,6 +87,7 @@ class CountryViewSet(viewsets.ModelViewSet):
class RegionSerializer(serializers.ModelSerializer):
parent_url = serializers.SerializerMethodField()
region_type = serializers.SerializerMethodField()
def get_parent_url(self, obj):
if obj.parent is None:
......@@ -87,22 +99,15 @@ class RegionSerializer(serializers.ModelSerializer):
.get_url(obj.parent, view_name='region-detail',
request=self.context['request'], format=None)
def get_region_type(self, obj):
return obj.r_type()
class Meta:
model = Region
fields = ('name', 'un_code', 'parent', 'parent_url')
fields = ('name', 'un_code', 'parent', 'parent_url', 'region_type')
class RegionViewSet(viewsets.ModelViewSet):
"""
retrieve:
Retourne un pays.
list:
Retourne une liste de pays.
create:
Créée un nouveau pays.
"""
permission_classes = (permissions.DjangoModelPermissions,)
queryset = Region.objects.all() # noqa: E1101
serializer_class = RegionSerializer
......@@ -2,7 +2,7 @@ from django.db import models
from django.core.validators import MinValueValidator, MaxValueValidator
from rex.utils.friendly_path import friendly_path
from rex.models.country import Country
from rex.models.localization import Country
import reverse_geocoder as rg
......
from django.conf.urls import url, include
from rex import views
from rest_framework import routers
from rex.models.country import CountryViewSet, RegionViewSet
from rex.models.localization import CountryViewSet, RegionViewSet
from rest_framework.documentation import include_docs_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