Exemplo n.º 1
0
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
Exemplo n.º 2
0
def associate_account_with_person(account_id):
    account = Account.query.filter(Account.id == account_id)[0]
    if account.person is not None:
        return account.person
    #For now do naive thing and assume we won't have name collisions
    names = account.name.split(' ')
    name_matches = Person.query.filter(Person.first_name == names[0],
                                       Person.last_name == names[-1],
                                       Person.is_user == False)
    person_id = -1
    if name_matches.count() == 0:
        # Create a person
        print("creating a person for the account")
        person = Person()
        person.first_name = names[0]
        person.last_name = names[-1]
        person.name = account.name
        person.slug = person.create_slug()
        person.photo_url = account.photo_url
        person.is_user = True
        db.session.add(person)
        db.session.commit()
        person_id = person.id
        account.add_person(person_id)
        db.session.add(account)
        db.session.commit()

    elif name_matches.count() == 1:
        # Associate account with person
        # TODO make it so you can have multiple accounts associated
        # with the same person
        print("associating single person with account")
        person = name_matches[0]
        person.is_user = True
        if person.photo_url is None or len(person.photo_url) is 0:
            person.photo_url = account.photo_url
        person.updated = datetime.now()
        db.session.add(person)
        db.session.commit()
        person_id = person.id
        account.add_person(person_id)
        db.session.add(account)
        db.session.commit()

    elif name_matches.count() > 1:
        print("Multiple people with same name")
    return person_id
Exemplo n.º 3
0
def create_person(first_name,
                  last_name,
                  full_name,
                  originator_id,
                  photo_url=''):
    matching_persons = Person.query.filter(Person.name == full_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)
        if photo_url and not person.photo_url:
            person.photo_url = photo_url
            db.session.add(person)
        print("creating a tag to mark %s as a possible duplicate" %
              person.slug)
    else:
        person = Person()
        person.name = full_name
        person.first_name = first_name
        person.last_name = last_name
        person.slug = person.create_slug()
        db.session.add(person)
        db.session.commit()
        tag = Tag()
        tag.initialize('added',
                       originator_id,
                       person.id,
                       subject_slug=person.slug,
                       type="metadata",
                       publicity="private")
        db.session.add(tag)
        db.session.commit()
        print("adding person %s" % person.slug)
    return person
Exemplo n.º 4
0
def upload_firebase():
    with open(app.config['FIREBASE_DUMP']) as f:
        firebase = json.loads(f.read())

    for (person_id, person) in firebase['persons']['data'].items():
        if 'name' not in person:
            continue
        new_person = Person()
        new_person.first_name = person['name'].split(' ')[0].title()
        if len(person['name'].split(' ')) > 1:
            new_person.last_name = ' '.join(
                person['name'].split(' ')[1:]).title()
        else:
            new_person.last_name = ' '
        new_person.name = person['name']
        new_person.slug = new_person.create_slug()
        #if new_person.first_name == "David":
        #    pdb.set_trace()
        if Person.query.filter(Person.first_name == new_person.first_name,
                               Person.last_name
                               == new_person.last_name).count() is 0:
            print("adding person %s" % new_person.slug)
            db.session.add(new_person)
            new_tag = Tag()
            new_tag = new_tag.initialize("Created from firebase",
                                         new_person.id,
                                         new_person.id,
                                         subject_slug=new_person.slug,
                                         originator_slug=new_person.slug,
                                         type="metadata",
                                         publicity="private")
            db.session.add(new_tag)
            firebase['persons']['data'][person_id]['id'] = new_person.id
            firebase['persons']['data'][person_id]['slug'] = new_person.slug
        else:
            firebase['persons']['data'][person_id]['id'] = Person.query.filter(
                Person.first_name == new_person.first_name,
                Person.last_name == new_person.last_name)[0].id
            firebase['persons']['data'][person_id][
                'slug'] = Person.query.filter(
                    Person.first_name == new_person.first_name,
                    Person.last_name == new_person.last_name)[0].slug
    for (tag_id, tag) in firebase['tags']['data'].items():
        if 'subject' not in tag or tag['subject'] in [
                'Climbing_Person7', 'Climbing_person8', '0', '1', '2', '3'
        ]:
            continue
        if tag['subject'] == 'Rachel_Zucker3':
            tag['subject'] = 'Rachel_Zucker1'
        if tag['subject'] == 'Seth_Berman16':
            tag['subject'] = 'Seth_Berman10'
        if tag['subject'] == 'Natalie_Dillon_14':
            tag['subject'] = 'Natalie_Dillon11'
        if tag['subject'] == 'Sasha_Sheng':
            tag['subject'] = 'Sasha_Sheng12'
        if tag['subject'] == 'Adrienne_Tran':
            tag['subject'] = 'Adrienne_Tran0'
        if tag['subject'] == 'Micah_Catlin':
            tag['subject'] = 'Micah_Catlin14'
        if tag['subject'] == 'Leo_Polovets9':
            tag['subject'] = 'Leo_Polovets2'
        new_tag = Tag()
        publicity = tag['publicity']
        type = find_tag_type(tag['label'])
        originator_id = Person.query.filter(
            Person.first_name == "Benjamin",
            Person.last_name == "Reinhardt")[0].id

        subject_id = firebase['persons']['data'][tag['subject']]['id']
        subject_slug = firebase['persons']['data'][tag['subject']]['slug']
        new_tag = new_tag.initialize(tag['label'],
                                     originator_id,
                                     subject_id,
                                     subject_slug=subject_slug,
                                     publicity=publicity,
                                     type=type)
        print(new_tag.to_deliverable())
        if publicity == "public" and "metadata" not in decode_tag_types(
                type) and Label.query.filter(
                    Label.text == tag['label']).count() == 0:
            print("Creating new label %s" % tag['label'])
            new_label = Label()
            new_label.text = tag['label']
            db.session.add(new_label)
            new_tag.label = new_label.id
        if Tag.query.filter(Tag.slug == new_tag.slug).count() == 0:
            print("adding tag %s" % new_tag.slug)
            db.session.add(new_tag)
        else:
            slug = Tag.query.filter(Tag.slug == new_tag.slug)[0].slug
            print("tag %s already exists" % slug)

    db.session.commit()

    return jsonify(firebase)