Ejemplo n.º 1
0
def community_create():
    form = CommunityCreateForm()

    form.tags.choices = [
        (t.title, t.title)
        for t in Tag.select(Tag.title).order_by(Tag.title).distinct()
    ]

    if form.validate_on_submit() and current_user.karma >= 50:
        community = Community()

        community.name = slugify(form.name.data)
        community.description = form.description.data
        community.maintainer = User.get(User.id == current_user.id)

        community.update_search_index()

        user = User.get(User.id == current_user.id)
        user.karma -= 50
        user.save()

        success = True

        try:
            community.save()

        except peewee.IntegrityError:
            flash(gettext('This name is already in use.'), 'error')
            success = False

        else:
            try:
                for element in form.tags.data.split(','):
                    if not element:
                        continue

                    tag = Tag()
                    # FIXME: slugify?
                    tag.title = element
                    tag.community = community
                    tag.save()

            except peewee.IntegrityError:
                flash(gettext('Unable to add tags.'), 'error')
                success = False

        if success:
            return redirect(url_for('community', community=community.name))

    return render_template('community_create.html', form=form)