From e36f9d42556057a2740a95b9c5efcc4a9a09bff0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathias=20W=C3=B8bbe?= Date: Tue, 6 Jun 2017 23:50:10 +0200 Subject: [PATCH 1/3] Flask application, routes to all the pages need for the app. Link to mock-up is found here: https://xd.adobe.com/view/10aed435-b28f-4924-a684-bcd0f5b91d76 --- VeganStuff/dummydata.py | 2 + VeganStuff/veganstuff.py | 139 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 VeganStuff/dummydata.py create mode 100644 VeganStuff/veganstuff.py diff --git a/VeganStuff/dummydata.py b/VeganStuff/dummydata.py new file mode 100644 index 0000000..8c7b35f --- /dev/null +++ b/VeganStuff/dummydata.py @@ -0,0 +1,2 @@ + +#TODO: Fill in the database with english items from your imagination. Keep it short but presentable. diff --git a/VeganStuff/veganstuff.py b/VeganStuff/veganstuff.py new file mode 100644 index 0000000..bc06c50 --- /dev/null +++ b/VeganStuff/veganstuff.py @@ -0,0 +1,139 @@ +from flask import Flask, render_template, request, redirect, jsonify, url_for + +# TODO: Make everything else here even though you should try to spread out the classes. Reference the MUB project. + +app = Flask(__name__) + + +@app.route('/') +@app.route('/home') +@app.route('/index') +@app.route('/categories/') +def index(): + print("working?") + # Show all categories + return render_template('index.html') + pass + + +@app.route('/categories//', methods=['GET', 'POST']) +def category(): + return render_template('index.html') + pass + + +@app.route('/categories//items/', methods=['GET', 'POST']) +def category_items(): + return render_template('index.html') + pass + + +@app.route('/categories//items//', methods=['GET', 'POST']) +def category_item(): + return render_template('index.html') + pass + + +@app.route('/categories//items/new', methods=['GET', 'POST']) +def category_item_new(): + return render_template('index.html') + pass + + +@app.route('/categories//items//update', methods=['GET', 'POST']) +def category_item_update(): + return render_template('index.html') + pass + + +@app.route('/categories//items//delete', methods=['GET', 'POST']) +def category_item_delete(): + return render_template('index.html') + pass + + +@app.route('/categories/new', methods=['GET', 'POST']) +def new_category(): + return render_template('index.html') + pass + + +@app.route('/categories//edit', methods=['GET', 'POST']) +def edit_category(): + return render_template('index.html') + pass + + +@app.route('/categories//delete', methods=['GET', 'POST']) +def delete_category(): + return render_template('index.html') + pass + + +@app.route('/shops/', methods=['GET', 'POST']) +def shops(): + return render_template('index.html') + pass + + +@app.route('/shops//', methods=['GET', 'POST']) +def shop(): + return render_template('index.html') + pass + + +@app.route('/shops/new', methods=['GET', 'POST']) +def shops_new(): + return render_template('index.html') + pass + + +@app.route('/shops//edit', methods=['GET', 'POST']) +def shops_update(): + return render_template('index.html') + pass + + +@app.route('/shops//delete', methods=['GET', 'POST']) +def shops_delete(): + return render_template('index.html') + pass + + +@app.route('/manufacturers/', methods=['GET', 'POST']) +def manufacturers(): + return render_template('index.html') + pass + + +@app.route('/manufacturers//', methods=['GET', 'POST']) +def manufacturer(): + return render_template('index.html') + pass + + +@app.route('/manufacturers/new', methods=['GET', 'POST']) +def manufacturer_new(): + return render_template('index.html') + pass + + +@app.route('/manufacturers//update', methods=['GET', 'POST']) +def manufacturer_update(): + return render_template('index.html') + pass + + +@app.route('/manufacturers//delete', methods=['GET', 'POST']) +def manufacturer_delete(): + return render_template('index.html') + pass + + +# Following routes are for creating a new entity from another page than the supposed one. Like from a 'New shop' button from the homepage. + + + +if __name__ == '__main__': + app.debug = True + app.run(host='0.0.0.0', port=5050) From 360cf2fc84e47bba682219208ec03eeda9ff7a8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathias=20W=C3=B8bbe?= Date: Tue, 6 Jun 2017 23:56:44 +0200 Subject: [PATCH 2/3] Flask application, still ony routes to all the pages need for the app. Link to mock-up is found here: https://xd.adobe.com/view/10aed435-b28f-4924-a684-bcd0f5b91d76 This time all the files are comitted, nothing really interesting by any means --- VeganStuff/README.md | 25 +++++++++++++++++++++++++ VeganStuff/models.py | 7 +++++++ VeganStuff/templates/index.html | 10 ++++++++++ 3 files changed, 42 insertions(+) create mode 100644 VeganStuff/README.md create mode 100644 VeganStuff/models.py create mode 100644 VeganStuff/templates/index.html diff --git a/VeganStuff/README.md b/VeganStuff/README.md new file mode 100644 index 0000000..90f24ae --- /dev/null +++ b/VeganStuff/README.md @@ -0,0 +1,25 @@ +# Project Name + +TODO: Write a project description + + +TODO: Describe the installation process + + +TODO: Write usage instructions + + +1. Fork it! +2. Create your feature branch: `git checkout -b my-new-feature` +3. Commit your changes: `git commit -am 'Add some feature'` +4. Push to the branch: `git push origin my-new-feature` +5. Submit a pull request :D + + +TODO: Write history + + +TODO: Write credits + + +TODO: Write license \ No newline at end of file diff --git a/VeganStuff/models.py b/VeganStuff/models.py new file mode 100644 index 0000000..46ef088 --- /dev/null +++ b/VeganStuff/models.py @@ -0,0 +1,7 @@ +from sqlalchemy import Column, ForeignKey, Integer, String +from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.orm import relationship +from sqlalchemy import create_engine + +Base = declarative_base() +# TODO: Create some database magic using sqlalchemy! \ No newline at end of file diff --git a/VeganStuff/templates/index.html b/VeganStuff/templates/index.html new file mode 100644 index 0000000..cfd4783 --- /dev/null +++ b/VeganStuff/templates/index.html @@ -0,0 +1,10 @@ + + + + + It works! + + + + + \ No newline at end of file From 6acc960be58f4732be4993093318a56e3f3369d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathias=20W=C3=B8bbe?= Date: Wed, 7 Jun 2017 22:20:11 +0200 Subject: [PATCH 3/3] Added DB models, everything should work as intended at this point. Routes are working and so are id's. Made new dummy HTML to check if results are as expected. --- VeganStuff/__init__.py | 0 VeganStuff/dbmodels.py | 89 +++++++++++++++++ VeganStuff/models.db | Bin 0 -> 24576 bytes VeganStuff/models.py | 7 -- VeganStuff/templates/category.html | 10 ++ VeganStuff/veganstuff.py | 154 ++++++++++++++++------------- 6 files changed, 182 insertions(+), 78 deletions(-) create mode 100644 VeganStuff/__init__.py create mode 100644 VeganStuff/dbmodels.py create mode 100644 VeganStuff/models.db delete mode 100644 VeganStuff/models.py create mode 100644 VeganStuff/templates/category.html diff --git a/VeganStuff/__init__.py b/VeganStuff/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/VeganStuff/dbmodels.py b/VeganStuff/dbmodels.py new file mode 100644 index 0000000..2b43a03 --- /dev/null +++ b/VeganStuff/dbmodels.py @@ -0,0 +1,89 @@ +import os +import sys +from sqlalchemy import Column, ForeignKey, NVARCHAR, INTEGER, BOOLEAN +from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.orm import relationship +from sqlalchemy import create_engine + +Base = declarative_base() + + +# TODO: Add picture path and add CRUD functionality, refer to those videos! + + +class Category(Base): + __tablename__ = 'category' + + id = Column(INTEGER, primary_key=True, nullable=False) + name = Column(NVARCHAR, nullable=False) + description = Column(NVARCHAR, nullable=False) + + @property + def serialize(self): + """Return object in easily serializable format""" + return { + 'name': self.name, + 'description': self.description, + 'Unique id': self.id + } + + +class Manufacturer(Base): + __tablename__ = 'manufacturer' + name = Column(NVARCHAR, primary_key=True, nullable=False) + description = Column(NVARCHAR, nullable=False) + + @property + def serialize(self): + """Return object in easily serializable format""" + return { + 'name and unique id': self.name, + 'description': self.description, + } + + +class Shop(Base): + __tablename__ = 'shop' + id = Column(INTEGER, primary_key=True, nullable=False) + name = Column(NVARCHAR, nullable=False) + description = Column(NVARCHAR, nullable=False) + + def serialize(self): + """Return object in easily serializable format""" + return { + 'name': self.name, + 'description': self.description, + } + + +class Item(Base): + __tablename__ = 'item' + id = Column(INTEGER, primary_key=True, nullable=False) + type = Column(BOOLEAN, nullable=False) + name = Column(NVARCHAR, nullable=False) + category = Column(INTEGER, ForeignKey('category.id'), nullable=False) + accidentally_vegan = Column(BOOLEAN, nullable=False) + ingredients = Column(NVARCHAR) + m_id = Column(NVARCHAR, ForeignKey('manufacturer.name'), nullable=False) + s_id = Column(INTEGER, ForeignKey('shop.id'), nullable=False) + # Make relationships so the tables know each other or something like that. + Category_id = relationship(Category) + Manufacturer_id = relationship(Manufacturer) + Shop_id = relationship(Shop) + + @property + def serialize(self): + """Return object in easily serializable format""" + return { + 'name': self.name, + 'category': self.category, + 'description': self.description, + 'shop id': self.s_id, + 'manufacturer': self.m_id, + 'unique id': self.id, + } + +# Make the database +engine = create_engine('sqlite:///models.db') + +Base.metadata.create_all(engine) diff --git a/VeganStuff/models.db b/VeganStuff/models.db new file mode 100644 index 0000000000000000000000000000000000000000..64534a1f92be03f3d4c566f0011543112a73543d GIT binary patch literal 24576 zcmeI%?`zXQ7zglV>kk*~IvJxN3?76+0uwC0@U2<6ZKXBUtO~;y7H8tIu zB|l_ami$oj%01_b^87YxG(RfU$A?PO-T&2m*0@cpI3NH42tWV=5P$##AOL~?L*U1_ zT5la4sXsdj|1=Tl_FyVY&2pGIWHdBS`;6oj(zQL`qpm$<=gc8{FeLkZzkfp7Ub2`8 zIvou9%(M#~0iO!8KbVenVHW@5@rlT~@(x=97xvow6ixcV?S%_GY+3ZqVWFu&$*GLJ+=y0rZ#8ku0>Kk?^qmPMf% z%l%3=)5~w!Ov@fws)d>UbN|Mf-aMh6h~3DaC4Lx`+piO+0X^BNx7uxWDYAf8(_5|Y z76M%DkxTNSBFZ>5z{}fB*y_009U<00Izz00iy?G+D*>|2q)WAOHafKmY;|fB*y_009U< z00LVmfbaiXIH^bp0uX=z1Rwwb2tWV=5P$##Ah0HY{r?&Wnh<~h1Rwwb2tWV=5P$## KAOL|a6!;CEQYx4L literal 0 HcmV?d00001 diff --git a/VeganStuff/models.py b/VeganStuff/models.py deleted file mode 100644 index 46ef088..0000000 --- a/VeganStuff/models.py +++ /dev/null @@ -1,7 +0,0 @@ -from sqlalchemy import Column, ForeignKey, Integer, String -from sqlalchemy.ext.declarative import declarative_base -from sqlalchemy.orm import relationship -from sqlalchemy import create_engine - -Base = declarative_base() -# TODO: Create some database magic using sqlalchemy! \ No newline at end of file diff --git a/VeganStuff/templates/category.html b/VeganStuff/templates/category.html new file mode 100644 index 0000000..65fd5c3 --- /dev/null +++ b/VeganStuff/templates/category.html @@ -0,0 +1,10 @@ + + + + + New category + + + + + \ No newline at end of file diff --git a/VeganStuff/veganstuff.py b/VeganStuff/veganstuff.py index bc06c50..3f00688 100644 --- a/VeganStuff/veganstuff.py +++ b/VeganStuff/veganstuff.py @@ -1,139 +1,151 @@ from flask import Flask, render_template, request, redirect, jsonify, url_for +from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker +from dbmodels import Base, Shop, Item, Manufacturer, Category # TODO: Make everything else here even though you should try to spread out the classes. Reference the MUB project. app = Flask(__name__) +engine = create_engine('sqlite:///models.db') +Base.metadata.bind = engine + +DBSession = sessionmaker(bind=engine) +sess = DBSession() + @app.route('/') @app.route('/home') @app.route('/index') @app.route('/categories/') def index(): - print("working?") - # Show all categories - return render_template('index.html') - pass + print("You are logged, muhahaha!") + # Show all categories + categories = sess.query(Category).all() + return render_template('index.html', category=categories) + pass @app.route('/categories//', methods=['GET', 'POST']) -def category(): - return render_template('index.html') - pass +def category(category_id): + categoriess = sess.query(Category).all + return render_template('category.html', categories=categoriess) + pass @app.route('/categories//items/', methods=['GET', 'POST']) -def category_items(): - return render_template('index.html') - pass +def category_items(category_id): + return render_template('index.html') + pass @app.route('/categories//items//', methods=['GET', 'POST']) -def category_item(): - return render_template('index.html') - pass +def category_item(category_id, item_id): + return render_template('index.html') + pass @app.route('/categories//items/new', methods=['GET', 'POST']) -def category_item_new(): - return render_template('index.html') - pass +def category_item_new(category_id): + return render_template('index.html') + pass @app.route('/categories//items//update', methods=['GET', 'POST']) -def category_item_update(): - return render_template('index.html') - pass +def category_item_update(category_id, item_id): + return render_template('index.html') + pass @app.route('/categories//items//delete', methods=['GET', 'POST']) -def category_item_delete(): - return render_template('index.html') - pass +def category_item_delete(category_id, item_id): + return render_template('index.html') + pass -@app.route('/categories/new', methods=['GET', 'POST']) +@app.route('/category/new', methods=['GET', 'POST']) def new_category(): - return render_template('index.html') - pass + return render_template('index.html') + pass -@app.route('/categories//edit', methods=['GET', 'POST']) -def edit_category(): - return render_template('index.html') - pass +@app.route('/category//edit', methods=['GET', 'POST']) +def edit_category(category_id): + return render_template('index.html') + pass -@app.route('/categories//delete', methods=['GET', 'POST']) -def delete_category(): - return render_template('index.html') - pass +@app.route('/category//delete', methods=['GET', 'POST']) +def delete_category(category_id): + return render_template('index.html') + pass @app.route('/shops/', methods=['GET', 'POST']) def shops(): - return render_template('index.html') - pass + return render_template('index.html') + pass @app.route('/shops//', methods=['GET', 'POST']) -def shop(): - return render_template('index.html') - pass +def shop(shops_id): + return render_template('index.html') + pass -@app.route('/shops/new', methods=['GET', 'POST']) +@app.route('/shop/new', methods=['GET', 'POST']) def shops_new(): - return render_template('index.html') - pass + return render_template('index.html') + pass -@app.route('/shops//edit', methods=['GET', 'POST']) -def shops_update(): - return render_template('index.html') - pass +@app.route('/shop//edit', methods=['GET', 'POST']) +def shops_update(shops_id): + return render_template('index.html') + pass -@app.route('/shops//delete', methods=['GET', 'POST']) -def shops_delete(): - return render_template('index.html') - pass +@app.route('/shop//delete', methods=['GET', 'POST']) +def shops_delete(shops_id): + return render_template('index.html') + pass @app.route('/manufacturers/', methods=['GET', 'POST']) def manufacturers(): - return render_template('index.html') - pass + return render_template('index.html') + pass @app.route('/manufacturers//', methods=['GET', 'POST']) -def manufacturer(): - return render_template('index.html') - pass +def manufacturer(manufacturer_id): + return render_template('index.html') + pass -@app.route('/manufacturers/new', methods=['GET', 'POST']) +@app.route('/manufacturer/new', methods=['GET', 'POST']) def manufacturer_new(): - return render_template('index.html') - pass - - -@app.route('/manufacturers//update', methods=['GET', 'POST']) -def manufacturer_update(): - return render_template('index.html') - pass - + return render_template('index.html') + pass -@app.route('/manufacturers//delete', methods=['GET', 'POST']) -def manufacturer_delete(): - return render_template('index.html') - pass +@app.route('/manufacturer//update', methods=['GET', 'POST']) +def manufacturer_update(manufacturer_id): + return render_template('index.html') + pass -# Following routes are for creating a new entity from another page than the supposed one. Like from a 'New shop' button from the homepage. +@app.route('/manufacturer//delete', methods=['GET', 'POST']) +def manufacturer_delete(manufacturer_id): + return render_template('index.html') + pass +""" +To actually be able to read from a database, remember to use id's AND call them, use unique naming whenever possible. +Conclusion of routes, add additional routes above this comment. +""" +# Standard convention. Call if this is run as the main module. if __name__ == '__main__': - app.debug = True - app.run(host='0.0.0.0', port=5050) + app.debug = True + app.run(host='0.0.0.0', port=5050)