def create(self, request): ''' Creates a relation record ''' from_person_id, from_person_id_valid = intTryParse(request.data.get('from_person_id')) to_person_id, to_person_id_valid = intTryParse(request.data.get('to_person_id')) relation_type, relation_type_valid = intTryParse(request.data.get('relation_type')) if not (from_person_id_valid and to_person_id_valid and relation_type_valid): return HttpResponse(status=400, content="Invalid to_person_id, from_person_id or relation_type") if relation_type not in (PARTNERED, RAISED, RAISED_BY): return HttpResponse(status=400, content="Invalid relation_type") if from_person_id == to_person_id: return HttpResponse(status=400, content="from_person_id cannot be to_person_id") # Ensure people exist person_queryset = Person.objects.filter(family_id = request.user.family_id) from_person = get_object_or_404(person_queryset, pk=from_person_id) to_person = get_object_or_404(person_queryset, pk=to_person_id) with reversion.create_revision(): relation = create_relation(request.user, from_person, to_person, relation_type) if not relation: raise Http404 # Store some meta-information. reversion.set_user(request.user) reversion.set_comment('Update ' + request.META.get('HTTP_X_REAL_IP')) serializer = RelationSerializer(relation, context={'request': request}) return Response(serializer.data)
def list(self, request): ''' Lists all relations between people in user's family ''' queryset = Relation.objects.filter(from_person__family_id = request.user.family_id) serializer = RelationSerializer(queryset, many=True, context={'request': request}) return Response(serializer.data)
def retrieve(self, request, pk=None): ''' Gets a single relation record ''' queryset = Relation.objects.filter(from_person__family_id = request.user.family_id) rel = get_object_or_404(queryset, pk=pk) serializer = RelationSerializer(rel, context={'request': request}) return Response(serializer.data)
def create(self, request): ''' Creates a new person record and links it to another person needs from_person_id, relation_type, name, gender, birth_year and address ''' queryset = Person.objects.filter(family_id=request.user.family_id) from_person_id, from_person_id_valid = intTryParse( request.data.get("from_person_id")) if not from_person_id_valid: return HttpResponse(status=400, content="Invalid from_person_id") from_person = get_object_or_404(queryset, pk=from_person_id) relation_type, relation_type_valid = intTryParse( request.data.get("relation_type")) if not relation_type_valid or relation_type not in (PARTNERED, RAISED, RAISED_BY): return HttpResponse(status=400, content="Invalid relation_type") name = request.data.get("name") if not name or len(name.strip()) == 0: return HttpResponse(status=400, content="Invalid name") gender = request.data.get("gender") if gender not in (MALE, FEMALE, OTHER, NON_BINARY, PREFER_NOT_TO_SAY): return HttpResponse(status=400, content="Invalid gender") birth_year, birth_year_valid = intTryParse( request.POST.get("birth_year")) if not birth_year_valid: birth_year = 0 with reversion.create_revision(): new_person = Person(name=name.strip(), gender=gender, family_id=from_person.family_id, birth_year=birth_year) address = request.data.get("address") if address: new_person.address = address # Hierarchy scores will eventually be deprecated if relation_type == PARTNERED: new_person.hierarchy_score = from_person.hierarchy_score elif relation_type == RAISED: new_person.hierarchy_score = from_person.hierarchy_score + 1 elif relation_type == RAISED_BY: new_person.hierarchy_score = from_person.hierarchy_score - 1 new_person.save() # Store some meta-information. reversion.set_user(request.user) reversion.set_comment('Create ' + request.META.get('HTTP_X_REAL_IP')) relation = create_relation(request.user, from_person, new_person, relation_type) relation_serializer = RelationSerializer(relation) person_serializer = PersonSerializer(new_person) return Response({ 'person': person_serializer.data, 'relation': relation_serializer.data })