Code

Ticket #2618: site_profiles.diff

File site_profiles.diff, 1.3 KB (added by jay@…, 8 years ago)
Line 
1Index: django/contrib/sites/models.py
2===================================================================
3--- django/contrib/sites/models.py      (revision 3677)
4+++ django/contrib/sites/models.py      (working copy)
5@@ -1,6 +1,10 @@
6 from django.db import models
7 from django.utils.translation import gettext_lazy as _
8+from django.core.exceptions import ImproperlyConfigured
9 
10+class SiteProfileNotAvailable(Exception):
11+    pass
12+
13 class SiteManager(models.Manager):
14     def get_current(self):
15         from django.conf import settings
16@@ -21,3 +25,20 @@
17 
18     def __str__(self):
19         return self.domain
20+   
21+    def get_profile(self):
22+        """
23+        Returns profile for this site. Raises
24+        SiteProfileNotAvailable if this site does not allow profiles.
25+        """
26+        if not hasattr(self, '_profile_cache'):
27+            from django.conf import settings
28+            if not settings.SITE_PROFILE_MODULE:
29+                raise SiteProfileNotAvailable
30+            try:
31+                app_label, model_name = settings.SITE_PROFILE_MODULE.split('.')
32+                model = models.get_model(app_label, model_name)
33+                self._profile_cache = model._default_manager.get(site__id__exact=self.id)
34+            except (ImportError, ImproperlyConfigured):
35+                raise SiteProfileNotAvailable
36+        return self._profile_cache