Use ORM for applicants
This commit is contained in:
31
lib/domain/Applicant.py
Normal file
31
lib/domain/Applicant.py
Normal file
@@ -0,0 +1,31 @@
|
||||
from peewee import CharField, UUIDField, SmallIntegerField, TextField, ForeignKeyField, JOIN
|
||||
|
||||
from lib.domain.Town import Town
|
||||
from lib.domain.BaseModel import BaseModel
|
||||
from lib.domain.ZipCode import ZipCode
|
||||
|
||||
|
||||
class Applicant(BaseModel):
|
||||
class Meta:
|
||||
table_name = "applicants"
|
||||
|
||||
id = UUIDField(primary_key=True)
|
||||
title = SmallIntegerField(default=0)
|
||||
first_name = CharField(null=False)
|
||||
last_name = CharField(null=False)
|
||||
street = CharField()
|
||||
house_number = CharField()
|
||||
zip_code = ForeignKeyField(ZipCode, column_name="zip_code", null=True)
|
||||
phone_number = CharField()
|
||||
mobile_number = CharField()
|
||||
email_address = CharField()
|
||||
salutation = TextField(null=False)
|
||||
|
||||
@classmethod
|
||||
def select_table_data(cls):
|
||||
return (
|
||||
Applicant
|
||||
.select(Applicant.id, Applicant.first_name, Applicant.last_name, ZipCode.id, ZipCode.zip_code, Town.town)
|
||||
.join(ZipCode, join_type="LEFT JOIN")
|
||||
.join(Town, join_type="LEFT JOIN")
|
||||
)
|
||||
8
lib/domain/BaseModel.py
Normal file
8
lib/domain/BaseModel.py
Normal file
@@ -0,0 +1,8 @@
|
||||
from peewee import Model, MySQLDatabase
|
||||
|
||||
database = MySQLDatabase(None)
|
||||
|
||||
|
||||
class BaseModel(Model):
|
||||
class Meta:
|
||||
database = database
|
||||
14
lib/domain/Country.py
Normal file
14
lib/domain/Country.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from peewee import AutoField, CharField
|
||||
|
||||
from lib.domain.BaseModel import BaseModel
|
||||
|
||||
|
||||
class Country(BaseModel):
|
||||
class Meta:
|
||||
table_name = "country"
|
||||
id = AutoField(column_name="countryid")
|
||||
name = CharField(max_length=200, unique=True)
|
||||
name_short = CharField(max_length=100, column_name="countryshort")
|
||||
nationality = CharField(max_length=100)
|
||||
iso2 = CharField(max_length=2, unique=True)
|
||||
iso3 = CharField(max_length=3, unique=True)
|
||||
12
lib/domain/Town.py
Normal file
12
lib/domain/Town.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from peewee import AutoField, CharField, ForeignKeyField
|
||||
|
||||
from lib.domain.BaseModel import BaseModel
|
||||
from lib.domain.Country import Country
|
||||
|
||||
|
||||
class Town(BaseModel):
|
||||
class Meta:
|
||||
table_name = "address"
|
||||
id = AutoField(column_name="addressid")
|
||||
town = CharField(max_length=50, column_name="city")
|
||||
country = ForeignKeyField(Country, column_name="countryid", backref="towns")
|
||||
13
lib/domain/ZipCode.py
Normal file
13
lib/domain/ZipCode.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from peewee import AutoField, CharField, ForeignKeyField
|
||||
|
||||
from lib.domain.BaseModel import BaseModel
|
||||
from lib.domain.Town import Town
|
||||
|
||||
|
||||
class ZipCode(BaseModel):
|
||||
class Meta:
|
||||
table_name = "postcode"
|
||||
|
||||
id = AutoField(column_name="postcodeid")
|
||||
zip_code = CharField(max_length=15, column_name="postcode")
|
||||
town = ForeignKeyField(Town, backref="zip_codes", column_name="addressid")
|
||||
Reference in New Issue
Block a user