From f83b3d5f8200890f1fdb05fac2d477b642102dca Mon Sep 17 00:00:00 2001 From: vincent Date: Wed, 26 May 2021 13:55:56 +0200 Subject: [PATCH] modify DB --- ITPlanning/app.py | 2 +- ITPlanning/db.py | 33 +++++++++++++++++++++------------ 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/ITPlanning/app.py b/ITPlanning/app.py index 01d181a..f73b59f 100644 --- a/ITPlanning/app.py +++ b/ITPlanning/app.py @@ -4,8 +4,8 @@ from logging.handlers import TimedRotatingFileHandler from flask import Flask from pony.flask import Pony from ITPlanning.api import api -from ITPlanning.db import init_database, release_database #from ITPlanning.frontend import frontend +from ITPlanning.db import init_database from ITPlanning.config import IniConfig logger = logging.getLogger("ITPlanning") diff --git a/ITPlanning/db.py b/ITPlanning/db.py index ca16157..fac4267 100644 --- a/ITPlanning/db.py +++ b/ITPlanning/db.py @@ -2,28 +2,30 @@ from datetime import datetime 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() + class Service(db.Entity): id = PrimaryKey(int, auto=True) name = Required(str, unique=True) localisation = Optional(str) max_appointement = Required(int) - plannings = Set('Planning') - service_category = Required('Service_category') - appointments = Set('Appointment') + plannings = Set("Planning") + service_category = Required("Service_category") + appointments = Set("Appointment") description = Optional(str) class Planning(db.Entity): id = PrimaryKey(int, auto=True) name = Required(str, unique=True) - working_plan = Required(LongStr) + working_plan = Required(Json) services = Set(Service) - appointments = Set('Appointment') - planners = Set('Planner') + appointments = Set("Appointment") + planners = Set("Planner") class Setting(db.Entity): @@ -41,8 +43,8 @@ class Appointment(db.Entity): is_unavaillable = Required(bool) service = Required(Service) planning = Required(Planning) - ticket = Optional('Ticket') - customer = Optional('Customer') + ticket = Optional("Ticket") + customer = Optional("Customer") notes = Optional(LongStr) @@ -54,7 +56,7 @@ class Customer(db.Entity): localisation = Optional(str) mobile_number = Optional(str) phone_number = Optional(str) - tickets = Set('Ticket') + tickets = Set("Ticket") appointments = Set(Appointment) notes = Optional(str) @@ -62,7 +64,7 @@ class Customer(db.Entity): class Ticket(db.Entity): id = PrimaryKey(int, auto=True) ref = Optional(str, unique=True) - service_category = Required('Service_category') + service_category = Required("Service_category") customer = Required(Customer) appointment = Required(Appointment) notes = Optional(str) @@ -83,6 +85,7 @@ class Planner(Customer): password = Required(str) is_admin = Optional(bool) + def parse_uri(database_uri): if not isinstance(database_uri, str): raise TypeError("Expecting a string") @@ -122,10 +125,16 @@ def parse_uri(database_uri): ) return dict() + def init_database(database_uri): settings = parse_uri(database_uri) 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(): db.disconnect()