123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- from django.db import models
- from datetime import datetime, timedelta
- import calendar
- # Create your models here.
- class ServiceTimeSlot(models.Model):
- ID = models.AutoField(primary_key=True)
- date = models.DateField(null=True, blank=True)
- start_time = models.TimeField(null=True, blank=True)
- end_time = models.TimeField(null=True, blank=True)
- address = models.CharField(max_length=256, null=True, blank=True)
- people_count = models.IntegerField(default=2)
- @staticmethod
- def search_by_date(service_date):
- return ServiceTimeSlot.objects.filter(date=service_date)
- @staticmethod
- def get_by_id(ts_id):
- return ServiceTimeSlot.objects.get(ID=ts_id)
- @staticmethod
- def search_by_year_month(year, month):
- return ServiceTimeSlot.objects.filter(date__year=year, date__month=month)
- class Volunteer(models.Model):
- ID = models.AutoField(primary_key=True)
- name = models.CharField(max_length=128) # 姓名
- gender = models.CharField(max_length=8) # 性别 男,女,保密
- mobile = models.BigIntegerField(unique=True) # 手机号码
- social_id = models.CharField(max_length=32) # 身份证号码
- political_outlook = models.CharField(max_length=32) # 政治面貌
- organization = models.CharField(max_length=256) # 单位或学校
- district_street = models.CharField(max_length=256) # 区或街道
- status = models.CharField(max_length=128) # 状态 active,pending
- @staticmethod
- def get_by_mobile(mobile_number):
- try:
- return Volunteer.objects.get(mobile=mobile_number)
- except:
- return None
- @staticmethod
- def get_by_id(v_id):
- return Volunteer.objects.get(ID=v_id)
- @staticmethod
- def pending_list():
- return Volunteer.objects.filter(status='pending')
- @staticmethod
- def approve_volunteer(v_id):
- Volunteer.objects.filter(ID=v_id, status='pending').update(status='active')
- @staticmethod
- def reject(v_id):
- Volunteer.objects.filter(ID=v_id, status='pending').delete()
- class ServiceBooking(models.Model):
- ID = models.AutoField(primary_key=True)
- timeslot_id = models.ForeignKey(ServiceTimeSlot, on_delete=models.CASCADE, blank=True, null=True,
- related_name='service_booking_to_timeslot')
- volunteer_id = models.ForeignKey(Volunteer, on_delete=models.CASCADE, blank=True, null=True,
- related_name='service_booking_to_volunteer')
- @staticmethod
- def search_by_volunteer(v_id):
- return ServiceBooking.objects.select_related('timeslot_id').filter(volunteer_id_id=v_id,
- timeslot_id__date__gte=datetime.now().date()).order_by(
- 'timeslot_id__date')
- @staticmethod
- def search_by_service_timeslot_list(service_list):
- return ServiceBooking.objects.filter(timeslot_id__in=service_list)
- # @staticmethod
- # def search_by_service_timeslot_list_temp(service_list):
- # return ServiceBooking.objects.select_related('timeslot_id').filter(timeslot_id__in=service_list).order_by('start_time')
- @staticmethod
- def search_by_service_timeslot_list_with_volunteer_info(service_list):
- return ServiceBooking.objects.filter(timeslot_id__in=service_list).select_related('volunteer_id')
- @staticmethod
- def search_by_service_timeslot(service):
- return ServiceBooking.objects.filter(timeslot_id=service)
- @staticmethod
- def check_exist_booking(ts_id, v_id):
- if len(ServiceBooking.objects.filter(timeslot_id=ts_id, volunteer_id=v_id)) > 0:
- return True
- else:
- return False
- @staticmethod
- def add(service_booking):
- if ServiceBooking.check_exist_booking(service_booking.timeslot_id, service_booking.volunteer_id):
- return 1
- else:
- service_booking.save()
- return 0
- @staticmethod
- def delete(timeslot, volunteer):
- ServiceBooking.objects.filter(timeslot_id_id=timeslot, volunteer_id_id=volunteer).delete()
- @staticmethod
- def get_monthly_report(date):
- input_date = datetime.strptime(date, '%Y-%m-%d')
- first_date_of_month = datetime(year=input_date.year, month=input_date.month, day=1)
- month = input_date.month
- if month == 12:
- month = 1
- else:
- month += 1
- first_date_of_next_month = datetime(year=input_date.year, month=month, day=1)
- monthly_report = ServiceBooking.objects.select_related('timeslot_id', 'volunteer_id')\
- .filter(timeslot_id__date__gte=first_date_of_month, timeslot_id__date__lt=first_date_of_next_month)\
- .order_by('timeslot_id__date', 'timeslot_id__start_time')\
- .values('timeslot_id__date', 'timeslot_id__start_time', 'timeslot_id__end_time', 'volunteer_id__name',
- 'volunteer_id__gender', 'volunteer_id__mobile', 'volunteer_id__social_id',
- 'volunteer_id__political_outlook', 'volunteer_id__organization', 'volunteer_id__district_street')
- return monthly_report
|