all repos — myprecious @ 8b59845ab9b9df4c76efe688a18d9418435fa850

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    if current_user.is_authenticated:
 25        return render("index.html", games=[])
 26    return render("index.html")
 27
 28@app.route('/login', methods=['GET', 'POST'])
 29def route_login():
 30    if current_user.is_authenticated:
 31        return redirect('/')
 32    if request.method == "GET":
 33        return render("login.html")
 34    
 35    form = request.form
 36    remember = parse_remember(form)
 37    
 38    error, user = handle_login(request.form)
 39    if error is None:
 40        login_user(user, remember=remember)
 41        return redirect("/")
 42    last_user = user["username"] if user else None
 43    return render("login.html", last_user=last_user, error=error)
 44    
 45
 46@app.route('/register', methods=['GET', 'POST'])
 47def route_register():
 48    if current_user.is_authenticated:
 49        return redirect('/')
 50    if request.method == "GET":
 51        return render("register.html")
 52    error = handle_register(request.form)
 53    if error is None:
 54        return render("done.html", text="Your registration request has been taken into account and will likely be processed in a few days.")
 55    return render("register.html", error=error)
 56    
 57@app.route('/logout')
 58def route_logout():
 59    logout_user()
 60    return redirect("/")
 61
 62@app.route('/search', methods=['GET', 'POST'])
 63def route_search():
 64    if not current_user.is_authenticated:
 65        return redirect('/login')
 66    if request.method == 'GET':
 67        return render("search.html")
 68    query = request.form["query"]
 69    search_response = search_game(query)
 70    games = handle_response(search_response)
 71    return render("search.html", games=games, query=query)
 72
 73@app.route('/upload', methods=['GET', 'POST'])
 74def route_upload():
 75    if not current_user.is_authenticated:
 76        return redirect('/login')
 77    if request.method == 'GET':
 78        info = request.args.get("info")
 79        if info is None:
 80            return render("upload.html", game=c.NO_GAME)
 81        return render("upload.html", game=obj_decode(info))
 82    
 83    error = handle_upload(request, current_user.id)
 84    if error is None:
 85        return render("done.html", text="Your save file was uploaded correctly.")
 86    return render("input.html", error=error)
 87
 88
 89@app.route('/admin', methods=['GET', 'POST'])
 90def route_admin():
 91    if not current_user.is_authenticated:
 92        return redirect('/')
 93    if current_user.id != 1:
 94        return redirect('/')
 95    
 96    if request.method == "GET":
 97        queue = get_queued_users()
 98        return render("admin.html", queue=queue)
 99    return render("admin.html")
100
101@app.route('/admin/allow/<username>')
102def route_admin_allow(username):
103    if not current_user.is_authenticated:
104        return redirect('/')
105    if current_user.id != 1:
106        return redirect('/')
107    
108    allow_user(username)
109    return redirect("/admin")
110
111@app.route('/admin/deny/<username>')
112def route_admin_deny(username):
113    if not current_user.is_authenticated:
114        return redirect('/')
115    if current_user.id != 1:
116        return redirect('/')
117    
118    deny_user(username)
119    return redirect("/admin")
120    
121
122@app.route('/about')
123def route_about():
124    return render("about.html")