model_handler.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. from .models import Message, User, Company, Modular, ModularEnablement
  2. from django.contrib.auth.hashers import check_password
  3. from datetime import datetime
  4. class UserHandler():
  5. @staticmethod
  6. def check_exist_email(email: str):
  7. try:
  8. User.objects.get(email = email)
  9. return True
  10. except:
  11. return False
  12. @staticmethod
  13. def login(email: str, password: str):
  14. try:
  15. user = User.objects.get(email = email)
  16. if check_password(password, user.password):
  17. return user
  18. else:
  19. return None
  20. except Exception as e:
  21. print(e)
  22. return None
  23. @staticmethod
  24. def get_by_id(id):
  25. try:
  26. user = User.objects.get(id = id)
  27. return user
  28. except:
  29. return None
  30. @staticmethod
  31. def search_by_company(company):
  32. return User.objects.filter(company=company)
  33. @staticmethod
  34. def search_like_name(name, company):
  35. return User.objects.filter(name__contains=name, company=company)
  36. class CompanyHandler():
  37. @staticmethod
  38. def get_by_id(id):
  39. try:
  40. company = Company.objects.get(id = id)
  41. return company
  42. except:
  43. return None
  44. class MessageHandler():
  45. @staticmethod
  46. def my_msg(user, is_read: bool=False):
  47. msg = Message.objects.filter(user_to=user)
  48. if is_read:
  49. msg = msg.filter(is_read=is_read)
  50. return msg
  51. @staticmethod
  52. def unread_msg_count(user) -> int:
  53. msg = MessageHandler.my_msg(user=user, is_read=False)
  54. return msg.count()
  55. class ModularEnablementHandler():
  56. @staticmethod
  57. def search_by_company(company: Company):
  58. enabled_modulars = ModularEnablement.objects.filter(company=company, expiration_date__gte = datetime.now())
  59. return enabled_modulars
  60. class InternalUserHandler():
  61. @staticmethod
  62. def login(name: str, password: str):
  63. try:
  64. user = User.objects.get(name = name)
  65. if check_password(password, user.password):
  66. return user
  67. else:
  68. return None
  69. except Exception as e:
  70. print(e)
  71. return None