all repos — myprecious @ 537992b95025dee96ceaf488d003a09bdea99dc4

A lightweight web service to backup precious game saves.

myprecious/views.py (view raw)

  1from myprecious import app, login_manager
  2from flask import request, redirect, render_template
  3from flask_login import login_user, logout_user, current_user
  4import myprecious.constants as c
  5from myprecious.utils import handle_response, parse_remember
  6from myprecious.auth import handle_register, handle_login, get_logged_user
  7from myprecious.db import get_queued_users, allow_user, deny_user
  8from myprecious.files import handle_upload
  9from myprecious.encoding import obj_decode
 10if app.debug:
 11    from myprecious.games_api_test import search_game
 12else:
 13    from myprecious.games_api import search_game
 14
 15def render(template, **context):
 16    return render_template(template, user=current_user, **context)
 17
 18@login_manager.user_loader
 19def load_user(user_id):
 20    return get_logged_user(user_id)
 21
 22@app.route('/')
 23def route_index():
 24    return render("index.html")
 25
 26@app.route('/login', methods=['GET', 'POST'])
 27def route_login():
 28    if current_user.is_authenticated:
 29        return redirect('/')
 30    if request.method == "GET":
 31        return render("login.html")
 32    
 33    form = request.form
 34    remember = parse_remember(form)
 35    
 36    error, user = handle_login(request.form)
 37    if error is None:
 38        login_user(user, remember=remember)
 39        return redirect("/")
 40    last_user = user["username"] if user else None
 41    return render("login.html", last_user=last_user, error=error)
 42    
 43
 44@app.route('/register', methods=['GET', 'POST'])
 45def route_register():
 46    if current_user.is_authenticated:
 47        return redirect('/')
 48    if request.method == "GET":
 49        return render("register.html")
 50    error = handle_register(request.form)
 51    if error is None:
 52        return render("done.html", text="Your registration request has been taken into account and will likely be processed in a few days.")
 53    return render("register.html", error=error)
 54    
 55@app.route('/logout')
 56def route_logout():
 57    logout_user()
 58    return redirect("/")
 59
 60@app.route('/search', methods=['GET', 'POST'])
 61def route_search():
 62    if not current_user.is_authenticated:
 63        return redirect('/login')
 64    if request.method == 'GET':
 65        return render("search.html")
 66    query = request.form["query"]
 67    search_response = search_game(query)
 68    games = handle_response(search_response)
 69    return render("search.html", games=games, query=query)
 70
 71@app.route('/upload', methods=['GET', 'POST'])
 72def route_upload():
 73    if not current_user.is_authenticated:
 74        return redirect('/login')
 75    if request.method == 'GET':
 76        info = request.args.get("info")
 77        if info is None:
 78            return render("upload.html", game=c.NO_GAME)
 79        return render("upload.html", game=obj_decode(info))
 80    
 81    error = handle_upload(request, current_user.id)
 82    if error is None:
 83        return render("done.html", text="Your save file was uploaded correctly.")
 84    return render("input.html", error=error)
 85
 86
 87@app.route('/admin', methods=['GET', 'POST'])
 88def route_admin():
 89    if not current_user.is_authenticated:
 90        return redirect('/')
 91    if current_user.id != 1:
 92        return redirect('/')
 93    
 94    if request.method == "GET":
 95        queue = get_queued_users()
 96        return render("admin.html", queue=queue)
 97    return render("admin.html")
 98
 99@app.route('/admin/allow/<username>')
100def route_admin_allow(username):
101    if not current_user.is_authenticated:
102        return redirect('/')
103    if current_user.id != 1:
104        return redirect('/')
105    
106    allow_user(username)
107    return redirect("/admin")
108
109@app.route('/admin/deny/<username>')
110def route_admin_deny(username):
111    if not current_user.is_authenticated:
112        return redirect('/')
113    if current_user.id != 1:
114        return redirect('/')
115    
116    deny_user(username)
117    return redirect("/admin")
118    
119
120@app.route('/about')
121def route_about():
122    return render("about.html")