-@app.route('/votes/admin/new')
-def new_vote():
- if not session.get('logged_in'):
- abort(401)
- return render_template('new_vote.html')
-
-@app.route('/votes/admin/add', methods=['POST'])
-def add_vote():
- if not session.get('logged_in'):
- abort(401)
- daten = date.today() + timedelta(days=int(request.form['days']))
- ndate = daten.strftime('%d %B %Y')
- transparent = 0
- public = 0
- multiplechoice = 0
- if 'transparent' in request.form.keys():
- transparent = 1
- if 'public' in request.form.keys():
- public = 1
- if 'multiplechoice' in request.form.keys():
- multiplechoice = 1
- g.db.execute('insert into votes (title, description, date_end, is_transparent, is_public, is_multiplechoice) values (?, ?, ?, ?, ?, ?)',
- [request.form['title'], request.form['description'], ndate, transparent, public, multiplechoice])
+@app.route('/admin/votes/list')
+def admin_votes():
+ if not session.get('user').get('is_admin'):
+ abort(401)
+ votes = query_db('select *, votes.id as voteid, groups.name as groupname from votes join groups on groups.id=votes.id_group order by id desc')
+ return render_template('admin_votes.html', votes=votes)
+
+@app.route('/admin/votes/add', methods=['GET', 'POST'])
+def admin_vote_add():
+ if not session.get('user').get('is_admin'):
+ abort(401)
+ if request.method == 'POST':
+ if request.form['title']:
+ if query_db('select * from votes where title = ?', [request.form['title']], one=True) is None:
+ date_begin = date.today()
+ date_end = date.today() + timedelta(days=int(request.form['days']))
+ transparent = 0
+ public = 0
+ multiplechoice = 0
+ if 'transparent' in request.form.keys():
+ transparent = 1
+ if 'public' in request.form.keys():
+ public = 1
+ if 'multiplechoice' in request.form.keys():
+ multiplechoice = 1
+ group = query_db('select id from groups where name = ?', [request.form['group']], one=True)
+ if group is None:
+ group[id] = 1
+ g.db.execute('insert into votes (title, description, category, date_begin, date_end, is_transparent, is_public, is_multiplechoice, id_group, id_author) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
+ [request.form['title'], request.form['description'], request.form['category'], date_begin, date_end, transparent, public, multiplechoice, group['id'], session['user']['id']])
+ g.db.commit()
+ vote = query_db('select * from votes where title = ? and date_begin = ? order by id desc',
+ [request.form['title'], date_begin], one=True)
+ if vote is None:
+ flash(u'Une erreur est survenue !', 'error')
+ return redirect(url_for('home'))
+ else:
+ if request.form['pattern'] in PATTERNS.keys():
+ pattern = PATTERNS[request.form['pattern']]
+ for choice in pattern:
+ g.db.execute('insert into choices (name, id_vote) values (?, ?)', [choice, vote['id']])
+ g.db.commit()
+ flash(u"Le vote a été créé", 'info')
+ return redirect(url_for('admin_vote_edit', voteid=vote['id']))
+ else:
+ flash(u'Le titre que vous avez choisi est déjà pris.', 'error')
+ else:
+ flash(u'Vous devez spécifier un titre.', 'error')
+ groups = query_db('select * from groups')
+ return render_template('admin_vote_new.html', groups=groups, patterns=PATTERNS)
+
+@app.route('/admin/votes/edit/<voteid>', methods=['GET', 'POST'])
+def admin_vote_edit(voteid):
+ if not session.get('user').get('is_admin'):
+ abort(401)
+ vote = query_db('select * from votes where id = ?', [voteid], one=True)
+ if vote is None:
+ abort(404)
+ if request.method == 'POST':
+ if request.form['title']:
+ # :TODO:maethor:120529: Calculer date_begin pour pouvoir y ajouter duration et obtenir date_end
+ transparent = 0
+ public = 0
+ if 'transparent' in request.form.keys():
+ transparent = 1
+ if 'public' in request.form.keys():
+ public = 1
+ isopen = 0
+ isterminated = 0
+ if request.form['status'] == 'Ouvert':
+ choices = query_db('select id_vote, count(*) as nb from choices where id_vote = ? group by id_vote', [voteid], one=True)
+ if choices is not None and choices['nb'] >= 2:
+ isopen = 1
+ else:
+ flash(u'Vous devez proposer au moins deux choix pour ouvrir le vote.', 'error')
+ elif request.form['status'] == u'Terminé':
+ isterminated = 1
+ if vote['is_open']:
+ isopen = 1
+ g.db.execute('update votes set title = ?, description = ?, category = ?, is_transparent = ?, is_public = ?, is_open = ?, is_terminated = ? where id = ?',
+ [request.form['title'], request.form['description'], request.form['category'], transparent, public, isopen, isterminated, voteid])
+ g.db.commit()
+ vote = query_db('select * from votes where id = ?', [voteid], one=True)
+ flash(u"Le vote a bien été mis à jour.", "success")
+ else:
+ flash(u'Vous devez spécifier un titre.', 'error')
+
+ # :TODO:maethor:120529: Calculer la durée du vote (différence date_end - date_begin)
+ vote['duration'] = 15
+ group = query_db('select name from groups where id = ?', [vote['id_group']], one=True)
+ choices = query_db('select * from choices where id_vote = ?', [voteid])
+ attachments = query_db('select * from attachments where id_vote = ?', [voteid])
+ return render_template('admin_vote_edit.html', vote=vote, group=group, choices=choices, attachments=attachments)
+
+@app.route('/admin/votes/addchoice/<voteid>', methods=['POST'])
+def admin_vote_addchoice(voteid):
+ if not session.get('user').get('is_admin'):
+ abort(401)
+ vote = query_db('select * from votes where id = ?', [voteid], one=True)
+ if vote is None:
+ abort(404)
+ g.db.execute('insert into choices (name, id_vote) values (?, ?)', [request.form['title'], voteid])