Exemplo n.º 1
0
def get_delete_image(img_owner_username, img_filename):
    if utils.validate_username(img_owner_username) and img_filename:
        user_id = session.get('user_id', None)

        fotogal_image = FotogalImage()

        if request.method == 'GET':
            (
                img_headers,
                img_content,
            ) = fotogal_image.get_data(user_id, img_owner_username,
                                       img_filename)

            if img_headers and img_content:
                resp = Response(img_content)

                for k, v in img_headers.items():
                    resp.headers.add(k, v)

                return resp

            else:
                flask_abort(404)

        elif request.method == 'DELETE':
            if fotogal_image.remove(user_id, img_filename):
                return 'OK', 200
            else:
                flask_abort(400)

    else:
        flask_abort(404)
Exemplo n.º 2
0
def show_profile(username):
    if utils.validate_username(username):

        fotogal_user = FotogalUser()
        profile_dict = fotogal_user.get_profile_props(username=username)

        user_id = profile_dict.get('id', None)
        session_user_id = session.get('user_id', None)

        if session_user_id == user_id:
            profile_owner = True
        else:
            profile_owner = False

        fotogal_images = FotogalImage()
        imgs_list = fotogal_images.get_user_imgs_list(user_id=user_id)
        imgs_posted_total = fotogal_images.get_posted_imgs_total(user_id)

        fotogal_follow = FotogalFollow()
        following_total = fotogal_follow.get_following_total(user_id)
        followers_total = fotogal_follow.get_followers_total(user_id)

        return render_template('profile.html',
                               username=username,
                               profile_owner=profile_owner,
                               profile_dict=profile_dict,
                               imgs_list=imgs_list,
                               imgs_posted_total=imgs_posted_total,
                               following_total=following_total,
                               followers_total=followers_total)

    flask_abort(400)
Exemplo n.º 3
0
def index():
    user_id = session.get('user_id', None)
    username = session.get('username', None)

    fotogal_user = FotogalUser()
    profile_dict = fotogal_user.get_profile_props(id=user_id)

    fotogal_image = FotogalImage()
    total_posted_images = fotogal_image.get_posted_imgs_total(user_id)

    fotogal_follow = FotogalFollow()
    following_total = fotogal_follow.get_following_total(user_id)

    if not following_total:
        return redirect(url_for('main.initial_suggestions'))
    else:
        # Initialize user settings
        user_settings = {
            'follow_list_step': 10,
            'follow_list_idx_low': 0,
            'follow_list_idx_high': 10,
            'query_limit': 5,
            'query_offset': 0
        }

        session['user_settings'] = user_settings

        return render_template('index.html',
                               user_id=user_id,
                               username=username,
                               total_posted_images=total_posted_images,
                               profile_dict=profile_dict)
Exemplo n.º 4
0
def profile_image_upload():
    """Upload profile image.

    """
    if 'profile_img' not in request.files:
        return jsonify({'status': '400', 'message': 'Bad Request'}), 400

    img_data = request.files['profile_img']
    img_filename = secure_filename(img_data.filename)

    if not utils.allowed_img_ext(img_filename):
        return jsonify({'status': '400', 'message': 'Bad Request'}), 400

    img_content = img_data.read()
    img_type = utils.return_img_type(img_content)

    if img_type:
        user_id = session['user_id']
        username = session['username']

        fotogal_user = FotogalUser()
        fotogal_image = FotogalImage()

        current_img_url = fotogal_user.get_profile_img_url(id=user_id)

        if current_img_url:
            # extract the filename portion from the url.
            current_img_filename = current_img_url[current_img_url.
                                                   rindex('/') + 1:]

            if not fotogal_image.remove(user_id, current_img_filename):
                return jsonify({
                    'status': '500',
                    'message': 'Internal Server Error'
                }), 500

        new_img_id = fotogal_image.save(user_id,
                                        img_filename,
                                        img_content,
                                        is_profile=True)
        img_data_dict = fotogal_image.get_img_props(new_img_id)

        profile_img_url = '/profile/%s/image/%s' % (
            username,
            img_data_dict['image_filename'],
        )
        profile_dict = {'profile_image_url': profile_img_url}

        if fotogal_user.update_profile_props(id=user_id,
                                             profile_dict=profile_dict):
            return jsonify({
                'status': '200',
                'message': 'OK',
                'profile_image_url': profile_img_url
            }), 200

    return jsonify({'status': '500', 'message': 'Internal Server Error'}), 500
Exemplo n.º 5
0
def my_images():
    """Returns a JSON that has the all images posted by a logged user.

    """
    user_id = session['user_id']

    fotogal_image = FotogalImage()

    img_list = fotogal_image.get_user_imgs_list(user_id=user_id,
                                                limit=100,
                                                offset=0)

    return jsonify({
        'status': '200',
        'message': 'OK',
        'image_list': img_list
    }), 200
Exemplo n.º 6
0
def timeline():
    """Return a JSON that has the timeline of photos.
    
    """
    if 'user_settings' not in session:
        return jsonify({'status': '404', 'message': 'Not Found'}), 404

    user_id = session['user_id']

    # Return the "follow_list"
    fotogal_follow = FotogalFollow()
    follow_list = fotogal_follow.get_follow_list(user_id=user_id)

    # Add my id to the follow_list to get my posted images
    follow_list.append(user_id)

    if len(follow_list) > 0:
        timeline_imgs_unordered_list = []
        posted_imgs_list = []

        fotogal_image = FotogalImage()

        for follow_user_id in follow_list:
            posted_imgs_list = fotogal_image.get_posted_imgs_list(
                user_id=user_id, follow_user_id=follow_user_id)

            for dict_tmp in posted_imgs_list:
                timeline_imgs_unordered_list.append(dict_tmp)

        # Order by created_ts
        timeline_imgs_list = utils.sort_list_by_timestamp(
            timeline_imgs_unordered_list)

        return jsonify({
            'status': '200',
            'message': 'OK',
            'image_list': timeline_imgs_list
        }), 200

    else:
        return jsonify({'status': '200', 'message': 'OK'}), 200
Exemplo n.º 7
0
def upload_image():
    if 'upload_img_file' not in request.files:
        return jsonify({'status': '400', 'message': 'Bad Request'}), 400

    img_data = request.files['upload_img_file']
    img_filename = secure_filename(img_data.filename)

    if utils.allowed_img_ext(img_filename):
        img_content = img_data.read()
        img_type = utils.return_img_type(img_content)

        if img_type:
            user_id = session['user_id']
            username = session['username']

            fotogal_image = FotogalImage()
            new_img_id = fotogal_image.save(user_id, img_filename, img_content,
                                            False)

            if new_img_id:
                img_data_dict = fotogal_image.get_img_props(new_img_id)

                fotogal_user = FotogalUser()
                profile_img_url = fotogal_user.get_profile_img_url(id=user_id)

                new_img_filename_url = '/%s/image/%s' % (
                    username,
                    img_data_dict['image_filename'],
                )

                return jsonify({
                    'status': '200',
                    'message': 'OK',
                    'image_id': new_img_id,
                    'image_owner': username,
                    'profile_image_url': profile_img_url,
                    'image_url': new_img_filename_url
                }), 200

    return jsonify({'status': '400', 'message': 'Bad Request'}), 400
Exemplo n.º 8
0
def get_image_profile_content(img_owner_username, img_filename):
    if utils.validate_username(img_owner_username) and img_filename:
        user_id = session.get('user_id', None)

        fotogal_image = FotogalImage()

        (
            img_headers,
            img_content,
        ) = fotogal_image.get_profile_img(img_owner_username, img_filename)

        if img_headers and img_content:
            resp = Response(img_content)

            for k, v in img_headers.items():
                resp.headers.add(k, v)

            return resp

        else:
            flask_abort(404)

    else:
        flask_abort(404)
Exemplo n.º 9
0
def image_like():
    """Process a photo like request.

    """
    img_id = request.form.get('image_id', type=int)

    user_id = session['user_id']

    if utils.validate_id(img_id):
        fotogal_image = FotogalImage()

        if request.method == 'PUT':
            fotogal_image.add_like(img_id, user_id)
        elif request.method == 'DELETE':
            fotogal_image.remove_like(img_id, user_id)

        return jsonify({'status': '200', 'message': 'OK'}), 200

    else:
        return jsonify({'status': '400', 'message': 'Bad Request'}), 400