Пример #1
0
def add_comment(request, object_id):
    '''Add a comment to a list and return the new comment's html.  Called asynchronously.'''
    form = CommentForm(request.POST)
    # our ajax validation should have ensured that the form was valid
    assert form.is_valid()
    comment = form.save(commit=False)
    # save miscelleneous request data for the curious adminstrator's sake
    comment.record_request(request)
    comment.save()
    result = JsonResponse({
        'id': comment.id,
        'html': comment.html(request),
    })
    # POLISH
    # Calculate the "expires" argument programmatically, or we're screwed
    # when 2068 rolls around.
    result.set_cookie('nickname', value=comment.nickname, max_age=157680000,
        expires='Mon, 31-Dec-68 10:00:00 GMT', path='/')
    # We're no longer requiring the user to provide an email address
#    result.set_cookie('email', value=comment.email, max_age=157680000,
#        expires='Mon, 31-Dec-68 10:00:00 GMT', path='/')
    return result
Пример #2
0
def scan(request, server_id, scan_id):
    server = Server.objects.get_by_user_and_server(request.user, server_id)
    if not server:
        return redirect('dashboard:servers')
    scan_task = ScanTask.objects.get_by_id(scan_id)
    if not scan_task or scan_task.user != request.user:
        return redirect('servers:scans', server_id=server.pk)
    ctx = {
        'scan': scan_task,
        'server': server,
    }
    if request.is_ajax():
        return JsonResponse({'stdout_colored': scan_task.stdout_colored})
    else:
        return render(request, 'servers/scan.html', ctx)
    def post(self, request, *args, **kwargs):
        entry_id = kwargs["entry_id"]
        entry = get_object_or_404(Entry, id=entry_id, published=True)
        if self.is_author:
            form = BlogAuthorCommentForm(request.POST,
                                        instance=Comment(entry=entry,
                                                         by_blog_author=True))
        else:
            form = CommentForm(request.POST, instance=Comment(entry=entry))
        cookies = {}

        if form.is_valid():
            comment = form.save()
            comment.check_for_spam(request)
            parent = comment.parent
            context = self.get_context_data(**kwargs)
            context["comment"] = comment
            response = dict(status="success", message=unicode(form.success_message),
                            comment=render_to_string("blog/include/comment.html",
                                                     Context(context)))

            if not self.is_author:
                cookies[AUTHOR_NAME_COOKIE] = form.cleaned_data['author_name']
                cookies[AUTHOR_EMAIL_COOKIE] = form.cleaned_data['author_email']
                cookies[AUTHOR_URL_COOKIE] = form.cleaned_data['author_url']
                cookies[NOTIFY_COOKIE] = form.cleaned_data['notify'] and u"1" or u""

                # Send notification to author
                if not comment.is_spam:
                    link = "http://%s%s#comment%i" % (Site.objects.get_current().domain,
                                                      entry.get_absolute_url(),
                                                      comment.id)
                    message_text = render_to_string("blog/email/author-notification.html",
                                                    Context(dict(comment=comment,
                                                                 link=link,
                                                                 entry=entry,
                                                                 blog=self.blog)))
                    message = EmailMessage(subject=_(u'New comment to \xab%(title)s\xbb') % dict(title=entry.title),
                                           body=message_text,
                                           to=[self.blog.author.email],
                                           from_email=settings.DEFAULT_FROM_EMAIL)
                    message.content_subtype = "html"
                    message.send(fail_silently=True)

            if not comment.is_spam and parent and parent.notify and parent.author_email and parent.author_email != comment.author_email:
                link = "http://%s%s#comment%i" % (Site.objects.get_current().domain,
                                                  entry.get_absolute_url(),
                                                  comment.id)
                unsubscribe_link = "http://%s%s" % (Site.objects.get_current().domain,
                                                  reverse("blog:unsubscribe_from_notifications",
                                                          kwargs=dict(comment_id=parent.id,
                                                                      secret=parent.secret)))
                message_text = render_to_string("blog/email/reply-notification.html",
                                                Context(dict(parent=parent,
                                                             comment=comment,
                                                             link=link,
                                                             unsubscribe_link=unsubscribe_link,
                                                             entry=entry,
                                                             blog=self.blog)))
                message = EmailMessage(subject=_(u'New reply to your comment for \xab%(title)s\xbb') % dict(title=entry.title),
                                       body=message_text,
                                       to=[parent.author_email],
                                       from_email=settings.DEFAULT_FROM_EMAIL)
                message.content_subtype = "html"
                message.send(fail_silently=True)


        else:
            errors = {}
            #noinspection PyUnresolvedReferences
            for field_name, errors_list in form.errors.items():
                errors[field_name] = errors_list[0]
            response = dict(status="error", errors=errors,
                            message=unicode(form.error_message))

        response = JsonResponse(response)
        if cookies:
            for k, v in cookies.items():
                response.set_cookie(k, v.encode('utf-8'), COOKIE_MAX_AGE)

        return response