Skip to main content

Base for flask server

Project description

base for flask server by Mixel Te

requirements.txt

Python 3.9.5

flask==2.1.2
sqlalchemy==2.0.19
werkzeug==2.3.6
sqlalchemy_serializer==1.4.1
pyjwt<2.10
flask_jwt_extended==4.5.2
PyMySQL==1.1.1

usage

scripts: python -m bafser

copy bafser_config.example.py to project root as bafser_config.py

create files:

.gitignore

.venv
*__pycache__*
logs/
db/
/images
/fonts
secret_key_jwt.txt
# data/_operations.py
from bfs import OperationsBase


class Operations(OperationsBase):
    oper_id = ("oper_id", "Operation description")
    oper_id2 = ("oper_id2", "Operation description")
    ...
# data/_roles.py
from bfs import RolesBase
from data._operations import Operations


class Roles(RolesBase):
    role_name = 2
    role_name2 = 3
    ...


Roles.ROLES = {
    Roles.role_name: {
        "name": "Role name",
        "operations": [
            Operations.oper_id,
            Operations.oper_id2,
        ]
    },
    Roles.role_name2: {
        "name": "Role name 2",
        "operations": [
            Operations.oper_id,
        ]
    },
}
# data/_tables.py
from bfs import TablesBase


class Tables(TablesBase):
    TableName = "TableName"
    AnotherTableName = "AnotherTableName"
# data/some_table.py
from bfs import SqlAlchemyBase, ObjMixin
from data._tables import Tables


class SomeTable(SqlAlchemyBase, ObjMixin):
    __tablename__ = Tables.SomeTable
    ...
  • IdMixin adds id column
  • ObjMixin adds id and deleted columns
  • SingletonMixin adds id column
# main.py
import sys
from bfs import AppConfig, create_app
from scripts.init_dev_values import init_dev_values


app, run = create_app(__name__, AppConfig(
    MESSAGE_TO_FRONTEND="",
    DEV_MODE="dev" in sys.argv,
    DELAY_MODE="delay" in sys.argv,
)
    .add_data_folder("FONTS_FOLDER", "fonts")
    .add_secret_key("API_SECRET_KEY", "secret_key_api.txt")
)

run(__name__ == "__main__", lambda: init_dev_values(True), port=5001)

modifying User and Image

User:

from sqlalchemy import Column, String
from sqlalchemy.orm import Session

from bfs import UserBase
from data._roles import Roles


class User(UserBase):
    newColumn = Column(String)

    @classmethod
    def new(cls, creator: "User", login: str, password: str, name: str, roles: list[int], newColumn: str, db_sess: Session = None) -> "User":
        return super().new(creator, login, password, name, roles, db_sess, newColumn=newColumn)

    @staticmethod
    def _new(db_sess: Session, user_kwargs: dict, newColumn: str):
        user = User(**user_kwargs, newColumn=newColumn)
        changes = [("newColumn", newColumn)]
        return user, changes

    @staticmethod
    def create_admin(db_sess: Session):
        fake_creator = User.get_fake_system()
        return User.new(fake_creator, "admin", "admin", "Админ", [Roles.admin], "newColumnValue", db_sess=db_sess)

Image:

# to use relationship you must name the file something other than image.py
# img.py for the example, because of the way sqlalchemy finds classes
image = orm.relationship("data.img.Image")
from typing import TypedDict, Union
from sqlalchemy import Column, String

from bfs import Image as ImageBase, get_json_values
from data.user import User


class ImageJson(TypedDict):
    data: str
    name: str
    newColumnData: str


TError = str


class Image(ImageBase):
    newColumn = Column(String)

    @classmethod
    def new(cls, creator: User, json: ImageJson) -> Union[tuple[None, TError], tuple["Image", None]]:
        return super().new(creator, json)

    @staticmethod
    def _new(creator: User, json: ImageJson, image_kwargs):
        newColumnData, values_error = get_json_values(json, ("newColumnData", None))
        if values_error:
            return None, None, values_error

        img = Image(**image_kwargs, newColumn=newColumnData)
        changes = [("newColumn", newColumnData)]
        return img, changes, None

csv header for requests log

reqid;ip;uid;asctime;method;url;level;message;code;json

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bafser-1.5.2.tar.gz (23.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bafser-1.5.2-py3-none-any.whl (35.8 kB view details)

Uploaded Python 3

File details

Details for the file bafser-1.5.2.tar.gz.

File metadata

  • Download URL: bafser-1.5.2.tar.gz
  • Upload date:
  • Size: 23.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for bafser-1.5.2.tar.gz
Algorithm Hash digest
SHA256 2ea32d315d7588baf1c8cbc28ee3828332fd847da7fc3117637079c6c1e5e508
MD5 bca49a894119f0656c81f69cecb9b73f
BLAKE2b-256 6d9de87b338c4c5b2ec58fd1210146e6cfc2ddc90de5ccfaf5764f25b08e7b7d

See more details on using hashes here.

Provenance

The following attestation bundles were made for bafser-1.5.2.tar.gz:

Publisher: main.yml on MixelTe/bafser

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bafser-1.5.2-py3-none-any.whl.

File metadata

  • Download URL: bafser-1.5.2-py3-none-any.whl
  • Upload date:
  • Size: 35.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for bafser-1.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 859d68001e954534f4a4a6cd7d652f524a34121c1cae8bf23e1fcc6f8f91c7ea
MD5 0cc60983440107ec6038c4fffdc5b4db
BLAKE2b-256 3fe074c5ef26a35240012a51a8a15bb3b576fdbbc23b4cc64a4f05e359bb02ed

See more details on using hashes here.

Provenance

The following attestation bundles were made for bafser-1.5.2-py3-none-any.whl:

Publisher: main.yml on MixelTe/bafser

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page