Commit 1a6bb9ab authored by Florent Chehab's avatar Florent Chehab

Moved everything to new models !

parent 7cfe87b2
This diff is collapsed.
from django.db import models from django.db import models
from rest_framework import serializers, permissions from rest_framework import serializers
from backend.models.tools import DictModeViewSet
from backend.models.location import Country from backend.models.location import Country
from backend.models.my_model import MyModel, MyModelSerializer, MyModelVersionnedViewSet
# Data model based on : https://unstats.un.org/unsd/methodology/m49/overview/ class City(MyModel):
class City(models.Model):
name = models.CharField(max_length=200) name = models.CharField(max_length=200)
local_name = models.CharField(max_length=200, null=True, blank=True) local_name = models.CharField(max_length=200, null=True, blank=True)
# We add an area to distinguish similarly named cities # We add an area to distinguish similarly named cities
...@@ -21,7 +18,7 @@ API RELATED STUFF BELLOW ...@@ -21,7 +18,7 @@ API RELATED STUFF BELLOW
""" """
class CitySerializer(serializers.ModelSerializer): class CitySerializer(MyModelSerializer):
country_url = serializers.SerializerMethodField() country_url = serializers.SerializerMethodField()
def get_country_url(self, obj): def get_country_url(self, obj):
...@@ -36,7 +33,6 @@ class CitySerializer(serializers.ModelSerializer): ...@@ -36,7 +33,6 @@ class CitySerializer(serializers.ModelSerializer):
fields = '__all__' fields = '__all__'
class CityViewSet(DictModeViewSet): class CityViewSet(MyModelVersionnedViewSet):
permission_classes = (permissions.DjangoModelPermissions,)
queryset = City.objects.all() # pylint: disable=E1101 queryset = City.objects.all() # pylint: disable=E1101
serializer_class = CitySerializer serializer_class = CitySerializer
from django.db import models from django.db import models
from backend.models.module import BasicModule from backend.models.module import BasicModule, BasicModuleSerializer, BasicModuleViewSet
from backend.models.location import City from backend.models.location import City
from rest_framework import viewsets, permissions
from backend.models.module import BasicModuleSerializer
#################### ####################
...@@ -20,8 +18,8 @@ class CityTransportSerializer(BasicModuleSerializer): ...@@ -20,8 +18,8 @@ class CityTransportSerializer(BasicModuleSerializer):
fields = '__all__' fields = '__all__'
class CityTransportViewSet(viewsets.ModelViewSet): # TODO switch to dict class CityTransportViewSet(BasicModuleViewSet):
permission_classes = (permissions.DjangoModelPermissions,)
queryset = CityTransport.objects.all() # pylint: disable=E1101 queryset = CityTransport.objects.all() # pylint: disable=E1101
serializer_class = CityTransportSerializer serializer_class = CityTransportSerializer
...@@ -45,8 +43,8 @@ class CityTourismSerializer(BasicModuleSerializer): ...@@ -45,8 +43,8 @@ class CityTourismSerializer(BasicModuleSerializer):
fields = '__all__' fields = '__all__'
class CityTourismViewSet(viewsets.ModelViewSet): # TODO switch to dict class CityTourismViewSet(BasicModuleViewSet):
permission_classes = (permissions.DjangoModelPermissions,)
queryset = CityTourism.objects.all() # pylint: disable=E1101 queryset = CityTourism.objects.all() # pylint: disable=E1101
serializer_class = CityTourismSerializer serializer_class = CityTourismSerializer
...@@ -70,8 +68,7 @@ class CityOtherStuffSerializer(BasicModuleSerializer): ...@@ -70,8 +68,7 @@ class CityOtherStuffSerializer(BasicModuleSerializer):
fields = '__all__' fields = '__all__'
# TODO switch to dict class CityOtherStuffViewSet(BasicModuleViewSet):
class CityOtherStuffViewSet(viewsets.ModelViewSet):
permission_classes = (permissions.DjangoModelPermissions,)
queryset = CityOtherStuff.objects.all() # pylint: disable=E1101 queryset = CityOtherStuff.objects.all() # pylint: disable=E1101
serializer_class = CityOtherStuffSerializer serializer_class = CityOtherStuffSerializer
from django.db import models from django.db import models
from backend.models.location import City from backend.models.location import City
from backend.models.module import VersionnedModuleSerializer from backend.models.module import Photo, PhotoSerializer, PhotoViewSet
from backend.models.module import Photo
from rest_framework import viewsets, permissions
class CityPhoto(Photo): class CityPhoto(Photo):
...@@ -16,14 +14,13 @@ class CityPhoto(Photo): ...@@ -16,14 +14,13 @@ class CityPhoto(Photo):
unique_together = ('city', 'photo') unique_together = ('city', 'photo')
class CityPhotoSerializer(VersionnedModuleSerializer): class CityPhotoSerializer(PhotoSerializer):
class Meta: class Meta:
model = CityPhoto model = CityPhoto
fields = '__all__' fields = '__all__'
class CityPhotoViewSet(viewsets.ModelViewSet): # TODO switch to dict class CityPhotoViewSet(PhotoViewSet):
permission_classes = (permissions.DjangoModelPermissions,)
queryset = CityPhoto.objects.all() # pylint: disable=E1101 queryset = CityPhoto.objects.all() # pylint: disable=E1101
serializer_class = CityPhotoSerializer serializer_class = CityPhotoSerializer
from django.db import models from django.db import models
from rest_framework import serializers, permissions from backend.models.my_model import MyModel, MyModelSerializer, MyModelVersionnedViewSet
from backend.models.tools import DictModeViewSet
# Data model based on : https://unstats.un.org/unsd/methodology/m49/overview/ # Data model based on : https://unstats.un.org/unsd/methodology/m49/overview/
class Country(models.Model): class Country(MyModel):
name = models.CharField(max_length=200) name = models.CharField(max_length=200)
iso_alpha2_code = models.CharField(primary_key=True, max_length=2) iso_alpha2_code = models.CharField(primary_key=True, max_length=2)
iso_alpha3_code = models.CharField( iso_alpha3_code = models.CharField(
...@@ -20,19 +19,13 @@ class Country(models.Model): ...@@ -20,19 +19,13 @@ class Country(models.Model):
max_length=3, null=True, blank=True) max_length=3, null=True, blank=True)
""" class CountrySerializer(MyModelSerializer):
API RELATED STUFF BELLOW
"""
class CountrySerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Country model = Country
fields = '__all__' fields = '__all__'
class CountryViewSet(DictModeViewSet): class CountryViewSet(MyModelVersionnedViewSet):
permission_classes = (permissions.DjangoModelPermissions,)
queryset = Country.objects.all() # pylint: disable=E1101 queryset = Country.objects.all() # pylint: disable=E1101
serializer_class = CountrySerializer serializer_class = CountrySerializer
from django.db import models from django.db import models
from backend.models.module import BasicModule from backend.models.module import BasicModule, BasicModuleSerializer, BasicModuleViewSet
from backend.models.location import Country from backend.models.location import Country
from rest_framework import viewsets, permissions
from backend.models.module import BasicModuleSerializer
#################### ####################
...@@ -20,9 +18,8 @@ class CountryVisaAdministrativeSerializer(BasicModuleSerializer): ...@@ -20,9 +18,8 @@ class CountryVisaAdministrativeSerializer(BasicModuleSerializer):
fields = '__all__' fields = '__all__'
# TODO switch to dict class CountryVisaAdministrativeViewSet(BasicModuleViewSet):
class CountryVisaAdministrativeViewSet(viewsets.ModelViewSet):
permission_classes = (permissions.DjangoModelPermissions,)
queryset = CountryVisaAdministrative.objects.all() # pylint: disable=E1101 queryset = CountryVisaAdministrative.objects.all() # pylint: disable=E1101
serializer_class = CountryVisaAdministrativeSerializer serializer_class = CountryVisaAdministrativeSerializer
...@@ -47,9 +44,8 @@ class CountryHealthInsuranceSerializer(BasicModuleSerializer): ...@@ -47,9 +44,8 @@ class CountryHealthInsuranceSerializer(BasicModuleSerializer):
fields = '__all__' fields = '__all__'
# TODO switch to dict class CountryHealthInsuranceViewSet(BasicModuleViewSet):
class CountryHealthInsuranceViewSet(viewsets.ModelViewSet):
permission_classes = (permissions.DjangoModelPermissions,)
queryset = CountryHealthInsurance.objects.all() # pylint: disable=E1101 queryset = CountryHealthInsurance.objects.all() # pylint: disable=E1101
serializer_class = CountryHealthInsuranceSerializer serializer_class = CountryHealthInsuranceSerializer
...@@ -73,9 +69,8 @@ class CountryOtherInsuranceSerializer(BasicModuleSerializer): ...@@ -73,9 +69,8 @@ class CountryOtherInsuranceSerializer(BasicModuleSerializer):
fields = '__all__' fields = '__all__'
# TODO switch to dict class CountryOtherInsuranceViewSet(BasicModuleViewSet):
class CountryOtherInsuranceViewSet(viewsets.ModelViewSet):
permission_classes = (permissions.DjangoModelPermissions,)
queryset = CountryOtherInsurance.objects.all() # pylint: disable=E1101 queryset = CountryOtherInsurance.objects.all() # pylint: disable=E1101
serializer_class = CountryOtherInsuranceSerializer serializer_class = CountryOtherInsuranceSerializer
...@@ -98,8 +93,8 @@ class CountryCultureSerializer(BasicModuleSerializer): ...@@ -98,8 +93,8 @@ class CountryCultureSerializer(BasicModuleSerializer):
fields = '__all__' fields = '__all__'
class CountryCultureViewSet(viewsets.ModelViewSet): # TODO switch to dict class CountryCultureViewSet(BasicModuleViewSet):
permission_classes = (permissions.DjangoModelPermissions,)
queryset = CountryCulture.objects.all() # pylint: disable=E1101 queryset = CountryCulture.objects.all() # pylint: disable=E1101
serializer_class = CountryCultureSerializer serializer_class = CountryCultureSerializer
...@@ -122,8 +117,8 @@ class CountryTransportSerializer(BasicModuleSerializer): ...@@ -122,8 +117,8 @@ class CountryTransportSerializer(BasicModuleSerializer):
fields = '__all__' fields = '__all__'
class CountryTransportViewSet(viewsets.ModelViewSet): # TODO switch to dict class CountryTransportViewSet(BasicModuleViewSet):
permission_classes = (permissions.DjangoModelPermissions,)
queryset = CountryTransport.objects.all() # pylint: disable=E1101 queryset = CountryTransport.objects.all() # pylint: disable=E1101
serializer_class = CountryTransportSerializer serializer_class = CountryTransportSerializer
...@@ -147,8 +142,8 @@ class CountryTourismSerializer(BasicModuleSerializer): ...@@ -147,8 +142,8 @@ class CountryTourismSerializer(BasicModuleSerializer):
fields = '__all__' fields = '__all__'
class CountryTourismViewSet(viewsets.ModelViewSet): # TODO switch to dict class CountryTourismViewSet(BasicModuleViewSet):
permission_classes = (permissions.DjangoModelPermissions,)
queryset = CountryTourism.objects.all() # pylint: disable=E1101 queryset = CountryTourism.objects.all() # pylint: disable=E1101
serializer_class = CountryTourismSerializer serializer_class = CountryTourismSerializer
...@@ -172,8 +167,7 @@ class CountryOtherStuffSerializer(BasicModuleSerializer): ...@@ -172,8 +167,7 @@ class CountryOtherStuffSerializer(BasicModuleSerializer):
fields = '__all__' fields = '__all__'
# TODO switch to dict class CountryOtherStuffViewSet(BasicModuleViewSet):
class CountryOtherStuffViewSet(viewsets.ModelViewSet):
permission_classes = (permissions.DjangoModelPermissions,)
queryset = CountryOtherStuff.objects.all() # pylint: disable=E1101 queryset = CountryOtherStuff.objects.all() # pylint: disable=E1101
serializer_class = CountryOtherStuffSerializer serializer_class = CountryOtherStuffSerializer
from django.db import models from django.db import models
from backend.models.location import Country from backend.models.location import Country
from backend.models.module import DriRestrictedModule, DriRestrictedModuleSerializer from backend.models.module import DriRestrictedModule, DriRestrictedModuleSerializer, DriRestrictedModuleViewSet
from rest_framework import viewsets, permissions
class CountryDri(DriRestrictedModule): class CountryDri(DriRestrictedModule):
...@@ -16,7 +15,6 @@ class CountryDriSerializer(DriRestrictedModuleSerializer): ...@@ -16,7 +15,6 @@ class CountryDriSerializer(DriRestrictedModuleSerializer):
fields = '__all__' fields = '__all__'
class CountryDriViewSet(viewsets.ModelViewSet): class CountryDriViewSet(DriRestrictedModuleViewSet):
permission_classes = (permissions.DjangoModelPermissions,)
queryset = CountryDri.objects.all() # pylint: disable=E1101 queryset = CountryDri.objects.all() # pylint: disable=E1101
serializer_class = CountryDriSerializer serializer_class = CountryDriSerializer
from django.db import models from django.db import models
from backend.models.location import Country from backend.models.location import Country
from backend.models.module import VersionnedModuleSerializer from backend.models.module import Photo, PhotoSerializer, PhotoViewSet
from backend.models.module import Photo
from rest_framework import viewsets, permissions
class CountryPhoto(Photo): class CountryPhoto(Photo):
...@@ -16,14 +14,13 @@ class CountryPhoto(Photo): ...@@ -16,14 +14,13 @@ class CountryPhoto(Photo):
unique_together = ('country', 'photo') unique_together = ('country', 'photo')
class CountryPhotoSerializer(VersionnedModuleSerializer): class CountryPhotoSerializer(PhotoSerializer):
class Meta: class Meta:
model = CountryPhoto model = CountryPhoto
fields = '__all__' fields = '__all__'
class CountryPhotoViewSet(viewsets.ModelViewSet): # TODO switch to dict class CountryPhotoViewSet(PhotoViewSet):
permission_classes = (permissions.DjangoModelPermissions,)
queryset = CountryPhoto.objects.all() # pylint: disable=E1101 queryset = CountryPhoto.objects.all() # pylint: disable=E1101
serializer_class = CountryPhotoSerializer serializer_class = CountryPhotoSerializer
from django.db import models from django.db import models
from backend.models.location import Country from backend.models.location import Country
from backend.models.module import Scholarship, BasicModuleSerializer from backend.models.module import Scholarship, ScholarshipSerializer, ScholarshipViewSet
from rest_framework import viewsets, permissions
class CountryScholarship(Scholarship): class CountryScholarship(Scholarship):
...@@ -9,14 +8,13 @@ class CountryScholarship(Scholarship): ...@@ -9,14 +8,13 @@ class CountryScholarship(Scholarship):
Country, related_name="country_scholarhip") Country, related_name="country_scholarhip")
class CountryScholarshipSerializer(BasicModuleSerializer): class CountryScholarshipSerializer(ScholarshipSerializer):
class Meta: class Meta:
model = CountryScholarship model = CountryScholarship
fields = '__all__' fields = '__all__'
class CountryScholarshipViewSet(viewsets.ModelViewSet): class CountryScholarshipViewSet(ScholarshipViewSet):
permission_classes = (permissions.DjangoModelPermissions,)
queryset = CountryScholarship.objects.all() # pylint: disable=E1101 queryset = CountryScholarship.objects.all() # pylint: disable=E1101
serializer_class = CountryScholarshipSerializer serializer_class = CountryScholarshipSerializer
from .versionnedModule import VersionnedModule, VersionnedModuleSerializer # noqa: F401 from .basicModule import BasicModule, BasicModuleSerializer, BasicModuleViewSet # noqa: F401
from .basicModule import BasicModule, BasicModuleSerializer # noqa: F401 from .scholarship import Scholarship, ScholarshipSerializer, ScholarshipViewSet # noqa: F401
from .scholarship import Scholarship # noqa: F401 from .photo import Photo, PhotoSerializer, PhotoViewSet # noqa: F401
from .photo import Photo # noqa: F401 from .driRestrictedModule import DriRestrictedModule, DriRestrictedModuleSerializer, DriRestrictedModuleViewSet # noqa: F401
from .driRestrictedModule import DriRestrictedModule, DriRestrictedModuleSerializer # noqa: F401
from .currency import Currency, CurrencyViewSet, CurrencySerializer # noqa: F401 from .currency import Currency, CurrencyViewSet, CurrencySerializer # noqa: F401
# MyModelVersionned, MyModelVersionnedSerializer # noqa: F401 # MyModelVersionned, MyModelVersionnedSerializer # noqa: F401
from django.db import models from django.db import models
from backend.models.tools import UsefullLinksField from backend.models.tools import UsefullLinksField
from backend.models.module import VersionnedModule, VersionnedModuleSerializer from backend.models.my_model import MyModelVersionned, MyModelVersionnedSerializer, MyModelVersionnedViewSet
class BasicModule(VersionnedModule): class BasicModule(MyModelVersionned):
comment = models.TextField(blank=True) comment = models.TextField(blank=True)
usefull_links = UsefullLinksField(blank=True) usefull_links = UsefullLinksField(blank=True)
...@@ -12,8 +12,12 @@ class BasicModule(VersionnedModule): ...@@ -12,8 +12,12 @@ class BasicModule(VersionnedModule):
abstract = True abstract = True
class BasicModuleSerializer(VersionnedModuleSerializer): class BasicModuleSerializer(MyModelVersionnedSerializer):
class Meta: class Meta:
model = BasicModule model = BasicModule
fields = '__all__' fields = '__all__'
class BasicModuleViewSet(MyModelVersionnedViewSet):
serializer_class = BasicModuleSerializer
from django.db import models from django.db import models
from rest_framework import serializers, viewsets, permissions from rest_framework import permissions
from django.core.validators import MinValueValidator from django.core.validators import MinValueValidator
from backend.models.my_model import MyModel, MyModelSerializer, MyModelViewSet
class Currency(models.Model): class Currency(MyModel):
code = models.CharField(primary_key=True, max_length=15) code = models.CharField(primary_key=True, max_length=15)
name = models.CharField(max_length=100) name = models.CharField(max_length=100)
symbol = models.CharField(max_length=5) symbol = models.CharField(max_length=5)
...@@ -14,13 +15,13 @@ class Currency(models.Model): ...@@ -14,13 +15,13 @@ class Currency(models.Model):
) )
class CurrencySerializer(serializers.ModelSerializer): class CurrencySerializer(MyModelSerializer):
class Meta: class Meta:
model = Currency model = Currency
fields = '__all__' fields = '__all__'
class CurrencyViewSet(viewsets.ModelViewSet): class CurrencyViewSet(MyModelViewSet):
permission_classes = (permissions.IsAdminUser,) permission_classes = (permissions.IsAdminUser,) # TODO : change
queryset = Currency.objects.all() # pylint: disable=E1101 queryset = Currency.objects.all() # pylint: disable=E1101
serializer_class = CurrencySerializer serializer_class = CurrencySerializer
from backend.models.module import BasicModule, BasicModuleSerializer from backend.models.module import BasicModule, BasicModuleSerializer, BasicModuleViewSet
class DriRestrictedModule(BasicModule): class DriRestrictedModule(BasicModule):
...@@ -8,5 +8,9 @@ class DriRestrictedModule(BasicModule): ...@@ -8,5 +8,9 @@ class DriRestrictedModule(BasicModule):
class DriRestrictedModuleSerializer(BasicModuleSerializer): class DriRestrictedModuleSerializer(BasicModuleSerializer):
# TODO Check edit restrictions class Meta:
pass model = DriRestrictedModule
class DriRestrictedModuleViewSet(BasicModuleViewSet):
serializer_class = DriRestrictedModuleSerializer
from django.db import models from django.db import models
from backend.models.module import VersionnedModule from backend.models.my_model import MyModelVersionned, MyModelVersionnedSerializer, MyModelVersionnedViewSet
from backend.utils.friendly_path import friendly_path from backend.utils.friendly_path import friendly_path
path_and_rename = friendly_path("uploads/universities/photos/", 'title') path_and_rename = friendly_path("uploads/universities/photos/", 'title')
class Photo(VersionnedModule): class Photo(MyModelVersionned):
photo = models.ImageField(upload_to=path_and_rename, max_length=250, photo = models.ImageField(upload_to=path_and_rename, max_length=250,
default='path/to/my/default/image.jpg') # TODO CHANGE ! default='path/to/my/default/image.jpg') # TODO CHANGE !
...@@ -15,3 +15,14 @@ class Photo(VersionnedModule): ...@@ -15,3 +15,14 @@ class Photo(VersionnedModule):
class Meta: class Meta:
abstract = True abstract = True
class PhotoSerializer(MyModelVersionnedSerializer):
class Meta:
model = Photo
fields = '__all__'
class PhotoViewSet(MyModelVersionnedViewSet):
serializer_class = PhotoSerializer
from django.db import models from django.db import models
from backend.models.module import BasicModule from backend.models.module import BasicModule, BasicModuleSerializer, BasicModuleViewSet
from .currency import Currency from .currency import Currency
from rest_framework import serializers from rest_framework import serializers
from django.core.validators import MinValueValidator from django.core.validators import MinValueValidator
...@@ -34,3 +34,14 @@ class Scholarship(BasicModule): ...@@ -34,3 +34,14 @@ class Scholarship(BasicModule):
class Meta: class Meta:
abstract = True abstract = True
class ScholarshipSerializer(BasicModuleSerializer):
class Meta:
model = Scholarship
fields = '__all__'
class ScholarshipViewSet(BasicModuleViewSet):
serializer_class = ScholarshipSerializer
from django.db import models
from rest_framework import serializers
from django.contrib.auth.models import User
from backend.signals import new_revision_saved
import reversion
class VersionnedModule(models.Model):
updated_at = models.DateTimeField(auto_now=True)
updated_by = models.ForeignKey(
User, null=True, on_delete=models.SET_NULL)
class Meta:
abstract = True
class VersionnedModuleSerializer(serializers.ModelSerializer):
updated_by_username = serializers.SerializerMethodField(read_only=True)
def get_updated_by_username(self, obj):
if obj.updated_by:
return obj.updated_by.username
else:
return None
def save(self, **kwargs):