def complete_registration(self, password): ''' Completes the registration of the new sign up ''' # Create a new family, user and person family = Family.objects.create(description=self.email_address) user = User.objects.create_user(email=self.email_address, password=password, name=self.name, family_id=family.id, language=self.language) person = Person(name=self.name, gender=self.gender, family=family, language=self.language, user=user, email=self.email_address, birth_year=self.birth_year) person.address = self.address person.save() # Delete sign up self.delete() return user
def add_relation_post(request, person_id=0, person=None): ''' Receives post information for a new relation ''' relation_type = int(request.POST.get("relation_type")) if relation_type not in (PARTNERED, RAISED, RAISED_BY): raise Http404 #If person does not exist, create a new person existing_person = int(request.POST.get("existing_person")) if not existing_person: new_name = request.POST.get("name").strip() if len(new_name) == 0: raise Http404 language = request.POST.get("language") #http://stackoverflow.com/a/2917399/1245362 if language not in [x[0] for x in settings.LANGUAGES]: raise Http404 gender = request.POST.get("gender") if gender not in (MALE, FEMALE, OTHER): raise Http404 birth_year, birth_year_valid = intTryParse( request.POST.get("birth_year")) if not birth_year_valid: birth_year = 0 new_person = Person(name=new_name, gender=gender, language=language, family_id=person.family_id, birth_year=birth_year) address = request.POST.get("address") if address: new_person.address = address if relation_type == PARTNERED: new_person.hierarchy_score = person.hierarchy_score elif relation_type == RAISED: new_person.hierarchy_score = person.hierarchy_score + 1 elif relation_type == RAISED_BY: new_person.hierarchy_score = person.hierarchy_score - 1 new_person.save() relation_id = new_person.id else: #Existing person relation_id = int(request.POST.get("relation_id")) new_relation = Relation(from_person_id=person.id, to_person_id=relation_id, relation_type=relation_type) new_relation.save() reevaluate_hierarchy_scores_of_orphans(request.user.family_id) return HttpResponseRedirect('/tree/{0}/'.format(person_id))
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 })