From 71f7bd1a3b501014eab6a08b24037a02e9d9ecdc Mon Sep 17 00:00:00 2001 From: Yusur Princeps Date: Thu, 6 Nov 2025 06:33:31 +0100 Subject: [PATCH] refactor code layout, move config to .env, add pyproject.toml --- .gitignore | 7 +++- CHANGELOG.md | 9 ++++- config.py | 3 -- pyproject.toml | 21 ++++++++++ run_example.py | 16 -------- {app => src/coriplus}/__init__.py | 37 +++++++++++------- {app => src/coriplus}/__main__.py | 0 {app => src/coriplus}/admin.py | 0 {app => src/coriplus}/ajax.py | 0 {app => src/coriplus}/api.py | 7 +++- {app => src/coriplus}/filters.py | 0 {app => src/coriplus}/models.py | 0 {app => src/coriplus}/reports.py | 0 {app => src/coriplus}/static/lib.js | 0 {app => src/coriplus}/static/style.css | 0 {app => src/coriplus}/templates/404.html | 0 {app => src/coriplus}/templates/about.html | 0 .../coriplus}/templates/admin_base.html | 0 .../coriplus}/templates/admin_home.html | 0 .../templates/admin_report_detail.html | 0 .../coriplus}/templates/admin_reports.html | 0 {app => src/coriplus}/templates/base.html | 0 .../coriplus}/templates/change_password.html | 0 .../coriplus}/templates/confirm_delete.html | 0 {app => src/coriplus}/templates/create.html | 0 {app => src/coriplus}/templates/edit.html | 0 .../coriplus}/templates/edit_profile.html | 0 {app => src/coriplus}/templates/explore.html | 0 {app => src/coriplus}/templates/feed.html | 0 {app => src/coriplus}/templates/homepage.html | 0 .../templates/includes/infobox_profile.html | 0 .../templates/includes/location_selector.html | 0 .../coriplus}/templates/includes/message.html | 0 .../templates/includes/notification.html | 0 .../templates/includes/pagination.html | 0 .../templates/includes/reported_message.html | 0 {app => src/coriplus}/templates/join.html | 0 {app => src/coriplus}/templates/login.html | 0 .../coriplus}/templates/notifications.html | 0 {app => src/coriplus}/templates/privacy.html | 0 .../coriplus}/templates/report_base.html | 0 .../coriplus}/templates/report_done.html | 0 .../coriplus}/templates/report_message.html | 0 .../coriplus}/templates/report_user.html | 0 {app => src/coriplus}/templates/terms.html | 0 .../coriplus}/templates/user_detail.html | 0 .../coriplus}/templates/user_list.html | 0 {app => src/coriplus}/utils.py | 0 {app => src/coriplus}/website.py | 5 ++- favicon.ico => src/favicon.ico | Bin robots.txt => src/robots.txt | 0 51 files changed, 64 insertions(+), 41 deletions(-) delete mode 100644 config.py create mode 100644 pyproject.toml delete mode 100644 run_example.py rename {app => src/coriplus}/__init__.py (77%) rename {app => src/coriplus}/__main__.py (100%) rename {app => src/coriplus}/admin.py (100%) rename {app => src/coriplus}/ajax.py (100%) rename {app => src/coriplus}/api.py (99%) rename {app => src/coriplus}/filters.py (100%) rename {app => src/coriplus}/models.py (100%) rename {app => src/coriplus}/reports.py (100%) rename {app => src/coriplus}/static/lib.js (100%) rename {app => src/coriplus}/static/style.css (100%) rename {app => src/coriplus}/templates/404.html (100%) rename {app => src/coriplus}/templates/about.html (100%) rename {app => src/coriplus}/templates/admin_base.html (100%) rename {app => src/coriplus}/templates/admin_home.html (100%) rename {app => src/coriplus}/templates/admin_report_detail.html (100%) rename {app => src/coriplus}/templates/admin_reports.html (100%) rename {app => src/coriplus}/templates/base.html (100%) rename {app => src/coriplus}/templates/change_password.html (100%) rename {app => src/coriplus}/templates/confirm_delete.html (100%) rename {app => src/coriplus}/templates/create.html (100%) rename {app => src/coriplus}/templates/edit.html (100%) rename {app => src/coriplus}/templates/edit_profile.html (100%) rename {app => src/coriplus}/templates/explore.html (100%) rename {app => src/coriplus}/templates/feed.html (100%) rename {app => src/coriplus}/templates/homepage.html (100%) rename {app => src/coriplus}/templates/includes/infobox_profile.html (100%) rename {app => src/coriplus}/templates/includes/location_selector.html (100%) rename {app => src/coriplus}/templates/includes/message.html (100%) rename {app => src/coriplus}/templates/includes/notification.html (100%) rename {app => src/coriplus}/templates/includes/pagination.html (100%) rename {app => src/coriplus}/templates/includes/reported_message.html (100%) rename {app => src/coriplus}/templates/join.html (100%) rename {app => src/coriplus}/templates/login.html (100%) rename {app => src/coriplus}/templates/notifications.html (100%) rename {app => src/coriplus}/templates/privacy.html (100%) rename {app => src/coriplus}/templates/report_base.html (100%) rename {app => src/coriplus}/templates/report_done.html (100%) rename {app => src/coriplus}/templates/report_message.html (100%) rename {app => src/coriplus}/templates/report_user.html (100%) rename {app => src/coriplus}/templates/terms.html (100%) rename {app => src/coriplus}/templates/user_detail.html (100%) rename {app => src/coriplus}/templates/user_list.html (100%) rename {app => src/coriplus}/utils.py (100%) rename {app => src/coriplus}/website.py (99%) rename favicon.ico => src/favicon.ico (100%) rename robots.txt => src/robots.txt (100%) diff --git a/.gitignore b/.gitignore index d320dae..546be68 100644 --- a/.gitignore +++ b/.gitignore @@ -4,8 +4,8 @@ __pycache__/ uploads/ *.pyc **~ -**/.*.swp -**/__pycache__/ +.*.swp +__pycache__/ venv .env .venv @@ -15,3 +15,6 @@ conf/ config/ \#*\# .\#* +node_modules/ +alembic.ini +**.egg-info \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 105a0ad..ff728bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ # Changelog -## 0.9-dev +## 0.10.0 ++ Codebase refactor (with breaking changes!) ++ Move ALL config to .env (config.py is NO MORE supported) ++ Config SITE_NAME replaced with APP_NAME + +## 0.9.0 * Website redesign: added some material icons, implemented via a `inline_svg` function, injected by default in templates and defined in `utils.py`. * Added positive feedback mechanism: now you can +1 a message. So, `score_message_add` and `score_message_remove` API endpoints were added, and `MessageUpvote` table was created. @@ -26,7 +31,7 @@ * Changed default `robots.txt`, adding report and admin-related lines. * Released official [Android client](https://github.com/sakuragasaki46/coriplusapp/releases/tag/v0.8.0). -## 0.7.1-dev +## 0.7.1 * Adding `messages_count`, `followers_count` and `following_count` to `profile_info` API endpoint (forgot to release). diff --git a/config.py b/config.py deleted file mode 100644 index a6e2b64..0000000 --- a/config.py +++ /dev/null @@ -1,3 +0,0 @@ -DEBUG = True -SECRET_KEY = 'hin6bab8ge25*r=x&+5$0kn=-#log$pt^#@vrqjld!^2ci@g*b' -SITE_NAME = 'Cori+' diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..f4d5b53 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,21 @@ +[project] +name = "sakuragasaki46_coriplus" +authors = [ + { name = "Sakuragasaki46" } +] +dynamic = ["version"] +dependencies = [ + "Python-Dotenv>=1.0.0", + "Flask", + "Flask-Login", + "Peewee" +] +requires-python = ">=3.10" +classifiers = [ + "Private :: X" +] + +[tool.setuptools.dynamic] +version = { attr = "coriplus.__version__" } + + diff --git a/run_example.py b/run_example.py deleted file mode 100644 index 4ab76a9..0000000 --- a/run_example.py +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/env python - -import sys -sys.path.insert(0, '../..') - -import argparse -parser = argparse.ArgumentParser() -parser.add_argument('-p', '--port', type=int, default=5000, - help='An alternative port where to run the server.') - -from app import app, create_tables - -if __name__ == '__main__': - args = parser.parse_args() - create_tables() - app.run(port=args.port) diff --git a/app/__init__.py b/src/coriplus/__init__.py similarity index 77% rename from app/__init__.py rename to src/coriplus/__init__.py index 0ef203e..4f01715 100644 --- a/app/__init__.py +++ b/src/coriplus/__init__.py @@ -16,24 +16,31 @@ For other, see `app.utils`. ''' from flask import ( - Flask, abort, flash, g, jsonify, redirect, render_template, request, - send_from_directory, session, url_for, __version__ as flask_version) -import hashlib -import datetime, time, re, os, sys, string, json, html -from functools import wraps + Flask, g, jsonify, render_template, request, + send_from_directory, __version__ as flask_version) +import os, sys from flask_login import LoginManager +import dotenv +import logging -__version__ = '0.9.0' +__version__ = '0.10.0-dev44' -# we want to support Python 3 only. +# we want to support Python 3.10+ only. # Python 2 has too many caveats. -if sys.version_info[0] < 3: - raise RuntimeError('Python 3 required') +# Python <=3.9 has harder type support. +if sys.version_info[0:2] < (3, 10): + raise RuntimeError('Python 3.10+ required') -os.chdir(os.path.dirname(os.path.dirname(__file__))) +BASEDIR = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) +os.chdir(BASEDIR) + +dotenv.load_dotenv() + +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) app = Flask(__name__) -app.config.from_pyfile('../config.py') +app.secret_key = os.environ['SECRET_KEY'] login_manager = LoginManager(app) @@ -53,7 +60,7 @@ def before_request(): try: g.db.connect() except OperationalError: - sys.stderr.write('database connected twice.\n') + logger.error('database connected twice.\n') @app.after_request def after_request(response): @@ -63,7 +70,7 @@ def after_request(response): @app.context_processor def _inject_variables(): return { - 'site_name': app.config['SITE_NAME'], + 'site_name': os.environ.get('APP_NAME', 'Cori+'), 'locations': locations, 'inline_svg': inline_svg } @@ -78,11 +85,11 @@ def error_404(body): @app.route('/favicon.ico') def favicon_ico(): - return send_from_directory(os.getcwd(), 'favicon.ico') + return send_from_directory(BASEDIR, 'src/favicon.ico') @app.route('/robots.txt') def robots_txt(): - return send_from_directory(os.getcwd(), 'robots.txt') + return send_from_directory(BASEDIR, 'src/robots.txt') @app.route('/uploads/.') def uploads(id, type='jpg'): diff --git a/app/__main__.py b/src/coriplus/__main__.py similarity index 100% rename from app/__main__.py rename to src/coriplus/__main__.py diff --git a/app/admin.py b/src/coriplus/admin.py similarity index 100% rename from app/admin.py rename to src/coriplus/admin.py diff --git a/app/ajax.py b/src/coriplus/ajax.py similarity index 100% rename from app/ajax.py rename to src/coriplus/ajax.py diff --git a/app/api.py b/src/coriplus/api.py similarity index 99% rename from app/api.py rename to src/coriplus/api.py index 6ee0fd3..5f58c2c 100644 --- a/app/api.py +++ b/src/coriplus/api.py @@ -7,6 +7,9 @@ from .models import User, UserProfile, Message, Upload, Relationship, Notificati MSGPRV_PUBLIC, MSGPRV_UNLISTED, MSGPRV_FRIENDS, MSGPRV_ONLYME, UPLOAD_DIRECTORY from .utils import check_access_token, Visibility, push_notification, unpush_notification, \ create_mentions, is_username, generate_access_token, pwdhash +import logging + +logger = logging.getLogger(__name__) bp = Blueprint('api', __name__, url_prefix='/api/V1') @@ -16,7 +19,7 @@ def get_message_info(message): except IndexError: media = None if media: - print(media) + logger.debug(media) return { 'id': message.id, 'user': { @@ -122,7 +125,7 @@ def create2(self): privacy=privacy) file = request.files.get('file') if file: - print('Uploading', file.filename) + logger.info('Uploading', file.filename) ext = file.filename.split('.')[-1] upload = Upload.create( type=ext, diff --git a/app/filters.py b/src/coriplus/filters.py similarity index 100% rename from app/filters.py rename to src/coriplus/filters.py diff --git a/app/models.py b/src/coriplus/models.py similarity index 100% rename from app/models.py rename to src/coriplus/models.py diff --git a/app/reports.py b/src/coriplus/reports.py similarity index 100% rename from app/reports.py rename to src/coriplus/reports.py diff --git a/app/static/lib.js b/src/coriplus/static/lib.js similarity index 100% rename from app/static/lib.js rename to src/coriplus/static/lib.js diff --git a/app/static/style.css b/src/coriplus/static/style.css similarity index 100% rename from app/static/style.css rename to src/coriplus/static/style.css diff --git a/app/templates/404.html b/src/coriplus/templates/404.html similarity index 100% rename from app/templates/404.html rename to src/coriplus/templates/404.html diff --git a/app/templates/about.html b/src/coriplus/templates/about.html similarity index 100% rename from app/templates/about.html rename to src/coriplus/templates/about.html diff --git a/app/templates/admin_base.html b/src/coriplus/templates/admin_base.html similarity index 100% rename from app/templates/admin_base.html rename to src/coriplus/templates/admin_base.html diff --git a/app/templates/admin_home.html b/src/coriplus/templates/admin_home.html similarity index 100% rename from app/templates/admin_home.html rename to src/coriplus/templates/admin_home.html diff --git a/app/templates/admin_report_detail.html b/src/coriplus/templates/admin_report_detail.html similarity index 100% rename from app/templates/admin_report_detail.html rename to src/coriplus/templates/admin_report_detail.html diff --git a/app/templates/admin_reports.html b/src/coriplus/templates/admin_reports.html similarity index 100% rename from app/templates/admin_reports.html rename to src/coriplus/templates/admin_reports.html diff --git a/app/templates/base.html b/src/coriplus/templates/base.html similarity index 100% rename from app/templates/base.html rename to src/coriplus/templates/base.html diff --git a/app/templates/change_password.html b/src/coriplus/templates/change_password.html similarity index 100% rename from app/templates/change_password.html rename to src/coriplus/templates/change_password.html diff --git a/app/templates/confirm_delete.html b/src/coriplus/templates/confirm_delete.html similarity index 100% rename from app/templates/confirm_delete.html rename to src/coriplus/templates/confirm_delete.html diff --git a/app/templates/create.html b/src/coriplus/templates/create.html similarity index 100% rename from app/templates/create.html rename to src/coriplus/templates/create.html diff --git a/app/templates/edit.html b/src/coriplus/templates/edit.html similarity index 100% rename from app/templates/edit.html rename to src/coriplus/templates/edit.html diff --git a/app/templates/edit_profile.html b/src/coriplus/templates/edit_profile.html similarity index 100% rename from app/templates/edit_profile.html rename to src/coriplus/templates/edit_profile.html diff --git a/app/templates/explore.html b/src/coriplus/templates/explore.html similarity index 100% rename from app/templates/explore.html rename to src/coriplus/templates/explore.html diff --git a/app/templates/feed.html b/src/coriplus/templates/feed.html similarity index 100% rename from app/templates/feed.html rename to src/coriplus/templates/feed.html diff --git a/app/templates/homepage.html b/src/coriplus/templates/homepage.html similarity index 100% rename from app/templates/homepage.html rename to src/coriplus/templates/homepage.html diff --git a/app/templates/includes/infobox_profile.html b/src/coriplus/templates/includes/infobox_profile.html similarity index 100% rename from app/templates/includes/infobox_profile.html rename to src/coriplus/templates/includes/infobox_profile.html diff --git a/app/templates/includes/location_selector.html b/src/coriplus/templates/includes/location_selector.html similarity index 100% rename from app/templates/includes/location_selector.html rename to src/coriplus/templates/includes/location_selector.html diff --git a/app/templates/includes/message.html b/src/coriplus/templates/includes/message.html similarity index 100% rename from app/templates/includes/message.html rename to src/coriplus/templates/includes/message.html diff --git a/app/templates/includes/notification.html b/src/coriplus/templates/includes/notification.html similarity index 100% rename from app/templates/includes/notification.html rename to src/coriplus/templates/includes/notification.html diff --git a/app/templates/includes/pagination.html b/src/coriplus/templates/includes/pagination.html similarity index 100% rename from app/templates/includes/pagination.html rename to src/coriplus/templates/includes/pagination.html diff --git a/app/templates/includes/reported_message.html b/src/coriplus/templates/includes/reported_message.html similarity index 100% rename from app/templates/includes/reported_message.html rename to src/coriplus/templates/includes/reported_message.html diff --git a/app/templates/join.html b/src/coriplus/templates/join.html similarity index 100% rename from app/templates/join.html rename to src/coriplus/templates/join.html diff --git a/app/templates/login.html b/src/coriplus/templates/login.html similarity index 100% rename from app/templates/login.html rename to src/coriplus/templates/login.html diff --git a/app/templates/notifications.html b/src/coriplus/templates/notifications.html similarity index 100% rename from app/templates/notifications.html rename to src/coriplus/templates/notifications.html diff --git a/app/templates/privacy.html b/src/coriplus/templates/privacy.html similarity index 100% rename from app/templates/privacy.html rename to src/coriplus/templates/privacy.html diff --git a/app/templates/report_base.html b/src/coriplus/templates/report_base.html similarity index 100% rename from app/templates/report_base.html rename to src/coriplus/templates/report_base.html diff --git a/app/templates/report_done.html b/src/coriplus/templates/report_done.html similarity index 100% rename from app/templates/report_done.html rename to src/coriplus/templates/report_done.html diff --git a/app/templates/report_message.html b/src/coriplus/templates/report_message.html similarity index 100% rename from app/templates/report_message.html rename to src/coriplus/templates/report_message.html diff --git a/app/templates/report_user.html b/src/coriplus/templates/report_user.html similarity index 100% rename from app/templates/report_user.html rename to src/coriplus/templates/report_user.html diff --git a/app/templates/terms.html b/src/coriplus/templates/terms.html similarity index 100% rename from app/templates/terms.html rename to src/coriplus/templates/terms.html diff --git a/app/templates/user_detail.html b/src/coriplus/templates/user_detail.html similarity index 100% rename from app/templates/user_detail.html rename to src/coriplus/templates/user_detail.html diff --git a/app/templates/user_list.html b/src/coriplus/templates/user_list.html similarity index 100% rename from app/templates/user_list.html rename to src/coriplus/templates/user_list.html diff --git a/app/utils.py b/src/coriplus/utils.py similarity index 100% rename from app/utils.py rename to src/coriplus/utils.py diff --git a/app/website.py b/src/coriplus/website.py similarity index 99% rename from app/website.py rename to src/coriplus/website.py index f5867e3..df43b86 100644 --- a/app/website.py +++ b/src/coriplus/website.py @@ -9,6 +9,9 @@ from sys import version as python_version from flask import Blueprint, abort, flash, redirect, render_template, request, url_for, __version__ as flask_version from flask_login import login_required, login_user, logout_user import json +import logging + +logger = logging.getLogger(__name__) bp = Blueprint('website', __name__) @@ -181,7 +184,7 @@ def create(): privacy=privacy) file = request.files.get('file') if file: - print('Uploading', file.filename) + logger.info('Uploading', file.filename) ext = file.filename.split('.')[-1] upload = Upload.create( type=ext, diff --git a/favicon.ico b/src/favicon.ico similarity index 100% rename from favicon.ico rename to src/favicon.ico diff --git a/robots.txt b/src/robots.txt similarity index 100% rename from robots.txt rename to src/robots.txt