Esempio n. 1
0
def paginate():
    page = int(request.args['page'])
    tag = request.args['tag']
    status = 'No good'
    message = "We're having trouble loading our resources right now! Please try again later."
    try:
        if tag is not None and tag != "all":
            tag = tag.lower()
            posts = list(reversed(list(Post.getManyByTag(tag))))
        else:
            posts = list(reversed(list(Post.getAll())))

        first_index = (page - 1) * POST_CONSTANTS.MAX_RESOURCES_PER_PAGE
        last_index = page * POST_CONSTANTS.MAX_RESOURCES_PER_PAGE
        posts = posts[first_index:last_index]
        if posts is not None:
            status = "OK"
            message = "Success!!!"
            for post in posts:
                post['_id'] = str(post['_id'])
                post['user_id'] = str(post['user_id'])
        return jsonify({
            "status": status,
            "message": message,
            "posts": posts,
            "page": page
        })
    except Exception as e:
        if len(str(e)) != 0:
            message = str(e)
        return jsonify({"status": status, "message": message})
Esempio n. 2
0
def new():
    if request.method == 'POST':
        title = request.form['title']
        description = request.form['description']
        body = request.form['body'].strip()
        tag = request.form['tag']
        videoURL = request.form['videoURL']
        status = "No Good"
        message = "Error: item not inserted"

        if not title or not description or not tag or (not body
                                                       and not videoURL):
            return {"status": status, "message": message}

        try:
            post_added = Post.new(title, description, body, tag, videoURL)
            if post_added:
                status = "OK"
                message = "Success!!!"
            return jsonify({
                "status": status,
                "message": message,
                "body": body,
                "tag": tag,
                "videoURL": videoURL,
                "timestamp": datetime.datetime.now()
            })
        except PostErrors.PostError as e:
            return e.message
    else:
        tags = list(Post.getAllTags())
        for i in range(len(tags)):
            tags[i] = tags[i]['tag']
        return render_template('posts/new.html', tags=tags)
Esempio n. 3
0
def all(tag):
    if tag is not None:
        posts = list(reversed(list(Post.getManyByTag(tag))))
    else:
        tag = 'all'
        posts = list(reversed(list(Post.getAll())))
    num_pages = math.ceil(len(posts) / POST_CONSTANTS.MAX_RESOURCES_PER_PAGE)
    max_pages_shown = POST_CONSTANTS.MAX_PAGES_SHOWN if num_pages >= POST_CONSTANTS.MAX_PAGES_SHOWN else num_pages
    page = 1
    if 'page' in request.args.keys():
        if int(request.args['page']) <= num_pages:
            page = int(request.args['page'])
        else:
            page = num_pages
    first_index = (page - 1) * POST_CONSTANTS.MAX_RESOURCES_PER_PAGE
    last_index = page * POST_CONSTANTS.MAX_RESOURCES_PER_PAGE
    posts = posts[first_index:last_index]
    tags = list(Post.getAllTags())
    for i in range(len(tags)):
        tags[i] = tags[i]['tag'].capitalize()
    css = [{'prefix': 'css/compiled/', 'name': 'resources'}]
    return render_template('resources.html',
                           posts=posts,
                           tags=tags,
                           css=css,
                           selectedTag=tag,
                           page=page,
                           max_per_page=POST_CONSTANTS.MAX_RESOURCES_PER_PAGE,
                           num_pages=num_pages,
                           max_pages_shown=max_pages_shown)
Esempio n. 4
0
 def new_post(self, title, content, date=datetime.datetime.utcnow()):
     post = Post(blog_id=self._id,
                 title=title,
                 content=content,
                 author=self.author,
                 created_date=date)
     post.save_to_mongo()
Esempio n. 5
0
def create_new_post(blog_id):
    if request.method == 'GET':
        return render_template('new_post.html', blog_id=blog_id)
    else:
        title = request.form['title']
        content = request.form['content']
        user = User.get_by_email(session['email'])

        new_post = Post(blog_id, title, content, user.email)
        new_post.save_to_mongo()

        return make_response(blog_posts(blog_id))
Esempio n. 6
0
def profile(user):
    user_id = user['_id']
    email = session['email']
    posts = reversed(list(Post.getAllByUserId(user_id)))
    # posts = reversed(list(Post.getAll()))
    tags = list(Post.getAllTags())
    for i in range(len(tags)):
        tags[i] = tags[i]['tag']
    return render_template('users/profile.html',
                           email=email,
                           posts=posts,
                           tags=tags)
Esempio n. 7
0
def search():
    input_tag = request.args[
        'input-tag'] if 'input-tag' in request.args else None
    select_tag = request.args[
        'select-tag'] if 'select-tag' in request.args else None
    tag = None
    status = "No Good"
    message = "We're having trouble loading our resources right now! Please try again later."
    try:
        if input_tag is not None:
            tag = input_tag
        elif select_tag is not None:
            tag = select_tag
        if tag is not None and tag != "all":
            tag = tag.lower()
            posts = list(reversed(list(Post.getManyByTag(tag))))
        else:
            posts = list(reversed(list(Post.getAll())))

        num_pages = math.ceil(
            len(posts) / POST_CONSTANTS.MAX_RESOURCES_PER_PAGE)
        max_pages_shown = POST_CONSTANTS.MAX_PAGES_SHOWN if num_pages >= POST_CONSTANTS.MAX_PAGES_SHOWN else num_pages
        posts = posts[:POST_CONSTANTS.MAX_RESOURCES_PER_PAGE]

        if posts is not None:
            status = "OK"
            message = "Success!!!"
            for post in posts:
                post['_id'] = str(post['_id'])
                post['user_id'] = str(post['user_id'])
        return jsonify({
            "status": status,
            "message": message,
            "posts": posts,
            "num_pages": num_pages,
            "max_pages_shown": max_pages_shown
        })
    except Exception:
        return jsonify({"status": status, "message": message})
Esempio n. 8
0
def delete(id):
    status = "No Good"
    message = "Error: could not delete post"
    try:
        post_deleted = Post.delete(id)
        if post_deleted:
            return redirect(url_for('posts.all'))
        return jsonify({
            "status": status,
            "message": message,
            "timestamp": datetime.datetime.now()
        })
    except PostErrors.PostError as e:
        return e.message
Esempio n. 9
0
def deleteTag():
    status = "No Good"
    message = "Error: could not delete post"
    tag = request.form['tag']
    try:
        tag_deleted = Post.deleteTag(tag)
        if tag_deleted:
            status = 'OK'
            message = 'Sucess!!!'
        return jsonify({
            "status": status,
            "message": message,
            "timestamp": datetime.datetime.now()
        })
    except PostErrors.PostError as e:
        return e.message
Esempio n. 10
0
def addTag():
    if request.method == 'POST':
        tag = request.form['tag'].lower()
        status = "No Good"
        message = "Error: tag not inserted"

        if not tag:
            return {"status": status, "message": message}

        try:
            tag_added = Post.addTag(tag)
            if tag_added:
                status = "OK"
                message = "Success!!!"
            return jsonify({
                "status": status,
                "message": message,
                "tag": tag,
                "timestamp": datetime.datetime.now()
            })
        except PostErrors.PostError as e:
            return e.message
    else:
        return render_template('posts/add-tag.html')
Esempio n. 11
0
def view(id):
    post = Post.getOneById(id)
    if post:
        css = [{'prefix': 'css/compiled/', 'name': 'resource'}]
        return render_template('resource.html', post=post, css=css)
Esempio n. 12
0
def tagEditor():
    tags = list(Post.getAllTags())
    for i in range(len(tags)):
        tags[i] = tags[i]['tag']
    css = [{'prefix': 'css/compiled/', 'name': 'tag-editor'}]
    return render_template('posts/tag-editor.html', tags=tags, css=css)
Esempio n. 13
0
 def get_posts(self):
     return Post.from_blog(self._id)