~jae/dn0magik-mc

b5541a8c2a365c4199e63645204a15ecb444d9b7 — Jae Lo Presti (DN0) 2 years ago 9f59745
DB: add object get method + basic DB utils (get uuid from username)
2 files changed, 21 insertions(+), 6 deletions(-)

M src/utils/db.py
A src/utils/dbutils.py
M src/utils/db.py => src/utils/db.py +14 -6
@@ 1,11 1,12 @@
from peewee import (
    SqliteDatabase,
    Model,
    Charfield,
    CharField,
    DateTimeField,
    IntegerField,
    ForeignKeyField,
    Model,
)
from playhouse import migrate

# TODO: SUPPORT OTHER DBS THAN SQLITE
database = SqliteDatabase("mc.db")


@@ 19,9 20,9 @@ class BaseModel(Model):

class Users(BaseModel):
    id = IntegerField(primary_key=True)
    username = Charfield(null=False)
    password = Charfield(null=True)  # NULL TRUE = IN CASE OF SSO LOGIN/REGISTER
    uuid = Charfield(null=False)
    username = CharField(null=False)
    password = CharField(null=True)  # NULL TRUE = IN CASE OF SSO LOGIN/REGISTER
    uuid = CharField(null=False)
    registered_on = DateTimeField(
        null=False
    )  # USED MAINLY FOR NICE DATA STATS IN api/statusapi:status_order_stats()


@@ 29,6 30,13 @@ class Users(BaseModel):

class UsernameHistory(BaseModel):
    id = IntegerField(primary_key=True)
    username = Charfield(null=False)
    username = CharField(null=False)
    changed_on = DateTimeField(null=False)
    uuid = ForeignKeyField(Users, backref="uuid")


def get_object(model, **kwargs):
    try:
        return model.get(**kwargs)
    except model.DoesNotExist:
        return None

A src/utils/dbutils.py => src/utils/dbutils.py +7 -0
@@ 0,0 1,7 @@
from utils.db import get_object, Users


def get_uuid_from_username(username: str):
    user = get_object(Users, username=username)

    return user.uuid if user else None