Show More
@@ -0,0 +1,38 b'' | |||
|
1 | # -*- coding: utf-8 -*- | |
|
2 | # Generated by Django 1.10.5 on 2017-02-24 21:10 | |
|
3 | from __future__ import unicode_literals | |
|
4 | ||
|
5 | import boards.models.base | |
|
6 | from django.db import migrations, models | |
|
7 | import django.db.models.deletion | |
|
8 | ||
|
9 | ||
|
10 | class Migration(migrations.Migration): | |
|
11 | ||
|
12 | dependencies = [ | |
|
13 | ('boards', '0057_tag_aliases'), | |
|
14 | ] | |
|
15 | ||
|
16 | operations = [ | |
|
17 | migrations.CreateModel( | |
|
18 | name='TagAlias', | |
|
19 | fields=[ | |
|
20 | ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | |
|
21 | ('name', models.CharField(db_index=True, max_length=100, unique=True)), | |
|
22 | ('locale', models.CharField(db_index=True, max_length=10)), | |
|
23 | ], | |
|
24 | options={ | |
|
25 | 'ordering': ('name',), | |
|
26 | }, | |
|
27 | bases=(models.Model, boards.models.base.Viewable), | |
|
28 | ), | |
|
29 | migrations.RemoveField( | |
|
30 | model_name='tag', | |
|
31 | name='aliases', | |
|
32 | ), | |
|
33 | migrations.AddField( | |
|
34 | model_name='tagalias', | |
|
35 | name='parent', | |
|
36 | field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='aliases', to='boards.Tag'), | |
|
37 | ), | |
|
38 | ] |
@@ -2,7 +2,8 b' from boards.models.attachment import FIL' | |||
|
2 | 2 | from django.contrib import admin |
|
3 | 3 | from django.utils.translation import ugettext_lazy as _ |
|
4 | 4 | from django.core.urlresolvers import reverse |
|
5 |
from boards.models import Post, Tag, Ban, Thread, Banner, Attachment, |
|
|
5 | from boards.models import Post, Tag, Ban, Thread, Banner, Attachment, \ | |
|
6 | KeyPair, GlobalId, TagAlias | |
|
6 | 7 | |
|
7 | 8 | |
|
8 | 9 | @admin.register(Post) |
@@ -90,6 +91,11 b' class TagAdmin(admin.ModelAdmin):' | |||
|
90 | 91 | search_fields = ('name',) |
|
91 | 92 | |
|
92 | 93 | |
|
94 | @admin.register(TagAlias) | |
|
95 | class TagAliasAdmin(admin.ModelAdmin): | |
|
96 | list_display = ('locale', 'name', 'parent') | |
|
97 | ||
|
98 | ||
|
93 | 99 | @admin.register(Thread) |
|
94 | 100 | class ThreadAdmin(admin.ModelAdmin): |
|
95 | 101 |
@@ -8,6 +8,7 b' from boards.models.signature import Glob' | |||
|
8 | 8 | from boards.models.attachment import Attachment |
|
9 | 9 | from boards.models.thread import Thread |
|
10 | 10 | from boards.models.post import Post |
|
11 | from boards.models.tag import TagAlias | |
|
11 | 12 | from boards.models.tag import Tag |
|
12 | 13 | from boards.models.user import Ban |
|
13 | 14 | from boards.models.banner import Banner |
@@ -19,7 +19,17 b' import boards' | |||
|
19 | 19 | |
|
20 | 20 | RELATED_TAGS_COUNT = 5 |
|
21 | 21 | |
|
22 | REGEX_TAG_ALIAS = r'{}:(\w+),' | |
|
22 | ||
|
23 | class TagAlias(models.Model, Viewable): | |
|
24 | class Meta: | |
|
25 | app_label = 'boards' | |
|
26 | ordering = ('name',) | |
|
27 | ||
|
28 | name = models.CharField(max_length=100, db_index=True, unique=True) | |
|
29 | locale = models.CharField(max_length=10, db_index=True) | |
|
30 | ||
|
31 | parent = models.ForeignKey('Tag', null=True, blank=True, | |
|
32 | related_name='aliases') | |
|
23 | 33 | |
|
24 | 34 | |
|
25 | 35 | class TagManager(models.Manager): |
@@ -39,7 +49,12 b' class TagManager(models.Manager):' | |||
|
39 | 49 | return ', '.join([tag.get_view() for tag in tags]) |
|
40 | 50 | |
|
41 | 51 | def get_by_alias(self, alias): |
|
42 | return self.filter(aliases__contains=":{},".format(alias)).first() | |
|
52 | tag = None | |
|
53 | try: | |
|
54 | tag = TagAlias.objects.get(name=alias).parent | |
|
55 | except TagAlias.DoesNotExist: | |
|
56 | pass | |
|
57 | return tag | |
|
43 | 58 | |
|
44 | 59 | |
|
45 | 60 | class Tag(models.Model, Viewable): |
@@ -60,7 +75,6 b' class Tag(models.Model, Viewable):' | |||
|
60 | 75 | |
|
61 | 76 | parent = models.ForeignKey('Tag', null=True, blank=True, |
|
62 | 77 | related_name='children') |
|
63 | aliases = models.TextField(blank=True) | |
|
64 | 78 | |
|
65 | 79 | def __str__(self): |
|
66 | 80 | return self.name |
@@ -99,11 +113,10 b' class Tag(models.Model, Viewable):' | |||
|
99 | 113 | def get_view(self): |
|
100 | 114 | locale = get_language() |
|
101 | 115 | |
|
102 | localized_tag_name = '' | |
|
103 | if self.aliases: | |
|
104 | match = re.search(REGEX_TAG_ALIAS.format(locale), self.aliases) | |
|
105 | if match: | |
|
106 | localized_tag_name = match.group(1) | |
|
116 | try: | |
|
117 | localized_tag_name = self.aliases.get(locale=locale).name | |
|
118 | except TagAlias.DoesNotExist: | |
|
119 | localized_tag_name = '' | |
|
107 | 120 | |
|
108 | 121 | name = '{} ({})'.format(self.name, localized_tag_name) if localized_tag_name else self.name |
|
109 | 122 | link = '<a class="tag" href="{}">{}</a>'.format( |
@@ -160,3 +173,5 b' class Tag(models.Model, Viewable):' | |||
|
160 | 173 | return Attachment.objects.filter( |
|
161 | 174 | attachment_posts__thread__tags__in=[self]).filter( |
|
162 | 175 | mimetype__in=FILE_TYPES_IMAGE).order_by('-attachment_posts__pub_time') |
|
176 | ||
|
177 |
General Comments 0
You need to be logged in to leave comments.
Login now