summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--askbot/models/__init__.py44
-rw-r--r--askbot/models/question.py53
-rw-r--r--askbot/models/tag.py8
3 files changed, 59 insertions, 46 deletions
diff --git a/askbot/models/__init__.py b/askbot/models/__init__.py
index 379b3825..ea515482 100644
--- a/askbot/models/__init__.py
+++ b/askbot/models/__init__.py
@@ -25,6 +25,7 @@ from askbot.const.message_keys import get_i18n_message
from askbot.conf import settings as askbot_settings
from askbot.models.question import Thread
from askbot.skins import utils as skin_utils
+from askbot.mail import messages
from askbot.models.question import QuestionView, AnonymousQuestion
from askbot.models.question import FavoriteQuestion
from askbot.models.tag import Tag, MarkedTag, get_group_names, get_groups
@@ -36,6 +37,7 @@ from askbot.models import signals
from askbot.models.badges import award_badges_signal, get_badge, BadgeData
from askbot.models.repute import Award, Repute, Vote
from askbot import auth
+from askbot.utils import category_tree
from askbot.utils.decorators import auto_now_timestamp
from askbot.utils.slug import slugify
from askbot.utils.diff import textDiff as htmldiff
@@ -301,6 +303,47 @@ def user_can_create_tags(self):
else:
return True
+def user_try_creating_tags(self,tag_names):
+ created_tags = list()
+ if self.can_create_tags():
+ for name in tag_names:
+ new_tag = Tag.objects.create(
+ name = name,
+ created_by = self,
+ used_count = 1
+ )
+ created_tags.append(new_tag)
+ #finally add tags to the relation and extend the modified list
+ elif tag_names:#notify admins by email about new tags
+ #maybe remove tags to report based on categories
+ #todo: maybe move this to tags_updated signal handler
+ if askbot_settings.TAG_SOURCE == 'category-tree':
+ category_names = category_tree.get_leaf_names()
+ tag_names = tag_names - category_names
+
+ if len(tag_names) > 0:
+ #todo: move all message sending codes to a separate module
+ body_text = messages.notify_admins_about_new_tags(
+ tags = tag_names,
+ user = self,
+ thread = self
+ )
+ site_name = askbot_settings.APP_SHORT_NAME
+ subject_line = _('New tags added to %s') % site_name
+ mail.mail_moderators(
+ subject_line,
+ body_text,
+ headers = {'Reply-To': self.email}
+ )
+ msg = _(
+ 'Tags %s are new and will be submitted for the '
+ 'moderators approval'
+ ) % ', '.join(tag_names)
+ self.message_set.create(message = msg)
+
+ return created_tags
+
+
def user_can_have_strong_url(self):
"""True if user's homepage url can be
followed by the search engine crawlers"""
@@ -2567,6 +2610,7 @@ User.add_to_class('set_admin_status', user_set_admin_status)
User.add_to_class('edit_group_membership', user_edit_group_membership)
User.add_to_class('is_group_member', user_is_group_member)
User.add_to_class('remove_admin_status', user_remove_admin_status)
+User.add_to_class('try_creating_tags', user_try_creating_tags)
User.add_to_class('is_moderator', user_is_moderator)
User.add_to_class('is_approved', user_is_approved)
User.add_to_class('is_watched', user_is_watched)
diff --git a/askbot/models/question.py b/askbot/models/question.py
index 3eb90852..fa62a537 100644
--- a/askbot/models/question.py
+++ b/askbot/models/question.py
@@ -798,24 +798,23 @@ class Thread(models.Model):
"""
Updates Tag associations for a thread to match the given
tagname string.
-
When tags are removed and their use count hits 0 - the tag is
automatically deleted.
-
When an added tag does not exist - it is created
+ If tag moderation is on - new tags are placed on the queue
Tag use counts are recalculated
-
A signal tags updated is sent
- *IMPORTANT*: self._question_post() has to exist when update_tags() is called!
+ *IMPORTANT*: self._question_post() has to
+ exist when update_tags() is called!
"""
previous_tags = list(self.tags.all())
previous_tagnames = set([tag.name for tag in previous_tags])
updated_tagnames = set(t for t in tagnames.strip().split(' '))
-
removed_tagnames = previous_tagnames - updated_tagnames
+
#remove tags from the question's tags many2many relation
#used_count values are decremented on all tags
removed_tags = self.remove_tags_by_names(removed_tagnames)
@@ -832,49 +831,11 @@ class Thread(models.Model):
if added_tagnames:
#find reused tags
reused_tags, new_tagnames = get_tags_by_names(added_tagnames)
- reused_tags.update(#undelete them
- deleted = False,
- deleted_by = None,
- deleted_at = None
- )
+ reused_tags.mark_undeleted()
added_tags = list(reused_tags)
- if user.can_create_tags():
- for name in new_tagnames:
- new_tag = Tag.objects.create(
- name = name,
- created_by = user,
- used_count = 1
- )
- added_tags.append(new_tag)
- #finally add tags to the relation and extend the modified list
- elif new_tagnames:#notify admins by email about new tags
- #maybe remove tags to report based on categories
- #todo: maybe move this to tags_updated signal handler
- if askbot_settings.TAG_SOURCE == 'category-tree':
- category_names = category_tree.get_leaf_names()
- new_tag_names = new_tag_names - category_names
-
- if len(new_tag_names) > 0:
- #todo: move all message sending codes to a separate module
- body_text = messages.notify_admins_about_new_tags(
- tags = new_tagnames,
- user = user,
- thread = self
- )
- site_name = askbot_settings.APP_SHORT_NAME
- subject_line = _('New tags added to %s') % site_name
- mail.mail_moderators(
- subject_line,
- body_text,
- headers = {'Reply-To': user.email}
- )
- msg = _(
- 'Tags %s are new and will be submitted for the '
- 'moderators approval'
- ) % ', '.join(new_tagnames)
- user.message_set.create(message = msg)
-
+ created_tags = user.try_creating_tags(new_tagnames)
+ added_tags.extend(created_tags)
#todo: not nice that assignment of added_tags is way above
self.tags.add(*added_tags)
modified_tags.extend(added_tags)
diff --git a/askbot/models/tag.py b/askbot/models/tag.py
index 80690201..95223bad 100644
--- a/askbot/models/tag.py
+++ b/askbot/models/tag.py
@@ -91,6 +91,14 @@ class TagQuerySet(models.query.QuerySet):
tag.used_count = tag.threads.count()
tag.save()
+ def mark_undeleted(self):
+ """removes deleted(+at/by) marks"""
+ self.update(#undelete them
+ deleted = False,
+ deleted_by = None,
+ deleted_at = None
+ )
+
def tags_match_some_wildcard(self, wildcard_tags = None):
"""True if any one of the tags in the query set
matches a wildcard