This repository has been archived by the owner on Jan 17, 2020. It is now read-only.
/
VoTe.py
273 lines (248 loc) · 8.68 KB
/
VoTe.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
from flask import Flask,jsonify,request,session
from werkzeug.security import generate_password_hash, check_password_hash
from authpy import auth
from model import User,Topic,choice,db,match_email,string_matcher
from data import Cache,Ip
from flask import g,render_template,url_for,redirect,flash,send_file,send_from_directory
import sqlite3
import os
from werkzeug.utils import secure_filename
vote =Flask(__name__)
Auth=auth()
cache=Cache()
ip=Ip()
vote.config["SQLALCHEMY_DATABASE_URI"]='sqlite:///VOOTE'
#vote.config["SQLALCHEMY_TRACK_MODIFICATIONS"]=True
vote.config["SECRET_KEY"]="6g73vut6svs766rdfd"
vote.config['DATABASE']='myblog.db'
vote.config["UPLOAD_FOLDER"]="/home/haibeeyy/mysite/uploads"
Allowed_extensions=set(["png","jpeg"])
db.init_app(vote)
db.create_all(app=vote)
per_query=30
def onError(error=None):
"returns some json object with error"
if error:
j_data={
"response":"error",
"message":str(error)
}
else:
j_data={
"response":"error",
"message":"invalid parameters"
}
return jsonify(j_data)
def json_message(response,message):
return {
"response":response,
"message":message
}
@vote.route("/",methods=["GET"])
def home():
result={} #would hold the result for all data that would be sent back to the client
arg=request.args.get("name")
if arg:
user_topic=User.query.filter_by(email=arg).first().topics
t_opics=[]
incremental=1
for t_ in user_topic:
arr=[t_.id,t_.title,t_.count]
to_be_added={
"topic":arr
}
t_opics.append(to_be_added)
incremental=incremental+1
result["users"]=t_opics
else:
result["users"]=[]
category=int(request.args.get("category"))
all_topic=Topic.query.all()
all_topic.reverse()
t_opics=[]
for topic in all_topic[category*per_query-per_query:category*per_query]:
Dict={
"id":topic.id,
"title":topic.title,
"choices":[[choice.name,choice.count] for choice in topic.choices],
"created_by":topic.users.first_name,
"date":topic.date_created,
"count":topic.count
}
t_opics.append(Dict)
result["topics"]=t_opics
result["response"]="ok"
return jsonify(result)
@vote.route("/signup",methods=["POST","GET"])
def signup():
if request.method=="GET":
return jsonify(json_message("error","unsupported request method"))
first_name=request.form["firstname"]
last_name=request.form["lastname"]
email=request.form["email"]
password=request.form["password"]
if Auth.In(email):
if match_email(email):
if "file" not in request.files:
return jsonify(json_message("error","invalid filename"))
file=request.files["file"]
if not file.filename:
return jsonify(json_message("error","invalid filename"))
if not file or file.filename.split(".")[-1] in Allowed_extensions:
return jsonify(json_message("error","invalid file type"))
secure_name=secure_filename(file.filename)
file.save(os.path.join(vote.config["UPLOAD_FOLDER"],secure_name))
user=User(first_name=first_name,last_name=last_name,email=email,password=generate_password_hash(password),image="image")
Dict={
"response":"ok",
"first_name":first_name,
"last_name":last_name,
"email":email,
"message":"you are signed in"
}
db.session.add(user)
db.session.commit()
Auth.insert(email)
return jsonify(Dict)
else:
return jsonify(json_message("error","invalid email"))
else:
return jsonify(json_message("error","user present"))
@vote.route("/file/<filename>")
def send_the_file(filename):
try:
return send_from_directory(vote.config["UPLOAD_FOLDER"],filename)
except Exception as e:
return onError(e)
@vote.route("/logout")
def logout():
session["logged_in"]=False
return jsonify(json_message("ok","log out"))
@vote.route("/login",methods=['POST','GET'])
def login():
if request.method=="GET":
Dict={
"response":'error',
"message":"unsupported request method"
}
return jsonify(Dict)
email=request.form["email"]
password=request.form["password"]
user=User.query.filter_by(email=email).first()
if generate_password_hash(password)==user.password or 1:
session["logged_in"]=True
Dict={
"response":"ok",
"message":"successfully logged in",
"logged_in":True
}
return jsonify(Dict)
else:
Dict={
"response":"error",
"user":user.email,
"logged_in":False,
"message":"username or password does not match"
}
return jsonify(Dict)
@vote.route("/create_poll",methods=['POST','GET'])
def create_poll():
if True:
#gets the email stored at the front end
#gets the user
#choices would be sent as query and number
cache_=request.args.get("cache")
if cache_:
email=request.args.get("email")
name=request.args.get("name")
topic=request.args.get("topic")
imagename=request.args.get("image")
cache.new_entry(email,name,topic,0,imagename)
return jsonify(json_message("ok","new pending option added"))
save=None
if request.method=="POST":
save=request.form["save"]
if save:
if "file" not in request.files:
return jsonify(json_message("error","no file selected"))
file=request.files["file"]
secure_name=secure_filename(file.filename)
file.save(os.path.join(vote.config["UPLOAD_FOLDER"],secure_name))
return jsonify(json_message("ok","new file added"))
email=request.args.get("email")
user=User.query.filter_by(email=email).first()
Topic_=request.args.get("topic")
T=Topic(title=Topic_,count=0,users=user)
for choice_ in cache.get_entry(email):
T.choices.append(choice(name=choice_[0],count=0,Topic=T,imagename=choice_[3]))
ip.new_entry(Topic_,"")
user.topics.append(T)
db.session.commit()
cache.delete_entry(email)
return jsonify(json_message("ok","you craeted a new poll"))
else:
return jsonify(json_message("error","not logged in"))
@vote.route("/vote")
def cast_vote():
t_opic=request.args.get("topic")#name of topic
person=request.args.get("person")
load=request.args.get("load")
vote=request.args.get("vote")
id=request.args.get("id")
if load:
the_topic=Topic.query.filter_by(id=id).first()#list of available topics
if not the_topic:
return jsonify(json_message("error","cant find this topic"))
response={}
x=0
for choice_ in the_topic.choices:
add={
"name":choice_.name,
"count":choice_.count,
"imagename":choice_.imagename
}
response[str(x)]=add
x+=1
return jsonify(response)
elif vote:
Id=request.args.get("ip")
if not ip.address_in_title(t_opic,Id):
the_topic=Topic.query.filter_by(title=t_opic).first()#get the topic
c_hoice=request.args.get("choices")#user selected choice
for choice_e in the_topic.choices:
if choice_e.name==c_hoice:
choice_e.count+=1
db.session.commit()
ip.new_entry(t_opic,Id)
the_topic.count+=1
return jsonify(json_message("ok","you just vote"))
else:
return jsonify(json_message("ok","you voted before"))
else:
return jsonify(json_message("error","invalidd parameters"))
@vote.route("/search")
def search():
arg=request.args.get("query")
all_topic=Topic.query.all()
Res={}
for topic in all_topic:
if string_matcher(arg,topic.title):
Dict={
"title":topic.title,
"choices":topic.choices
}
Res[topic.title]=Dict
return jsonify(Res)
@vote.route("/profile")
def profile():
email=request.args.get("email")
user=User.query.filter_by(email=email).first()
Dict={
"first_name":user.first_name,
"last_name":user.last_name,
"image":user.image,
"date":user.date_created
}
return jsonify(Dict)
if __name__ == "__main__":
vote.run(debug=True)