-
Notifications
You must be signed in to change notification settings - Fork 1
/
oberPoll.py
191 lines (132 loc) · 5.39 KB
/
oberPoll.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
from flask import *
from models import db, Users, Polls, Topics, Options, UserPolls
from werkzeug.security import generate_password_hash, check_password_hash
from flask_migrate import Migrate
from datetime import datetime
from config import SQLALCHEMY_DATABASE_URI
oberPoll = Flask(__name__)
#Loading config from config.py
oberPoll.config.from_object('config')
db.init_app(oberPoll)
db.create_all(app=oberPoll)
migrate = Migrate(oberPoll, db, render_as_batch=True)
#Home page
@oberPoll.route('/')
def home():
return render_template('index.html')
#Signing up user
@oberPoll.route('/signup', methods=['GET','POST'])
def signup():
#if Post
if request.method == 'POST':
#Get details from form
email = request.form['email']
username = request.form['username']
password = request.form['password']
#Hash password for security
password = generate_password_hash(password)
user = Users(email=email, username=username, password=password)
db.session.add(user)
db.session.commit()
flash('Congratulations! Your account has been created! Please login.')
return redirect(url_for('home'))
#Render the template if its a GET request
return render_template('signup.html')
#Exists for no real reason
@oberPoll.route('/favicon.ico')
def test():
return "This sucks!"
#Logs user in
@oberPoll.route('/login', methods =['POST'])
def login():
username = request.form['username']
password = request.form['password']
user = Users.query.filter_by(username=username).first()
if user:
password_hash = user.password
if check_password_hash(password_hash, password):
session['user'] = username
flash('Login was successful! Yay!')
else:
flash('Username or password was incorrect. Please try again.', 'error')
return redirect(url_for('home'))
#Logs user out
@oberPoll.route('/logout')
def logout():
if 'user' in session:
session.pop('user')
flash('You have successfully been logged out!')
return redirect(url_for('home'))
#Redirects to createpoll.html
@oberPoll.route('/polls', methods=['GET'])
def createPoll():
return render_template('createpoll.html')
#Redirects back to index as all polls show up there
@oberPoll.route('/polls/<poll_name>')
def poll():
return render_template('index.html')
#API endpoint that returns JSON object for specific poll (poll_name = title)
@oberPoll.route('/api/poll/<poll_name>')
def api_poll(poll_name):
poll = Topics.query.filter(Topics.title.like(poll_name)).first()
return jsonify({'Polls': [poll.to_json()]}) if poll else jsonify({'message': 'poll not found'})
#Add and receive polls from or to the database
@oberPoll.route('/api/polls', methods=['GET', 'POST'])
def api_polls():
if request.method == 'POST':
#Get poll and save in the database
poll = request.get_json()
for key, value in poll.items():
if not value:
return jsonify({'message': 'value for {} is empty'.format(key)})
title = poll['title']
options_query = lambda option: Options.query.filter(Options.name.like(option))
options = [Polls(option=Options(name=option))
if options_query(option).count() == 0
else Polls(option=options_query(option).first()) for option in poll['options']]
new_topic = Topics(title=title, options=options)
db.session.add(new_topic)
db.session.commit()
return jsonify({'message': 'Poll was successfully created!'})
else:
#Query the database and return all polls as JSON objects
polls= Topics.query.join(Polls).order_by(Topics.id.desc()).all()
all_polls ={'Polls': [poll.to_json() for poll in polls]}
return jsonify(all_polls)
#Api end point that returns all options
@oberPoll.route('/api/polls/options')
def api_polls_options():
all_options = [option.to_json() for option in Options.query.all()]
return jsonify(all_options)
@oberPoll.route('/api/poll/close', methods=['PATCH'])
def api_poll_close():
poll = request.get_json()
poll_title, close = (poll['poll_title'], poll['close'])
join_tables = Polls.query.join(Topics).join(Options)
topic = Topics.query.filter_by(title=poll_title, status=True).first()
if not topic:
return jsonify({'message': 'Poll not found'})
if close == 1:
topic.status = False
db.session.commit()
return jsonify({'message': 'The poll was closed'})
#Api endpoint for voting
@oberPoll.route('/api/poll/vote', methods=['PATCH'])
def api_poll_vote():
poll = request.get_json()
poll_title, option = (poll['poll_title'], poll['option'])
join_tables = Polls.query.join(Topics).join(Options)
topic = Topics.query.filter_by(title=poll_title, status=True).first()
user = Users.query.filter_by(username=session['user']).first()
if not topic:
return jsonify({'message': 'Sorry! this poll has been closed'})
# filter options
option = join_tables.filter(Topics.title.like(poll_title)).filter(Options.name.like(option)).first()
# increment vote_count by 1 if the option was found
if option:
option.vote_count += 1
db.session.commit()
return jsonify({'message': 'Thank you for voting'})
return jsonify({'message': 'option or poll was not found please try again'})
if __name__ == "__main__":
oberPoll.run()