diff --git a/expcs/settings.py b/expcs/settings.py index edf0760..f272adf 100644 --- a/expcs/settings.py +++ b/expcs/settings.py @@ -28,13 +28,20 @@ ALLOWED_HOSTS = ["*"] # Let the web server handle that. That's its job. # Application definition INSTALLED_APPS = [ + # Django internals 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + + # Third party apps + 'polymorphic', + + # In-project apps 'dbsettings', + 'profiles', ] MIDDLEWARE = [ diff --git a/profiles/__init__.py b/profiles/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/profiles/admin.py b/profiles/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/profiles/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/profiles/apps.py b/profiles/apps.py new file mode 100644 index 0000000..5501fda --- /dev/null +++ b/profiles/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class ProfilesConfig(AppConfig): + name = 'profiles' diff --git a/profiles/helpers.py b/profiles/helpers.py new file mode 100644 index 0000000..d143cc5 --- /dev/null +++ b/profiles/helpers.py @@ -0,0 +1,5 @@ +CLIENT_PERMISSION_CHOICES = ( +) + +OPERATOR_PERMISSION_CHOICES = ( +) \ No newline at end of file diff --git a/profiles/models.py b/profiles/models.py new file mode 100644 index 0000000..a46724a --- /dev/null +++ b/profiles/models.py @@ -0,0 +1,54 @@ +from django.db.models import OneToOneField, SET_NULL, Model, ForeignKey, CASCADE, CharField +from django.contrib.auth import get_user_model + +from polymorphic.models import PolymorphicModel +from multiselectfield import MultiSelectField +from django_countries.fields import CountryField +from vies.types import MEMBER_COUNTRY_CODES, VATIN + +from profiles.helpers import CLIENT_PERMISSION_CHOICES, OPERATOR_PERMISSION_CHOICES + +# Create your models here. + +class Client(Model): + company = CharField(max_length=255, blank=True, null=True) + country = CountryField() + vatid = CharField(max_length=255, blank=True, null=True) + + def validate_vatid(self): + if not self.vatid: + return -1 + if self.vatid.lower().startswith("eu"): # pylint: disable=no-member + return -2 + if not (self.vatid.upper()[:2]) in list(MEMBER_COUNTRY_CODES): # pylint: disable=no-member + return -3 + if not ((self.country.code == self.vatid.upper()[:2]) or (self.country.code == "GR" and self.vatid.upper()[:2] == "EL")): # pylint: disable=no-member + return -4 + return 1 if VATIN.from_str(self.vatid).is_valid else 0 + + def vatid_data(self, force=False): + if force or self.validate_vatid() == 1: + return VATIN.from_str(self.vatid).data + raise ValueError("VAT ID is not valid or cannot be validated") + +class Profile(PolymorphicModel): + user = OneToOneField(get_user_model(), on_delete=SET_NULL, null=True) + client = ForeignKey(Client, on_delete=CASCADE) + first_name = CharField(max_length=255) + last_name = CharField(max_length=255) + +class ClientProfile(Profile): + permissions = MultiSelectField(choices=CLIENT_PERMISSION_CHOICES, null=True) + address1 = CharField(max_length=255) + address2 = CharField(max_length=255, null=True, blank=True) + +class OperatorProfile(Profile): + permissions = MultiSelectField(choices=OPERATOR_PERMISSION_CHOICES, null=True) + +class ClientPermissionGroup(Model): + name = CharField(max_length=255) + permissions = MultiSelectField(choices=CLIENT_PERMISSION_CHOICES, null=True) + +class OperatorPermissionGroup(Model): + name = CharField(max_length=255) + permissions = MultiSelectField(choices=OPERATOR_PERMISSION_CHOICES, null=True) \ No newline at end of file diff --git a/profiles/tests.py b/profiles/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/profiles/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/profiles/views.py b/profiles/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/profiles/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. diff --git a/requirements.txt b/requirements.txt index 118a4dc..97a74b1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,4 @@ django[argon2] mysqlclient +django-polymorphic +django-vies \ No newline at end of file