diff options
author | Adolfo Fitoria <adolfo.fitoria@gmail.com> | 2012-10-24 17:07:32 -0600 |
---|---|---|
committer | Adolfo Fitoria <adolfo.fitoria@gmail.com> | 2012-10-24 17:07:32 -0600 |
commit | c55b82c939e2427cfcbd195bbc94fe5949599987 (patch) | |
tree | 82bdf530a16c17821b96bb9d41ace299ea9b44cc | |
parent | 0da328b5ba0dc8bdb3803a85cef150ffc8a69bb7 (diff) | |
parent | f6877916d84c43a86e239e2343c6779257c8f9c7 (diff) | |
download | askbot-c55b82c939e2427cfcbd195bbc94fe5949599987.tar.gz askbot-c55b82c939e2427cfcbd195bbc94fe5949599987.tar.bz2 askbot-c55b82c939e2427cfcbd195bbc94fe5949599987.zip |
Merge branch 'master' into django1.4
-rw-r--r-- | askbot/conf/group_settings.py | 5 | ||||
-rw-r--r-- | askbot/context.py | 2 | ||||
-rw-r--r-- | askbot/forms.py | 8 | ||||
-rw-r--r-- | askbot/models/__init__.py | 10 | ||||
-rw-r--r-- | askbot/models/post.py | 13 | ||||
-rw-r--r-- | askbot/models/question.py | 15 | ||||
-rw-r--r-- | askbot/models/tag.py | 26 | ||||
-rw-r--r-- | askbot/models/user.py | 18 | ||||
-rw-r--r-- | askbot/tests/db_api_tests.py | 11 | ||||
-rw-r--r-- | askbot/tests/question_views_tests.py | 13 | ||||
-rw-r--r-- | askbot/tests/thread_model_tests.py | 5 | ||||
-rw-r--r-- | askbot/views/commands.py | 3 | ||||
-rw-r--r-- | askbot/views/users.py | 8 |
13 files changed, 58 insertions, 79 deletions
diff --git a/askbot/conf/group_settings.py b/askbot/conf/group_settings.py index 2933b831..04be618f 100644 --- a/askbot/conf/group_settings.py +++ b/askbot/conf/group_settings.py @@ -20,14 +20,15 @@ settings.register( ) def group_name_update_callback(old_name, new_name): - from askbot.models.tag import get_global_group, clean_group_name + from askbot.models.tag import clean_group_name + from askbot.models import Group cleaned_new_name = clean_group_name(new_name.strip()) if new_name == '': #name cannot be empty return old_name - group = get_global_group() + group = Group.objects.get_global_group() group.name = cleaned_new_name group.save() return new_name diff --git a/askbot/context.py b/askbot/context.py index 5e53febc..61f833cb 100644 --- a/askbot/context.py +++ b/askbot/context.py @@ -69,7 +69,7 @@ def application_settings(request): ) #load id's and names of all groups - global_group = models.tag.get_global_group() + global_group = models.Group.objects.get_global_group() groups = models.Group.objects.exclude_personal() groups = groups.exclude(id=global_group.id) groups_data = list(groups.values('id', 'name')) diff --git a/askbot/forms.py b/askbot/forms.py index 0659e338..1c5bc3d6 100644 --- a/askbot/forms.py +++ b/askbot/forms.py @@ -12,7 +12,6 @@ from django.contrib.auth.models import User from django_countries import countries from askbot.utils.forms import NextUrlField, UserNameField from askbot.mail import extract_first_email_address -from askbot.models.tag import get_groups from recaptcha_works.fields import RecaptchaField from askbot.conf import settings as askbot_settings from askbot.conf import get_tag_display_filter_strategy_choices @@ -958,10 +957,10 @@ class CreateAskWidgetForm(forms.Form, FormWithHideableFields): ) def __init__(self, *args, **kwargs): - from askbot.models import Tag + from askbot.models import Group, Tag super(CreateAskWidgetForm, self).__init__(*args, **kwargs) self.fields['group'] = forms.ModelChoiceField( - queryset=get_groups().exclude_personal(), + queryset=Group.objects.exclude_personal(), required=False ) self.fields['tag'] = forms.ModelChoiceField(queryset=Tag.objects.get_content_tags(), @@ -985,10 +984,11 @@ class CreateQuestionWidgetForm(forms.Form, FormWithHideableFields): ) def __init__(self, *args, **kwargs): + from askbot.models import Group super(CreateQuestionWidgetForm, self).__init__(*args, **kwargs) self.fields['tagnames'] = TagNamesField() self.fields['group'] = forms.ModelChoiceField( - queryset=get_groups().exclude(name__startswith='_internal'), + queryset=Group.objects.exclude(name__startswith='_internal'), required=False ) diff --git a/askbot/models/__init__.py b/askbot/models/__init__.py index f86005ee..73996a82 100644 --- a/askbot/models/__init__.py +++ b/askbot/models/__init__.py @@ -41,9 +41,6 @@ from askbot.models.question import QuestionView, AnonymousQuestion from askbot.models.question import DraftQuestion from askbot.models.question import FavoriteQuestion from askbot.models.tag import Tag, MarkedTag -from askbot.models.tag import get_global_group -from askbot.models.tag import get_group_names -from askbot.models.tag import get_groups from askbot.models.tag import format_personal_group_name from askbot.models.user import EmailFeedSetting, ActivityAuditStatus, Activity from askbot.models.user import GroupMembership @@ -2300,7 +2297,7 @@ def user_get_foreign_groups(self): """returns a query set of groups to which user does not belong""" #todo: maybe cache this query user_group_ids = self.get_groups().values_list('id', flat = True) - return get_groups().exclude(id__in = user_group_ids) + return Group.objects.exclude(id__in = user_group_ids) def user_get_primary_group(self): """a temporary function - returns ether None or @@ -3501,9 +3498,8 @@ def add_user_to_global_group(sender, instance, created, **kwargs): ``instance`` is an instance of ``User`` class """ if created: - from askbot.models.tag import get_global_group instance.edit_group_membership( - group=get_global_group(), + group=Group.objects.get_global_group(), user=instance, action='add' ) @@ -3696,6 +3692,4 @@ __all__ = [ 'ReplyAddress', 'get_model', - 'get_group_names', - 'get_groups' ] diff --git a/askbot/models/post.py b/askbot/models/post.py index 0c5e2b00..36bc5037 100644 --- a/askbot/models/post.py +++ b/askbot/models/post.py @@ -30,8 +30,7 @@ from askbot.models.user import EmailFeedSetting from askbot.models.user import Group from askbot.models.user import GroupMembership from askbot.models.tag import Tag, MarkedTag -from askbot.models.tag import get_groups, tags_match_some_wildcard -from askbot.models.tag import get_global_group +from askbot.models.tag import tags_match_some_wildcard from askbot.conf import settings as askbot_settings from askbot import exceptions from askbot.utils import markup @@ -62,7 +61,7 @@ class PostQuerySet(models.query.QuerySet): def get_for_user(self, user): if askbot_settings.GROUPS_ENABLED: if user is None or user.is_anonymous(): - groups = [get_global_group()] + groups = [Group.objects.get_global_group()] else: groups = user.get_groups() return self.filter(groups__in = groups).distinct() @@ -692,7 +691,7 @@ class Post(models.Model): groups = [group] self.add_to_groups(groups) - global_group = get_global_group() + global_group = Group.objects.get_global_group() if group != global_group: self.remove_from_groups((global_group,)) else: @@ -706,7 +705,7 @@ class Post(models.Model): groups = user.get_groups(private=True) self.add_to_groups(groups) - self.remove_from_groups((get_global_group(),)) + self.remove_from_groups((Group.objects.get_global_group(),)) if len(groups) == 0: message = 'Sharing did not work, because group is unknown' @@ -714,13 +713,13 @@ class Post(models.Model): def make_public(self): """removes the privacy mark from users groups""" - groups = (get_global_group(),) + groups = (Group.objects.get_global_group(),) self.add_to_groups(groups) def is_private(self): """true, if post belongs to the global group""" if askbot_settings.GROUPS_ENABLED: - group = get_global_group() + group = Group.objects.get_global_group() return not self.groups.filter(id=group.id).exists() return False diff --git a/askbot/models/question.py b/askbot/models/question.py index f94265b0..e3f9146b 100644 --- a/askbot/models/question.py +++ b/askbot/models/question.py @@ -17,13 +17,10 @@ from askbot.conf import settings as askbot_settings from askbot import mail from askbot.mail import messages from askbot.models.tag import Tag -from askbot.models.tag import get_groups -from askbot.models.tag import get_global_group from askbot.models.tag import get_tags_by_names from askbot.models.tag import filter_accepted_tags, filter_suggested_tags from askbot.models.tag import delete_tags, separate_unused_tags from askbot.models.base import DraftContent, BaseQuerySetManager -from askbot.models.tag import Tag, get_groups from askbot.models.post import Post, PostRevision from askbot.models.post import PostToGroup from askbot.models.user import Group, PERSONAL_GROUP_NAME_PREFIX @@ -41,7 +38,7 @@ class ThreadQuerySet(models.query.QuerySet): if user.is_authenticated(): groups = user.get_groups() else: - groups = [get_global_group()] + groups = [Group.objects.get_global_group()] return self.filter(groups__in=groups).distinct() class ThreadManager(BaseQuerySetManager): @@ -825,7 +822,7 @@ class Thread(models.Model): thread_posts = self.posts.all() if askbot_settings.GROUPS_ENABLED: if user is None or user.is_anonymous(): - groups = (get_global_group(),) + groups = (Group.objects.get_global_group(),) else: groups = user.get_groups() @@ -1078,7 +1075,7 @@ class Thread(models.Model): def make_public(self, recursive=False): """adds the global group to the thread""" - groups = (get_global_group(), ) + groups = (Group.objects.get_global_group(), ) self.add_to_groups(groups, recursive=recursive) if recursive == False: self._question_post().make_public() @@ -1093,13 +1090,13 @@ class Thread(models.Model): groups = [group] self.add_to_groups(groups) - global_group = get_global_group() + global_group = Group.objects.get_global_group() if group != global_group: self.remove_from_groups((global_group,)) else: groups = user.get_groups(private=True) self.add_to_groups(groups) - self.remove_from_groups((get_global_group(),)) + self.remove_from_groups((Group.objects.get_global_group(),)) self._question_post().make_private(user, group_id) @@ -1110,7 +1107,7 @@ class Thread(models.Model): def is_private(self): """true, if thread belongs to the global group""" if askbot_settings.GROUPS_ENABLED: - group = get_global_group() + group = Group.objects.get_global_group() return not self.groups.filter(id=group.id).exists() return False diff --git a/askbot/models/tag.py b/askbot/models/tag.py index 647ea5cf..d7e91eb5 100644 --- a/askbot/models/tag.py +++ b/askbot/models/tag.py @@ -9,21 +9,6 @@ from askbot import const from askbot.conf import settings as askbot_settings from askbot.utils import category_tree -def get_global_group(): - """Returns the global group, - if necessary, creates one - """ - #todo: when groups are disconnected from tags, - #find comment as shown below in the test cases and - #revert the values - #todo: change groups to django groups - group_name = askbot_settings.GLOBAL_GROUP_NAME - from askbot.models import Group - try: - return Group.objects.get(name=group_name) - except Group.DoesNotExist: - return Group.objects.create(name=group_name) - def delete_tags(tags): """deletes tags in the list""" tag_ids = [tag.id for tag in tags] @@ -277,7 +262,8 @@ class TagManager(BaseQuerySetManager): return created_tags def clean_group_name(name): - """group names allow spaces, + """todo: move to the models/user.py + group names allow spaces, tag names do not, so we use this method to replace spaces with dashes""" return re.sub('\s+', '-', name.strip()) @@ -332,11 +318,3 @@ class MarkedTag(models.Model): class Meta: app_label = 'askbot' - -def get_groups(): - from askbot.models import Group - return Group.objects.all() - -def get_group_names(): - #todo: cache me - return get_groups().values_list('name', flat = True) diff --git a/askbot/models/user.py b/askbot/models/user.py index e5cccbf3..00e9428a 100644 --- a/askbot/models/user.py +++ b/askbot/models/user.py @@ -15,7 +15,7 @@ from askbot import const from askbot.conf import settings as askbot_settings from askbot.utils import functions from askbot.models.base import BaseQuerySetManager -from askbot.models.tag import Tag, get_global_group +from askbot.models.tag import Tag from askbot.models.tag import clean_group_name#todo - delete this from askbot.forms import DomainNameField from askbot.utils.forms import email_is_allowed @@ -411,7 +411,7 @@ class GroupQuerySet(models.query.QuerySet): def get_for_user(self, user=None, private=False): if private: - global_group = get_global_group() + global_group = Group.objects.get_global_group() return self.filter( user=user ).exclude(id=global_group.id) @@ -428,6 +428,20 @@ class GroupManager(BaseQuerySetManager): def get_query_set(self): return GroupQuerySet(self.model) + def get_global_group(self): + """Returns the global group, + if necessary, creates one + """ + #todo: when groups are disconnected from tags, + #find comment as shown below in the test cases and + #revert the values + #todo: change groups to django groups + group_name = askbot_settings.GLOBAL_GROUP_NAME + try: + return self.get_query_set().get(name=group_name) + except Group.DoesNotExist: + return self.get_query_set().create(name=group_name) + def create(self, **kwargs): name = kwargs['name'] try: diff --git a/askbot/tests/db_api_tests.py b/askbot/tests/db_api_tests.py index 8a6ffe0e..232b847a 100644 --- a/askbot/tests/db_api_tests.py +++ b/askbot/tests/db_api_tests.py @@ -15,7 +15,6 @@ from askbot.tests.utils import with_settings from askbot import models from askbot import const from askbot.conf import settings as askbot_settings -from askbot.models.tag import get_global_group import datetime class DBApiTests(AskbotTestCase): @@ -186,6 +185,8 @@ class DBApiTests(AskbotTestCase): self.assertTrue(saved_question.thread.answer_count == 1) def test_unused_tag_is_auto_deleted(self): + import pdb + pdb.set_trace() self.user.retag_question(self.question, tags='one-tag') tag = models.Tag.objects.get(name='one-tag') self.assertEquals(tag.used_count, 1) @@ -515,11 +516,11 @@ class GroupTests(AskbotTestCase): def test_global_group_name_setting_changes_group_name(self): askbot_settings.update('GLOBAL_GROUP_NAME', 'all-people') - group = get_global_group() + group = models.Group.objects.get_global_group() self.assertEqual(group.name, 'all-people') def test_ask_global_group_by_id_works(self): - group = get_global_group() + group = models.Group.objects.get_global_group() q = self.post_question(user=self.u1, group_id=group.id) self.assertEqual(q.groups.count(), 2) self.assertEqual(q.groups.filter(name=group.name).exists(), True) @@ -601,7 +602,7 @@ class GroupTests(AskbotTestCase): data['thread'].make_public(recursive=True) - global_group = get_global_group() + global_group = models.Group.objects.get_global_group() groups = [global_group, private_group, self.u1.get_personal_group()] self.assertObjectGroupsEqual(data['thread'], groups) self.assertObjectGroupsEqual(data['question'], groups) @@ -616,7 +617,7 @@ class GroupTests(AskbotTestCase): thread = data['thread'] thread.add_to_groups([private_group], recursive=True) - global_group = get_global_group() + global_group = models.Group.objects.get_global_group() groups = [global_group, private_group, self.u1.get_personal_group()] self.assertObjectGroupsEqual(thread, groups) self.assertObjectGroupsEqual(data['question'], groups) diff --git a/askbot/tests/question_views_tests.py b/askbot/tests/question_views_tests.py index b1836f9e..223f65f6 100644 --- a/askbot/tests/question_views_tests.py +++ b/askbot/tests/question_views_tests.py @@ -3,7 +3,6 @@ from askbot.conf import settings as askbot_settings from askbot import const from askbot.tests.utils import AskbotTestCase from askbot import models -from askbot.models.tag import get_global_group from django.core.urlresolvers import reverse @@ -36,7 +35,7 @@ class PrivateQuestionViewsTests(AskbotTestCase): self.assertTrue(const.POST_STATUS['private'] in title) question = models.Thread.objects.get(id=1) self.assertEqual(question.title, self.qdata['title']) - self.assertFalse(get_global_group() in set(question.groups.all())) + self.assertFalse(models.Group.objects.get_global_group() in set(question.groups.all())) #private question is not accessible to unauthorized users self.client.logout() @@ -67,7 +66,7 @@ class PrivateQuestionViewsTests(AskbotTestCase): response2 = self.client.get(question.get_absolute_url()) dom = BeautifulSoup(response2.content) title = dom.find('h1').text - self.assertTrue(get_global_group() in set(question.groups.all())) + self.assertTrue(models.Group.objects.get_global_group() in set(question.groups.all())) self.assertEqual(title, self.qdata['title']) self.client.logout() @@ -88,7 +87,7 @@ class PrivateQuestionViewsTests(AskbotTestCase): response2 = self.client.get(question.get_absolute_url()) dom = BeautifulSoup(response2.content) title = dom.find('h1').text - self.assertFalse(get_global_group() in set(question.groups.all())) + self.assertFalse(models.Group.objects.get_global_group() in set(question.groups.all())) self.assertTrue(const.POST_STATUS['private'] in title) def test_private_checkbox_is_on_when_editing_private_question(self): @@ -136,7 +135,7 @@ class PrivateAnswerViewsTests(AskbotTestCase): data={'text': 'some answer text', 'post_privately': 'checked'} ) answer = self.question.thread.get_answers(user=self.user)[0] - self.assertFalse(get_global_group() in set(answer.groups.all())) + self.assertFalse(models.Group.objects.get_global_group() in set(answer.groups.all())) self.client.logout() user2 = self.create_user('user2') @@ -182,7 +181,7 @@ class PrivateAnswerViewsTests(AskbotTestCase): data={'text': 'edited answer text', 'select_revision': 'false'} ) answer = self.reload_object(answer) - self.assertTrue(get_global_group() in answer.groups.all()) + self.assertTrue(models.Group.objects.get_global_group() in answer.groups.all()) self.client.logout() response = self.client.get(self.question.get_absolute_url()) self.assertTrue('edited answer text' in response.content) @@ -200,7 +199,7 @@ class PrivateAnswerViewsTests(AskbotTestCase): ) #check that answer is not visible to the "everyone" group answer = self.reload_object(answer) - self.assertFalse(get_global_group() in answer.groups.all()) + self.assertFalse(models.Group.objects.get_global_group() in answer.groups.all()) #check that countent is not seen by an anonymous user self.client.logout() response = self.client.get(self.question.get_absolute_url()) diff --git a/askbot/tests/thread_model_tests.py b/askbot/tests/thread_model_tests.py index baf51e8e..33a2055c 100644 --- a/askbot/tests/thread_model_tests.py +++ b/askbot/tests/thread_model_tests.py @@ -1,7 +1,6 @@ from askbot.tests.utils import AskbotTestCase from askbot.conf import settings as askbot_settings from askbot import models -from askbot.models.tag import get_global_group import django.core.mail from django.core.urlresolvers import reverse @@ -63,7 +62,7 @@ class ThreadModelTestsWithGroupsEnabled(AskbotTestCase): def test_answer_to_private_question_is_not_globally_visible(self): question = self.post_question(user=self.admin, is_private=True) answer = self.post_answer(question=question, user=self.admin, is_private=False) - global_group = get_global_group() + global_group = models.Group.objects.get_global_group() self.assertEqual( global_group in set(answer.groups.all()), False @@ -74,7 +73,7 @@ class ThreadModelTestsWithGroupsEnabled(AskbotTestCase): question = self.post_question(user=self.user, group_id=self.group.id) #answer posted by a group member answer = self.post_answer(question=question, user=self.admin, is_private=False) - global_group = get_global_group() + global_group = models.Group.objects.get_global_group() self.assertEqual( global_group in set(answer.groups.all()), False diff --git a/askbot/views/commands.py b/askbot/views/commands.py index f7d22f48..5a76790f 100644 --- a/askbot/views/commands.py +++ b/askbot/views/commands.py @@ -30,7 +30,6 @@ from askbot import models from askbot import forms from askbot.conf import should_show_sort_by_relevance from askbot.conf import settings as askbot_settings -from askbot.models.tag import get_global_group from askbot.utils import category_tree from askbot.utils import decorators from askbot.utils import url_utils @@ -649,7 +648,7 @@ def add_tag_category(request): def get_groups_list(request): """returns names of group tags for the autocomplete function""" - global_group = get_global_group() + global_group = models.Group.objects.get_global_group() groups = models.Group.objects.exclude_personal() group_names = groups.exclude( name=global_group.name diff --git a/askbot/views/users.py b/askbot/views/users.py index c22fd294..b89e46cb 100644 --- a/askbot/views/users.py +++ b/askbot/views/users.py @@ -39,8 +39,6 @@ from askbot.conf import settings as askbot_settings from askbot import models from askbot import exceptions from askbot.models.badges import award_badges_signal -from askbot.models.tag import get_global_group -from askbot.models.tag import get_groups from askbot.models.tag import format_personal_group_name from askbot.skins.loaders import render_into_skin from askbot.search.state_manager import SearchState @@ -65,7 +63,7 @@ def show_users(request, by_group=False, group_id=None, group_slug=None): """Users view, including listing of users by group""" if askbot_settings.GROUPS_ENABLED and not by_group: - default_group = get_global_group() + default_group = models.Group.objects.get_global_group() group_slug = slugify(default_group.name) new_url = reverse('users_by_group', kwargs={'group_id': default_group.id, @@ -178,7 +176,7 @@ def show_users(request, by_group=False, group_id=None, group_slug=None): #extra context for the groups if askbot_settings.GROUPS_ENABLED: #todo: cleanup this branched code after groups are migrated to auth_group - user_groups = get_groups().exclude_personal() + user_groups = models.Group.objects.exclude_personal() if len(user_groups) <= 1: assert(user_groups[0].name == askbot_settings.GLOBAL_GROUP_NAME) user_groups = None @@ -491,7 +489,7 @@ def user_stats(request, user, context): user_groups = models.Group.objects.get_for_user(user = user) user_groups = user_groups.exclude_personal() - global_group = get_global_group() + global_group = models.Group.objects.get_global_group() user_groups = user_groups.exclude(name=global_group.name) if request.user == user: |