diff options
author | Evgeny Fadeev <evgeny.fadeev@gmail.com> | 2014-05-13 18:02:20 -0300 |
---|---|---|
committer | Evgeny Fadeev <evgeny.fadeev@gmail.com> | 2014-05-13 18:02:20 -0300 |
commit | d3f23e60c7137ba5e8aa7d8869099627ff26e4de (patch) | |
tree | bb04f41e7a522fe4657a6ffe5f9a54daa52057e4 | |
parent | f8a4502cccb42f13cb1576e917c0aa418a42e904 (diff) | |
download | askbot-d3f23e60c7137ba5e8aa7d8869099627ff26e4de.tar.gz askbot-d3f23e60c7137ba5e8aa7d8869099627ff26e4de.tar.bz2 askbot-d3f23e60c7137ba5e8aa7d8869099627ff26e4de.zip |
fixed change of language on whole thread when editing question
-rw-r--r-- | askbot/media/style/style.css | 6 | ||||
-rw-r--r-- | askbot/media/style/style.less | 5 | ||||
-rw-r--r-- | askbot/models/question.py | 60 | ||||
-rw-r--r-- | askbot/models/tag.py | 14 | ||||
-rw-r--r-- | askbot/tests/user_model_tests.py | 3 | ||||
-rw-r--r-- | askbot/tests/utils.py | 2 | ||||
-rw-r--r-- | askbot/views/writers.py | 7 |
7 files changed, 84 insertions, 13 deletions
diff --git a/askbot/media/style/style.css b/askbot/media/style/style.css index 0f0119f3..e1787790 100644 --- a/askbot/media/style/style.css +++ b/askbot/media/style/style.css @@ -1733,7 +1733,11 @@ ul#related-tags li { } .ask-page .lang-selector, .edit-question-page .lang-selector { - margin: 1px 0 0 5px; + margin: 9px 0 0 0; +} +.ask-page .lang-selector select, +.edit-question-page .lang-selector select { + margin: 3px 0; } .ask-page #id_post_author_username, .question-page #id_post_author_username, diff --git a/askbot/media/style/style.less b/askbot/media/style/style.less index e4a5623b..748591df 100644 --- a/askbot/media/style/style.less +++ b/askbot/media/style/style.less @@ -1829,7 +1829,10 @@ ul#related-tags li { max-width: 395px; } .lang-selector { - margin: 1px 0 0 5px; + margin: 9px 0 0 0; + select { + margin: 3px 0; + } } } diff --git a/askbot/models/question.py b/askbot/models/question.py index c2df8b60..65e1168a 100644 --- a/askbot/models/question.py +++ b/askbot/models/question.py @@ -4,6 +4,7 @@ import re from django.conf import settings as django_settings from django.db import models +from django.db.models import F from django.contrib.auth.models import User from django.core import cache # import cache, not from cache import cache, to be able to monkey-patch cache.cache in test cases from django.core import exceptions as django_exceptions @@ -829,6 +830,65 @@ class Thread(models.Model): self.save() self.invalidate_cached_data() + def set_tags_language_code(self, language_code=None): + """sets language code to tags of this thread. + If lang code of the tag does not coincide with that + of thread, we replace the tag with the one of correct + lang code. If necessary, tags are created and + the used_counts are updated. + """ + wrong_lang_tags = list() + for tag in self.tags.all(): + if tag.language_code != language_code: + wrong_lang_tags.append(tag) + + #remove wrong tags + self.tags.remove(*wrong_lang_tags) + #update used counts of the wrong tags + wrong_lang_tag_names = list() + for tag in wrong_lang_tags: + wrong_lang_tag_names.append(tag.name) + if tag.used_count > 0: + tag.used_count -= 1 + tag.save() + + #load existing tags and figure out which tags don't exist + reused_tags, new_tagnames = get_tags_by_names( + wrong_lang_tag_names, + language_code=language_code + ) + reused_tags.mark_undeleted() + #tag moderation is in the call below + created_tags = Tag.objects.create_in_bulk( + language_code=self.language_code, + tag_names=new_tagnames, + user=self.last_activity_by, + auto_approve=True + ) + #add the tags + added_tags = list(reused_tags) + list(created_tags) + self.tags.add(*added_tags) + #increment the used counts and save tags + tag_ids = [tag.id for tag in added_tags] + Tag.objects.filter(id__in=tag_ids).update(used_count=F('used_count') + 1) + + def set_language_code(self, language_code=None): + assert(language_code) + + #save language code on thread + self.language_code = language_code + self.save() + + #save language code on all posts + #for some reason "update" fails in postgres - possibly b/c of the FTS + for post in self.posts.all(): + post.language_code = language_code + post.save() + + #make sure that tags have correct language code + self.set_tags_language_code(language_code) + + def set_accepted_answer(self, answer, timestamp): if answer and answer.thread != self: raise ValueError("Answer doesn't belong to this thread") diff --git a/askbot/models/tag.py b/askbot/models/tag.py index b78f9ba1..1934f81a 100644 --- a/askbot/models/tag.py +++ b/askbot/models/tag.py @@ -181,9 +181,9 @@ class TagManager(BaseQuerySetManager): """temporary function that filters out the group tags""" return self.all() - def create(self, name=None, created_by=None, **kwargs): + def create(self, name=None, created_by=None, auto_approve=False, **kwargs): """Creates a new tag""" - if created_by.can_create_tags() or is_preapproved_tag_name(name): + if auto_approve or created_by.can_create_tags() or is_preapproved_tag_name(name): status = Tag.STATUS_ACCEPTED else: status = Tag.STATUS_SUGGESTED @@ -223,7 +223,7 @@ class TagManager(BaseQuerySetManager): ) % ', '.join(tag_names) user.message_set.create(message = msg) - def create_in_bulk(self, tag_names=None, user=None, language_code=None): + def create_in_bulk(self, tag_names=None, user=None, language_code=None, auto_approve=False): """creates tags by names. If user can create tags, then they are set status ``STATUS_ACCEPTED``, otherwise the status will be set to ``STATUS_SUGGESTED``. @@ -231,6 +231,7 @@ class TagManager(BaseQuerySetManager): One exception: if suggested tag is in the category tree and source of tags is category tree - then status of newly created tag is ``STATUS_ACCEPTED`` + if `auto_approve` is True then tags are auto-accepted """ #load suggested tags @@ -241,13 +242,13 @@ class TagManager(BaseQuerySetManager): ) #deal with suggested tags - if user.can_create_tags(): + if auto_approve or user.can_create_tags(): #turn previously suggested tags into accepted pre_suggested_tags.update(status = Tag.STATUS_ACCEPTED) else: #increment use count and add user to "suggested_by" for tag in pre_suggested_tags: - tag.times_used += 1 + tag.used_count += 1 tag.suggested_by.add(user) tag.save() @@ -262,7 +263,8 @@ class TagManager(BaseQuerySetManager): new_tag = Tag.objects.create( name=tag_name, created_by=user, - language_code=language_code + language_code=language_code, + auto_approve=auto_approve ) created_tags.append(new_tag) diff --git a/askbot/tests/user_model_tests.py b/askbot/tests/user_model_tests.py index b11fb151..786991a0 100644 --- a/askbot/tests/user_model_tests.py +++ b/askbot/tests/user_model_tests.py @@ -31,7 +31,8 @@ class UserModelTests(AskbotTestCase): bulk_subscription = models.BulkTagSubscription.objects.create( tag_names=[one_tag.name, another_tag.name], group_list=[global_group], - tag_author=the_boss + tag_author=the_boss, + language_code='en' ) user = self.create_user('someone') diff --git a/askbot/tests/utils.py b/askbot/tests/utils.py index 0a207464..bb9a6103 100644 --- a/askbot/tests/utils.py +++ b/askbot/tests/utils.py @@ -290,7 +290,7 @@ class AskbotTestCase(TestCase): except models.User.DoesNotExist: user = self.create_user('tag_creator') - tag = models.Tag(created_by = user, name = tag_name) + tag = models.Tag(created_by=user, name=tag_name, language_code='en') tag.save() return tag diff --git a/askbot/views/writers.py b/askbot/views/writers.py index 17c1db1c..88e5e0cf 100644 --- a/askbot/views/writers.py +++ b/askbot/views/writers.py @@ -434,9 +434,6 @@ def edit_question(request, id): if form.cleaned_data['reveal_identity']: question.thread.remove_author_anonymity() - if 'language' in form.cleaned_data: - question.thread.language_code = form.cleaned_data['language'] - is_anon_edit = form.cleaned_data['stay_anonymous'] is_wiki = form.cleaned_data.get('wiki', question.wiki) post_privately = form.cleaned_data['post_privately'] @@ -455,6 +452,10 @@ def edit_question(request, id): is_private = post_privately, suppress_email=suppress_email ) + + if 'language' in form.cleaned_data: + question.thread.set_language_code(form.cleaned_data['language']) + return HttpResponseRedirect(question.get_absolute_url()) else: #request type was "GET" |