all repos — myprecious @ 7804286fa26985d959e2a42aa3b382b19ac8cfdb

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
  4from werkzeug.utils import secure_filename
  5from contextlib import suppress
  6import myprecious.Constants as c
  7from myprecious.Utils import handle_response, parse_remember
  8from myprecious.Auth import handle_register, handle_login, get_logged_user
  9from myprecious.Encoding import obj_decode
 10import os
 11if c.DEBUG_SWITCH:
 12    from myprecious.GamesApiTest import search_game
 13else:
 14    from myprecious.GamesApi import search_game
 15
 16def render(template, **context):
 17    return render_template(template, user=current_user, **context)
 18
 19@login_manager.user_loader
 20def load_user(user_id):
 21    return get_logged_user(user_id)
 22
 23@app.route('/')
 24def route_index():
 25    return render("index.html")
 26
 27@app.route('/login', methods=['GET', 'POST'])
 28def route_login():
 29    if current_user.is_authenticated:
 30        return redirect('/')
 31    if request.method == "GET":
 32        return render("login.html")
 33    
 34    form = request.form
 35    remember = parse_remember(form)
 36    
 37    error, user = handle_login(request.form)
 38    if error is None:
 39        login_user(user, remember=remember)
 40        return redirect("/")
 41    last_user = user.username if user else None
 42    return render("login.html", last_user=last_user, error=error)
 43    
 44
 45@app.route('/register', methods=['GET', 'POST'])
 46def route_register():
 47    if current_user.is_authenticated:
 48        return redirect('/')
 49    if request.method == "GET":
 50        return render("register.html")
 51    error = handle_register(request.form)
 52    if error is None:
 53        return render("register_done.html")
 54    return render("register.html", error=error)
 55    
 56@app.route('/logout')
 57def route_logout():
 58    logout_user()
 59    return redirect("/")
 60
 61@app.route('/search', methods=['GET', 'POST'])
 62def route_search():
 63    if not current_user.is_authenticated:
 64        return redirect('/login')
 65    if request.method == 'GET':
 66        return render("search.html")
 67    query = request.form["query"]
 68    search_response = search_game(query)
 69    return render("search.html", games=handle_response(search_response), 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        game = obj_decode(info)
 80        return render("upload.html", game=game)
 81        
 82    f = request.files['file']
 83    try:
 84        game_id = int(request.form['game_id'])
 85        platform_id = int(request.form['platform_id'])
 86    except ValueError:
 87        return redirect("/upload")
 88    
 89    # TODO: use IGDB api to validate game_id, platform_id and title before adding
 90    # TODO: save game in DB
 91
 92    save_folder = os.path.join(c.BASE_DIRECTORY, c.CONTENT_DIRECTORY, str(current_user.id), str(game_id), str(platform_id))
 93    with suppress(FileExistsError):
 94        os.makedirs(save_folder)
 95    if f.filename is None:
 96        return redirect("/upload")
 97    save_file = os.path.join(save_folder, secure_filename(f.filename))
 98    f.save(save_file)
 99    return render("index.html")
100
101
102@app.route('/admin', methods=['GET', 'POST'])
103def route_admin():
104    if not current_user.is_authenticated:
105        return redirect('/')
106    
107    if current_user.id != 1:
108        return redirect('/')
109    
110    if request.method == "GET":
111        return render("admin.html")
112    
113    return render("admin.html")
114
115@app.route('/about')
116def route_about():
117    return render("about.html")