Skip to content

Commit

Permalink
Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Virashu committed Jul 24, 2024
1 parent f18e7a0 commit 93fc19c
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 45 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/pylint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ jobs:
python -m pip install --upgrade pip
pip install pylint
pip install -r requirements.txt
- name: Analysing the code with pylint
- name: Analyzing the code with pylint
run: |
pylint -f json2 $(git ls-files '*.py') | tee pylint.json
- name: Archive production artifacts
pylint -f json2 $(git ls-files '*.py') | jq '.statistics.score' > pylint.json
- name: Archive artifacts
uses: actions/upload-artifact@v4
with:
name: Upload pylint.json
Expand Down
6 changes: 3 additions & 3 deletions typings/flask_login.pyi
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import datetime
from types import NotImplementedType
from typing import Any, Literal, TypeAlias, TypeVar, Callable
from typing import Any, Callable, Literal, TypeAlias, TypeVar

from _typeshed import Incomplete
from typing_extensions import deprecated
from blinker import NamedSignal
from flask import Flask, Response, Request
from flask import Flask, Request, Response
from flask.testing import FlaskClient
from typing_extensions import deprecated

__version__: str
__all__ = [
Expand Down
1 change: 0 additions & 1 deletion y/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from .cli import main


main()
16 changes: 8 additions & 8 deletions y/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import flask
import flask_login

from .database import database, db_session
from .database.user import User
from .database import database
from .database.database import db_session
from .database.post import Post
from .database.user import User
from .forms import CreatePostForm, EditProfileForm, LoginForm, SignupForm

ROOT = str(pathlib.Path(__file__).parent.parent.resolve()) # Path of the project
Expand All @@ -23,9 +24,6 @@
login_manager.init_app(app)


db_session.global_init(f"{ROOT}/runtime/y.db")


@login_manager.user_loader
def load_user(user_id: str) -> User | None:
db_sess = db_session.create_session()
Expand Down Expand Up @@ -181,8 +179,8 @@ def create_post():
if answer_to:
back = flask.request.values["back"]
return flask.redirect(f"/post?post_id={answer_to}&back={back}")
else:
return flask.redirect("/profile")

return flask.redirect("/profile")

return flask.render_template("create_post.html", form=form)

Expand Down Expand Up @@ -254,7 +252,9 @@ def post_details():
if flask.request.method == "POST":
if "answer" in flask.request.form:
return flask.redirect(
f"/create-post?answer_to={flask.request.values['post_id']}&back={flask.request.values['back']}"
"/create-post"
+ f"?answer_to={flask.request.values['post_id']}"
+ f"&back={flask.request.values['back']}"
)

if reaction := flask.request.form.get("reaction"):
Expand Down
4 changes: 4 additions & 0 deletions y/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import pathlib

ROOT: str = str(pathlib.Path(__file__).parent.parent.resolve())
DB_PATH: str = f"{ROOT}/runtime/y.db"
3 changes: 1 addition & 2 deletions y/database/__all_models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
__all__ = ["user", "post"]

from . import user
from . import post
from . import post, user
8 changes: 7 additions & 1 deletion y/database/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
import uuid
from typing import Optional

from . import db_session
from ..config import DB_PATH
from .db_session import SessionFactory
from .post import Post
from .user import User

db_session = SessionFactory(DB_PATH)


def create_user(
username: str, display_name: str, email: str, hashed_password: str
Expand Down Expand Up @@ -130,9 +133,12 @@ def login_user(username: str, password: str) -> User | None:

db_sess = db_session.create_session()
user = db_sess.query(User).filter(User.username == username).first()

if user and str(user.hashed_password) == password:
return user

return None


def get_answers_to_post(post_id: str) -> list[Post]:
db_sess = db_session.create_session()
Expand Down
35 changes: 13 additions & 22 deletions y/database/db_session.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,24 @@
__all__ = ("global_init", "create_session", "SqlAlchemyBase")
__all__ = ("SqlAlchemyBase", "SessionFactory")

import sqlalchemy as sa
import sqlalchemy.orm as orm
from sqlalchemy.orm import Session
from sqlalchemy import orm

SqlAlchemyBase = orm.declarative_base()

__factory = None

class SessionFactory:
_factory: orm.sessionmaker[orm.Session] | None = None

def global_init(db_file: str) -> None:
global __factory
def __init__(self, db_file: str) -> None:
db_url = f"sqlite:///{db_file.strip()}?check_same_thread=False"

if __factory:
return
engine = sa.create_engine(db_url, echo=False)
self._factory = orm.sessionmaker(engine)

if not db_file or not db_file.strip():
raise Exception("Missing file argument")
SqlAlchemyBase.metadata.create_all(engine)

conn_str = f"sqlite:///{db_file.strip()}?check_same_thread=False"
def create_session(self) -> orm.Session:
if self._factory is None:
raise RuntimeError("DB is not initialized")

engine = sa.create_engine(conn_str, echo=False)
__factory = orm.sessionmaker(engine)

SqlAlchemyBase.metadata.create_all(engine)


def create_session() -> Session:
global __factory
if __factory is None:
raise RuntimeError("Call global_init() first")
return __factory()
return self._factory()
2 changes: 1 addition & 1 deletion y/database/user.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import datetime

import flask_login
import sqlalchemy as sql

from .db_session import SqlAlchemyBase
import flask_login


class User(SqlAlchemyBase, flask_login.UserMixin):
Expand Down
8 changes: 4 additions & 4 deletions y/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from wtforms.validators import DataRequired


def hash(string: str) -> str:
def hash_md5(string: str) -> str:
if not string:
return string
return hashlib.md5(string.encode()).hexdigest()
Expand All @@ -14,7 +14,7 @@ def hash(string: str) -> str:
class LoginForm(flask_wtf.FlaskForm):
username = wtforms.StringField("Username", validators=[DataRequired()])
password = wtforms.PasswordField(
"Password", validators=[DataRequired()], filters=[hash]
"Password", validators=[DataRequired()], filters=[hash_md5]
)
remember_me = wtforms.BooleanField("Remember me")
submit = wtforms.SubmitField("Log in")
Expand All @@ -25,7 +25,7 @@ class SignupForm(flask_wtf.FlaskForm):
email = wtforms.StringField("Email", validators=[DataRequired()])
display_name = wtforms.StringField("Display name", validators=[DataRequired()])
password = wtforms.PasswordField(
"Password", validators=[DataRequired()], filters=[hash]
"Password", validators=[DataRequired()], filters=[hash_md5]
)
remember_me = wtforms.BooleanField("Remember me")
submit = wtforms.SubmitField("Sign up")
Expand All @@ -41,7 +41,7 @@ class EditProfileForm(flask_wtf.FlaskForm):
username = wtforms.StringField("Username", validators=[DataRequired()])
email = wtforms.StringField("Email", validators=[DataRequired()])
password = wtforms.PasswordField(
"Password", validators=[DataRequired()], filters=[hash]
"Password", validators=[DataRequired()], filters=[hash_md5]
)
display_name = wtforms.StringField("Display name", validators=[DataRequired()])
description = wtforms.StringField("Description", validators=[DataRequired()])
Expand Down

0 comments on commit 93fc19c

Please sign in to comment.