Exemple #1
0
def create_person():
    if request.method == 'POST':
        p = Person()
        p.Name = request.form['name']
        session.add(p)
        session.commit()
        return jsonify({'result': True, 'person': p.to_json()})
Exemple #2
0
def people():
    if request.method == 'GET':
        q = request.args.get('q')

        if q is None:
            p = session.query(Person).order_by(Person.Name).all()
        else:
            p = session.query(Person).order_by(Person.Name).filter(Person.Name.like('%' + q + '%'))
        return jsonify({'people': JsonSerialize.convert_list_to_json(p)})

    if request.method == 'POST':
        directory = request.form['directory']
        result = AjaxResult().to_json()

        new_count = 0
        folders = os.listdir(directory)
        for folder in folders:
            # Check if person already exists in database
            exists = session.query(Person).filter(Person.Name == folder).count()

            # If they don't exist, add them to database
            if exists == 0:
                p = Person()
                p.Name = folder
                session.add(p)
                session.commit()
                new_count += 1

            if new_count == 0:
                result = {'message': 'No new people were added to the database', 'count': new_count}
            elif new_count == 1:
                result = {'message': 'One new person was added to the database', 'count': new_count}
            else:
                result = {'message': '{} new people were added to the database'.format(new_count), 'count': new_count}

        return jsonify(result)