示例#1
0
def snippet_detail(request, snippet_id):
    return object_detail(
        request,
        queryset=Snippet.objects.all(),
        object_id=snippet_id,
        extra_context={'flag_form': SnippetFlagForm()},
    )
示例#2
0
def flag_snippet(request, snippet_id, template_name='cab/flag_snippet.html'):
    snippet = get_object_or_404(Snippet, id=snippet_id)
    snippet_flag = SnippetFlag(snippet=snippet, user=request.user)

    if request.method == 'POST':
        form = SnippetFlagForm(request.POST, instance=snippet_flag)

        if form.is_valid():
            snippet_flag = form.save()

            admin_link = request.build_absolute_uri(
                reverse('admin:cab_snippetflag_changelist')
            )

            mail_admins(
                'Snippet flagged: "%s"' % (snippet.title),
                '%s\n\nAdmin link: %s' % (snippet_flag, admin_link),
                fail_silently=True,
            )

            messages.info(request, 'Thank you for helping improve the site!')
            return redirect(snippet)
        else:
            if request.is_ajax():
                return redirect(snippet)
                messages.error(request, 'Invalid form submission')
    else:
        form = SnippetFlagForm(instance=snippet_flag)
    return render(request, template_name, {
        'form': form,
        'snippet': snippet,
    })
示例#3
0
def flag_snippet(request, snippet_id):
    snippet = get_object_or_404(Snippet, id=snippet_id)
    snippet_flag = SnippetFlag(snippet=snippet, user=request.user)
    form = SnippetFlagForm(request.POST, instance=snippet_flag)
    
    if form.is_valid():
        snippet_flag = form.save()
        
        admin_link = request.build_absolute_uri(
            reverse('admin:cab_snippetflag_changelist')
        )
        
        mail_admins(
            'Snippet flagged: "%s"' % (snippet.title),
            '%s\n\nAdmin link: %s' % (snippet_flag, admin_link),
            fail_silently=True,
        )
        
        messages.info(request, 'Thank you for helping improve the site!')
    else:
        messages.error(request, 'Invalid form submission')
    
    return HttpResponseRedirect(snippet.get_absolute_url())