models.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. from django.db import models
  2. from datetime import datetime, timedelta
  3. import calendar
  4. # Create your models here.
  5. class ServiceTimeSlot(models.Model):
  6. ID = models.AutoField(primary_key=True)
  7. date = models.DateField(null=True, blank=True)
  8. start_time = models.TimeField(null=True, blank=True)
  9. end_time = models.TimeField(null=True, blank=True)
  10. address = models.CharField(max_length=256, null=True, blank=True)
  11. people_count = models.IntegerField(default=2)
  12. @staticmethod
  13. def search_by_date(service_date):
  14. return ServiceTimeSlot.objects.filter(date=service_date)
  15. @staticmethod
  16. def get_by_id(ts_id):
  17. return ServiceTimeSlot.objects.get(ID=ts_id)
  18. @staticmethod
  19. def search_by_year_month(year, month):
  20. return ServiceTimeSlot.objects.filter(date__year=year, date__month=month)
  21. class Volunteer(models.Model):
  22. ID = models.AutoField(primary_key=True)
  23. name = models.CharField(max_length=128) # 姓名
  24. gender = models.CharField(max_length=8) # 性别 男,女,保密
  25. mobile = models.BigIntegerField(unique=True) # 手机号码
  26. social_id = models.CharField(max_length=32) # 身份证号码
  27. political_outlook = models.CharField(max_length=32) # 政治面貌
  28. organization = models.CharField(max_length=256) # 单位或学校
  29. district_street = models.CharField(max_length=256) # 区或街道
  30. status = models.CharField(max_length=128) # 状态 active,pending
  31. @staticmethod
  32. def get_by_mobile(mobile_number):
  33. try:
  34. return Volunteer.objects.get(mobile=mobile_number)
  35. except:
  36. return None
  37. @staticmethod
  38. def get_by_id(v_id):
  39. return Volunteer.objects.get(ID=v_id)
  40. @staticmethod
  41. def pending_list():
  42. return Volunteer.objects.filter(status='pending')
  43. @staticmethod
  44. def approve_volunteer(v_id):
  45. Volunteer.objects.filter(ID=v_id, status='pending').update(status='active')
  46. @staticmethod
  47. def reject(v_id):
  48. Volunteer.objects.filter(ID=v_id, status='pending').delete()
  49. class ServiceBooking(models.Model):
  50. ID = models.AutoField(primary_key=True)
  51. timeslot_id = models.ForeignKey(ServiceTimeSlot, on_delete=models.CASCADE, blank=True, null=True,
  52. related_name='service_booking_to_timeslot')
  53. volunteer_id = models.ForeignKey(Volunteer, on_delete=models.CASCADE, blank=True, null=True,
  54. related_name='service_booking_to_volunteer')
  55. @staticmethod
  56. def search_by_volunteer(v_id):
  57. return ServiceBooking.objects.select_related('timeslot_id').filter(volunteer_id_id=v_id,
  58. timeslot_id__date__gte=datetime.now().date()).order_by(
  59. 'timeslot_id__date')
  60. @staticmethod
  61. def search_by_service_timeslot_list(service_list):
  62. return ServiceBooking.objects.filter(timeslot_id__in=service_list)
  63. # @staticmethod
  64. # def search_by_service_timeslot_list_temp(service_list):
  65. # return ServiceBooking.objects.select_related('timeslot_id').filter(timeslot_id__in=service_list).order_by('start_time')
  66. @staticmethod
  67. def search_by_service_timeslot_list_with_volunteer_info(service_list):
  68. return ServiceBooking.objects.filter(timeslot_id__in=service_list).select_related('volunteer_id')
  69. @staticmethod
  70. def search_by_service_timeslot(service):
  71. return ServiceBooking.objects.filter(timeslot_id=service)
  72. @staticmethod
  73. def check_exist_booking(ts_id, v_id):
  74. if len(ServiceBooking.objects.filter(timeslot_id=ts_id, volunteer_id=v_id)) > 0:
  75. return True
  76. else:
  77. return False
  78. @staticmethod
  79. def add(service_booking):
  80. if ServiceBooking.check_exist_booking(service_booking.timeslot_id, service_booking.volunteer_id):
  81. return 1
  82. else:
  83. service_booking.save()
  84. return 0
  85. @staticmethod
  86. def delete(timeslot, volunteer):
  87. ServiceBooking.objects.filter(timeslot_id_id=timeslot, volunteer_id_id=volunteer).delete()
  88. @staticmethod
  89. def get_monthly_report(date):
  90. input_date = datetime.strptime(date, '%Y-%m-%d')
  91. first_date_of_month = datetime(year=input_date.year, month=input_date.month, day=1)
  92. month = input_date.month
  93. if month == 12:
  94. month = 1
  95. else:
  96. month += 1
  97. first_date_of_next_month = datetime(year=input_date.year, month=month, day=1)
  98. monthly_report = ServiceBooking.objects.select_related('timeslot_id', 'volunteer_id')\
  99. .filter(timeslot_id__date__gte=first_date_of_month, timeslot_id__date__lt=first_date_of_next_month)\
  100. .order_by('timeslot_id__date', 'timeslot_id__start_time')\
  101. .values('timeslot_id__date', 'timeslot_id__start_time', 'timeslot_id__end_time', 'volunteer_id__name',
  102. 'volunteer_id__gender', 'volunteer_id__mobile', 'volunteer_id__social_id',
  103. 'volunteer_id__political_outlook', 'volunteer_id__organization', 'volunteer_id__district_street')
  104. return monthly_report