Exemplo n.º 1
0
def get_subreddits():
    """
    important and widely imported method because a list of
    the top 30 subreddits are present on every page in the sidebar
    """

    # TODO:
    # convert to mongodb
    # subreddits = []
    # subreddits = Subreddit.query.filter(Subreddit.id != 1)[:25]
    subreddits = Subreddit.find({"id": {"$ne": 1}})[:25]
    app.logger.debug("in frontends.views: subreddits: {}".format(subreddits))
    return subreddits
Exemplo n.º 2
0
def thread_permalink(subreddit_name=None, thread_id=None, title=None):
    """
    """
    thread_id = thread_id  #or -99
    app.logger.debug("thread_id: {}".format(thread_id))
    thread = Thread.find_one({"id": ObjectId(thread_id)})

    app.logger.debug(Subreddit.find({"name": subreddit_name}))
    subreddit = Subreddit.find_one({"name": subreddit_name})
    subreddits = get_subreddits()
    return render_template('threads/permalink.html',
                           user=g.user,
                           thread=thread,
                           cur_subreddit=subreddit,
                           subreddits=subreddits)
Exemplo n.º 3
0
def view_all():
    """
    """
    return render_template('subreddits/all.html',
                           user=g.user,
                           subreddits=Subreddit.find())
Exemplo n.º 4
0
def submit(subreddit_name=None):
    """
    """
    if g.user is None:
        flash('You must be logged in to submit posts!', 'danger')
        return redirect(url_for('frontends.login', next=request.path))
    user_id = g.user.id

    subreddits = Subreddit.find({"name": subreddit_name})
    subreddit = None
    if subreddits.count() > 0:
        subreddit = subreddits[0]
    if not subreddit:
        flash('Select a subreddit!', 'danger')
        # abort(404)
        return redirect(url_for('frontends.home', next=request.path))

    form = SubmitForm(request.form)
    if form.validate_on_submit():
        title = form.title.data.strip()
        link = form.link.data.strip()
        text = form.text.data.strip()

        thread_data = {
            "title": title,
            "link": link,
            "text": text,
            "user_id": user_id,
            "subreddit_id": subreddit.id,
            "user": g.user,
            "subreddit": subreddit
        }
        thread = Thread(**thread_data)

        if not meets_thread_criteria(thread):
            return render_template('threads/submit.html',
                                   form=form,
                                   user=g.user,
                                   cur_subreddit=subreddit.name)

        thread.update()
        thread.commit()
        app.logger.debug("adding thread: subreddit name: {}".format(
            thread.subreddit))
        app.logger.debug("adding thread: subreddit name: {}".format(
            thread.subreddit.fetch().name))
        # db.threads.add_thread
        # db.session.add(thread)
        # db.session.commit()

        # thread.set_hotness()
        # thread.add_thread()

        flash('thanks for submitting!', 'success')
        return redirect(
            url_for('subreddits.permalink', subreddit_name=subreddit.name))
    return render_template('threads/submit.html',
                           form=form,
                           user=g.user,
                           cur_subreddit=subreddit,
                           subreddits=get_subreddits())