Beispiel #1
0
def mod_collections(story_id, action):
    # getting story from db
    story = Story.query.get(story_id)
    # checking if story exists
    if story:
        if action == 'add':
            # checking if collection exists
            exisiting_collection = Collection.query.filter_by(
                user_id=current_user.id, collection_id=story.id).first()
            if exisiting_collection:
                raise RequestError('failed')
            # adding story to collections
            new_collection = Collection(collection_id=story.id,
                                        user_id=current_user.id)
            db.session.add(new_collection)
            db.session.commit()
            # adding new collection end
            message = {"message": "success"}
            return jsonify(message), 201

        if action == 'remove':
            collection = Collection.query.filter_by(
                user_id=current_user.id, collection_id=story.id).first()
            db.session.delete(collection)
            db.session.commit()
            message = {"message": "success"}
            return jsonify(message), 201

    else:
        raise RequestError('failed')
Beispiel #2
0
def create_story():
    # getting story from json
    data = request.get_json()

    if data:

        # add new Story to User model
        new_story = Story(title=data['story_title'],
                          content=data['story_content'],
                          story_image=data['story_image'],
                          author=current_user)
        # add new Story to User model end

        # adding new Story to database
        db.session.add(new_story)
        db.session.commit()
        # adding new Story to database end

        # conmputing and returning message
        message = {
            "status": "success",
            "link": "/" + current_user.username + "?status=new-post"
        }
        return jsonify(message), 201
        # computing and returning message end
    else:
        raise RequestError('failed')
Beispiel #3
0
def follow():

    # defining variables
    data = request.get_json()
    username = data['username']
    action = data['action']

    # getting user from the db
    user_to_follow = User.query.filter_by(username=username).first()

    # validating the user's existence
    if user_to_follow is None:
        raise RequestError('user not found')

    if user_to_follow == current_user:
        raise RequestError('you cannot follow yourself')

    # following the user
    if action == 'follow':
        current_user.follow(user_to_follow)
        db.session.commit()
        return jsonify({
            "message": "success",
            "username": username,
            "count": user_to_follow.followers.count()
        }), 201

    # unfollowing the user
    if action == 'unfollow':
        current_user.unfollow(user_to_follow)
        db.session.commit()
        return jsonify({
            "message": "success",
            "username": username,
            "count": user_to_follow.followers.count()
        }), 201
Beispiel #4
0
def comment():
    data = request.get_json()

    content = data['content']
    story_id = data['story_id']
    story = Story.query.get(story_id)

    if data and story:
        new_comment = Comment(content=content,
                              author=current_user,
                              story=story)
        db.session.add(new_comment)
        db.session.commit()
        message = {
            "message": "success",
            "data": {
                "author": current_user.username,
                "img": current_user.profile_pic,
                "content": content
            }
        }
        return jsonify(message)
    else:
        raise RequestError('failed')
Beispiel #5
0
def login():

    user_data = request.get_json()

    # defining variables
    form = LoginForm()
    blog_title = "Sign In | Start Telling Your Stories"
    # defining variables end

    if user_data:
        email = user_data['email']
        password = user_data['password']
        remember_me = user_data['remember_me']
        existing_user = User.query.filter_by(email=email.lower()).first()

        if existing_user and bcrypt.check_password_hash(
                existing_user.password, password):
            # logging user in using flask login (login_user) for session management
            login_user(existing_user, remember=remember_me)
            return jsonify({"message": "Login Successful"}), 200
        elif existing_user:
            raise RequestError('Invalid Password')
        else:
            raise RequestError('Invalid Credentials')

    # redirecting using for accessing page if already logged in
    if current_user.is_authenticated:
        return redirect(url_for('dashboard', username=current_user.username))
    # redirecting using for accessing page if already logged in end

    # Validating form on submit
    if form.validate_on_submit():
        # checking if user exists
        existing_user = User.query.filter_by(
            email=form.email.data.lower()).first()
        # checking if password matches signup password
        if existing_user and bcrypt.check_password_hash(
                existing_user.password, form.password.data):
            # logging user in using flask login (login_user) for session management
            login_user(existing_user, remember=form.remember_me.data)
            next_page = request.args.get("next")
            # sending flased message
            flash("Login Successful", "success")
            # logging user in using flask login (login_user) for session management end
            # redirecting user to dashboard
            return redirect(next_page) if next_page else redirect(
                url_for('dashboard', username=existing_user.username))
            # redirecting user to dashboard end

        # checking if user exists end
        elif existing_user:
            flash("Invalid Sign In Password", "danger")
        else:
            flash("That User Does not Exist", "danger")
        # checking if password matches signup password end
        # sending flased message end

    # Validating form on submit end
    return render_template('login.html',
                           form=form,
                           title=blog_title,
                           current_year=current_year)