modify DB

This commit is contained in:
vincent 2021-05-26 13:55:56 +02:00
parent d91d0b398c
commit f83b3d5f82
2 changed files with 22 additions and 13 deletions

View File

@ -4,8 +4,8 @@ from logging.handlers import TimedRotatingFileHandler
from flask import Flask from flask import Flask
from pony.flask import Pony from pony.flask import Pony
from ITPlanning.api import api from ITPlanning.api import api
from ITPlanning.db import init_database, release_database
#from ITPlanning.frontend import frontend #from ITPlanning.frontend import frontend
from ITPlanning.db import init_database
from ITPlanning.config import IniConfig from ITPlanning.config import IniConfig
logger = logging.getLogger("ITPlanning") logger = logging.getLogger("ITPlanning")

View File

@ -2,28 +2,30 @@
from datetime import datetime from datetime import datetime
from urllib.parse import urlparse, parse_qsl from urllib.parse import urlparse, parse_qsl
from pony.orm import Database, Required, Optional, Set, PrimaryKey, LongStr from pony.orm import Database, Required, Optional, Set, PrimaryKey, LongStr, Json
from pony.orm import DatabaseError
db = Database() db = Database()
class Service(db.Entity): class Service(db.Entity):
id = PrimaryKey(int, auto=True) id = PrimaryKey(int, auto=True)
name = Required(str, unique=True) name = Required(str, unique=True)
localisation = Optional(str) localisation = Optional(str)
max_appointement = Required(int) max_appointement = Required(int)
plannings = Set('Planning') plannings = Set("Planning")
service_category = Required('Service_category') service_category = Required("Service_category")
appointments = Set('Appointment') appointments = Set("Appointment")
description = Optional(str) description = Optional(str)
class Planning(db.Entity): class Planning(db.Entity):
id = PrimaryKey(int, auto=True) id = PrimaryKey(int, auto=True)
name = Required(str, unique=True) name = Required(str, unique=True)
working_plan = Required(LongStr) working_plan = Required(Json)
services = Set(Service) services = Set(Service)
appointments = Set('Appointment') appointments = Set("Appointment")
planners = Set('Planner') planners = Set("Planner")
class Setting(db.Entity): class Setting(db.Entity):
@ -41,8 +43,8 @@ class Appointment(db.Entity):
is_unavaillable = Required(bool) is_unavaillable = Required(bool)
service = Required(Service) service = Required(Service)
planning = Required(Planning) planning = Required(Planning)
ticket = Optional('Ticket') ticket = Optional("Ticket")
customer = Optional('Customer') customer = Optional("Customer")
notes = Optional(LongStr) notes = Optional(LongStr)
@ -54,7 +56,7 @@ class Customer(db.Entity):
localisation = Optional(str) localisation = Optional(str)
mobile_number = Optional(str) mobile_number = Optional(str)
phone_number = Optional(str) phone_number = Optional(str)
tickets = Set('Ticket') tickets = Set("Ticket")
appointments = Set(Appointment) appointments = Set(Appointment)
notes = Optional(str) notes = Optional(str)
@ -62,7 +64,7 @@ class Customer(db.Entity):
class Ticket(db.Entity): class Ticket(db.Entity):
id = PrimaryKey(int, auto=True) id = PrimaryKey(int, auto=True)
ref = Optional(str, unique=True) ref = Optional(str, unique=True)
service_category = Required('Service_category') service_category = Required("Service_category")
customer = Required(Customer) customer = Required(Customer)
appointment = Required(Appointment) appointment = Required(Appointment)
notes = Optional(str) notes = Optional(str)
@ -83,6 +85,7 @@ class Planner(Customer):
password = Required(str) password = Required(str)
is_admin = Optional(bool) is_admin = Optional(bool)
def parse_uri(database_uri): def parse_uri(database_uri):
if not isinstance(database_uri, str): if not isinstance(database_uri, str):
raise TypeError("Expecting a string") raise TypeError("Expecting a string")
@ -122,10 +125,16 @@ def parse_uri(database_uri):
) )
return dict() return dict()
def init_database(database_uri): def init_database(database_uri):
settings = parse_uri(database_uri) settings = parse_uri(database_uri)
db.bind(**settings) db.bind(**settings)
db.generate_mapping(create_tables=True) db.generate_mapping(check_tables=False)
try:
db.check_tables()
except DatabaseError:
db.create_tables()
def release_database(): def release_database():
db.disconnect() db.disconnect()