all repos — myprecious @ e8c2016807a630fd6f00a7c7061ab15a0f7ffd9b

A lightweight web service to backup precious game saves.

code cleanup
Andronaco Marco marco.andronaco@olivetti.com
Tue, 18 Jul 2023 00:43:31 +0200
commit

e8c2016807a630fd6f00a7c7061ab15a0f7ffd9b

parent

7804286fa26985d959e2a42aa3b382b19ac8cfdb

D .vscode/launch.json

@@ -1,33 +0,0 @@

-{ - // Use IntelliSense to learn about possible attributes. - // Hover to view descriptions of existing attributes. - // For more information, visit: <https://go.microsoft.com/fwlink/?linkid=830387> - "version": "0.2.0", - "configurations": [ - { - "name": "myprecious", - "type": "python", - "request": "launch", - "cwd": "${workspaceFolder}", - "module": "poetry", - "python": "C:\\Users\\07501300\\AppData\\Local\\pypoetry\\Cache\\virtualenvs\\myprecious-MjU9b_nO-py3.11", - "args": [ - "run", - "python", - "-m",//"waitress-serve", "", "0.0.0.0", "--port", "5000", "main:app" - "waitress-serve", - "--host", - "0.0.0.0", - "--port", - "5000", - "main:app" - ], - "justMyCode": true, - "stopOnEntry": false, - "console": "integratedTerminal", - "env": { - "DEBUG_SWITCH": "True" - } - } - ] - }
M README.mdREADME.md

@@ -9,3 +9,8 @@ ## Usage

``` poetry run waitress-serve myprecious:app ``` + +### Debug +``` +poetry run flask --app myprecious run --port 1111 --debug +```
M myprecious/Auth.pymyprecious/auth.py

@@ -1,5 +1,5 @@

-import myprecious.Constants as c -from myprecious.Db import add_user_to_queue, get_user_from_username, get_user_from_id +import myprecious.constants as c +from myprecious.db import add_user_to_queue, get_user_from_username, get_user_from_id from flask_login import UserMixin class User(UserMixin):
M myprecious/Db.pymyprecious/db.py

@@ -1,6 +1,6 @@

import uuid, hashlib, sqlite3 -import myprecious.Constants as c +import myprecious.constants as c def hash(password: str): salt = uuid.uuid4().hex
M myprecious/GamesApi.pymyprecious/games_api.py

@@ -1,7 +1,7 @@

from igdb.wrapper import IGDBWrapper from urllib.parse import urlencode from contextlib import suppress -import Constants as c +import myprecious.constants as c import requests import os, json
M myprecious/Utils.pymyprecious/utils.py

@@ -1,5 +1,5 @@

-from myprecious.Encoding import obj_encode -import myprecious.Constants as c +from myprecious.encoding import obj_encode +import myprecious.constants as c def handle_platform(game, platform): try:
M myprecious/__init__.pymyprecious/__init__.py

@@ -1,7 +1,7 @@

from flask import Flask from flask_login import LoginManager -import myprecious.Constants as c -from myprecious.Db import init_db +import myprecious.constants as c +from myprecious.db import init_db app = Flask(__name__) login_manager = LoginManager(app)
A myprecious/files.py

@@ -0,0 +1,38 @@

+import os +from contextlib import suppress +from werkzeug.utils import secure_filename +import myprecious.constants as c + +def get_title_platform(game_id, platform_id): + # TODO: use IGDB api to validate game_id, platform_id and return title and platform name + res = { "title": "title", "platform": "platform"} + if res is None: + raise Exception + return res["title"], res["platform"] + +def handle_upload(request, user_id): + f = request.files['file'] + if f.filename is None: + return "Bad file upload." + try: + game_id = int(request.form['game_id']) + platform_id = int(request.form['platform_id']) + except ValueError: + return "Wrong parameter type." + + try: + title, platform = get_title_platform(game_id, platform_id) + except Exception: + return "Bad or manipulated game data." + + # TODO: save game in DB + # db.add_platform(platform_id, name) + # db.add_game(game_id, platform_id, title) + # db.add_save(user_id, game_id, platform_id, f.filename) + + save_folder = os.path.join(c.BASE_DIRECTORY, c.CONTENT_DIRECTORY, str(user_id), str(game_id), str(platform_id)) + with suppress(FileExistsError): + os.makedirs(save_folder) + save_file = os.path.join(save_folder, secure_filename(f.filename)) + f.save(save_file) + return None
A myprecious/templates/done.html

@@ -0,0 +1,5 @@

+{% extends "base.html" %} +{% block title %}done{% endblock %} +{% block content %} +<p>{{ text }}</p> +{% endblock %}
D myprecious/templates/register_done.html

@@ -1,5 +0,0 @@

-{% extends "base.html" %} -{% block title %}done{% endblock %} -{% block content %} -<p>Your registration request has been taken into account and will likely be processed in a few days.</p> -{% endblock %}
M myprecious/templates/upload.htmlmyprecious/templates/upload.html

@@ -23,4 +23,7 @@ <input type="file" name="file" required />

{% endif %} <input type="submit" value="submit save" {% if not game.platform %}disabled{% endif %}/> </form> +{% if error %} +<p class="error">{{ error }}</p> +{% endif %} {% endblock %}
M myprecious/views.pymyprecious/views.py

@@ -1,17 +1,15 @@

from myprecious import app, login_manager from flask import request, redirect, render_template from flask_login import login_user, logout_user, current_user -from werkzeug.utils import secure_filename -from contextlib import suppress -import myprecious.Constants as c -from myprecious.Utils import handle_response, parse_remember -from myprecious.Auth import handle_register, handle_login, get_logged_user -from myprecious.Encoding import obj_decode -import os -if c.DEBUG_SWITCH: - from myprecious.GamesApiTest import search_game +import myprecious.constants as c +from myprecious.utils import handle_response, parse_remember +from myprecious.auth import handle_register, handle_login, get_logged_user +from myprecious.files import handle_upload +from myprecious.encoding import obj_decode +if app.debug: + from myprecious.games_api_test import search_game else: - from myprecious.GamesApi import search_game + from myprecious.games_api import search_game def render(template, **context): return render_template(template, user=current_user, **context)

@@ -50,7 +48,7 @@ if request.method == "GET":

return render("register.html") error = handle_register(request.form) if error is None: - return render("register_done.html") + return render("done.html", text="Your registration request has been taken into account and will likely be processed in a few days.") return render("register.html", error=error) @app.route('/logout')

@@ -66,7 +64,8 @@ if request.method == 'GET':

return render("search.html") query = request.form["query"] search_response = search_game(query) - return render("search.html", games=handle_response(search_response), query=query) + games = handle_response(search_response) + return render("search.html", games=games, query=query) @app.route('/upload', methods=['GET', 'POST']) def route_upload():

@@ -76,27 +75,12 @@ if request.method == 'GET':

info = request.args.get("info") if info is None: return render("upload.html", game=c.NO_GAME) - game = obj_decode(info) - return render("upload.html", game=game) - - f = request.files['file'] - try: - game_id = int(request.form['game_id']) - platform_id = int(request.form['platform_id']) - except ValueError: - return redirect("/upload") + return render("upload.html", game=obj_decode(info)) - # TODO: use IGDB api to validate game_id, platform_id and title before adding - # TODO: save game in DB - - save_folder = os.path.join(c.BASE_DIRECTORY, c.CONTENT_DIRECTORY, str(current_user.id), str(game_id), str(platform_id)) - with suppress(FileExistsError): - os.makedirs(save_folder) - if f.filename is None: - return redirect("/upload") - save_file = os.path.join(save_folder, secure_filename(f.filename)) - f.save(save_file) - return render("index.html") + error = handle_upload(request, current_user.id) + if error is None: + return render("done.html", text="Your save file was uploaded correctly.") + return render("input.html", error=error) @app.route('/admin', methods=['GET', 'POST'])