# HG changeset patch # User neko259 # Date 2017-02-25 17:52:23 # Node ID b58ef72661932662dc4fce9152a5180d22daf24f # Parent 16ef0a81d343bd28cca8e55c52375d1751ae4b96 Fixed thread creation diff --git a/boards/admin.py b/boards/admin.py --- a/boards/admin.py +++ b/boards/admin.py @@ -2,7 +2,8 @@ from boards.models.attachment import FIL from django.contrib import admin from django.utils.translation import ugettext_lazy as _ from django.core.urlresolvers import reverse -from boards.models import Post, Tag, Ban, Thread, Banner, Attachment, KeyPair, GlobalId +from boards.models import Post, Tag, Ban, Thread, Banner, Attachment, \ + KeyPair, GlobalId, TagAlias @admin.register(Post) @@ -90,6 +91,11 @@ class TagAdmin(admin.ModelAdmin): search_fields = ('name',) +@admin.register(TagAlias) +class TagAliasAdmin(admin.ModelAdmin): + list_display = ('locale', 'name', 'parent') + + @admin.register(Thread) class ThreadAdmin(admin.ModelAdmin): diff --git a/boards/migrations/0058_auto_20170224_2310.py b/boards/migrations/0058_auto_20170224_2310.py new file mode 100644 --- /dev/null +++ b/boards/migrations/0058_auto_20170224_2310.py @@ -0,0 +1,38 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.5 on 2017-02-24 21:10 +from __future__ import unicode_literals + +import boards.models.base +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('boards', '0057_tag_aliases'), + ] + + operations = [ + migrations.CreateModel( + name='TagAlias', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(db_index=True, max_length=100, unique=True)), + ('locale', models.CharField(db_index=True, max_length=10)), + ], + options={ + 'ordering': ('name',), + }, + bases=(models.Model, boards.models.base.Viewable), + ), + migrations.RemoveField( + model_name='tag', + name='aliases', + ), + migrations.AddField( + model_name='tagalias', + name='parent', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='aliases', to='boards.Tag'), + ), + ] diff --git a/boards/models/__init__.py b/boards/models/__init__.py --- a/boards/models/__init__.py +++ b/boards/models/__init__.py @@ -8,6 +8,7 @@ from boards.models.signature import Glob from boards.models.attachment import Attachment from boards.models.thread import Thread from boards.models.post import Post +from boards.models.tag import TagAlias from boards.models.tag import Tag from boards.models.user import Ban from boards.models.banner import Banner diff --git a/boards/models/tag.py b/boards/models/tag.py --- a/boards/models/tag.py +++ b/boards/models/tag.py @@ -19,7 +19,17 @@ import boards RELATED_TAGS_COUNT = 5 -REGEX_TAG_ALIAS = r'{}:(\w+),' + +class TagAlias(models.Model, Viewable): + class Meta: + app_label = 'boards' + ordering = ('name',) + + name = models.CharField(max_length=100, db_index=True, unique=True) + locale = models.CharField(max_length=10, db_index=True) + + parent = models.ForeignKey('Tag', null=True, blank=True, + related_name='aliases') class TagManager(models.Manager): @@ -39,7 +49,12 @@ class TagManager(models.Manager): return ', '.join([tag.get_view() for tag in tags]) def get_by_alias(self, alias): - return self.filter(aliases__contains=":{},".format(alias)).first() + tag = None + try: + tag = TagAlias.objects.get(name=alias).parent + except TagAlias.DoesNotExist: + pass + return tag class Tag(models.Model, Viewable): @@ -60,7 +75,6 @@ class Tag(models.Model, Viewable): parent = models.ForeignKey('Tag', null=True, blank=True, related_name='children') - aliases = models.TextField(blank=True) def __str__(self): return self.name @@ -99,11 +113,10 @@ class Tag(models.Model, Viewable): def get_view(self): locale = get_language() - localized_tag_name = '' - if self.aliases: - match = re.search(REGEX_TAG_ALIAS.format(locale), self.aliases) - if match: - localized_tag_name = match.group(1) + try: + localized_tag_name = self.aliases.get(locale=locale).name + except TagAlias.DoesNotExist: + localized_tag_name = '' name = '{} ({})'.format(self.name, localized_tag_name) if localized_tag_name else self.name link = '{}'.format( @@ -160,3 +173,5 @@ class Tag(models.Model, Viewable): return Attachment.objects.filter( attachment_posts__thread__tags__in=[self]).filter( mimetype__in=FILE_TYPES_IMAGE).order_by('-attachment_posts__pub_time') + +