Ejemplo n.º 1
0
def post_submit(user=None, community=None):
    entry = Proposal(community='', title='', content='')

    if community is None:
        community = request.form.get('community') or None
        if community and community[0:2] == "c/":
            community = community[2:]

    if request.method == 'POST':
        try:
            entry.title = request.form.get('title') or ''
            entry.content = request.form.get('content') or ''
            entry.published = request.form.get('published') or True
            entry.vote_options = request.form.get('internalvote') or None
            entry.usepositions = (True if request.form.get('use-positions')
                                  == '' else False)
            entry.author = User.get(User.id == current_user.id)
            entry.community = Community.get(Community.name == community)

            if not (entry.title and entry.community and entry.author):
                flash('Community and Title are required.', 'error')

            else:
                entry.update_search_index()

                # Wrap the call to save in a transaction so we can roll it back
                # cleanly in the event of an integrity error.
                try:
                    with database.atomic():
                        entry.save()

                except peewee.IntegrityError:
                    flash('This title is already in use.', 'error')
                else:

                    if entry.published:
                        return redirect(
                            url_for('post_details',
                                    community=entry.community.name,
                                    slug=entry.slug))
                    else:
                        abort(404)

        except peewee.DoesNotExist:
            flash('Community and Title are required.', 'error')

    if community is not None:
        community = Community.get_or_none(Community.name == community)

    return render_template('submit.html', entry=entry, community=community)
Ejemplo n.º 2
0
def community_subscribe(community):
    fu = CommunityUser(community=Community.get(Community.name == community),
                       user=User.get(User.id == current_user.id))

    try:
        with database.atomic():
            fu.save()
    except peewee.IntegrityError as e:
        print(e)

    else:
        return redirect(url_for("community", community=community))

    # flash error message

    return redirect(url_for("community", community=community))