def create_or_find_person(person_dict, logged_in_profile_username): people_with_same_name = People.objects.filter(Name=person_dict.get('name')) # First, try to match RT id if person_dict.get('id'): try: person = People.objects.get(RottenTomatoesId=person_dict.get('id')) return person except People.DoesNotExist: pass # or try to match name with or without an RT id elif people_with_same_name.count() == 1: person = people_with_same_name[0] if person.RottenTomatoesId: person_notification(1, person) return person people_with_same_name_null = people_with_same_name.filter(Q(RottenTomatoesId=None) | Q(RottenTomatoesId='')) # Next, try to find one entry with null RT id if people_with_same_name_null.count() == 1: person = people_with_same_name_null[0] if person_dict.get('id'): person.RottenTomatoesId = person_dict.get('id') person.save() person_notification(2, person) else: person_notification(1, person) return person # Lastly, add new person try: person = People(Name=person_dict.get('name'),RottenTomatoesId=person_dict.get('id')) if people_with_same_name.count() > 1: person_notification(3, person) person.full_clean() person.save() property_logger.info(person_dict.get('name').encode('ascii', 'replace') + ' Create Success by ' + logged_in_profile_username) return person except ValidationError: property_logger.info(person_dict.get('name').encode('ascii', 'replace') + ' Create Failure by ' + logged_in_profile_username) return None
def inset_people(request): global username if request.POST: global username user = Users.objects.get(username = username) post = request.POST new_people = People( username1_id = user.id, name = post["name"], nation = post["nation"], phone = post["phone"], email = post["email"], date = post["date"], address = post["address"], postkey_id = Salary.objects.get(post_name=post["post_name"]).id, birthday = post["birthday"] ) if post["sex"] == "M": new_people.sex = True else: new_people.sex = False new_people.save() return render_to_response("insert_people.html")