def modify_news(id): news = data.execute("SELECT * FROM News WHERE n_id = ?", id) if empty(news) or session['username'] != news[0]['creator']: flash("You are not permitted to edit this newsitem") return redirect(url_front()) news = news[0] if request.method == 'POST': if 'cancel' in request.form: return redirect(url_front()) b = data.Bucket(request.form) if 'delete' in request.form: b >> ("DELETE FROM News WHERE n_id = ?", id) if b.title == "": flash("Please enter a title") return html.back() b.text b >> ("UPDATE News $ WHERE n_id = ?", id) return redirect(url_front()) else: w = html.WebBuilder() w.form() w.formtable() w.textfield("title", "Overskrift") w.textarea("text", "Tekst") w.html('<button type="submit" name="delete" value="delete">Slet nyhed</button>', "") form = w.create(news) return render_template("form.html", form=form)
def add_news(): if request.method == 'POST': if 'cancel' in request.form: return redirect(url_front()) creator = session['username'] created = now() title = request.form['title'] get_flashed_messages() if title == "": flash("Please enter a title") return html.back() text = request.form['text'] data.execute("INSERT INTO News(creator, created, title, text) VALUES(?,?,?,?)", creator, created, title, text) return redirect(url_front()) else: w = html.WebBuilder() w.form() w.formtable() w.textfield("title", "Overskrift") w.textarea("text", "Tekst") form = w.create() return render_template("form.html", form=form)
def new(key): time.sleep(random.randint(2, 6)) # EXPLANATION: weed out old creation keys delete_old_keys() # EXPLANATION: Check if key exists/is valid result = data.execute( "SELECT key, email FROM User_creation_keys WHERE key = ?", key) if empty(result): time.sleep(random.randint(5, 21)) # TODO: Send to errorpage? return redirect(url_front()) if request.method == "POST": if 'cancel' in request.form: data.execute("DELETE FROM User_creation_keys WHERE key = ?", key) flash("Oprettelse anulleret") return redirect(url_front()) b = data.Bucket(request.form) if not sanitize_username(b.username): flash("Ugyldigt brugernavn") return html.back() if b.password1 != b.password2: flash("Du gav to forskellige løsener, prøv igen") return html.back() if b.password1 == "": flash("Du skal vælge et løsen") return html.back() data.execute("DELETE FROM User_creation_keys WHERE key = ?", key) create_user(b.username, b.password1, b.name, b.email) flash("Ny bruger oprettet") session['logged_in'] = True session['username'] = b.username return redirect(url_for("usermanager.settings")) else: wb = html.WebBuilder() wb.form() wb.formtable() wb.textfield("username", "Brugernavn (Hvad du bliver kaldt på DIKU):") wb.textfield("name", "Fulde navn:") wb.textfield("email", "Email:", value=result[0]["email"]) wb.password("password1", "Løsen") wb.password("password2", "Gentag løsen") form = wb.create() return render_template("form.html", form=form)
def new(key): time.sleep(random.randint(2,6)) # EXPLANATION: weed out old creation keys delete_old_keys() # EXPLANATION: Check if key exists/is valid result = data.execute("SELECT key, email FROM User_creation_keys WHERE key = ?", key) if empty(result): time.sleep(random.randint(5,21)) # TODO: Send to errorpage? return redirect(url_front()) if request.method == "POST": if 'cancel' in request.form: data.execute("DELETE FROM User_creation_keys WHERE key = ?", key) flash("Oprettelse anulleret") return redirect(url_front()) b = data.Bucket(request.form) if not sanitize_username(b.username): flash("Ugyldigt brugernavn") return html.back() if b.password1 != b.password2: flash("Du gav to forskellige løsener, prøv igen") return html.back() if b.password1 == "": flash("Du skal vælge et løsen") return html.back() data.execute("DELETE FROM User_creation_keys WHERE key = ?", key) create_user(b.username, b.password1, b.name, b.email) flash("Ny bruger oprettet") session['logged_in'] = True session['username'] = b.username return redirect(url_for("usermanager.settings")) else: wb = html.WebBuilder() wb.form() wb.formtable() wb.textfield("username", "Brugernavn (Hvad du bliver kaldt på DIKU):") wb.textfield("name", "Fulde navn:") wb.textfield("email", "Email:", value=result[0]["email"]) wb.password("password1", "Løsen") wb.password("password2", "Gentag løsen") form = wb.create() return render_template("form.html", form=form)
def invite(): if request.method == "POST": if 'cancel' in request.form: return redirect(url_front()) email_addresses = request.form['email'] for email_address in email_addresses.splitlines(): key = generate_key() url = config.URL + url_for("usermanager.new", key=key) text = invite_mail.format(url=url) data.execute( "UPDATE User_creation_keys SET email = ? WHERE key = ?", email_address, key) mail.send(email_address, "Invitation til GRIS", text) flash("Invitationer sendt") message = mail.invitation_send_adminmail.format( email=request.form['email']) mail.admin("User Invited", message) return redirect(url_for("usermanager.overview")) else: w = html.WebBuilder() w.form() w.formtable() w.textarea("email", "Emails (1 pr. linje)") form = w.create() return render_template("form.html", form=form)
def invite(): if request.method == "POST": if 'cancel' in request.form: return redirect(url_front()) email_addresses = request.form['email'] for email_address in email_addresses.splitlines(): key = generate_key() url = config.URL + url_for("usermanager.new", key=key) text = invite_mail.format(url=url) data.execute("UPDATE User_creation_keys SET email = ? WHERE key = ?", email_address, key) mail.send(email_address, "Invitation til GRIS", text) flash("Invitationer sendt") message = mail.invitation_send_adminmail.format(email=request.form['email']) mail.admin("User Invited", message) return redirect(url_for("usermanager.overview")) else: w = html.WebBuilder() w.form() w.formtable() w.textarea("email", "Emails (1 pr. linje)") form = w.create() return render_template("form.html", form=form)
def login(): error = None if request.method == 'POST': username = request.form['username'] raw_password = request.form['password'] if 'forgot' in request.form: try: forgot_password(username) except Exception as e: if str(e) != "No such user/No valid email": raise flash("Kunne ikke sende en mail til denne bruger") return redirect(url_for('usermanager.login')) return render_template("usermanager/forgot.html", username=username) user = data.execute('SELECT password, deleted FROM Users WHERE username = ?', username) if empty(user) or not password.check(raw_password, user[0]['password']): flash('Invalid username or password') elif user[0]["deleted"]: flash('Sorry, your user has been deleted') else: session['logged_in'] = True session['username'] = username groups = data.execute('SELECT groupname FROM Group_users WHERE username = ?', username) groups = [group['groupname'] for group in groups] session['groups'] = groups update_password(username, raw_password) flash("Login succesful") return redirect(session.pop('login_origin', url_front())) return render_template("usermanager/login.html", error=error)
def settings(m_id): if request.method == "POST": if 'cancel' in request.form: return redirect(url_front()) b = data.Bucket(request.form) if b.mentor_names == "": b.mentor_names = "Unavngivet mentorhold" if b.year.isdecimal(): b.year = int(b.year) else: flash("Please enter a valid year") return html.back() b >> ("UPDATE Mentorteams $ WHERE m_id = ?", m_id) mentors = request.form['mentors'] mentors = mentors.replace('"', '') mentors = mentors.replace('"', '') mentors = [name.split()[0] for name in re.split(';\s', mentors) if name != ""] old = data.execute("SELECT username FROM Mentors WHERE m_id = ?", m_id) old = [mentor['username'] for mentor in old] for mentor in set(old) - set(mentors): data.execute("DELETE FROM Mentors WHERE m_id = ? and username = ?", m_id, mentor) for mentor in sorted(set(mentors) - set(old)): data.execute("INSERT INTO Mentors(m_id, username) VALUES (?, ?)", m_id, mentor) return redirect(url_for('mentorteams.mentorteam', m_id=m_id)) else: teams = data.execute("SELECT * FROM Mentorteams WHERE m_id = ?", m_id) if len(teams) != 1: flash(escape("Det hold findes ikke")) return redirect(url_for("mentorteams.overview")) team = teams[0] all_mentors = data.execute("SELECT * FROM Users WHERE username IN (Select username from Group_users where groupname = 'mentor')") all_mentors = ['\\"{0}\\" {1}'.format(mentor['username'], mentor['name']) for mentor in all_mentors] all_mentors.sort() actual_mentors = data.execute("SELECT * FROM Mentors INNER JOIN Users USING(username) WHERE m_id = ?", m_id) actual_mentors = ['"{0}" {1}; '.format(mentor['username'], mentor['name']) for mentor in actual_mentors] actual_mentors.sort() actual_mentors ="".join(actual_mentors) w = html.WebBuilder() w.form() w.formtable() w.textfield("mentor_names", "Navn") w.textfield("year", "År") w.html(html.autocomplete_multiple(all_mentors, "mentors", default=actual_mentors), description="Mentorer", value="abekat") form = w.create(team) return render_template("mentorteams/settings.html", form=form)
def renew_password(key): # EXPLANATION: weed out old password keys delete_old_keys() result = data.execute( "SELECT * FROM User_forgotten_password_keys WHERE key = ?", key) if len(result) != 1: flash("Linket du fulgte er desvære udløbet, prøv igen") return redirect(url_front()) result = result[0] if request.method == "POST": b = data.Bucket(request.form) if b.new1 != b.new2: flash("De to løsner er ikke ens") return redirect(url_for('usermanager.renew_password', key=key)) if b.new1 == "": flash("Du specificerede ikke et nyt løsen") return redirect(url_for('usermanager.renew_password', key=key)) data.execute("DELETE FROM User_forgotten_password_keys WHERE key = ?", key) update_password(result['username'], b.new1) session['logged_in'] = True session['username'] = result['username'] return redirect(url_front()) else: w = html.WebBuilder() w.form() w.formtable() w.password("new1", "Nyt løsen") w.password("new2", "Gentag nyt løsen") form = w.create() return render_template("form.html", form=form)
def renew_password(key): # EXPLANATION: weed out old password keys delete_old_keys() result = data.execute("SELECT * FROM User_forgotten_password_keys WHERE key = ?", key) if len(result) != 1: flash("Linket du fulgte er desvære udløbet, prøv igen") return redirect(url_front()) result = result[0] if request.method == "POST": b = data.Bucket(request.form) if b.new1 != b.new2: flash("De to løsner er ikke ens") return redirect(url_for('usermanager.renew_password', key=key)) if b.new1 == "": flash("Du specificerede ikke et nyt løsen") return redirect(url_for('usermanager.renew_password', key=key)) data.execute("DELETE FROM User_forgotten_password_keys WHERE key = ?", key) update_password(result['username'], b.new1) session['logged_in'] = True session['username'] = result['username'] return redirect(url_front()) else: w = html.WebBuilder() w.form() w.formtable() w.password("new1", "Nyt løsen") w.password("new2", "Gentag nyt løsen") form = w.create() return render_template("form.html", form=form)
def login(): error = None if request.method == 'POST': username = request.form['username'] raw_password = request.form['password'] if 'forgot' in request.form: try: forgot_password(username) except Exception as e: if str(e) != "No such user/No valid email": raise flash("Kunne ikke sende en mail til denne bruger") return redirect(url_for('usermanager.login')) return render_template("usermanager/forgot.html", username=username) user = data.execute( 'SELECT password, deleted FROM Users WHERE username = ?', username) if empty(user) or not password.check(raw_password, user[0]['password']): flash('Invalid username or password') elif user[0]["deleted"]: flash('Sorry, your user has been deleted') else: session['logged_in'] = True session['username'] = username groups = data.execute( 'SELECT groupname FROM Group_users WHERE username = ?', username) groups = [group['groupname'] for group in groups] session['groups'] = groups update_password(username, raw_password) flash("Login succesful") return redirect(session.pop('login_origin', url_front())) return render_template("usermanager/login.html", error=error)
def settings(t_id): if request.method == "POST": if 'cancel' in request.form: return redirect(url_front()) b = data.Bucket(request.form) b.theme b.type b.notes if b.tour_name == "": b.tour_name = "Unavngiven rustur" if b.year.isdecimal(): b.year = int(b.year) else: flash("Please enter a valid year") return html.back() b >> ("UPDATE Tours $ WHERE t_id = ?", t_id) tutors = request.form['tutors'] tutors = tutors.replace('"', '') tutors = tutors.replace('"', '') tutors = [name.split()[0] for name in re.split(';\s', tutors) if name != ""] old = data.execute("SELECT username FROM Tours_tutors WHERE t_id = ?", t_id) old = [tutor['username'] for tutor in old] for tutor in set(old) - set(tutors): data.execute("DELETE FROM Tours_tutors WHERE t_id = ? and username = ?", t_id, tutor) for tutor in sorted(set(tutors) - set(old)): try: data.execute("INSERT INTO Tours_tutors(t_id, username) VALUES (?, ?)", t_id, tutor) except psycopg2.IntegrityError as e: if e.pgerror.startswith('ERROR: insert or update on table "tours_tutors" violates foreign key constraint "tours_tutors_username_fkey"'): flash("No tutor: "+tutor) raise return redirect(url_for('rustours.rustour', t_id=t_id)) else: tours = data.execute("SELECT * FROM Tours WHERE t_id = ?", t_id) if len(tours) != 1: flash(escape("Den tur findes ikke")) return redirect(url_for("rustours.overview")) tour = tours[0] all_tutors = data.execute("SELECT * FROM Users WHERE username IN (Select username from Group_users where groupname = 'rkg')") all_tutors = ['\\"{0}\\" {1}'.format(tutor['username'], tutor['name']) for tutor in all_tutors] all_tutors.sort() actual_tutors = data.execute("SELECT * FROM Tours_tutors INNER JOIN Users USING(username) WHERE t_id = ?", t_id) actual_tutors = ['"{0}" {1}; '.format(tutor['username'], tutor['name']) for tutor in actual_tutors] actual_tutors.sort() actual_tutors = "".join(actual_tutors) w = html.WebBuilder() w.form() w.formtable() w.textfield("tour_name", "Navn") w.textfield("theme", "Tema") w.textfield("year", "År") w.select("type", "Type", [('p', 'Pigetur'), ('t', 'Transetur'), ('m', 'Munketur')]) w.html(html.autocomplete_multiple(all_tutors, "tutors", default=actual_tutors), description="Vejledere", value="abekat") w.textarea("notes", "Noter") form = w.create(tour) return render_template("rustours/settings.html", form=form, t_id=t_id)
def settings(t_id): if request.method == "POST": if 'cancel' in request.form: return redirect(url_front()) b = data.Bucket(request.form) b.theme b.type b.notes if b.tour_name == "": b.tour_name = "Unavngiven rustur" if b.year.isdecimal(): b.year = int(b.year) else: flash("Please enter a valid year") return html.back() b >> ("UPDATE Tours $ WHERE t_id = ?", t_id) tutors = request.form['tutors'] tutors = tutors.replace('"', '') tutors = tutors.replace('"', '') tutors = [ name.split()[0] for name in re.split(';\s', tutors) if name != "" ] old = data.execute("SELECT username FROM Tours_tutors WHERE t_id = ?", t_id) old = [tutor['username'] for tutor in old] for tutor in set(old) - set(tutors): data.execute( "DELETE FROM Tours_tutors WHERE t_id = ? and username = ?", t_id, tutor) for tutor in sorted(set(tutors) - set(old)): try: data.execute( "INSERT INTO Tours_tutors(t_id, username) VALUES (?, ?)", t_id, tutor) except psycopg2.IntegrityError as e: if e.pgerror.startswith( 'ERROR: insert or update on table "tours_tutors" violates foreign key constraint "tours_tutors_username_fkey"' ): flash("No tutor: " + tutor) raise return redirect(url_for('rustours.rustour', t_id=t_id)) else: tours = data.execute("SELECT * FROM Tours WHERE t_id = ?", t_id) if len(tours) != 1: flash(escape("Den tur findes ikke")) return redirect(url_for("rustours.overview")) tour = tours[0] all_tutors = data.execute( "SELECT * FROM Users WHERE username IN (Select username from Group_users where groupname = 'rkg')" ) all_tutors = [ '\\"{0}\\" {1}'.format(tutor['username'], tutor['name']) for tutor in all_tutors ] all_tutors.sort() actual_tutors = data.execute( "SELECT * FROM Tours_tutors INNER JOIN Users USING(username) WHERE t_id = ?", t_id) actual_tutors = [ '"{0}" {1}; '.format(tutor['username'], tutor['name']) for tutor in actual_tutors ] actual_tutors.sort() actual_tutors = "".join(actual_tutors) w = html.WebBuilder() w.form() w.formtable() w.textfield("tour_name", "Navn") w.textfield("theme", "Tema") w.textfield("year", "År") w.select("type", "Type", [('p', 'Pigetur'), ('t', 'Transetur'), ('m', 'Munketur')]) w.html(html.autocomplete_multiple(all_tutors, "tutors", default=actual_tutors), description="Vejledere", value="abekat") w.textarea("notes", "Noter") form = w.create(tour) return render_template("rustours/settings.html", form=form, t_id=t_id)