database.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. """Database module"""
  2. from datetime import datetime, timedelta
  3. from app import SESSION
  4. from app.models import Player, Department, DepartmentStat, State
  5. def get_latest_professor(state_id, department_type):
  6. """Get latest professor from database"""
  7. session = SESSION()
  8. department = get_department(session, state_id, department_type)
  9. professor = department.department_stats.order_by(DepartmentStat.date_time.desc()).first()
  10. session.close()
  11. return professor
  12. def get_player(session, player_id, player_name):
  13. """Get player from database"""
  14. player = session.query(Player).get(player_id)
  15. if player is None:
  16. player = Player()
  17. player.id = player_id
  18. player.name = player_name
  19. session.add(player)
  20. session.commit()
  21. return player
  22. def get_department(session, state_id, department_type):
  23. """Get department from database"""
  24. department = session.query(Department).filter(
  25. Department.state_id == state_id
  26. ).filter(
  27. Department.department_type == department_type
  28. ).first()
  29. if department is None:
  30. department = Department()
  31. state = session.query(State).get(state_id)
  32. if not state:
  33. state = State()
  34. state.id = state_id
  35. session.add(state)
  36. department.state_id = state.id
  37. department.department_type = department_type
  38. session.add(department)
  39. session.commit()
  40. return department
  41. def save_professors(state_id, department_type, professors):
  42. """Save professors to database"""
  43. session = SESSION()
  44. department = get_department(session, state_id, department_type)
  45. for professor in professors:
  46. player = get_player(session, professor['id'], professor['name'])
  47. department_stat = DepartmentStat()
  48. department_stat.department_id = department.id
  49. department_stat.date_time = professor['date_time']
  50. department_stat.points = professor['points']
  51. department_stat.player_id = player.id
  52. session.add(department_stat)
  53. session.commit()
  54. session.close()
  55. def get_yesterday_professors(state_id, department_type):
  56. """Get professors from yesterday"""
  57. session = SESSION()
  58. department = get_department(session, state_id, department_type)
  59. until_date = datetime.today()
  60. if until_date.hour < 19:
  61. until_date = until_date - timedelta(1)
  62. until_date = until_date.replace(hour=19, minute=0, second=0)
  63. from_date = until_date - timedelta(1)
  64. professors = session.query(DepartmentStat) \
  65. .filter(DepartmentStat.department_id == department.id) \
  66. .filter(DepartmentStat.date_time >= from_date) \
  67. .filter(DepartmentStat.date_time < until_date) \
  68. .all()
  69. return professors
  70. def get_amount_of_points(state_id, department_type, player_id):
  71. """Get amount of points in department by player id"""
  72. session = SESSION()
  73. department = get_department(session, state_id, department_type)
  74. until_date = datetime.today()
  75. if until_date.hour < 19:
  76. until_date = until_date - timedelta(1)
  77. until_date = until_date.replace(hour=19, minute=0, second=0)
  78. from_date = until_date - timedelta(14)
  79. professors = session.query(DepartmentStat) \
  80. .filter(DepartmentStat.department_id == department.id) \
  81. .filter(DepartmentStat.player_id == player_id) \
  82. .filter(DepartmentStat.date_time >= from_date) \
  83. .filter(DepartmentStat.date_time < until_date) \
  84. .all()
  85. total_points = 0
  86. for professor in professors:
  87. total_points += professor.points
  88. return total_points