Exemplo n.º 1
0
def submit():
    """
    """
    if g.user is None:
        flash('You must be logged in to submit subreddits!', 'danger')
        return redirect(url_for('frontends.login', next=request.path))

    form = SubmitForm(request.form)
    user_id = g.user.id

    if form.validate_on_submit():
        name = form.name.data.strip()
        desc = form.desc.data.strip()

        subreddit = Subreddit.query.filter_by(name=name).first()
        if subreddit:
            flash('subreddit already exists!', 'danger')
            return render_template('subreddits/submit.html', form=form, user=g.user,
                subreddits=get_subreddits())
        new_subreddit = Subreddit(name=name, desc=desc, admin_id=user_id)

        if not meets_subreddit_criterea(subreddit):
            return render_template('subreddits/submit.html', form=form, user=g.user,
                subreddits=get_subreddits())

        db.session.add(new_subreddit)
        db.session.commit()

        flash('Thanks for starting a community! Begin adding posts to your community\
                by clicking the red button to the right.', 'success')
        return redirect(url_for('subreddits.permalink', subreddit_name=new_subreddit.name))
    return render_template('subreddits/submit.html', form=form, user=g.user,
            subreddits=get_subreddits())
Exemplo n.º 2
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

    subreddit = Subreddit.query.filter_by(name=subreddit_name).first()
    if not subreddit:
        abort(404)

    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 = Thread(title=title, link=link, text=text,
                user_id=user_id, subreddit_id=subreddit.id)

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

        db.session.add(thread)
        db.session.commit()

        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())
Exemplo n.º 3
0
def home_page(username=None):
    if not username:
        abort(404)
    user = User.query.filter_by(username=username).first()
    if not user:
        abort(404)
    return render_template('users/profile.html', user=g.user, current_user=user,
            subreddits = get_subreddits())
Exemplo n.º 4
0
def thread_permalink(subreddit_name=None, thread_id=None, title=None):
    """
    """
    thread_id = thread_id or -99
    thread = Thread.query.get_or_404(int(thread_id))
    subreddit = Subreddit.query.filter_by(name=subreddit_name).first()
    subreddits = get_subreddits()
    return render_template('threads/permalink.html', user=g.user, thread=thread,
            cur_subreddit=subreddit, subreddits=subreddits)
Exemplo n.º 5
0
def home(trending=False):
    """
    If not trending we order by creation date
    """
    trending = True if request.args.get('trending') else False
    subreddits = get_subreddits()
    thread_paginator = process_thread_paginator(trending)

    return render_template('home.html', user=g.user,
            subreddits=subreddits, cur_subreddit=home_subreddit(),
            thread_paginator=thread_paginator)
Exemplo n.º 6
0
def thread_permalink(subreddit_name=None, thread_id=None, title=None):
    """
    """
    thread_id = thread_id or -99
    thread = Thread.query.get_or_404(int(thread_id))
    subreddit = Subreddit.query.filter_by(name=subreddit_name).first()
    subreddits = get_subreddits()
    return render_template('threads/permalink.html',
                           user=g.user,
                           thread=thread,
                           cur_subreddit=subreddit,
                           subreddits=subreddits)
Exemplo n.º 7
0
def permalink(subreddit_name=""):
    """
    """
    subreddit = Subreddit.query.filter_by(name=subreddit_name).first()
    if not subreddit:
        abort(404)

    trending = True if request.args.get('trending') else False
    thread_paginator = process_thread_paginator(trending=trending, subreddit=subreddit)
    subreddits = get_subreddits()

    return render_template('home.html', user=g.user, thread_paginator=thread_paginator,
        subreddits=subreddits, cur_subreddit=subreddit)
Exemplo n.º 8
0
def home(trending=False):
    """
    If not trending we order by creation date
    """
    trending = True if request.args.get('trending') else False
    subreddits = get_subreddits()
    thread_paginator = process_thread_paginator(trending)

    return render_template('home.html',
                           user=g.user,
                           subreddits=subreddits,
                           cur_subreddit=home_subreddit(),
                           thread_paginator=thread_paginator)
Exemplo n.º 9
0
def search():
    """
    Allows users to search threads and comments
    """
    query = request.args.get('query')
    rs = search_module.search(query, orderby='creation', search_title=True,
            search_text=True, limit=100)

    thread_paginator = process_thread_paginator(rs=rs)
    rs = rs.all()
    num_searches = len(rs)
    subreddits = get_subreddits()

    return render_template('home.html', user=g.user,
            subreddits=subreddits, cur_subreddit=home_subreddit(),
            thread_paginator=thread_paginator, num_searches=num_searches)
Exemplo n.º 10
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

    subreddit = Subreddit.query.filter_by(name=subreddit_name).first()
    if not subreddit:
        abort(404)

    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 = Thread(title=title,
                        link=link,
                        text=text,
                        user_id=user_id,
                        subreddit_id=subreddit.id)

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

        db.session.add(thread)
        db.session.commit()
        thread.set_hotness()

        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())
Exemplo n.º 11
0
def search():
    """
    Allows users to search threads and comments
    """
    query = request.args.get('query')
    rs = search_module.search(query,
                              orderby='creation',
                              search_title=True,
                              search_text=True,
                              limit=100)

    thread_paginator = process_thread_paginator(rs=rs)
    rs = rs.all()
    num_searches = len(rs)
    subreddits = get_subreddits()

    return render_template('home.html',
                           user=g.user,
                           subreddits=subreddits,
                           cur_subreddit=home_subreddit(),
                           thread_paginator=thread_paginator,
                           num_searches=num_searches)