Start implementing models

This commit is contained in:
Kumi 2022-08-09 06:27:55 +00:00
parent edb2b9c4c5
commit e533710345
Signed by: kumi
GPG key ID: ECBCC9082395383F
9 changed files with 108 additions and 2 deletions

View file

18
datastore/forms/auth.py Normal file
View file

@ -0,0 +1,18 @@
from django import forms
from django.contrib.auth.hashers import make_password
from django.core.exceptions import ValidationError
from ..models.auth import APIUser
class APIUserForm(forms.ModelForm):
model = APIUser
fields = ["username", "password"]
def clean_password(self):
raw = self.cleaned_data['password']
if not raw:
return ValidationError("You did not enter a password.")
return make_password(raw)

View file

View file

@ -0,0 +1,5 @@
import uuid
def get_upload_path(instance, filename):
return f"{str(uuid.uuid4)}/{filename}"

View file

@ -1,6 +1,9 @@
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
from django.db import models
from django.utils import timezone
from django.contrib.auth.hashers import check_password
import uuid
from ..managers import UserManager
@ -9,7 +12,7 @@ class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField('email address', unique=True)
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
date_joined = models.DateTimeField(default=timezone.now)
date_joined = models.DateTimeField(auto_now_add=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
@ -18,3 +21,21 @@ class User(AbstractBaseUser, PermissionsMixin):
def __str__(self):
return self.email
class APIUser(models.Model):
username = models.CharField(max_length=256)
password = models.CharField(max_length=256)
def check_password(self, password):
return check_password(password, self.password)
class APIToken(models.Model):
value = models.UUIDField(default=uuid.uuid4)
user = models.ForeignKey(APIUser, models.CASCADE)
expiry = models.DateTimeField()
created_at = models.DateTimeField(auto_now_add=True)
def is_valid(self):
return timezone.now() < self.expiry

View file

22
datastore/models/crew.py Normal file
View file

@ -0,0 +1,22 @@
from django.db import models
from .places import Nationality, ZipPlaces
class CrewMember(models.Model):
pin = models.IntegerField(primary_key=True)
first_name = models.CharField(max_length=128)
middle_name = models.CharField(max_length=128, null=True, blank=True)
last_name = models.CharField(max_length=128)
calling_name = models.CharField(max_length=128)
nationality = models.ForeignKey(Nationality, models.PROTECT)
place_of_birth = models.CharField(max_length=128)
first_address = models.ForeignKey(Address, models.PROTECT)
second_address = models.ForeignKey(Address, models.PROTECT, null=True)
class Address(models.Model):
address = models.TextField()
zipplace = models.ForeignKey(ZipPlaces, models.PROTECT)

View file

@ -0,0 +1,38 @@
from django.db import models
from base64 import b64encode
from ..helpers.uploads import get_upload_path
"""
These models are used by Adonis to return tens of thousands of
lines of unneeded data.
"""
class Airport(models.Model):
city_code = models.CharField(max_length=256, null=True, blank=True)
code = models.CharField(max_length=256, null=True, blank=True, unique=True)
country_name = models.CharField(max_length=256, null=True, blank=True)
name = models.CharField(max_length=256, null=True, blank=True)
class ZipPlaces(models.Model):
postcode = models.CharField(max_length=64)
postplace = models.CharField(max_length=256)
country = models.ForeignKey(Nationality, models.PROTECT)
class Nationality(models.Model):
country = models.ForeignKey(Country, models.PROTECT)
class Country(models.Model):
code = models.CharField(max_length=16)
text = models.CharField(max_length=128)
class PhoneCode(models.Model):
code = models.CharField(max_length=16)
flag = models.ImageField(null=True, upload_to=get_upload_path)
text = models.CharField(max_length=128)
@property
def flag_base64(self):
return b64encode(self.flag.read())

View file

@ -2,6 +2,8 @@ Django
django-autosecretkey
pillow
# For MySQL
mysqlclient