Ejemplo n.º 1
0
def permalink(subreddit_name="", sort_type="hot"):
    """
    """
    atom_url = url_for('subreddits.atom_feed',
                       subreddit_name=subreddit_name,
                       sort_type=sort_type,
                       _external=True)
    sort_type = validate_sort_type(sort_type)
    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,
                                                sort_type=sort_type)
    subreddits = get_subreddits()

    return render_template('home.html',
                           atom_url=atom_url,
                           thread_paginator=thread_paginator,
                           subreddits=subreddits,
                           cur_subreddit=subreddit,
                           page_title=subreddit.name,
                           sort_type=sort_type)
Ejemplo n.º 2
0
def inject():
    version = '.'.join(VERSION_NUM.split("-")[0:3])
    return dict(quote=quote,
                version=version,
                csrf_token=generate_csrf_token,
                slugify=slugify,
                subreddits=get_subreddits(),
                user=g.get('user'))
Ejemplo n.º 3
0
def user_profile(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',
                           current_user=user,
                           cur_subreddit=None,
                           page_title=user.username,
                           page_subtitle=user.university,
                           subreddits=get_subreddits())
Ejemplo n.º 4
0
 def set_choices(self):
     choices = [(x.id, x.name) for x in get_subreddits()]
     self.subreddit.choices = [(-1, "Select a subreddit")] + choices
Ejemplo n.º 5
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

    if subreddit_name:
        is_bookmarklet = False
        subreddit = Subreddit.query.filter_by(name=subreddit_name).first()
        form = submit_pub_form(request.form)
        submit_title = subreddit_name
        if not subreddit:
            abort(404)
    else:
        # Bookmarklet
        is_bookmarklet = True
        url = request.args.get('url') or ""
        form = submit_pub_bookmarklet_form(request.form, pub_id=url)
        form.set_choices()

        if form.subreddit.data:
            subreddit = Subreddit.query.filter_by(
                id=form.subreddit.data).first()
            if subreddit:
                subreddit_name = subreddit.name
                submit_title = subreddit_name
        else:
            subreddit = None
            submit_title = "upvote.pub!"

    # Check if pub has already been submitted
    if form.pub_id.data and subreddit_name:
        publication = get_publication(form.pub_id.data)
        if publication:
            for thread in publication.threads:
                if thread.subreddit.name == subreddit_name:
                    flash(
                        f"That pub has already been submitted to {subreddit.name}!",
                        'warning')
                    return redirect(
                        url_for('threads.thread_permalink',
                                thread_id=thread.id,
                                subreddit_name=subreddit.name,
                                title=slugify(publication.pub_title)))

    if form.validate_on_submit():
        # Fetch data from publication
        pub_id = form.pub_id.data
        pub_item = fetch_pub(pub_id)

        if not pub_item:
            flash(
                "Could not find a pub with the ID: '{}'".format(
                    form.pub_id.data), 'danger')
            return render_template('threads/submit_post.html',
                                   page_title=f"Submit to {submit_title}",
                                   form=form,
                                   cur_subreddit=subreddit.name)

        thread = Thread(user_id=user_id,
                        subreddit_id=subreddit.id,
                        publication_id=pub_item.id)

        db.session.add(thread)
        db.session.commit()
        flash("Thank you for submitting!", "success")
        return redirect(
            url_for('threads.thread_permalink',
                    thread_id=thread.id,
                    subreddit_name=thread.subreddit.name,
                    title=slugify(pub_item.pub_title)))
    return render_template('threads/submit_post.html',
                           form=form,
                           page_title=f"Submit to {submit_title}",
                           subreddits=get_subreddits(),
                           is_bookmarklet=is_bookmarklet)