2021-09-08 22:01:10 +00:00
|
|
|
import logging
|
|
|
|
import datetime
|
2022-05-07 21:07:55 +00:00
|
|
|
import typing as t
|
2022-04-11 05:23:55 +00:00
|
|
|
from peewee import (
|
|
|
|
CharField,
|
|
|
|
DoesNotExist,
|
|
|
|
AutoField,
|
|
|
|
DateTimeField,
|
2022-08-20 12:32:39 +00:00
|
|
|
IntegerField,
|
2022-04-11 05:23:55 +00:00
|
|
|
)
|
|
|
|
from playhouse.shortcuts import model_to_dict
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
from app.classes.models.base_model import BaseModel
|
|
|
|
from app.classes.shared.helpers import Helpers
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-03-08 04:40:44 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2023-02-15 23:01:21 +00:00
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
# Roles Class
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2022-04-11 05:23:55 +00:00
|
|
|
class Roles(BaseModel):
|
2021-09-08 22:01:10 +00:00
|
|
|
role_id = AutoField()
|
|
|
|
created = DateTimeField(default=datetime.datetime.now)
|
|
|
|
last_update = DateTimeField(default=datetime.datetime.now)
|
|
|
|
role_name = CharField(default="", unique=True, index=True)
|
2022-08-20 12:32:39 +00:00
|
|
|
manager = IntegerField(null=True)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
table_name = "roles"
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
# Roles Helpers
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2022-04-14 02:10:25 +00:00
|
|
|
class HelperRoles:
|
2022-04-11 05:23:55 +00:00
|
|
|
def __init__(self, database):
|
|
|
|
self.database = database
|
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_all_roles():
|
2022-05-07 21:07:55 +00:00
|
|
|
return Roles.select()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_all_role_ids() -> t.List[int]:
|
|
|
|
return [role.role_id for role in Roles.select(Roles.role_id).execute()]
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_roleid_by_name(role_name):
|
|
|
|
try:
|
|
|
|
return (Roles.get(Roles.role_name == role_name)).role_id
|
|
|
|
except DoesNotExist:
|
|
|
|
return None
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_role(role_id):
|
|
|
|
return model_to_dict(Roles.get(Roles.role_id == role_id))
|
|
|
|
|
2022-05-07 21:07:55 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_role_columns(
|
|
|
|
role_id: t.Union[str, int], column_names: t.List[str]
|
|
|
|
) -> t.List[t.Any]:
|
|
|
|
columns = [getattr(Roles, column) for column in column_names]
|
|
|
|
return model_to_dict(
|
|
|
|
Roles.select(*columns).where(Roles.role_id == role_id).get(),
|
|
|
|
only=columns,
|
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_role_column(role_id: t.Union[str, int], column_name: str) -> t.Any:
|
|
|
|
column = getattr(Roles, column_name)
|
2022-05-20 10:15:50 +00:00
|
|
|
return getattr(
|
|
|
|
Roles.select(column).where(Roles.role_id == role_id).get(), column_name
|
|
|
|
)
|
2022-05-07 21:07:55 +00:00
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
2022-08-20 12:32:39 +00:00
|
|
|
def add_role(role_name, manager):
|
2022-03-23 02:50:12 +00:00
|
|
|
role_id = Roles.insert(
|
|
|
|
{
|
|
|
|
Roles.role_name: role_name.lower(),
|
2022-04-11 05:23:55 +00:00
|
|
|
Roles.created: Helpers.get_time_as_string(),
|
2022-08-20 12:32:39 +00:00
|
|
|
Roles.manager: manager,
|
2022-03-23 02:50:12 +00:00
|
|
|
}
|
|
|
|
).execute()
|
2021-09-08 22:01:10 +00:00
|
|
|
return role_id
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def update_role(role_id, up_data):
|
|
|
|
return Roles.update(up_data).where(Roles.role_id == role_id).execute()
|
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
def remove_role(self, role_id):
|
2022-05-09 23:08:49 +00:00
|
|
|
return Roles.delete().where(Roles.role_id == role_id).execute()
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2022-05-09 23:08:49 +00:00
|
|
|
def role_id_exists(role_id) -> bool:
|
2022-05-25 11:24:34 +00:00
|
|
|
return Roles.select().where(Roles.role_id == role_id).exists()
|