SHARE
TWEET

Untitled

a guest Sep 15th, 2019 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class EventViewSet(viewsets.ModelViewSet):
  2.     queryset = models.Event.objects.all()
  3.     serializer_class = serializers.EventSerializer
  4.     permission_classes = (permissions.AllowAny,)
  5.     filter_backends = (django_filters.rest_framework.DjangoFilterBackend,)
  6.     filterset_fields = ('patient', 'type')
  7.     pagination_class = None
  8.  
  9.     def create(self, request, *args, **kwargs):
  10.         patient_id = request.data['patient']
  11.         patient = models.Patient.objects.filter(pk=patient_id)
  12.  
  13.         if not patient.exists():
  14.             raise Http404
  15.         else:
  16.             patient = patient.get()
  17.  
  18.         keys = request.data.keys()
  19.         if 'doctor' not in keys:
  20.             request.data['doctor'] = models.Patient.objects.get(pk=patient_id).doctor_id
  21.         if 'card' not in keys:
  22.             request.data['card'] = models.Card.objects.get(patient_id=patient_id).pk
  23.  
  24.         if request.data['type'] == models.Event.MENSTRUATION:
  25.             models.Event.objects.filter(
  26.                 (Q(type=models.Event.MENSTRUATION) | Q(type=models.Event.OVULATION)) & Q(patient_id=patient_id)
  27.             ).delete()
  28.  
  29.             start_date = parse_datetime(request.data['start_date'])
  30.             cycle = request.data['duration']
  31.  
  32.             serializer = self.get_serializer(data=request.data)
  33.             serializer.is_valid(raise_exception=True)
  34.             self.perform_create(serializer)
  35.             headers = self.get_success_headers(serializer.data)
  36.  
  37.             if cycle % 2 == 0:
  38.                 ovulation_delta_days = cycle / 2 - 2
  39.             else:
  40.                 ovulation_delta_days = cycle // 2 - 2
  41.  
  42.             year_offset = start_date + timedelta(days=365)
  43.             while start_date < year_offset:
  44.                 models.Event.objects.create(
  45.                     type=models.Event.OVULATION,
  46.                     doctor_id=request.data['doctor'],
  47.                     card_id=request.data['card'],
  48.                     start_date=start_date + timedelta(days=ovulation_delta_days),
  49.                     duration=5,
  50.                     patient_id=patient_id
  51.                 )
  52.  
  53.                 start_date += timedelta(days=cycle)
  54.                 request.data['start_date'] = start_date
  55.  
  56.                 serializer = self.get_serializer(data=request.data)
  57.                 serializer.is_valid(raise_exception=True)
  58.                 self.perform_create(serializer)
  59.             return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
  60.  
  61.         elif request.data['type'] == models.Event.PREGNANCY:
  62.             patient.state = models.Patient.PREGNANT
  63.             patient.save()
  64.  
  65.         elif request.data['type'] == models.Event.ABORTION:
  66.             patient.state = models.Patient.DEFAULT
  67.             patient.save()
  68.  
  69.         elif request.data['type'] == models.Event.INTAKE:
  70.             start_date = parse_datetime(request.data['start_date'])
  71.             request.data['start_date'] = start_date + timedelta(days=4)
  72.             models.Event.objects.create(
  73.                 type=models.Event.TREATMENT_ASSIGNMENT,
  74.                 doctor_id=request.data['doctor'],
  75.                 card_id=request.data['card'],
  76.                 start_date=start_date,
  77.                 patient_id=patient_id
  78.             )
  79.  
  80.         elif request.data['type'] == models.Event.ANALYSIS:
  81.             #details = request.data['analysisType']
  82.             #details = models.Analyzes.FRAGMENTATION
  83.             details = 'FRAGMENTATION'
  84.  
  85.  
  86.         return super().create(request, *args, **kwargs)
  87.  
  88.     def get_serializer(self, *args, **kwargs):
  89.         """ if an array is passed, set serializer to many """
  90.         if isinstance(kwargs.get('data', {}), list):
  91.             kwargs['many'] = True
  92.         return super().get_serializer(*args, **kwargs)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top