Ejemplo n.º 1
0
def newPost():
    error = None
    form = PostsForm(CombinedMultiDict((request.files, request.form)))
    if request.method == 'POST' and form.validate_on_submit():
        if form.photo.data:
            photo = form.photo.data
            caption = form.caption.data
            if photo.filename == '':
                error = 'No selected file'
            if photo and allowed_file(photo.filename):
                filename = secure_filename(photo.filename)
                newpost = Posts(user_id=current_user.id,
                                image_URI=photo,
                                caption=caption)
                photo.save(os.path.join(newpost.image_URI, filename))
                db.session.add(newpost)
                db.session.commit()
                return jsonify({'messages': 'Photo Post successfully'})
            else:
                error = 'File not allowed'
                return jsonify({'errors': error})
        else:
            caption = form.caption.data
            newpost = Posts(user_id=current_user.id, caption=caption)
            db.session.add(newpost)
            db.session.commit()
            return jsonify({'messages': 'Post successfully'})
    else:
        return jsonify({'errors': form_errors(form)})
Ejemplo n.º 2
0
def register():
    error = None
    form = RegistrationForm()
    if request.method == 'POST' and form.validate_on_submit():
        username = form.username.data
        plain_password = form.plain_password.data
        conf_password = form.conf_password.data
        first_name = form.first_name.data
        last_name = form.last_name.data
        email = form.email.data
        location = form.location.data
        if not Users.query.filter_by(
                email=email).first() and not Users.query.filter_by(
                    user_name=username).first():
            user = Users(user_name=username,
                         first_name=first_name,
                         last_name=last_name,
                         email=email,
                         plain_password=plain_password,
                         location=location)
            db.session.add(user)
            db.session.commit()
            return jsonify({'messages': 'You have successfully registered'})
        else:
            error = "Email and/or username already exists"
            return jsonify({'errors': error})
    else:
        return jsonify({'errors': form_errors(form)})
Ejemplo n.º 3
0
def login():
    error = None
    form = LoginForm()
    if request.method == 'POST' and form.validate_on_submit():
        username = form.username.data
        plain_password = form.plain_password.data
        user = Users.query.filter_by(user_name=username).first()
        if user and user.is_correct_password(plain_password):
            login_user(user)
            payload = {
                'id': current_user.id,
                'username': current_user.user_name
            }
            token = jwt.encode(payload,
                               app.config['TOKEN_SECRET'],
                               algorithm='HS256')
            userdata = [
                current_user.user_name, current_user.first_name,
                current_user.last_name, current_user.location,
                current_user.joined_on, token, current_user.id
            ]
            return jsonify(data={'user_credentials': userdata},
                           message="Token Generated")
        else:
            error = "Invalid email and/or password"
            return jsonify({'errors': error})
    else:
        return jsonify({'errors': form_errors(form)})
Ejemplo n.º 4
0
def userLogin():
    if current_user.is_authenticated:
        return jsonify(errors=[{'message': 'User already logged in'}])
    form = LoginF()
    if request.method == 'POST' and form.validate_on_submit():
        username = form.username.data
        password = form.password.data

        user=Users.query.filter_by(username=username,password=password).first()
        if user is not None:
            login_user(user)
            payload = {'user_id' : user.id}
            token = jwt.encode(payload, csrf)
            return jsonify(response = [{'message': 'You have successfully logged in', 'token': token, 'userid': user.id, 'photo':'/static/uploads/'+user.profile_photo}])
        else:
            return jsonify(errors= [{'error':['Username or password is incorrect']}])
    return jsonify(errors= [{'error':form_errors(form)}])
Ejemplo n.º 5
0
def upload():
    form = upload_Form()
    if request.method == 'POST' and form.validate_on_submit():
        description = request.form['description']
        file = request.form['file']
        if file.filename == '':
            error = 'No selected file'
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            flash('File Saved', 'success')
            return jsonify({
                'message': 'File upload successful',
                'file': file,
                'description': description
            })
    else:
        return jsonify({'errors': form_errors(form)})
Ejemplo n.º 6
0
def login():
    form = LoginF()
    
    if request.method == "POST" and form.validate_on_submit():
        username = form.username.data
        password = form.password.data
        
        user = Users.query.filter_by(username=username).first()
        
        if user != None and check_password_hash(user.password, password):
            payload = {'user': user.username}
            jwt_token = jwt.encode(payload,app.config['SECRET_KEY'],algorithm = "HS256")
            response = {'message': 'User successfully logged in','token':jwt_token, "user_id": user.id}
            
            return jsonify(response)
            
        return jsonify(errors="Username or password is incorrect")
    
    return jsonify(errors=form_errors(form))
Ejemplo n.º 7
0
def newPost(user_id):
    error = None
    form = PostsForm()
    if request.method == 'POST' and form.validate_on_submit():
        photo = form.photo.data
        caption = form.caption.data
        if photo.filename == '':
            error = 'No selected file'
        if photo and allowed_file(photo.filename):
            filename = secure_filename(photo.filename)
            newpost = Posts(user_id=user_id, photo=photo, caption=caption)
            file.save(os.path.join(newpost.post_URI, filename))
            db.session.add(newpost)
            db.session.commit()
            return jsonify(message="Post successfully")
        else:
            error = 'File not allowed'
            return jsonify({'errors': error})
    else:
        return jsonify({'errors': form_errors(form)})
Ejemplo n.º 8
0
def register():
    error=None
    form = RegisterF()
    if request.method == 'POST' and form.validate_on_submit():
        username = request.username.data
        password = request.password.data
        firstname = request.firstname.data
        lastname = request.lastname.data
        email = request.email.data
        location = request.location.data
        if not Users.query.filter_by(email = email).first() and not Users.query.filter_by(username = username).first():
            users = users(user_name = username, first_name = first_name, last_name = last_name, email = email, plain_password = password,location=location)
            db.session.add(users)
            db.session.commit()
            #flash success message
            return url_for('login')
        else:
            error = "Email and/or username already exists"
            return jsonify({'errors': error})
    else:
        return jsonify({'errors':form_errors(form)})
Ejemplo n.º 9
0
def posts(user_id):
    
    if request.method == 'GET':
        posts = Posts.query.filter_by(user_id = user_id).all()
        
        user = Users.query.filter_by(id=user_id).first()
        user_follower_count = len(Follows.query.filter_by(user_id=user.id).all())
        response = {"status": "ok", "post_data":{"firstname":user.first_name, "lastname": user.last_name, "location": user.location, "joined_on": "Member since "+strf_time(user.joined_on, "%B %Y"), "bio": user.biography, "postCount": len(posts), "followers": user_follower_count, "profile_image": os.path.join(app.config['PROFILE_IMG_UPLOAD_FOLDER'],user.profile_photo), "posts":[]}}
        
        for post in posts:
            postObj = {"id":post.id, "user_id": post.user_id, "photo": os.path.join(app.config['POST_IMG_UPLOAD_FOLDER'], post.photo), "caption": post.caption, "created_on": post.created_on}
            response["post_data"]["posts"].append(postObj)
        
        return jsonify(response)
    
    
    if request.method == 'POST':
        
        form = PostF()
        
        if form.validate_on_submit():
            
            u_id = form.user_id.data
            photo = form.photo.data
            captn = form.caption.data
            
            user = Users.query.filter_by(id=u_id).first()
            
            filename = user.username+secure_filename(photo.filename)
            
            create_date = str(datetime.date.today())
            post = Posts(user_id=u_id,photo=filename,caption=captn ,created_on=create_date)
            photo.save(os.path.join("./app", app.config['POST_IMG_UPLOAD_FOLDER'],filename))
            db.session.add(post)
            db.session.commit()
            return jsonify(status=201, message="Post Created")
            
            
        print form.errors.items()
        return jsonify(status=200, errors=form_errors(form))