-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOLDproject3.py
More file actions
232 lines (189 loc) · 9.26 KB
/
Copy pathOLDproject3.py
File metadata and controls
232 lines (189 loc) · 9.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# ---------------python functinality imports----------------------------------
from flask import (Flask, render_template, request, redirect, jsonify, url_for, flash, g, abort)
from flask import session as login_session
import random
import string
import httplib2
import json
from flask import make_response
import requests
# ----------------------------------------------------------------------
# ----------- google sign in -----------------------------
from oauth2client.client import flow_from_clientsecrets
from oauth2client.client import FlowExchangeError
# --------------------------------------------------------
# ------------------------- DB imports---------------------
from sqlalchemy import create_engine, asc, desc
from sqlalchemy.orm import sessionmaker
from db_setup import Base, User, Category, Item
# ---------------------------------------------------------
# ----- login and security imports-------------------------
from flask_httpauth import HTTPBasicAuth
auth = HTTPBasicAuth()
# ---------------------------------------------------------
app = Flask(__name__)
# connect to the data base and create a session-----------
engine = create_engine('sqlite:///catalogApp.db')
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
session = DBSession()
# ---------------------------------------------------------
@auth.verify_password
def verify_password(userEmail, password):
user = session.query(User).filter_by(t_email=userEmail).first()
if not user or not user.verify_password(password):
return False
g.user = user
return True
@app.route('/')
@app.route('/CatalogApp')
def mainPage():
if 'user_email' in login_session:
print(login_session['user_email'])
categories = session.query(Category).all()
latestItems = session.query(Item).order_by(desc(Item.t_id)).limit(7)
return render_template('privateMain.html', mainCategories = categories, mainItems = latestItems, current_user=login_session['user_email'])
else:
print(" nothin in login session")
categories = session.query(Category).all()
latestItems = session.query(Item).order_by(desc(Item.t_id)).limit(7)
return render_template('publicMain.html', mainCategories = categories, mainItems = latestItems)
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['usermail']
password = request.form['userpass']
if verify_password(username,password):
current_user = g.user
categories = session.query(Category).all()
latestItems = session.query(Item).order_by(desc(Item.t_id)).limit(7)
login_session['user_name'] = current_user.t_name
login_session['user_email'] = current_user.t_email
login_session['user_picture'] = current_user.t_picture
login_session['user_id'] = current_user.t_id
print(login_session['user_name'])
print(login_session['user_email'])
print(login_session['user_picture'])
print(login_session['user_id'])
return render_template('privateMain.html', mainCategories = categories, mainItems = latestItems, current_user=current_user.t_email)
else:
print("Error in login credentials")
return render_template('login.html')
else:
print("this is the GET and first response")
return render_template('login.html')
@app.route('/logout')
def logout():
# remove the user email from the session
login_session.pop('user_email', None)
return redirect(url_for('mainPage'))
@app.route('/users', methods=['POST'])
def new_user():
if request.method == 'POST':
new_user = User(t_name=request.form['newusername'],t_email=request.form['newusermail'], t_picture='Nothing at all nothing at all')
new_user.hash_password(request.form['newuserpass'])
session.add(new_user)
session.commit()
return redirect(url_for('login'))
else:
print("this is by default the first action its a GET request")
return render_template('login.html')
@app.route('/CatalogApp/<category_name>/items', methods=['GET', 'POST'])
def catPage(category_name):
if 'user_email' in login_session:
categories = session.query(Category).all()
for cati in categories:
if cati.t_catName == category_name:
catx = cati.t_id
latestItems = session.query(Item).filter_by(t_catId=catx)
totalItems = session.query(Item).filter_by(t_catId=catx).count()
return render_template('categoryX.html', catpageCatName=category_name, catpageItems=latestItems, catpageCategories=categories, t_items=totalItems, current_user=login_session['user_email'])
else:
print ('there is nothin is login session')
categories = session.query(Category).all()
for cati in categories:
if cati.t_catName == category_name:
catx = cati.t_id
latestItems = session.query(Item).filter_by(t_catId=catx)
totalItems = session.query(Item).filter_by(t_catId=catx).count()
print(totalItems)
return render_template('publicCategoryX.html', catpageCatName=category_name, catpageItems=latestItems, catpageCategories=categories, t_items=totalItems)
@app.route('/CatalogApp/<category_name>/new_item', methods=['GET', 'POST'])
def newItemPage(category_name):
if 'user_email' in login_session:
categories = session.query(Category).all()
for cati in categories:
if cati.t_catName == category_name:
catx = cati.t_id
if request.method == 'POST':
new_item = Item(t_itemName=request.form['item_name'], t_itemDescription=request.form['i_description'], t_userId=1,t_catId=catx)
session.add(new_item)
session.commit()
flash("new item added to the database")
return redirect(url_for('mainPage'))
else: # the method used in this call is GET
print("this is a get call")
return render_template('newItem.html', current_cat=category_name, current_user=login_session['user_email'])
else:
return redirect(url_for('login'))
@app.route('/CatalogApp/<category_name>/<item_name>', methods=['GET', 'POST'])
def itemPage(category_name, item_name):
if 'user_email' in login_session:
itemX = session.query(Item).filter_by(t_itemName=item_name).first()
return render_template('itemDesc.html', itempage=itemX, category_name=category_name, current_user=login_session['user_email'])
else:
itemX = session.query(Item).filter_by(t_itemName=item_name).first()
return render_template('publicItemDesc.html', itempage=itemX, category_name=category_name)
@app.route('/CatalogApp/<item_name>/edit', methods=['GET', 'POST'])
def editItemPage(item_name):
if 'user_email' in login_session:
edit_item = session.query(Item).filter_by(t_itemName=item_name).one()
categories = session.query(Category).all()
for cati in categories:
if cati.t_id == edit_item.t_catId:
catx = cati
if request.method == 'POST':
# values received
print("receiving values from the form")
print("item name: %s" % request.form['newItemName'])
print("item description: %s" % request.form['newDescription'])
print("item catid: %s" % request.form['categories'])
edit_item.t_itemName = request.form['newItemName']
edit_item.t_itemDescription = request.form['newDescription']
edit_item.t_userId = 1
edit_item.t_catId = int(request.form['categories'])
session.add(edit_item)
session.commit()
flash("item edited")
return redirect(url_for('mainPage'))
else:
print("this is the GET call and its the first part to run")
return render_template('editItem.html', current_item=edit_item, current_cat=catx, current_user=login_session['user_email'])
return "render_template('itemDesc.html') "
else:
return redirect(url_for('login'))
@app.route('/CatalogApp/<item_name>/delete', methods=['GET', 'POST'])
def deleteItemPage(item_name):
if 'user_email' in login_session:
delete_item = session.query(Item).filter_by(t_itemName=item_name).one()
categories = session.query(Category).all()
for cati in categories:
if cati.t_id == delete_item.t_catId:
catx = cati
if request.method == 'POST':
session.delete(delete_item)
session.commit()
flash("item deleted")
return redirect(url_for('mainPage'))
else:
return render_template('deleteItem.html', current_item=delete_item, current_cat=catx, current_user=login_session['user_email'])
else:
return redirect(url_for('login'))
@app.route('/CatalogApp/JSON')
def allCatalogJSON():
products = session.query(Item).all()
return jsonify(allproducts=[p.serialize for p in products])
if __name__ == '__main__':
app.secret_key = 'do not share this baby'
app.debug = True
app.run(host='0.0.0.0', port=8000)