1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- from .models import Message, User, Company, Modular, ModularEnablement
- from django.contrib.auth.hashers import check_password
- from datetime import datetime
- class UserHandler():
- @staticmethod
- def check_exist_email(email: str):
- try:
- User.objects.get(email = email)
- return True
- except:
- return False
- @staticmethod
- def login(email: str, password: str):
- try:
- user = User.objects.get(email = email)
- if check_password(password, user.password):
- return user
- else:
- return None
- except Exception as e:
- print(e)
- return None
- @staticmethod
- def get_by_id(id):
- try:
- user = User.objects.get(id = id)
- return user
- except:
- return None
- @staticmethod
- def search_by_company(company):
- return User.objects.filter(company=company)
- @staticmethod
- def search_like_name(name, company):
- return User.objects.filter(name__contains=name, company=company)
- class CompanyHandler():
- @staticmethod
- def get_by_id(id):
- try:
- company = Company.objects.get(id = id)
- return company
- except:
- return None
- class MessageHandler():
- @staticmethod
- def my_msg(user, is_read: bool=False):
- msg = Message.objects.filter(user_to=user)
- if is_read:
- msg = msg.filter(is_read=is_read)
-
- return msg
- @staticmethod
- def unread_msg_count(user) -> int:
- msg = MessageHandler.my_msg(user=user, is_read=False)
- return msg.count()
- class ModularEnablementHandler():
- @staticmethod
- def search_by_company(company: Company):
- enabled_modulars = ModularEnablement.objects.filter(company=company, expiration_date__gte = datetime.now())
- return enabled_modulars
- class InternalUserHandler():
- @staticmethod
- def login(name: str, password: str):
- try:
- user = User.objects.get(name = name)
- if check_password(password, user.password):
- return user
- else:
- return None
- except Exception as e:
- print(e)
- return None
|