|
1 | 1 | importos |
2 | 2 | fromdatetimeimportdatetime |
3 | | - |
4 | | -fromflaskimportFlask, redirect, render_template, request, send_from_directory, url_for |
| 3 | +fromflaskimportFlask, jsonify, redirect, render_template, request, send_from_directory, url_for |
5 | 4 | fromflask_migrateimportMigrate |
6 | 5 | fromflask_sqlalchemyimportSQLAlchemy |
7 | 6 | fromflask_wtf.csrfimportCSRFProtect |
8 | | - |
| 7 | +fromsqlalchemyimportMetaData |
9 | 8 |
|
10 | 9 | app=Flask(__name__, static_folder='static') |
11 | 10 | csrf=CSRFProtect(app) |
|
36 | 35 |
|
37 | 36 | @app.route('/', methods=['GET']) |
38 | 37 | defindex(): |
39 | | -print('Request for index page received') |
40 | | -restaurants=Restaurant.query.all() |
41 | | -returnrender_template('index.html', restaurants=restaurants) |
42 | | - |
43 | | -@app.route('/<int:id>', methods=['GET']) |
44 | | -defdetails(id): |
45 | | -restaurant=Restaurant.query.where(Restaurant.id==id).first() |
46 | | -reviews=Review.query.where(Review.restaurant==id) |
47 | | -returnrender_template('details.html', restaurant=restaurant, reviews=reviews) |
48 | | - |
49 | | -@app.route('/create', methods=['GET']) |
50 | | -defcreate_restaurant(): |
51 | | -print('Request for add restaurant page received') |
52 | | -returnrender_template('create_restaurant.html') |
53 | | - |
54 | | -@app.route('/add', methods=['POST']) |
55 | | -@csrf.exempt |
56 | | -defadd_restaurant(): |
57 | | -try: |
58 | | -name=request.values.get('restaurant_name') |
59 | | -street_address=request.values.get('street_address') |
60 | | -description=request.values.get('description') |
61 | | -except (KeyError): |
62 | | -# Redisplay the question voting form. |
63 | | -returnrender_template('add_restaurant.html',{ |
64 | | -'error_message': "You must include a restaurant name, address, and description", |
65 | | - }) |
66 | | -else: |
67 | | -restaurant=Restaurant() |
68 | | -restaurant.name=name |
69 | | -restaurant.street_address=street_address |
70 | | -restaurant.description=description |
71 | | -db.session.add(restaurant) |
72 | | -db.session.commit() |
| 38 | +returnrender_template('index.html') |
| 39 | + |
| 40 | +@app.route('/dining_hall/<name>', methods= ['GET']) |
| 41 | +defdiningHall(name): |
| 42 | + |
| 43 | +# TODO Write SQL Query to get all food items at dining hall ->{name} |
| 44 | + |
| 45 | +sql=f''' |
| 46 | + SELECT NULL |
| 47 | + ''' |
| 48 | + |
| 49 | +withdb.engine.connect() asconn: |
| 50 | +result=conn.execute(db.text(sql)).fetchall() |
| 51 | + |
| 52 | +rows= [dict(row._mapping) forrowinresult] |
| 53 | +returnrender_template('index.html', results=rows) |
| 54 | + |
| 55 | +@app.route('/meat/<name>', methods= ['GET']) |
| 56 | +defmeat(name): |
73 | 57 |
|
74 | | -returnredirect(url_for('details', id=restaurant.id)) |
| 58 | +# TODO Write SQL Query to get all food items containing meat ->{name} |
75 | 59 |
|
76 | | -@app.route('/review/<int:id>', methods=['POST']) |
| 60 | +sql=f''' |
| 61 | + SELECT NULL |
| 62 | + ''' |
| 63 | + |
| 64 | +withdb.engine.connect() asconn: |
| 65 | +result=conn.execute(db.text(sql)).fetchall() |
| 66 | + |
| 67 | +rows= [dict(row._mapping) forrowinresult] |
| 68 | +returnrender_template('index.html', results=rows) |
| 69 | + |
| 70 | +@app.route('/meal_type/<name>', methods= ['GET']) |
| 71 | +defmealType(name): |
| 72 | +name=name.lower() |
| 73 | + |
| 74 | +# TODO Write SQL Query to get all food item served during meal_type ->{name} |
| 75 | + |
| 76 | +sql=f''' |
| 77 | + SELECT NULL |
| 78 | + ''' |
| 79 | + |
| 80 | +withdb.engine.connect() asconn: |
| 81 | +result=conn.execute(db.text(sql)).fetchall() |
| 82 | + |
| 83 | +rows= [dict(row._mapping) forrowinresult] |
| 84 | +returnrender_template('index.html', results=rows) |
| 85 | + |
| 86 | + |
| 87 | +@app.route('/add_dhall', methods=['POST']) |
77 | 88 | @csrf.exempt |
78 | | -defadd_review(id): |
| 89 | +defadd_dhall(): |
79 | 90 | try: |
80 | | -user_name=request.values.get('user_name') |
81 | | -rating=request.values.get('rating') |
82 | | -review_text=request.values.get('review_text') |
83 | | -except (KeyError): |
84 | | -#Redisplay the question voting form. |
85 | | -returnrender_template('add_review.html',{ |
86 | | -'error_message': "Error adding review", |
| 91 | +name=request.form.get('name') |
| 92 | +ingredients=request.form.get('ingredients') |
| 93 | +station=request.form.get('station') |
| 94 | +location=request.form.get('location') |
| 95 | +meal_type=request.form.get('meal_type') |
| 96 | +item_id=request.form.get('item_id') # BIGINT |
| 97 | +exceptKeyErrorase: |
| 98 | +returnjsonify({'error': f'Missing parameter: {str(e)}'}), 400 |
| 99 | + |
| 100 | +sql=''' |
| 101 | + INSERT INTO public."Dhall" ("Name", "Ingredients", "Station", "Location", "Meal Type", "Item ID") |
| 102 | + VALUES (:name, :ingredients, :station, :location, :meal_type, :item_id) |
| 103 | + ''' |
| 104 | + |
| 105 | +withdb.engine.begin() asconn: |
| 106 | +conn.execute(db.text(sql),{ |
| 107 | +'name': name, |
| 108 | +'ingredients': ingredients, |
| 109 | +'station': station, |
| 110 | +'location': location, |
| 111 | +'meal_type': meal_type, |
| 112 | +'item_id': int(item_id) |
87 | 113 | }) |
88 | | -else: |
89 | | -review=Review() |
90 | | -review.restaurant=id |
91 | | -review.review_date=datetime.now() |
92 | | -review.user_name=user_name |
93 | | -review.rating=int(rating) |
94 | | -review.review_text=review_text |
95 | | -db.session.add(review) |
96 | | -db.session.commit() |
97 | | - |
98 | | -returnredirect(url_for('details', id=id)) |
99 | | - |
100 | | -@app.context_processor |
101 | | -defutility_processor(): |
102 | | -defstar_rating(id): |
103 | | -reviews=Review.query.where(Review.restaurant==id) |
104 | | - |
105 | | -ratings= [] |
106 | | -review_count=0 |
107 | | -forreviewinreviews: |
108 | | -ratings+= [review.rating] |
109 | | -review_count+=1 |
110 | | - |
111 | | -avg_rating=sum(ratings) /len(ratings) ifratingselse0 |
112 | | -stars_percent=round((avg_rating/5.0) *100) ifreview_count>0else0 |
113 | | -return{'avg_rating': avg_rating, 'review_count': review_count, 'stars_percent': stars_percent} |
114 | | - |
115 | | -returndict(star_rating=star_rating) |
| 114 | + |
| 115 | +returnredirect(url_for('index')) |
| 116 | + |
| 117 | +@app.route('/add_dhall_form', methods=['GET']) |
| 118 | +defadd_dhall_form(): |
| 119 | +returnrender_template('add.html') |
116 | 120 |
|
117 | 121 | @app.route('/favicon.ico') |
118 | 122 | deffavicon(): |
|
0 commit comments