示例#1
0
def fork_snippet(snippet_uuid):
    to_fork = get_snippet_by_uuid(snippet_uuid)
    if to_fork.is_private() and to_fork.user != current_user:
        raise Unauthorized("This snippet is private.")
    new_snippet = Snippet(to_fork.body)
    new_snippet.title = to_fork.title
    new_snippet.language = to_fork.language
    new_snippet.parent_fork = to_fork
    new_snippet.user = current_user
    db.session.add(new_snippet)
    db.session.commit()
    return redirect(
        url_for('snippets.show_snippet', snippet_uuid=new_snippet.get_uuid()))
示例#2
0
def index():
    """Route allows users to create a new snippet"""
    snippet_form = SnippetForm()
    populate_choice_field(snippet_form)
    if snippet_form.validate_on_submit():
        s = Snippet(snippet_form.snippet.data)
        if snippet_form.title.data:
            s.title = snippet_form.title.data
        s.language = Language.query.get(snippet_form.language.data)
        if not current_user.is_anonymous():
            s.user = current_user
            s.private = snippet_form.private.data
        db.session.add(s)
        db.session.commit()
        return redirect(
            url_for('snippets.show_snippet', snippet_uuid=s.get_uuid()))
    if current_user.is_authenticated() and current_user.default_language:
        snippet_form.language.default = current_user.default_language_id
        snippet_form.process()

    return render_template('snippets/compose.html', form=snippet_form)