def parse_individual(element: IndividualElement, tree): child = Person() # get names from element names = list(get_names(element)) # save legal name # assumes first name in list is primary name legal_name = LegalName() parse_name_dict(names[0], legal_name) legal_name.tree = tree legal_name.save() child.legal_name = legal_name # save alternate names for name_dict in names[1:]: alternate_name = AlternateName() parse_name_dict(name_dict, alternate_name) alternate_name.person = child alternate_name.tree = tree alternate_name.save() child.gender = parse_gender(get_value(element, tags.GEDCOM_TAG_SEX)) # Is saving related models necessary, or will it cascade? birth_event_element = get_next_child_element(element, tags.GEDCOM_TAG_BIRTH) if birth_event_element: child.birth_date = parse_event_date(birth_event_element) child.birth_location = parse_event_location(birth_event_element) death_event_element = get_next_child_element(element, tags.GEDCOM_TAG_DEATH) if death_event_element: child.death_date = parse_event_date(death_event_element) child.death_location = parse_event_location(death_event_element) child.living = 'Dead' # living defaults to Unknown; change to living = has birth year and not has death year? child.tree = tree child.save() return element.get_pointer(), child
def save_person(request, current_tree, template_name, current_person: Person = None): if current_tree.creator == request.user: name_form = None person_form = None alt_name_formset = None if request.method == 'POST': # if this is a POST request we need to process the form data name_form = AddNameForm( request.POST, instance=current_person.legal_name if current_person else None) person_form = AddPersonForm(request.POST, instance=current_person) # Tuple that contains validation status of each filled form form_validations = ( person_form.is_valid(), name_form.is_valid(), ) # Create/Save instances if all forms are valid. if all(form_validations): # Create a Legal Name instance from name form's data created_legal_name = name_form.save(commit=False) created_legal_name.tree = current_tree created_legal_name.save() if not current_person: # Create a Person instance from person form's data # Person instance's Legal Name attribute will be a foreign key current_person = person_form.save(commit=False) current_person.tree = current_tree current_person.legal_name = created_legal_name current_person.save() # Create Alternate Name for person alt_name_formset = AlternateNameFormSet( request.POST, instance=current_person) if alt_name_formset.is_valid(): alt_names = alt_name_formset.save(commit=False) for alt_name in alt_names: alt_name.tree = current_tree alt_name.save() # Check each location form's data and query for existing Location # instances. # If location exists, stores it in the corresponding location # variable and sets location_created boolean to false # If it doesn't exist, create a new instance from form's data and # set location_created boolean to true birth_location, birth_loc_was_created = Location.objects.get_or_create( city=person_form.cleaned_data['birth_city'], state=person_form.cleaned_data['birth_state'], country=person_form.cleaned_data['birth_country']) death_location, death_loc_was_created = Location.objects.get_or_create( city=person_form.cleaned_data['death_city'], state=person_form.cleaned_data['death_state'], country=person_form.cleaned_data['death_country']) # if new location instances were created, save them in the DB if birth_loc_was_created: birth_location.save() if death_loc_was_created: death_location.save() # Assign the location instances as keys in Person instance current_person.birth_location = birth_location current_person.death_location = death_location current_person.save() # redirect to back to the Tree detail page that # Person was created in return redirect('tree_detail', pk=current_person.tree.id) # if a GET (or any other method) we'll create a blank form if current_person: name_form = name_form or AddNameForm( instance=current_person.legal_name) initial_data = dict() if current_person.birth_location: initial_data.update({ 'birth_city': current_person.birth_location.city, 'birth_state': current_person.birth_location.state, 'birth_country': current_person.birth_location.country, }) if current_person.death_location: initial_data.update({ 'death_city': current_person.death_location.city, 'death_state': current_person.death_location.state, 'death_country': current_person.death_location.country }) person_form = person_form or AddPersonForm(instance=current_person, initial=initial_data) alt_name_formset = alt_name_formset or AlternateNameFormSet() else: name_form = name_form or AddNameForm() person_form = person_form or AddPersonForm() alt_name_formset = alt_name_formset or AlternateNameFormSet() context = { 'name_form': name_form, 'person_form': person_form, 'alt_name_formset': alt_name_formset, } return render(request, template_name, context) else: raise Http404