From 81913d2d1b1793f7f230c5d1502c730c64f2721e Mon Sep 17 00:00:00 2001 From: Wout Schellaert Date: Sat, 4 Apr 2015 14:11:55 +0200 Subject: [PATCH] Refactored create_database, put every addition in seperate file --- .gitignore | 3 +- app/create_database.py | 98 ----------------------------------- database/add_admins.py | 20 +++++++ database/add_oceans_garden.py | 37 +++++++++++++ database/add_simpizza.py | 20 +++++++ database/create_database.py | 67 ++++++++++++++++++++++++ {db => database}/muhscheme | 0 7 files changed, 145 insertions(+), 100 deletions(-) delete mode 100644 app/create_database.py create mode 100644 database/add_admins.py create mode 100644 database/add_oceans_garden.py create mode 100644 database/add_simpizza.py create mode 100644 database/create_database.py rename {db => database}/muhscheme (100%) diff --git a/.gitignore b/.gitignore index 18f9f43..5e55b1a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ # Byte-compiled / optimized / DLL files __pycache__/ -*.py[cod] # C extensions *.so @@ -58,7 +57,7 @@ target/ # ConfigFile app/config.py -# Do not add db file +# Do not add database file *.db tmp/ diff --git a/app/create_database.py b/app/create_database.py deleted file mode 100644 index 75113ed..0000000 --- a/app/create_database.py +++ /dev/null @@ -1,98 +0,0 @@ -from models import * -from app import db -from itertools import product - - -def recreate_db(): - db.drop_all() - db.create_all() - - feli = User() - feli.configure("feliciaan", True, 0) - db.session.add(feli) - - destro = User() - destro.configure('destro', True, 0) - db.session.add(destro) - - iepoev = User() - iepoev.configure('iepoev', True, 1) - db.session.add(iepoev) - # To future developers, add yourself here - -############################################ -# Chinees autogenerate # -############################################ -zetmelen = ["Nasi", "Bami"] -vlezen = ["Rundsvlees", "Varkensvlees"] -sauzen = ["Balisaus", "Yu siang saus", "Gon boa saus", "Curry saus", "Oestersaus", "Zwarte pepersaus", - "Champignons", "Chinese champignons", "A la Maleisïe"] -specials = ["Kippenbolletjes zoetzuur", "varkenbolletjes zoetzuur", "Nazi Babi Pangang", "Bami Babi Pangang", - "Diverse groenten met bami(Vegetarisch)", "Diverse groenten met nazi(Vegetarisch)"] - - -def add_chinees(): - chinees = Location() - chinees.configure("Oceans's Garden", "Zwijnaardsesteenweg 399 9000 Gent, tel: 09/222.72.74", "http://oceangarden.byethost3.com/studentenmenus.html") - db.session.add(chinees) - - def chinees_create_entry(zetmeel, vlees="", saus=""): - entry = Product() - entry.configure(chinees, "{} {} {}".format(zetmeel, vlees, saus).rstrip(), 550) - db.session.add(entry) - - for zetmeel, vlees, saus in product(zetmelen, vlezen, sauzen): - chinees_create_entry(zetmeel, vlees, saus) - - for special in specials: - chinees_create_entry(special) - -############################################# - -############################################ -# Simpizza autogenerate # -############################################ -pizzas = ['Bolognese de luxe', 'Hawaï', 'Popeye', 'Pepperoni', 'Seafood', 'Hot pizzaaah!!!', 'Salmon delight', - 'Full option', 'Pitza kebab', 'Multi cheese', '4 Seasons', 'Mega fish', 'Creamy multi cheese', - 'Green fiësta', 'Chicken bbq', 'Funky chicken', 'Veggie', 'Meat lovers', 'Scampi mampi', 'Tabasco', - 'Chicken time', 'Meatballs', 'Tuna', 'Anchovy', 'Calzone', 'Bbq meatballs', 'Creamy chicken', 'Hot bolognese'] - - -def add_simpizza(): - simpizza = Location() - simpizza.configure("Sim-pizza", "De Pintelaan 252 9000 Gent, tel: 09/321.02.00", "http://simpizza.be") - db.session.add(simpizza) - - for pizza in pizzas: - entry = Product() - entry.configure(simpizza, pizza, 1195) - db.session.add(entry) - - -def commit(): - # commit all the things - db.session.commit() - print("Committing successful") - -locations = {'Ocean\'s Garden': add_chinees, 'Sim-pizza': add_simpizza} - -print('Create database script!') -print('=======================\n\n') - -db_key = 'recreate this database now!' -input_db = input("To recreate the db type: '" + db_key + "'") -if db_key in input_db: - print("recreating the database!") - recreate_db() - for location in locations.values(): - location() - commit() -else: - print("not recreating the database!") - # DID NOT RECREATE ASK IF YOU WANT TO RECREATE THE NEXT ONES - for name, function in locations.items(): - add_location = input("Do you want to add " + name + "? Press: 'Y'") - print("Adding " + name) - if add_location.lower() in ['y', 'yes']: - function() - commit() \ No newline at end of file diff --git a/database/add_admins.py b/database/add_admins.py new file mode 100644 index 0000000..70602bc --- /dev/null +++ b/database/add_admins.py @@ -0,0 +1,20 @@ +from app import db +from models import User + + +def add(): + db.drop_all() + db.create_all() + + feli = User() + feli.configure("feliciaan", True, 0) + db.session.add(feli) + + destro = User() + destro.configure('destro', True, 0) + db.session.add(destro) + + iepoev = User() + iepoev.configure('iepoev', True, 1) + db.session.add(iepoev) + # To future developers, add yourself here \ No newline at end of file diff --git a/database/add_oceans_garden.py b/database/add_oceans_garden.py new file mode 100644 index 0000000..df3ab53 --- /dev/null +++ b/database/add_oceans_garden.py @@ -0,0 +1,37 @@ +from models import Location, Product +from app import db +from itertools import product + + +zetmelen = ["Nasi", "Bami"] +vlezen = ["Rundsvlees", "Varkensvlees", "Kippenstukkjes"] +sauzen = ["Balisaus", "Yu siang saus", "Gon boa saus", "Curry saus", "Oestersaus", "Zwarte pepersaus", + "Champignons", "Chinese champignons", "A la Maleisïe"] + +specials = ["Nasi Kippenbolletjes Zoetzuur", "Bami Kippenbolletjes Zoetzuur", + "Nasi Varkenbolletjes Zoetzuur", "Bami Varkenbolletjes Zoetzuur", + "Nasi Babi Pangang", "Bami Babi Pangang", + "Diverse groenten met Bami", + "Diverse groenten met Nasi"] + + +def add(): + chinees = Location() + chinees.configure("Oceans's Garden", + "Zwijnaardsesteenweg 399 9000 Gent, tel: 09/222.72.74", + "http://oceangarden.byethost3.com/studentenmenus.html") + db.session.add(chinees) + + def chinees_create_entry(name): + entry = Product() + entry.configure(chinees, name, 550) + db.session.add(entry) + + def chinees_create_regulat(zetmeel, vlees="", saus=""): + chinees_create_entry("{} {} {}".format(zetmeel, vlees, saus).rstrip()) + + for z, v, s in product(zetmelen, vlezen, sauzen): + chinees_create_regulat(z, v, s) + + for special in specials: + chinees_create_entry(special) \ No newline at end of file diff --git a/database/add_simpizza.py b/database/add_simpizza.py new file mode 100644 index 0000000..59bfb2e --- /dev/null +++ b/database/add_simpizza.py @@ -0,0 +1,20 @@ +from models import Location, Product +from app import db + + +pizzas = ['Bolognese de luxe', 'Hawaï', 'Popeye', 'Pepperoni', 'Seafood', 'Hot pizzaaah!!!', + 'Salmon delight', 'Full option', 'Pitza kebab', 'Multi cheese', '4 Seasons', 'Mega fish', + 'Creamy multi cheese', 'Green fiësta', 'Chicken bbq', 'Funky chicken', 'Veggie', + 'Meat lovers' 'Scampi mampi', 'Tabasco', 'Chicken time', 'Meatballs', 'Tuna', 'Anchovy', + 'Calzone', 'Bbq meatballs', 'Creamy chicken', 'Hot bolognese'] + + +def add(): + simpizza = Location() + simpizza.configure("Sim-pizza", "De Pintelaan 252 9000 Gent, tel: 09/321.02.00", "http://simpizza.be") + db.session.add(simpizza) + + for pizza in pizzas: + entry = Product() + entry.configure(simpizza, pizza, 1195) + db.session.add(entry) \ No newline at end of file diff --git a/database/create_database.py b/database/create_database.py new file mode 100644 index 0000000..58de912 --- /dev/null +++ b/database/create_database.py @@ -0,0 +1,67 @@ +from app import db +from database import add_oceans_garden, add_admins, add_simpizza + + +locations = {"Admins": add_admins.add(), "Ocean's Garden": add_oceans_garden.add(), "SimPizza": add_simpizza.add()} +yes = ["yes", "y", "Y"] +no = ["no", "n", "N"] + + +# Commit all the things +def commit(): + db.session.commit() + print("Committing successful") + + +def check_if_overwrite(): + answer = input("Do you want to overwrite the previous database? (y/N) ") + return answer in yes + + +def add_all(): + for loc in locations.keys(): + print("Adding {}.".format(loc)) + locations[loc]() + + +def recreate_from_scratch(): + confirmation = "Are you very very sure? (Will delete previous entry's!) (y/N) " + check = "I acknowledge any repercussions!" + if input(confirmation) in yes and input('Type: "{}" ').format(check) == check: + print("Overwriting the database!") + add_all() + + +def add_to_current(): + available = [loc for loc in locations.keys()] + + def add_numbers(): + return " ".join(["{}({}), ".format(loc, i) for i, loc in enumerate(available)]).rstrip(", ") + + while input("Do you still want to add something? (Y/n) ") not in no: + print("What do you want to add? (Use numbers, or A for all, or C for chancel) ") + answer = input("Available: {} : ".format(add_numbers())) + if answer == "A": + add_all() + elif answer == "C": + pass + elif answer in [str(x) for x in range(len(available))]: + answer = int(answer) + print("Adding {}.".format(available[answer])) + locations[available[answer]]() + del available[answer] + else: + print("Not a valid answer.") + print("Thank you for adding, come again!") + + +def init(): + print('Database modification script!') + print('=============================\n\n') + if check_if_overwrite(): + recreate_from_scratch() + else: + add_to_current() + + +init() diff --git a/db/muhscheme b/database/muhscheme similarity index 100% rename from db/muhscheme rename to database/muhscheme