View raw

1 # -*- mode: python; -*- 2 3 import os 4 from datetime import datetime 5 import app.models as model 6 from app import create_app, db 7 8 9 app = create_app() 10 db_name = "test.db" 11 app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///" + db_name 12 db_path = f"app/{db_name}" 13 14 15 if os.path.exists(db_path): 16 os.remove(db_path) 17 print(f"Existing database {db_path} has been deleted successfully.\n") 18 else: 19 print(f"Database {db_path} does not exist yet, creating now.\n") 20 21 with app.app_context(): 22 db.create_all() 23 24 25 modules = [ 26 { 27 "name": "calculator", 28 "description": "Determine fertilizer recipes based on your farm model and desired crops for the season.", 29 }, 30 { 31 "name": "creator", 32 "description": "Create a model of your farm and get an idea of baseline water usage.", 33 }, 34 { 35 "name": "customers", 36 "description": "View and add customers.", 37 }, 38 { 39 "name": "ferti", 40 "description": "Record all your measurements and plot trends.", 41 }, 42 { 43 "name": "invoices", 44 "description": "Create invoices populated with customer and order informations.", 45 }, 46 { 47 "name": "orders", 48 "description": "Log and filter orders and their payment status.", 49 }, 50 { 51 "name": "products", 52 "description": "View and add products made by the farm.", 53 }, 54 { 55 "name": "stock", 56 "description": "Keep track of your inventory.", 57 }, 58 ] 59 60 61 users = [ 62 { 63 # "primary_key": 64 "username": "dingles", 65 "hashed_password": "foo", 66 "name_first": "Dingles", 67 "name_last": "Dingleberry", 68 "email": "dingleberryd@example.com", 69 "phone_mobile": 888_420_8888, 70 "phone_alternative": 694_206_9420, 71 }, 72 { 73 # "primary_key": 74 "username": "dingles", 75 "hashed_password": "foo", 76 "name_first": "Dingles", 77 "name_last": "Dingleberry", 78 "email": "dingleberryd@example.com", 79 "phone_mobile": 888_420_8888, 80 "phone_alternative": 694_206_9420, 81 }, 82 { 83 # "primary_key": 84 "username": "dingles", 85 "hashed_password": "foo", 86 "name_first": "Dingles", 87 "name_last": "Dingleberry", 88 "email": "dingleberryd@example.com", 89 "phone_mobile": 888_420_8888, 90 "phone_alternative": 694_206_9420, 91 }, 92 { 93 # "primary_key": 94 "username": "dingles", 95 "hashed_password": "foo", 96 "name_first": "Dingles", 97 "name_last": "Dingleberry", 98 "email": "dingleberryd@example.com", 99 "phone_mobile": 888_420_8888, 100 "phone_alternative": 694_206_9420, 101 }, 102 { 103 # "primary_key": 104 "username": "dingles", 105 "hashed_password": "foo", 106 "name_first": "Dingles", 107 "name_last": "Dingleberry", 108 "email": "dingleberryd@example.com", 109 "phone_mobile": 888_420_8888, 110 "phone_alternative": 694_206_9420, 111 }, 112 ] 113 114 customers = 5 * [ 115 { 116 "name": "Intermarché", 117 "name_alternative": "Le grand magasin", 118 "code_customer": "101010", 119 "code_accounting": "foobar200", 120 "address": "123 rue du Marché", 121 "postal_code": "01630", 122 "city": "Challex", 123 "country": "France", 124 "phone": 888_000_8888, 125 "website": "lesupermarché@example.com", 126 "email": "lesupermarché@example.com", 127 "professional_id_1": "1", 128 "professional_id_2": "2", 129 "tax_id": "IMPOTS", 130 "payment_terms": "immédiatement", 131 } 132 ] 133 134 135 def populate_table(table, items): 136 """The table is a class inheriting from db.Model. The items are a 137 list of dictionaries each representing an entry.""" 138 for item in items: 139 new_record = table(**item) 140 print(f"Adding {item}") 141 db.session.add(new_record) 142 db.session.commit() 143 144 print(f"Successfully committed all items to {table.__tablename__}.\n") 145 146 147 with app.app_context(): 148 populate_table(model.Module, modules) 149 populate_table(model.User, users) 150 populate_table(model.Customer, customers) 151