Commit 3df37d03 authored by Mathis Dedial's avatar Mathis Dedial
Browse files

Merge branch 'ldap' into 'master'

Make use of setattr and getattr

See merge request !10
parents 9a00422e dc907c44
Pipeline #2726 passed with stage
in 23 seconds
......@@ -24,7 +24,7 @@ def index():
'LEFT JOIN vote AS vote_2 ON vote_2.burger_id=burger.id AND vote_2.nethz= :nethz '
'GROUP BY burger.id '
'ORDER BY vote_count DESC, timestamp')
burger_list = db.engine.execute(querystring, nethz=getattr(g, 'user', '')).fetchall()
burger_list = db.engine.execute(querystring, nethz=getattr(session, 'user', '')).fetchall()
return render_template('vote.html',
burger_list=burger_list,
bread=BREAD,
......@@ -54,12 +54,12 @@ def vote():
# User is casting their vote
if request.form.get('action') == 'like':
# Check if the user hasn't already voted for the same burger
if Vote.query.filter_by(nethz=g.user, burger_id=burger_id).first():
if Vote.query.filter_by(nethz=getattr(session, 'user', ''), burger_id=burger_id).first():
return die('You have already liked this burger.')
# Add vote
vote = Vote(
nethz=g.user,
nethz=getattr(session, 'user', None),
burger_id=burger_id,
)
db.session.add(vote)
......@@ -69,7 +69,7 @@ def vote():
# User is revoking their vote
elif request.form.get('action') == 'unlike':
# Find the vote
vote = Vote.query.filter_by(nethz=g.user, burger_id=burger_id).first()
vote = Vote.query.filter_by(nethz=getattr(session, 'user', None), burger_id=burger_id).first()
if vote:
# Delete the vote
......@@ -86,7 +86,7 @@ def login():
'''
Login page. LDAP-Login only.
'''
if getattr(g, 'user', None):
if getattr(session, 'user', None):
return redirect(url_for('index'))
if request.method == 'POST':
user = request.form['user']
......@@ -96,7 +96,7 @@ def login():
if test is None or passwd == '':
flash('Invalid credentials', 'error')
else:
setattr(g, 'user', user)
setattr(session, 'user', user)
# Create instance of BurgerUser
burger_user = BurgerUser(user)
# Log in BurgerUser
......@@ -111,7 +111,6 @@ def logout():
Logout page
'''
session.pop('nethz', None)
g.user = None
flash('Successfully logged out.')
return redirect(url_for('index'))
......@@ -159,7 +158,7 @@ def create():
return die('Please describe your burger in a few short sentences.')
# Check that the user hasn't already created a burger
if Burger.query.filter_by(nethz=g.user).first():
if Burger.query.filter_by(nethz=getattr(session, 'user', None)).first():
# In this case, send the user back to the voting page
flash('You can only create one burger.', 'error')
return redirect(url_for('index'))
......@@ -168,7 +167,7 @@ def create():
burger = Burger(
name=name,
description=description,
nethz=g.user,
nethz=getattr(session, 'user', None),
timestamp=datetime.now(),
bread=bread,
patty=patty,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment