crafty-4/app/classes/models/roles.py

76 lines
2.2 KiB
Python
Raw Normal View History

2021-09-08 22:01:10 +00:00
import logging
import datetime
from peewee import (
CharField,
DoesNotExist,
AutoField,
DateTimeField,
)
from playhouse.shortcuts import model_to_dict
2021-09-08 22:01:10 +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
# **********************************************************************************
2021-09-08 22:01:10 +00:00
# Roles Class
# **********************************************************************************
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)
class Meta:
table_name = "roles"
# **********************************************************************************
2021-09-08 22:01:10 +00:00
# Roles Helpers
# **********************************************************************************
class HelperRoles:
def __init__(self, database):
self.database = database
2021-09-08 22:01:10 +00:00
@staticmethod
def get_all_roles():
query = Roles.select()
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):
role_id = Roles.insert(
{
Roles.role_name: role_name.lower(),
Roles.created: Helpers.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()
def remove_role(self, role_id):
with self.database.atomic():
role = Roles.get(Roles.role_id == role_id)
return role.delete_instance()
2021-09-08 22:01:10 +00:00
@staticmethod
def role_id_exists(role_id):
if not HelperRoles.get_role(role_id):
2021-09-08 22:01:10 +00:00
return False
return True