models.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. from __future__ import unicode_literals
  2. import string
  3. import warnings
  4. from django.core.exceptions import ImproperlyConfigured, ValidationError
  5. from django.db import models
  6. from django.db.models.signals import pre_save, pre_delete
  7. from django.utils.deprecation import RemovedInDjango19Warning
  8. from django.utils.encoding import python_2_unicode_compatible
  9. from django.utils.translation import ugettext_lazy as _
  10. from .requests import RequestSite as RealRequestSite
  11. from .shortcuts import get_current_site as real_get_current_site
  12. SITE_CACHE = {}
  13. def _simple_domain_name_validator(value):
  14. """
  15. Validates that the given value contains no whitespaces to prevent common
  16. typos.
  17. """
  18. if not value:
  19. return
  20. checks = ((s in value) for s in string.whitespace)
  21. if any(checks):
  22. raise ValidationError(
  23. _("The domain name cannot contain any spaces or tabs."),
  24. code='invalid',
  25. )
  26. class SiteManager(models.Manager):
  27. def get_current(self):
  28. """
  29. Returns the current ``Site`` based on the SITE_ID in the
  30. project's settings. The ``Site`` object is cached the first
  31. time it's retrieved from the database.
  32. """
  33. from django.conf import settings
  34. try:
  35. sid = settings.SITE_ID
  36. except AttributeError:
  37. raise ImproperlyConfigured(
  38. "You're using the Django \"sites framework\" without having "
  39. "set the SITE_ID setting. Create a site in your database and "
  40. "set the SITE_ID setting to fix this error.")
  41. try:
  42. current_site = SITE_CACHE[sid]
  43. except KeyError:
  44. current_site = self.get(pk=sid)
  45. SITE_CACHE[sid] = current_site
  46. return current_site
  47. def clear_cache(self):
  48. """Clears the ``Site`` object cache."""
  49. global SITE_CACHE
  50. SITE_CACHE = {}
  51. @python_2_unicode_compatible
  52. class Site(models.Model):
  53. domain = models.CharField(_('domain name'), max_length=100,
  54. validators=[_simple_domain_name_validator])
  55. name = models.CharField(_('display name'), max_length=50)
  56. objects = SiteManager()
  57. class Meta:
  58. db_table = 'django_site'
  59. verbose_name = _('site')
  60. verbose_name_plural = _('sites')
  61. ordering = ('domain',)
  62. def __str__(self):
  63. return self.domain
  64. class RequestSite(RealRequestSite):
  65. def __init__(self, *args, **kwargs):
  66. warnings.warn(
  67. "Please import RequestSite from django.contrib.sites.requests.",
  68. RemovedInDjango19Warning, stacklevel=2)
  69. super(RequestSite, self).__init__(*args, **kwargs)
  70. def get_current_site(request):
  71. warnings.warn(
  72. "Please import get_current_site from django.contrib.sites.shortcuts.",
  73. RemovedInDjango19Warning, stacklevel=2)
  74. return real_get_current_site(request)
  75. def clear_site_cache(sender, **kwargs):
  76. """
  77. Clears the cache (if primed) each time a site is saved or deleted
  78. """
  79. instance = kwargs['instance']
  80. try:
  81. del SITE_CACHE[instance.pk]
  82. except KeyError:
  83. pass
  84. pre_save.connect(clear_site_cache, sender=Site)
  85. pre_delete.connect(clear_site_cache, sender=Site)