Beispiel #1
0
def create_post():
    userAuthenticated, user = isAuthenticated(
        flaskRequest.headers['authorization'])

    if userAuthenticated:
        description = flaskRequest.form.get('description')
        title = flaskRequest.form.get('title')

        # save img tmp
        image = flaskRequest.files['image']
        hashedImageName = f'{uuid4()}-{image.filename.replace(" ", "").replace("(", "").replace(")", "")}'
        imageKey = f'gallery/{hashedImageName}'
        tmpPath = f'{rootDir}/tmp/{hashedImageName}'
        image.save(tmpPath)

        # upload
        s3 = S3()
        s3.uploadFile(tmpPath, f'gallery/{hashedImageName}')
        os.remove(tmpPath)

        post = Posts(imageUrl=f'{bucketURL}/gallery/{hashedImageName}',
                     imageKey=imageKey,
                     author=user['id'],
                     description=description,
                     title=title)

        post.save()

        return jsonify({"data": post})
    else:
        return "Token not found or expired. Please login again.", 403
Beispiel #2
0
def createPost():
    if request.json['key'] == os.getenv('KEY'):
        newPost = Posts(title=request.json['title'],
                        titleURL=request.json['title'].lower().replace(
                            ' ', '-'),
                        body=request.json['body'])
        newPost.save()
        return {"message": "Post sucessfully created"}, 200
    else:
        return {"message": "Post creation unsuccessful"}, 403
Beispiel #3
0
def createpost():
    if session.get("authenticated", '') is True:
        if(request.method == 'POST'):
            # add entry to the database...
            title = request.form.get('title')
            description = request.form.get('description')
            record = Posts(tittle=title, Description=description, date="234")
            db.session.add(record)
            db.session.commit()
        return render_template('createpost.html')
    else:
        return redirect(url_for('Login'))
Beispiel #4
0
def postLike(postId):
    userAuthenticated, user = isAuthenticated(
        flaskRequest.headers['authorization'])

    if userAuthenticated:
        post = Posts.objects(id=postId).first()
        if not post:
            return "No post was found with that ID.", 400

        post['likes'] += 1
        post.save()

        return jsonify({"data": post})
    else:
        return "Token missing or expired. Please login again.", 403
Beispiel #5
0
def changePostActiveStatus():
    userAuthenticated, user = isAuthenticated(
        flaskRequest.headers['authorization'])

    if userAuthenticated:
        request = json.loads(flaskRequest.data)

        post = Posts.objects(id=request["id"]).first()

        if not post:
            return "No post was found with that ID.", 400

        post['active'] = request['active']
        post.save()

        return jsonify({"data": post})
    else:
        return "Token missing or expired. Please login again.", 403
Beispiel #6
0
def getPosts(isActive):
    userAuthenticated, user = isAuthenticated(
        flaskRequest.headers['authorization'])

    if userAuthenticated:
        active = True if isActive == 1 else False

        posts = Posts.objects(active=active)

        for post in posts:
            author = Users.objects(id=post['author']['id']).first()
            authorData = EmbeddedUsers(name=author['name'],
                                       imageUrl=author['imageUrl'])
            post["authorData"] = authorData
            post.save()

        return jsonify({"data": posts})
    else:
        return "Token missing or expired. Please login again.", 403
Beispiel #7
0
def deletePost(postId):
    token = flaskRequest.headers['authorization']
    userAuthenticated, user = isAuthenticated(token)

    if userAuthenticated:
        post = Posts.objects(id=postId).first()

        if not post:
            return "Can't delete an unexisting post", 400

        if post['author']['id'] != user['id']:
            return "Only the author of that post can delete it.", 400

        s3 = S3()
        s3.deleteFile(post['imageKey'])

        post.delete()

        return "Deleted", 204
    else:
        return "Token missing or expired. Please login again.", 403
Beispiel #8
0
def create_comment():
    userAuthenticated, user = isAuthenticated(
        flaskRequest.headers['authorization'])

    if userAuthenticated:
        request = json.loads(flaskRequest.data)

        post = Posts.objects(id=request['postId']).first()

        if not post:
            return 'No post was found with this id', 400

        comment = Comments(content=request['content'],
                           author=user['id'],
                           identifier=f'{uuid4()}',
                           authorName=user['name'],
                           imageUrl=user['imageUrl'])

        post['comments'].append(comment)
        post.save()

        return jsonify({"data": post})
    else:
        return "Token missing or expired. Please login again.", 403