-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·190 lines (163 loc) · 7.18 KB
/
app.py
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
from flask import Flask, flash, render_template, redirect, url_for, request, session
from flask_sqlalchemy import SQLAlchemy
from random import randint
import random
import models
import forms
import os
from ranked import RBO, getRankedList, convertToDict
app = Flask(__name__)
app.secret_key = 's3cr3t'
app.config.from_object('config')
db = SQLAlchemy(app, session_options={'autocommit': False})
toStoreID = -1
@app.route('/logout')
def logout():
session['logged_in']=False
return redirect(url_for('login'))
@app.route('/display/<userId>')
def discover_page(userId):
# memes = db.session.query(models.Meme).all()
memes = db.session.query(models.Meme).filter(models.Meme.memeid < 301)
return render_template('meme-pg.html',memes=memes, userId=userId)
@app.route('/results/<userId>')
def match_results(userId):
currentUser = db.session.query(models.tagcount).filter_by(uid=userId)
nameList=None
partnerList = []
if(currentUser.count()!=0):
partners = db.session.query(models.tagcount).filter(models.tagcount.uid != userId).all()
myDict = convertToDict(currentUser[0])
myTagList = getRankedList(myDict)
potentialPartners = {}
for p in partners:
partnerDict = convertToDict(p)
pList = getRankedList(partnerDict)
potentialPartners[p.uid] = RBO(myTagList, pList)
finalPartners = getRankedList(potentialPartners)
for partner in finalPartners:
user = db.session.query(models.Users).filter_by(uid=partner).one()
partnerList.append(user)
return render_template('match-results-pg.html', partners=partnerList, userId=userId)
@app.route('/', methods=['GET', 'POST'])
def login():
error = None
if request.method == 'POST':
loweredName = request.form['username'].lower()
users = db.session.query(models.Users).filter_by(name=loweredName)
if(users.count()!=0):
particularUser = users[0]
session['name']=particularUser.name
password = users[0].password
if request.form['password'] != password:
error = 'Invalid Credentials. Please try again.'
else:
session['logged_in']=True
return redirect(url_for('profile_page',userId=particularUser.uid))
else:
error = 'Invalid Crendentials. Please try again.'
return render_template('layout.html', error=error)
@app.route('/<error>', methods=['GET', 'POST'])
def loginerror(error):
if request.method == 'POST':
loweredName = request.form['username'].lower()
users = db.session.query(models.Users).filter_by(name=loweredName)
if(users.count()!=0):
particularUser = users[0]
session['name']=particularUser.name
password = users[0].password
if request.form['password'] != password:
error = 'Invalid Credentials. Please try again.'
else:
session['logged_in']=True
return redirect(url_for('profile_page',userId=particularUser.uid))
else:
error = 'Invalid Crendentials. Please try again.'
return render_template('layout.html', error=error)
@app.route('/profile/<userId>')
def profile_page(userId):
users = db.session.query(models.tagcount).filter_by(uid=userId)
# tagcount
name = db.session.query(models.Users).filter_by(uid=userId)
numMemes = db.session.query(db.func.max(models.Meme.memeid)).scalar()
memeIDOne = randint(1,numMemes)
memeIDTwo = randint(1,numMemes)
memeIDThree = randint(1,numMemes)
memePicOne = db.session.query(models.Meme).filter_by(memeid=memeIDOne).one()
memePicTwo = db.session.query(models.Meme).filter_by(memeid=memeIDTwo).one()
memePicThree = db.session.query(models.Meme).filter_by(memeid=memeIDThree).one()
# user
particularUser = None
if(users.count()!=0):
particularUser = users[0]
particularName = name[0]
return render_template('profile-pg.html',particularUser=particularUser,particularName=particularName,userId=userId,
picOne=memePicOne.filepath,picTwo=memePicTwo.filepath,picThree=memePicThree.filepath)
@app.route('/memes/<userId>', methods = ['GET', 'POST'])
def landing_page(userId):
taggedMemes = db.session.query(models.hastag).all()
tagmemeID = []
for meme in taggedMemes:
tagmemeID.append(meme.memeid)
chooseFrom = list(set(tagmemeID))
randomMeme = random.choice(chooseFrom)
current = randomMeme
meme = db.session.query(models.Meme).filter(models.Meme.memeid == current).one()
if request.method == 'POST':
if request.form['submit'] == "yes":
opinion = models.Opinion(userId, current, 1)
db.session.add(opinion)
db.session.commit()
flash('+Record was successfully added')
elif request.form['submit'] == "no":
opinion = models.Opinion(userId, current, 0)
db.session.add(opinion)
db.session.commit()
flash('+Record was successfully added')
return render_template('meme-pg-new.html', meme=meme,userId=userId)
@app.route('/registration' , methods=['POST'])
def registration():
if request.method == 'POST':
loweredName = request.form['username'].lower()
users = db.session.query(models.Users).filter_by(name=loweredName)
largestUid = db.session.query(db.func.max(models.Users.uid)).scalar()
if(users.count()==0):
if(request.form['password']==request.form['confirm_password']):
newUser = models.Users(largestUid+1,str(loweredName),str(request.form['password']),str(request.form['fblink']),1)
db.session.add(newUser)
db.session.commit()
flash('+Record was successfully added')
return redirect(url_for('login'))
else:
error = 'Passwords do not match!'
return redirect(url_for('loginerror', error=error))
else:
error = 'User already exists!'
return redirect(url_for('loginerror', error=error))
@app.route('/tags/<userId>', methods = ['GET', 'POST'])
def assign_tags(userId):
global toStoreID
taggedMemes = db.session.query(models.hastag).all()
everyMeme = db.session.query(models.Meme).all()
tagmemeID = []
everymemeID = []
for meme in taggedMemes:
tagmemeID.append(meme.memeid)
for meme in everyMeme:
everymemeID.append(meme.memeid)
untaggedMemeID = list(set(everymemeID)-set(tagmemeID))
tagMeme = random.choice(untaggedMemeID)
if toStoreID == -1:
toStoreID=tagMeme
meme = db.session.query(models.Meme).filter(models.Meme.memeid == tagMeme).one()
if request.method == 'POST':
selected = request.form.getlist('tag')
for i in selected:
hastag = models.hastag(toStoreID, i)
db.session.add(hastag)
db.session.commit()
toStoreID = tagMeme
return render_template('tag-pg.html', meme = meme,userId=userId)
if __name__ == '__main__':
port = int(os.environ.get("PORT",5000))
app.run(host='0.0.0.0', port=port)