예제 #1
0
def _get_page_by_untyped_arg(page_lookup, request, site_id):
    """
    The `page_lookup` argument can be of any of the following types:
    - Integer: interpreted as `pk` of the desired page
    - String: interpreted as `reverse_id` of the desired page
    - `dict`: a dictionary containing keyword arguments to find the desired page
    (for instance: `{'pk': 1}`)
    - `Page`: you can also pass a Page object directly, in which case there will be no database lookup.
    - `None`: the current page will be used
    """
    if page_lookup is None:
        return request.current_page
    if isinstance(page_lookup, Page):
        if request.current_page and request.current_page.pk == page_lookup.pk:
            return request.current_page
        return page_lookup
    if isinstance(page_lookup, six.string_types):
        page_lookup = {'reverse_id': page_lookup}
    elif isinstance(page_lookup, six.integer_types):
        page_lookup = {'pk': page_lookup}
    elif not isinstance(page_lookup, dict):
        raise TypeError('The page_lookup argument can be either a Dictionary, Integer, Page, or String.')
    page_lookup.update({'site': site_id})
    try:
        if 'pk' in page_lookup:
            page = Page.objects.all().get(**page_lookup)
            if request and use_draft(request):
                if page.publisher_is_draft:
                    return page
                else:
                    return page.publisher_draft
            else:
                if page.publisher_is_draft:
                    return page.publisher_public
                else:
                    return page
        else:
            return get_page_queryset(request).get(**page_lookup)
    except Page.DoesNotExist:
        site = Site.objects.get_current()
        subject = _('Page not found on %(domain)s') % {'domain': site.domain}
        body = _("A template tag couldn't find the page with lookup arguments `%(page_lookup)s\n`. "
                 "The URL of the request was: http://%(host)s%(path)s") \
               % {'page_lookup': repr(page_lookup), 'host': site.domain, 'path': request.path_info}
        if settings.DEBUG:
            raise Page.DoesNotExist(body)
        else:
            mw = get_middleware()
            if getattr(settings, 'SEND_BROKEN_LINK_EMAILS', False):
                mail_managers(subject, body, fail_silently=True)
            elif 'django.middleware.common.BrokenLinkEmailsMiddleware' in mw:
                middle = BrokenLinkEmailsMiddleware()
                domain = request.get_host()
                path = request.get_full_path()
                referer = force_text(request.META.get('HTTP_REFERER', ''), errors='replace')
                if not middle.is_ignorable_request(request, path, domain, referer):
                    mail_managers(subject, body, fail_silently=True)
            return None
예제 #2
0
 def test_404_error_reporting_ignored_url(self):
     self.req.path = self.req.path_info = 'foo_url/that/does/not/exist'
     BrokenLinkEmailsMiddleware().process_response(self.req, self.resp)
     self.assertEqual(len(mail.outbox), 0)
예제 #3
0
 def test_404_error_reporting_no_referer(self):
     BrokenLinkEmailsMiddleware().process_response(self.req, self.resp)
     self.assertEqual(len(mail.outbox), 0)
예제 #4
0
 def test_404_error_reporting(self):
     self.req.META['HTTP_REFERER'] = '/another/url/'
     BrokenLinkEmailsMiddleware().process_response(self.req, self.resp)
     self.assertEqual(len(mail.outbox), 1)
     self.assertIn('Broken', mail.outbox[0].subject)
예제 #5
0
파일: tests.py 프로젝트: zhuohq/django
 def test_referer_equal_to_requested_url_without_trailing_slash_when_append_slash_is_unset(
         self):
     self.req.path = self.req.path_info = '/regular_url/that/does/not/exist/'
     self.req.META['HTTP_REFERER'] = self.req.path_info[:-1]
     BrokenLinkEmailsMiddleware().process_response(self.req, self.resp)
     self.assertEqual(len(mail.outbox), 1)
예제 #6
0
파일: tests.py 프로젝트: zhuohq/django
 def test_referer_equal_to_requested_url_on_another_domain(self):
     self.req.META[
         'HTTP_REFERER'] = 'http://anotherserver%s' % self.req.path
     BrokenLinkEmailsMiddleware().process_response(self.req, self.resp)
     self.assertEqual(len(mail.outbox), 1)
예제 #7
0
def _get_page_by_untyped_arg(page_lookup, request, site_id):
    """
    The `page_lookup` argument can be of any of the following types:
    - Integer: interpreted as `pk` of the desired page
    - String: interpreted as `reverse_id` of the desired page
    - `dict`: a dictionary containing keyword arguments to find the desired page
    (for instance: `{'pk': 1}`)
    - `Page`: you can also pass a Page object directly, in which case there will be no database lookup.
    - `None`: the current page will be used
    """
    if page_lookup is None:
        return request.current_page
    if isinstance(page_lookup, Page):
        if request.current_page and request.current_page.pk == page_lookup.pk:
            return request.current_page
        return page_lookup
    if isinstance(page_lookup, six.string_types):
        page_lookup = {'reverse_id': page_lookup}
    elif isinstance(page_lookup, six.integer_types):
        page_lookup = {'pk': page_lookup}
    elif not isinstance(page_lookup, dict):
        raise TypeError(
            'The page_lookup argument can be either a Dictionary, Integer, Page, or String.'
        )
    page_lookup.update({'site': site_id})
    try:
        if 'pk' in page_lookup:
            page = Page.objects.all().get(**page_lookup)
            if request and use_draft(request):
                if page.publisher_is_draft:
                    return page
                else:
                    return page.publisher_draft
            else:
                if page.publisher_is_draft:
                    return page.publisher_public
                else:
                    return page
        else:
            return get_page_queryset(request).get(**page_lookup)
    except Page.DoesNotExist:
        site = Site.objects.get_current()
        subject = _('Page not found on %(domain)s') % {'domain': site.domain}
        body = _("A template tag couldn't find the page with lookup arguments `%(page_lookup)s\n`. "
                 "The URL of the request was: http://%(host)s%(path)s") \
               % {'page_lookup': repr(page_lookup), 'host': site.domain, 'path': request.path_info}
        if settings.DEBUG:
            raise Page.DoesNotExist(body)
        else:
            if getattr(settings, 'SEND_BROKEN_LINK_EMAILS', False):
                mail_managers(subject, body, fail_silently=True)
            elif 'django.middleware.common.BrokenLinkEmailsMiddleware' in settings.MIDDLEWARE_CLASSES:
                middle = BrokenLinkEmailsMiddleware()
                domain = request.get_host()
                path = request.get_full_path()
                referer = force_text(request.META.get('HTTP_REFERER', ''),
                                     errors='replace')
                if not middle.is_ignorable_request(request, path, domain,
                                                   referer):
                    mail_managers(subject, body, fail_silently=True)
            return None
예제 #8
0
 def test_referer_equal_to_requested_url_without_trailing_slash_with_no_append_slash(
     self, ):
     self.req.path = self.req.path_info = "/regular_url/that/does/not/exist/"
     self.req.META["HTTP_REFERER"] = self.req.path_info[:-1]
     BrokenLinkEmailsMiddleware(self.get_response)(self.req)
     self.assertEqual(len(mail.outbox), 1)
예제 #9
0
 def test_referer_equal_to_requested_url_on_another_domain(self):
     self.req.META[
         "HTTP_REFERER"] = "http://anotherserver%s" % self.req.path
     BrokenLinkEmailsMiddleware(self.get_response)(self.req)
     self.assertEqual(len(mail.outbox), 1)
예제 #10
0
 def test_404_error_reporting_ignored_url(self):
     self.req.path = self.req.path_info = "foo_url/that/does/not/exist"
     BrokenLinkEmailsMiddleware(self.get_response)(self.req)
     self.assertEqual(len(mail.outbox), 0)
예제 #11
0
 def test_404_error_reporting(self):
     self.req.META["HTTP_REFERER"] = "/another/url/"
     BrokenLinkEmailsMiddleware(self.get_response)(self.req)
     self.assertEqual(len(mail.outbox), 1)
     self.assertIn("Broken", mail.outbox[0].subject)
예제 #12
0
 def test_404_error_nonascii_referrer(self):
     # Such referer strings should not happen, but anyway, if it happens,
     # let's not crash
     self.req.META['HTTP_REFERER'] = b'http://testserver/c/\xd0\xbb\xd0\xb8/'
     BrokenLinkEmailsMiddleware().process_response(self.req, self.resp)
     self.assertEqual(len(mail.outbox), 1)
예제 #13
0
def _get_email_subject(domain, referer):
	result = 'Broken '
	if BrokenLinkEmailsMiddleware.is_internal_request(None, domain, referer):
		result += 'INTERNAL '
	result += 'link on ' + domain
	return result