2021-09-08 22:01:10 +00:00
|
|
|
import logging
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
from app.classes.shared.helpers import helper
|
|
|
|
|
|
|
|
try:
|
2022-03-23 02:50:12 +00:00
|
|
|
from peewee import (
|
|
|
|
SqliteDatabase,
|
|
|
|
Model,
|
|
|
|
CharField,
|
|
|
|
DoesNotExist,
|
|
|
|
AutoField,
|
|
|
|
DateTimeField,
|
|
|
|
)
|
2021-09-08 22:01:10 +00:00
|
|
|
from playhouse.shortcuts import model_to_dict
|
|
|
|
|
|
|
|
except ModuleNotFoundError as e:
|
2022-03-08 04:40:44 +00:00
|
|
|
helper.auto_installer_fix(e)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-03-08 04:40:44 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2022-03-23 02:50:12 +00:00
|
|
|
peewee_logger = logging.getLogger("peewee")
|
2022-03-08 04:40:44 +00:00
|
|
|
peewee_logger.setLevel(logging.INFO)
|
2022-03-23 02:50:12 +00:00
|
|
|
database = SqliteDatabase(
|
|
|
|
helper.db_path, pragmas={"journal_mode": "wal", "cache_size": -1024 * 10}
|
|
|
|
)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
# ************************************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
# Roles Class
|
2022-03-23 02:50:12 +00:00
|
|
|
# ************************************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
class Roles(Model):
|
|
|
|
role_id = AutoField()
|
|
|
|
created = DateTimeField(default=datetime.datetime.now)
|
|
|
|
last_update = DateTimeField(default=datetime.datetime.now)
|
|
|
|
role_name = CharField(default="", unique=True, index=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
table_name = "roles"
|
|
|
|
database = database
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
|
|
|
|
# ************************************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
# Roles Helpers
|
2022-03-23 02:50:12 +00:00
|
|
|
# ************************************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
class helper_roles:
|
|
|
|
@staticmethod
|
|
|
|
def get_all_roles():
|
|
|
|
query = Roles.select()
|
2022-01-26 01:45:30 +00:00
|
|
|
return query
|
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))
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def add_role(role_name):
|
2022-03-23 02:50:12 +00:00
|
|
|
role_id = Roles.insert(
|
|
|
|
{
|
|
|
|
Roles.role_name: role_name.lower(),
|
|
|
|
Roles.created: helper.get_time_as_string(),
|
|
|
|
}
|
|
|
|
).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()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def remove_role(role_id):
|
|
|
|
with database.atomic():
|
|
|
|
role = Roles.get(Roles.role_id == role_id)
|
|
|
|
return role.delete_instance()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def role_id_exists(role_id):
|
|
|
|
if not roles_helper.get_role(role_id):
|
|
|
|
return False
|
|
|
|
return True
|
2022-01-26 01:45:30 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
roles_helper = helper_roles()
|