|
1 | 1 | import uuid
|
| 2 | +import regex |
2 | 3 | from taggit.models import TagBase, GenericTaggedItemBase, TaggedItemBase
|
3 | 4 | from django.db import models
|
4 | 5 | from django.utils.text import slugify
|
|
8 | 9 |
|
9 | 10 | class CustomTag(TagBase):
|
10 | 11 | guid = models.UUIDField(default=uuid.uuid1, editable=False)
|
11 |
| - slug = models.SlugField( |
12 |
| - verbose_name=_("Slug"), |
13 |
| - unique=True, |
14 |
| - max_length=100, |
15 |
| - allow_unicode=True |
16 |
| - ) |
| 12 | + slug = models.SlugField(verbose_name=_("Slug"), max_length=100, allow_unicode=True) |
17 | 13 | name = models.CharField(verbose_name=_("Name"), unique=True, max_length=100)
|
18 | 14 |
|
19 | 15 | class Meta:
|
20 | 16 | db_table = "tagging_custom_tag"
|
21 | 17 | verbose_name = _("Tag")
|
22 | 18 | verbose_name_plural = _("Tags")
|
23 | 19 | app_label = 'tagging'
|
24 |
| - |
25 |
| - def save(self, *args, **kwargs): |
26 |
| - self.slug = self.slugify(self.name) |
27 |
| - self.full_clean() |
28 |
| - return super().save(*args, **kwargs) |
| 20 | + unique_together = ('name', 'slug',) |
29 | 21 |
|
30 | 22 | def slugify(self, tag, i=None):
|
31 |
| - return slugify(tag, allow_unicode=True) |
| 23 | + abugidas = regex.compile( |
| 24 | + u"[\p{Bengali}" |
| 25 | + u"\p{Buhid}" |
| 26 | + u"\p{Devanagari}" |
| 27 | + u"\p{Gujarati}" |
| 28 | + u"\p{Gurmukhi}" |
| 29 | + u"\p{Hanunoo}" |
| 30 | + u"\p{Kannada}" |
| 31 | + u"\p{Khmer}" |
| 32 | + u"\p{Lao}" |
| 33 | + u"\p{Limbu}" |
| 34 | + u"\p{Malayalam}" |
| 35 | + u"\p{Mongolian}" |
| 36 | + u"\p{Myanmar}" |
| 37 | + u"\p{Phags-pa}" |
| 38 | + u"\p{Sinhala}" |
| 39 | + u"\p{Tagalog}" |
| 40 | + u"\p{Tamil}" |
| 41 | + u"\p{Telugu}" |
| 42 | + u"\p{Thaana}" |
| 43 | + u"\p{Thai}" |
| 44 | + u"\p{Tibetan}" |
| 45 | + u"\p{Yi}]+", regex.VERBOSE) |
| 46 | + |
| 47 | + whitespace = regex.compile(u"\p{Z}+") |
| 48 | + |
| 49 | + if regex.search(abugidas, tag): |
| 50 | + return regex.sub(whitespace, '-', tag.strip().lower()) |
| 51 | + else: |
| 52 | + return slugify(tag, allow_unicode=True) |
| 53 | + |
32 | 54 |
|
33 | 55 | class TaggedItems(GenericTaggedItemBase, TaggedItemBase):
|
34 | 56 | tag = models.ForeignKey(
|
35 | 57 | CustomTag,
|
36 | 58 | on_delete=models.CASCADE,
|
37 | 59 | related_name="%(app_label)s_%(class)s_items",
|
38 | 60 | )
|
39 |
| - |
40 |
| - class Meta: |
41 |
| - unique_together = ("content_type", "object_id", "tag") |
|
0 commit comments