Ticket #14386: 14386.diff
File 14386.diff, 21.6 KB (added by , 14 years ago) |
---|
-
django/contrib/auth/forms.py
1 1 from django.contrib.auth.models import User 2 2 from django.contrib.auth import authenticate 3 3 from django.contrib.auth.tokens import default_token_generator 4 from django.contrib.sites.models import Site4 from django.contrib.sites.models import get_current_site 5 5 from django.template import Context, loader 6 6 from django import forms 7 7 from django.utils.translation import ugettext_lazy as _ … … 117 117 return email 118 118 119 119 def save(self, domain_override=None, email_template_name='registration/password_reset_email.html', 120 use_https=False, token_generator=default_token_generator, from_email=None ):120 use_https=False, token_generator=default_token_generator, from_email=None, request=None): 121 121 """ 122 122 Generates a one-use only link for resetting password and sends to the user 123 123 """ 124 124 from django.core.mail import send_mail 125 125 for user in self.users_cache: 126 126 if not domain_override: 127 current_site = Site.objects.get_current()127 current_site = get_current_site(request) 128 128 site_name = current_site.name 129 129 domain = current_site.domain 130 130 else: -
django/contrib/auth/views.py
10 10 from django.views.decorators.csrf import csrf_protect 11 11 from django.core.urlresolvers import reverse 12 12 from django.shortcuts import render_to_response, get_object_or_404 13 from django.contrib.sites.models import Site, RequestSite 13 from django.contrib.sites.models import Site, RequestSite, get_current_site 14 14 from django.http import HttpResponseRedirect, Http404 15 15 from django.template import RequestContext 16 16 from django.utils.http import urlquote, base36_to_int … … 54 54 55 55 request.session.set_test_cookie() 56 56 57 if Site._meta.installed: 58 current_site = Site.objects.get_current() 59 else: 60 current_site = RequestSite(request) 57 current_site = get_current_site(request) 61 58 62 59 return render_to_response(template_name, { 63 60 'form': form, … … 75 72 if redirect_to: 76 73 return HttpResponseRedirect(redirect_to) 77 74 else: 75 current_site = get_current_site(request) 78 76 return render_to_response(template_name, { 77 'site': current_site, 78 'site_name': current_site.name, 79 79 'title': _('Logged out') 80 80 }, context_instance=RequestContext(request)) 81 81 else: … … 115 115 opts['use_https'] = request.is_secure() 116 116 opts['token_generator'] = token_generator 117 117 opts['from_email'] = from_email 118 opts['email_template_name'] = email_template_name 119 opts['request'] = request 118 120 if is_admin_site: 119 121 opts['domain_override'] = request.META['HTTP_HOST'] 120 else:121 opts['email_template_name'] = email_template_name122 if not Site._meta.installed:123 opts['domain_override'] = RequestSite(request).domain124 122 form.save(**opts) 125 123 return HttpResponseRedirect(post_reset_redirect) 126 124 else: -
django/contrib/contenttypes/tests.py
1 """ 2 Make sure that the content type cache (see ContentTypeManager) works correctly. 3 Lookups for a particular content type -- by model or by ID -- should hit the 4 database only on the first lookup. 1 from django import db 2 from django.conf import settings 3 from django.contrib.contenttypes.models import ContentType 4 from django.contrib.sites.models import Site, get_current_site 5 from django.contrib.contenttypes.views import shortcut 6 from django.core.exceptions import ObjectDoesNotExist 7 from django.http import HttpRequest 8 from django.test import TestCase 5 9 6 First, let's make sure we're dealing with a blank slate (and that DEBUG is on so 7 that queries get logged):: 8 9 >>> from django.conf import settings 10 >>> settings.DEBUG = True 11 12 >>> from django.contrib.contenttypes.models import ContentType 13 >>> ContentType.objects.clear_cache() 14 15 >>> from django import db 16 >>> db.reset_queries() 10 class ContentTypesTests(TestCase): 11 def setUp(self): 12 # First, let's make sure we're dealing with a blank slate (and that 13 # DEBUG is on so that queries get logged) 14 settings.DEBUG = True 15 ContentType.objects.clear_cache() 16 db.reset_queries() 17 17 18 At this point, a lookup for a ContentType should hit the DB:: 19 20 >>> ContentType.objects.get_for_model(ContentType) 21 <ContentType: content type> 18 def test_lookup_cache(self): 19 """Make sure that the content type cache (see ContentTypeManager) 20 works correctly. Lookups for a particular content type -- by model or 21 by ID -- should hit the database only on the first lookup.""" 22 23 #At this point, a lookup for a ContentType should hit the DB 24 ContentType.objects.get_for_model(ContentType) 25 self.assertEqual(1, len(db.connection.queries)) 26 27 #A second hit, though, won't hit the DB, nor will a lookup by ID 28 ct = ContentType.objects.get_for_model(ContentType) 29 self.assertEqual(1, len(db.connection.queries)) 30 ContentType.objects.get_for_id(ct.id) 31 self.assertEqual(1, len(db.connection.queries)) 32 33 #Once we clear the cache, another lookup will again hit the DB 34 ContentType.objects.clear_cache() 35 ContentType.objects.get_for_model(ContentType) 36 len(db.connection.queries) 37 self.assertEqual(2, len(db.connection.queries)) 22 38 23 >>> len(db.connection.queries) 24 1 25 26 A second hit, though, won't hit the DB, nor will a lookup by ID:: 27 28 >>> ct = ContentType.objects.get_for_model(ContentType) 29 >>> len(db.connection.queries) 30 1 31 >>> ContentType.objects.get_for_id(ct.id) 32 <ContentType: content type> 33 >>> len(db.connection.queries) 34 1 35 36 Once we clear the cache, another lookup will again hit the DB:: 37 38 >>> ContentType.objects.clear_cache() 39 >>> ContentType.objects.get_for_model(ContentType) 40 <ContentType: content type> 41 >>> len(db.connection.queries) 42 2 43 44 Don't forget to reset DEBUG! 45 46 >>> settings.DEBUG = False 47 """ 48 No newline at end of file 39 def test_shortcut_view(self): 40 ''' Check that the shortcut view (used for the admin "view on site" 41 functionality) returns a complete URL regardless of whether the sites 42 framework is installed''' 43 44 request = HttpRequest() 45 request.META = { 46 "SERVER_NAME": "Example.com", 47 "SERVER_PORT": "80", 48 } 49 from django.contrib.auth.models import User 50 user_ct = ContentType.objects.get_for_model(User) 51 obj = User.objects.create(username="john") 52 response = shortcut(request, user_ct.id, obj.id) 53 self.assertEqual("http://example.com/users/john/", response._headers.get("location")[1]) 54 Site._meta.installed=False 55 response = shortcut(request, user_ct.id, obj.id) 56 self.assertEqual("http://Example.com/users/john/", response._headers.get("location")[1]) 57 Site._meta.installed=True 58 59 def tearDown(self): 60 settings.DEBUG = False 61 No newline at end of file -
django/contrib/contenttypes/views.py
1 1 from django import http 2 2 from django.contrib.contenttypes.models import ContentType 3 from django.contrib.sites.models import Site 3 from django.contrib.sites.models import Site, get_current_site 4 4 from django.core.exceptions import ObjectDoesNotExist 5 5 6 6 def shortcut(request, content_type_id, object_id): … … 26 26 # Otherwise, we need to introspect the object's relationships for a 27 27 # relation to the Site object 28 28 object_domain = None 29 opts = obj._meta 29 30 if Site._meta.installed: 31 opts = obj._meta 30 32 31 # First, look for an many-to-many relationship to Site. 32 for field in opts.many_to_many: 33 if field.rel.to is Site: 34 try: 35 # Caveat: In the case of multiple related Sites, this just 36 # selects the *first* one, which is arbitrary. 37 object_domain = getattr(obj, field.name).all()[0].domain 38 except IndexError: 39 pass 40 if object_domain is not None: 41 break 42 43 # Next, look for a many-to-one relationship to Site. 44 if object_domain is None: 45 for field in obj._meta.fields: 46 if field.rel and field.rel.to is Site: 33 # First, look for an many-to-many relationship to Site. 34 for field in opts.many_to_many: 35 if field.rel.to is Site: 47 36 try: 48 object_domain = getattr(obj, field.name).domain 49 except Site.DoesNotExist: 37 # Caveat: In the case of multiple related Sites, this just 38 # selects the *first* one, which is arbitrary. 39 object_domain = getattr(obj, field.name).all()[0].domain 40 except IndexError: 50 41 pass 51 42 if object_domain is not None: 52 43 break 53 44 45 # Next, look for a many-to-one relationship to Site. 46 if object_domain is None: 47 for field in obj._meta.fields: 48 if field.rel and field.rel.to is Site: 49 try: 50 object_domain = getattr(obj, field.name).domain 51 except Site.DoesNotExist: 52 pass 53 if object_domain is not None: 54 break 55 54 56 # Fall back to the current site (if possible). 55 57 if object_domain is None: 56 58 try: 57 object_domain = Site.objects.get_current().domain59 object_domain = get_current_site(request).domain 58 60 except Site.DoesNotExist: 59 61 pass 60 62 -
django/contrib/gis/sitemaps/views.py
1 1 from django.http import HttpResponse, Http404 2 2 from django.template import loader 3 from django.contrib.sites.models import Site3 from django.contrib.sites.models import get_current_site 4 4 from django.core import urlresolvers 5 5 from django.core.paginator import EmptyPage, PageNotAnInteger 6 6 from django.contrib.gis.db.models.fields import GeometryField … … 15 15 This view generates a sitemap index that uses the proper view 16 16 for resolving geographic section sitemap URLs. 17 17 """ 18 current_site = Site.objects.get_current()18 current_site = get_current_site(request) 19 19 sites = [] 20 20 protocol = request.is_secure() and 'https' or 'http' 21 21 for section, site in sitemaps.items(): -
django/contrib/sitemaps/__init__.py
1 from django.contrib.sites.models import get_current_site 1 2 from django.core import urlresolvers, paginator 2 3 import urllib 3 4 … … 60 61 paginator = property(_get_paginator) 61 62 62 63 def get_urls(self, page=1): 63 from django.contrib.sites.models import Site 64 current_site = Site.objects.get_current() 64 current_site = get_current_site(self.request) 65 65 urls = [] 66 66 for item in self.paginator.page(page).object_list: 67 67 loc = "http://%s%s" % (current_site.domain, self.__get('location', item)) … … 77 77 78 78 class FlatPageSitemap(Sitemap): 79 79 def items(self): 80 from django.contrib.sites.models import Site 81 current_site = Site.objects.get_current() 82 return current_site.flatpage_set.filter(registration_required=False) 80 from django.contrib.sites.models import get_current_site 81 current_site = get_current_site(self.request) 82 if hasattr(current_site, "flatpage_set"): 83 return current_site.flatpage_set.filter(registration_required=False) 84 else: 85 return () 83 86 84 87 class GenericSitemap(Sitemap): 85 88 priority = None -
django/contrib/sitemaps/tests/basic.py
2 2 from django.conf import settings 3 3 from django.contrib.auth.models import User 4 4 from django.contrib.flatpages.models import FlatPage 5 from django.contrib.sites.models import Site 5 6 from django.test import TestCase 6 7 from django.utils.formats import localize 7 8 from django.utils.translation import activate … … 74 75 self.assertContains(response, '<loc>http://example.com%s</loc>' % public.url) 75 76 # Private flatpage should not be in the sitemap 76 77 self.assertNotContains(response, '<loc>http://example.com%s</loc>' % private.url) 78 79 def test_requestsite_sitemap(self): 80 # Make sure hitting the flatpages sitemap without the sites framework 81 # installed doesn't raise an exception 82 Site._meta.installed = False 83 response = self.client.get('/flatpages/sitemap.xml') 84 # Retrieve the sitemap. 85 response = self.client.get('/simple/sitemap.xml') 86 # Check for all the important bits: 87 self.assertEquals(response.content, """<?xml version="1.0" encoding="UTF-8"?> 88 <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> 89 <url><loc>http://testserver/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url> 90 </urlset> 91 """ % date.today().strftime('%Y-%m-%d')) 92 Site._meta.installed = True 93 77 94 -
django/contrib/sitemaps/views.py
1 1 from django.http import HttpResponse, Http404 2 2 from django.template import loader 3 from django.contrib.sites.models import Site3 from django.contrib.sites.models import get_current_site 4 4 from django.core import urlresolvers 5 5 from django.utils.encoding import smart_str 6 6 from django.core.paginator import EmptyPage, PageNotAnInteger 7 7 8 8 def index(request, sitemaps): 9 current_site = Site.objects.get_current()9 current_site = get_current_site(request) 10 10 sites = [] 11 11 protocol = request.is_secure() and 'https' or 'http' 12 12 for section, site in sitemaps.items(): 13 site.request = request 13 14 if callable(site): 14 15 pages = site().paginator.num_pages 15 16 else: … … 32 33 maps = sitemaps.values() 33 34 page = request.GET.get("p", 1) 34 35 for site in maps: 36 site.request = request 35 37 try: 36 38 if callable(site): 37 39 urls.extend(site().get_urls(page)) -
django/contrib/sites/models.py
75 75 76 76 def delete(self): 77 77 raise NotImplementedError('RequestSite cannot be deleted.') 78 79 def get_current_site(request): 80 """ 81 Checks if contrib.sites is installed and returns either the current 82 ``Site`` object or a ``RequestSite`` object based on the request. 83 """ 84 if Site._meta.installed: 85 current_site = Site.objects.get_current() 86 else: 87 current_site = RequestSite(request) 88 return current_site -
django/contrib/sites/tests.py
1 """ 2 >>> from django.contrib.sites.models import Site 3 >>> from django.conf import settings 4 >>> Site(id=settings.SITE_ID, domain="example.com", name="example.com").save() 1 from django.conf import settings 2 from django.contrib.sites.models import Site, RequestSite, get_current_site 3 from django.core.exceptions import ObjectDoesNotExist 4 from django.http import HttpRequest 5 from django.test import TestCase 5 6 6 # Make sure that get_current() does not return a deleted Site object. 7 >>> s = Site.objects.get_current() 8 >>> isinstance(s, Site) 9 True 10 11 >>> s.delete() 12 >>> Site.objects.get_current() 13 Traceback (most recent call last): 14 ... 15 DoesNotExist: Site matching query does not exist. 16 17 # After updating a Site object (e.g. via the admin), we shouldn't return a 18 # bogus value from the SITE_CACHE. 19 >>> _ = Site.objects.create(id=settings.SITE_ID, domain="example.com", name="example.com") 20 >>> site = Site.objects.get_current() 21 >>> site.name 22 u"example.com" 23 >>> s2 = Site.objects.get(id=settings.SITE_ID) 24 >>> s2.name = "Example site" 25 >>> s2.save() 26 >>> site = Site.objects.get_current() 27 >>> site.name 28 u"Example site" 29 """ 7 class SitesFrameworkTests(TestCase): 8 def setUp(self): 9 Site(id=settings.SITE_ID, domain="example.com", name="example.com").save() 10 11 def test_site_manager(self): 12 # Make sure that get_current() does not return a deleted Site object. 13 s = Site.objects.get_current() 14 self.assert_(isinstance(s, Site)) 15 s.delete() 16 self.assertRaises(ObjectDoesNotExist, Site.objects.get_current) 17 18 def test_site_cache(self): 19 # After updating a Site object (e.g. via the admin), we shouldn't return a 20 # bogus value from the SITE_CACHE. 21 site = Site.objects.get_current() 22 self.assertEqual(u"example.com", site.name) 23 s2 = Site.objects.get(id=settings.SITE_ID) 24 s2.name = "Example site" 25 s2.save() 26 site = Site.objects.get_current() 27 self.assertEqual(u"Example site", site.name) 28 29 def test_get_current(self): 30 # Test that the correct Site object is returned 31 request = HttpRequest() 32 request.META = { 33 "SERVER_NAME": "example.com", 34 "SERVER_PORT": "80", 35 } 36 site = get_current_site(request) 37 self.assert_(isinstance(site, Site)) 38 self.assertEqual(site.id, settings.SITE_ID) 39 40 # Test that an exception is raised if the sites framework is installed 41 # but there is no matching Site 42 site.delete() 43 self.assertRaises(ObjectDoesNotExist, get_current_site, request) 44 45 # A RequestSite is returned if the sites framework is not installed 46 Site._meta.installed = False 47 site = get_current_site(request) 48 self.assert_(isinstance(site, RequestSite)) 49 self.assertEqual(site.name, u"example.com") 50 Site._meta.installed = True 51 -
django/contrib/syndication/views.py
1 1 import datetime 2 2 from django.conf import settings 3 from django.contrib.sites.models import Site, RequestSite3 from django.contrib.sites.models import get_current_site 4 4 from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist 5 5 from django.http import HttpResponse, Http404 6 6 from django.template import loader, Template, TemplateDoesNotExist, RequestContext … … 91 91 Returns a feedgenerator.DefaultFeed object, fully populated, for 92 92 this feed. Raises FeedDoesNotExist for invalid parameters. 93 93 """ 94 if Site._meta.installed: 95 current_site = Site.objects.get_current() 96 else: 97 current_site = RequestSite(request) 94 current_site = get_current_site(request) 98 95 99 96 link = self.__get_dynamic_attr('link', obj) 100 97 link = add_domain(current_site.domain, link) -
docs/ref/contrib/sites.txt
107 107 Hooking into the current site from views 108 108 ---------------------------------------- 109 109 110 On a lower level, you can use the sites framework in your Django views to do110 You can use the sites framework in your Django views to do 111 111 particular things based on the site in which the view is being called. 112 112 For example:: 113 113 … … 148 148 else: 149 149 # Do something else. 150 150 151 .. versionchanged:: 1.3 152 153 For code which relies on getting the current domain but cannot be certain 154 that the sites framework will be installed for any given project, there is a 155 utility function :func:`~django.contrib.sites.models.get_current_site` that 156 takes a request object as an argument and returns either a Site instance (if 157 the sites framework is installed) or a RequestSite instance (if it is not). 158 This allows loose coupling with the sites framework and provides a usable 159 fallback for cases where it is not installed. 160 151 161 Getting the current domain for display 152 162 -------------------------------------- 153 163