You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

autoslugfield.py 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. """
  2. AutoSlugField taken from django-extensions at
  3. 15d3eb305957cee4768dd86e44df1bdad341a10e
  4. Uses Oscar's slugify function instead of Django's
  5. Copyright (c) 2007 Michael Trier
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. """
  22. import re
  23. from django.utils import six
  24. from django.db.models import SlugField
  25. try:
  26. from django.utils.encoding import force_unicode # NOQA
  27. except ImportError:
  28. from django.utils.encoding import force_text as force_unicode # NOQA
  29. from oscar.core.utils import slugify
  30. class AutoSlugField(SlugField):
  31. """ AutoSlugField
  32. By default, sets editable=False, blank=True.
  33. Required arguments:
  34. populate_from
  35. Specifies which field or list of fields the slug is populated from.
  36. Optional arguments:
  37. separator
  38. Defines the used separator (default: '-')
  39. overwrite
  40. If set to True, overwrites the slug on every save (default: False)
  41. Inspired by SmileyChris' Unique Slugify snippet:
  42. http://www.djangosnippets.org/snippets/690/
  43. """
  44. def __init__(self, *args, **kwargs):
  45. kwargs.setdefault('blank', True)
  46. kwargs.setdefault('editable', False)
  47. populate_from = kwargs.pop('populate_from', None)
  48. if populate_from is None:
  49. raise ValueError("missing 'populate_from' argument")
  50. else:
  51. self._populate_from = populate_from
  52. self.separator = kwargs.pop('separator', six.u('-'))
  53. self.overwrite = kwargs.pop('overwrite', False)
  54. self.uppercase = kwargs.pop('uppercase', False)
  55. self.allow_duplicates = kwargs.pop('allow_duplicates', False)
  56. super(AutoSlugField, self).__init__(*args, **kwargs)
  57. def _slug_strip(self, value):
  58. """
  59. Cleans up a slug by removing slug separator characters that occur at
  60. the beginning or end of a slug.
  61. If an alternate separator is used, it will also replace any instances
  62. of the default '-' separator with the new separator.
  63. """
  64. re_sep = '(?:-|%s)' % re.escape(self.separator)
  65. value = re.sub('%s+' % re_sep, self.separator, value)
  66. return re.sub(r'^%s+|%s+$' % (re_sep, re_sep), '', value)
  67. def get_queryset(self, model_cls, slug_field):
  68. for field, model in model_cls._meta.get_fields_with_model():
  69. if model and field == slug_field:
  70. return model._default_manager.all()
  71. return model_cls._default_manager.all()
  72. def slugify_func(self, content):
  73. if content:
  74. return slugify(content)
  75. return ''
  76. def create_slug(self, model_instance, add): # NOQA (too complex)
  77. # get fields to populate from and slug field to set
  78. if not isinstance(self._populate_from, (list, tuple)):
  79. self._populate_from = (self._populate_from, )
  80. slug_field = model_instance._meta.get_field(self.attname)
  81. # only set slug if empty and first-time save, or when overwrite=True
  82. if add and not getattr(model_instance, self.attname) or self.overwrite:
  83. # slugify the original field content and set next step to 2
  84. slug_for_field = lambda field: self.slugify_func(getattr(model_instance, field)) # NOQA
  85. slug = self.separator.join(map(slug_for_field, self._populate_from)) # NOQA
  86. next = 2
  87. else:
  88. # get slug from the current model instance
  89. slug = getattr(model_instance, self.attname)
  90. # model_instance is being modified, and overwrite is False,
  91. # so instead of doing anything, just return the current slug
  92. return slug
  93. # strip slug depending on max_length attribute of the slug field
  94. # and clean-up
  95. slug_len = slug_field.max_length
  96. if slug_len:
  97. slug = slug[:slug_len]
  98. slug = self._slug_strip(slug)
  99. if self.uppercase:
  100. slug = slug.upper()
  101. original_slug = slug
  102. if self.allow_duplicates:
  103. return slug
  104. # exclude the current model instance from the queryset used in finding
  105. # the next valid slug
  106. queryset = self.get_queryset(model_instance.__class__, slug_field)
  107. if model_instance.pk:
  108. queryset = queryset.exclude(pk=model_instance.pk)
  109. # form a kwarg dict used to impliment any unique_together contraints
  110. kwargs = {}
  111. for params in model_instance._meta.unique_together:
  112. if self.attname in params:
  113. for param in params:
  114. kwargs[param] = getattr(model_instance, param, None)
  115. kwargs[self.attname] = slug
  116. # increases the number while searching for the next valid slug
  117. # depending on the given slug, clean-up
  118. while not slug or queryset.filter(**kwargs):
  119. slug = original_slug
  120. end = '%s%s' % (self.separator, next)
  121. end_len = len(end)
  122. if slug_len and len(slug) + end_len > slug_len:
  123. slug = slug[:slug_len - end_len]
  124. slug = self._slug_strip(slug)
  125. slug = '%s%s' % (slug, end)
  126. kwargs[self.attname] = slug
  127. next += 1
  128. return slug
  129. def pre_save(self, model_instance, add):
  130. value = force_unicode(self.create_slug(model_instance, add))
  131. setattr(model_instance, self.attname, value)
  132. return value
  133. def get_internal_type(self):
  134. return "SlugField"
  135. def south_field_triple(self):
  136. "Returns a suitable description of this field for South."
  137. # We'll just introspect the _actual_ field.
  138. from south.modelsinspector import introspector
  139. field_class = '%s.AutoSlugField' % self.__module__
  140. args, kwargs = introspector(self)
  141. kwargs.update({
  142. 'populate_from': repr(self._populate_from),
  143. 'separator': repr(self.separator),
  144. 'overwrite': repr(self.overwrite),
  145. 'allow_duplicates': repr(self.allow_duplicates),
  146. })
  147. # That's our definition!
  148. return (field_class, args, kwargs)
  149. def deconstruct(self):
  150. name, path, args, kwargs = super(AutoSlugField, self).deconstruct()
  151. kwargs['populate_from'] = self._populate_from
  152. if not self.separator == six.u('-'):
  153. kwargs['separator'] = self.separator
  154. if self.overwrite is not False:
  155. kwargs['overwrite'] = True
  156. if self.allow_duplicates is not False:
  157. kwargs['allow_duplicates'] = True
  158. return name, path, args, kwargs