Exemplo n.º 1
0
def copy_snippet_from_form(form, snippet=None):
    if snippet is None:
        snippet = CodeSnippet()

    for field_name, clean_val in form.cleaned_data.items():
        setattr(snippet, field_name, clean_val)

    current_user = User.get_current()
    if not snippet.is_saved():
        snippet.creator = current_user
    snippet.modifier = current_user

    return snippet
Exemplo n.º 2
0
def get_snippet_or_404_on_error(snippet_id):
    "Raise a 404 if the supplied key is invalid"
    try:
        snippet_id = int(snippet_id)
    except (TypeError, ValueError):
        raise Http404()

    return CodeSnippet.get_by_id(snippet_id)
Exemplo n.º 3
0
def new_snippet(request):
    "Creates a new code snippet"
    template_name = "snippets/edit.html"

    if request.method == "POST":
        form = CodeSnippetForm(request.POST)
        if form.is_valid():
            snippet = CodeSnippet()
            snippet = copy_snippet_from_form(form, snippet)
            snippet.put()
            index_snippet_with_search(snippet)

            return HttpResponseRedirect(reverse("snippets:snippet-detail", kwargs={"snippet_id": snippet.key().id()}))
    else:
        form = CodeSnippetForm()

    ctx = {}
    ctx["form"] = form
    ctx["form_action"] = reverse("snippets:new-snippet")

    return render_to_response(template_name, context_instance=RequestContext(request, ctx))
Exemplo n.º 4
0
def _reindex(request):
    "Debug view for reindexing all code snippets with search"
    # TODO: remove
    i = Index(name="snippets")

    docs = []
    for s in CodeSnippet.all():
        docs.append(
            CodeSnippetDocument(
                doc_id=s.key().id(),
                title=s.title,
                code=s.code,
                language_id=s.language,
                language_readable=s.get_language(),
                creator_email=s.creator.email,
                created=s.created.date(),
                modified=datetime.now().date(),
            )
        )
    i.add(docs)

    return HttpResponse("Done")
Exemplo n.º 5
0
    def get_context_data(self, **kwargs):
        ctx = super(Home, self).get_context_data(**kwargs)
        ctx["latest_snippets"] = CodeSnippet.all().order("modified").fetch(5)

        return ctx