Esempio n. 1
0
 def validate_spouse_details(self, data):
     serializer = None
     if data.get('spouse_details'):
         serializer = MemberSerializer(data=data['spouse_details'])
     else:
         serializer = MemberSerializer(
             Member.objects.get(id=data['spouse']), data={}, partial=True)
     serializer.is_valid()
     return serializer
Esempio n. 2
0
    def update(self, request, pk):
        member = Member.objects.get(id=pk)
        serializer = MemberSerializer(member, request.data, partial=True)
        errors = {}
        if not serializer.is_valid():
            errors = serializer.errors

        serializer_spouse = None
        if (request.data.get('married')):
            if not request.data.get('spouse_details',
                                    {}) and not request.data.get('spouse'):
                errors['spouse_details'] = {
                    'spouse': 'This field may not be blank'
                }
            else:
                serializer_spouse = self.validate_spouse_details(request.data)
                if serializer_spouse.errors:
                    errors['spouse_details'] = serializer_spouse.errors

        c = self.validate_children(request.data.get('children', []), member)
        if c[0]:
            errors['children'] = c[1]
        if errors:
            return Response(errors, status.HTTP_400_BAD_REQUEST)

        member = serializer.save()
        #update children first to prevent any children created due to marriage from being deleted
        self.update_children(c[2], serializer.instance)
        self.update_spouse(serializer_spouse, member)
        print(MemberSerializer(member).data)
        return Response(MemberSerializer(member).data)
 def load(self):
     for i in range(1, self.sheet.nrows):
         r = self.get_detail(i)
         m = MemberSerializer(data=self.format_details(r))
         if not m.is_valid():
             print(i, m.errors)
         else:
             m.save()
     self.set_spouse()
Esempio n. 4
0
 def post(self, request):
     """
     It creates a member object/s.
     :param request: http request
     :return: http response
     """
     serializer = MemberSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Esempio n. 5
0
    def create(self, request):
        serializer = MemberSerializer(data=request.data)
        serializer_children = ChildSerializer(data=request.data.get(
            'children', []),
                                              many=True)
        errors = {}
        if not serializer.is_valid():
            errors = serializer.errors

        serializer_spouse = None
        if (request.data.get('married')):
            if not request.data.get('spouse_details',
                                    {}) and not request.data.get('spouse'):
                errors['spouse_details'] = {
                    'spouse': 'This field may not be blank'
                }
            else:
                serializer_spouse = self.validate_spouse_details(request.data)
                if serializer_spouse.errors:
                    errors['spouse_details'] = serializer_spouse.errors

        if not serializer_children.is_valid():
            errors['children'] = serializer_children.errors
        if errors:
            return Response(errors, status.HTTP_400_BAD_REQUEST)

        member = serializer.save()
        serializer_children.save()

        parent_role = 'father' if member.gender == 'M' else 'mother'
        for child in serializer_children.instance:
            setattr(child, parent_role, serializer.instance)
            child.save()

        self.update_spouse(serializer_spouse, member)
        return Response(MemberSerializer(serializer.instance).data)
 def set_spouse(self):
     for i in range(1, self.sheet.nrows):
         r = self.get_detail(i)
         f = self.format_details(r)
         m = self.search_name(f['first_name'] + ' ' + f['middle_name'] +
                              ' ' + f['last_name'])
         if m.spouse:
             continue
         for child in f['children']:
             if child:
                 m_role = 'father' if m.gender == 'M' else 'mother'
                 child_obj = Child(first_name=child['first_name'],
                                   middle_name=child['middle_name'])
                 setattr(child_obj, m_role, m)
                 child_obj.save()
         if f['spouse_details'].get('first_name') and len(
                 r['spouse'].strip().split(' ')) > 1:
             s = self.search_name(f['spouse_details']['first_name'] + ' ' +
                                  f['spouse_details']['middle_name'] + ' ' +
                                  f['spouse_details']['last_name'])
             if s and s.first_name.lower(
             ) == f['spouse_details']['first_name'].lower() and (
                     s.middle_name.lower()
                     == f['spouse_details']['middle_name'].lower()
                     or s.last_name.lower()
                     == f['spouse_details']['last_name'].lower()):
                 self.update_spouse(m, s)
             else:
                 serializer_spouse = MemberSerializer(
                     data=f['spouse_details'])
                 if serializer_spouse.is_valid():
                     serializer_spouse.save(dummy=True)
                     self.update_spouse(m, serializer_spouse.instance)
                 else:
                     print(f, 'spouse details invalid ',
                           serializer_spouse.errors)