def create_person(): data = request.get_json() or {} (account_id, originator_id) = get_account_and_person(data['token']) person = Person() #handle an updated person if 'id' in data: q = Person.query.filter(Person.slug == data['id']) if q.count() > 0: person = q[0] person.updated = datetime.datetime.now() if 'first_name' not in data and 'last_name' not in data and 'name' not in data: return "ERROR: missing required field" if 'name' in data: name = data['name'].strip().title() first_name = name.split(' ')[0] if len(name.split(' ')) > 1: last_name = ' '.join(name.split(' ')[1:]) else: last_name = '' else: first_name = data['first_name'].strip().title() last_name = data['last_name'].strip().title() name = ' '.join(first_name, last_name) # For now names will be singular and we'll just note possible duplicates matching_persons = Person.query.filter(Person.name == name) #assume we found a dumplicate person if matching_persons.count() > 0: #pdb.set_trace() person = matching_persons[0] tag = Tag() tag.initialize('possible duplicate', originator_id, person.id, type="metadata", publicity="private") if Tag.query.filter(Tag.slug == tag.slug).count() == 0: db.session.add(tag) print("creating a tag to mark %s as a possible duplicate" % person.slug) else: person = Person() person.name = name person.first_name = first_name person.last_name = last_name person.slug = person.create_slug() db.session.add(person) tag = Tag() tag.initialize('added', originator_id, person.id, subject_slug=person.slug, type="metadata", publicity="private") db.session.add(tag) print("adding person %s" % person.slug) response = jsonify(person.to_deliverable()) db.session.commit() return response