Ejemplo n.º 1
0
def person(person_id):
    if request.method == 'GET':
        if person_id is not None:
            p = session.query(Person).get(person_id)
            return jsonify(AjaxResult(True, '{0} retrieved successfully'.format(p.Name), p.to_json()).to_json())
        else:
            return jsonify(AjaxResult(False, 'No person was found'))

    if request.method == 'DELETE':
        p = session.query(Person).get(person_id)
        session.delete(p)
        session.commit()
        return jsonify({'result': 'Person deleted successfully'})

    if request.method == 'PUT':
        p = session.query(Person).get(person_id)

        if request.form['name'] is not None and request.form['name'] != '':
            p.Name = request.form['name']

        if request.form['facebook'] is not None and request.form['facebook'] != '':
            p.FacebookUserName = request.form['facebook']

        if request.form['instagram'] is not None and request.form['instagram'] != '':
            p.InstagramUserName = request.form['instagram']

        if request.form['twitter'] is not None and request.form['twitter'] != '':
            p.TwitterUserName = request.form['twitter']

        session.commit()

        return jsonify(AjaxResult(True, 'Person updated successfully!').to_json())
Ejemplo n.º 2
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()})
Ejemplo n.º 3
0
def favorite_image():
    img_id = request.form.get('imgId')
    fav = request.form.get('favorite')

    if fav == 'true':
        fav = 0
    elif fav == 'false':
        fav = 1

    img = session.query(Image).get(int(img_id))
    img.Favorite = fav
    session.commit()

    return jsonify({'result': fav})
Ejemplo n.º 4
0
def image(image_id):
    if request.method == 'GET':
        img = session.query(Image).get(image_id)
        img.Tags.sort(key=lambda x: x.TagName)
        return render_template('images/editImage.jade', title='Edit Image', img=img)

    if request.method == 'DELETE':
        img = session.query(Image).get(image_id)
        img_path = os.path.join(config.STORAGE_CONTAINER, img.Image_Name)
        # Delete file
        if os.path.exists(img_path):
            os.remove(img_path)

        # Delete record from database
        session.query(Image).filter(Image.ImageId == image_id).delete()
        session.commit()

        engine.execute('DELETE FROM images_tags WHERE ImageId = {0}'.format(image_id))
        engine.execute('DELETE FROM images_people WHERE ImageId = {0}'.format(image_id))
        # Delete from images albums
        # Delete from images collections

        return jsonify(AjaxResult(True, 'Image was deleted successfully').to_json())
Ejemplo n.º 5
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)