Пример #1
0
    def test_join_group(self):
        #create group
        group = models.Group(name='somegroup')
        group.openness = models.Group.OPEN
        group.save()
        #join
        self.u1 = self.create_user('user1')
        self.u1.join_group(group)
        #assert membership of askbot group object
        found_count = self.u1.get_groups().filter(name='somegroup').count()
        self.assertEqual(found_count, 1)

        #closed group
        closed_group = models.Group(name="secretgroup")
        closed_group.openness = models.Group.CLOSED
        closed_group.save()

        #join (should raise exception)
        self.assertRaises(exceptions.PermissionDenied, self.u1.join_group,
                          closed_group)
        #testing force parameter
        self.u1.join_group(closed_group, force=True)

        #assert membership of askbot group object
        found_count = self.u1.get_groups().filter(name='secretgroup').count()
        self.assertEqual(found_count, 1)
Пример #2
0
    def test_save_object_description_works(self):
        group = models.Group(name='somegroup')
        group.save()
        admin = self.create_user('admin', status='d')
        self.client.login(user_id=admin.id, method='force')
        post_data = {
            'object_id': group.id,
            'model_name': 'Group',
            'text': 'some description'
        }
        self.client.post(  #ajax post
            reverse('save_object_description'),
            data=post_data,
            HTTP_X_REQUESTED_WITH='XMLHttpRequest')
        group = self.reload_object(group)
        self.assertEqual(group.description.text, 'some description')

        #test edit
        post_data['text'] = 'edited description'
        self.client.post(  #second post to edit
            reverse('save_object_description'),
            data=post_data,
            HTTP_X_REQUESTED_WITH='XMLHttpRequest')
        group = self.reload_object(group)
        self.assertEqual(group.description.text, 'edited description')
Пример #3
0
    def test_restrictive_response_publishing(self):
        #restrictive model should work even with groups
        #in common between the asker and the problemer
        common_group = models.Group(name='common', openness=models.Group.OPEN)
        common_group.save()
        self.admin.join_group(common_group)
        self.user.join_group(common_group)

        self.group.moderate_problems_to_enquirers = True
        self.group.save()
        exercise = self.post_exercise(user=self.user, group_id=self.group.id)
        problem = self.post_problem(exercise=exercise, user=self.admin)

        #problem and the user don't have groups in common
        problem_groups = set(problem.groups.all())
        user_groups = set(self.user.get_groups())
        self.assertEqual(len(problem_groups & user_groups), 0)

        #publish the problem
        self.client.login(user_id=self.admin.id, method='force')
        self.client.post(reverse('publish_problem'),
                         data={'problem_id': problem.id},
                         HTTP_X_REQUESTED_WITH='XMLHttpRequest')
        #todo: test redirect

        problem = self.reload_object(problem)
        problem_groups = set(problem.groups.all())
        self.assertEqual(len(problem_groups & user_groups), 1)
Пример #4
0
    def test_restrictive_response_publishing(self):
        # restrictive model should work even with groups
        # in common between the asker and the answerer
        common_group = models.Group(
                        name='common',
                        openness=models.Group.OPEN
                    )
        common_group.save()
        self.admin.join_group(common_group)
        self.user.join_group(common_group)

        self.group.moderate_answers_to_enquirers = True
        self.group.save()
        question = self.post_question(user=self.user, group_id=self.group.id)
        answer = self.post_answer(question=question, user=self.admin)

        # answer and the user don't have groups in common
        answer_groups = set(answer.groups.all())
        user_groups = set(self.user.get_groups())
        self.assertEqual(len(answer_groups & user_groups), 0)

        # publish the answer
        self.client.login(user_id=self.admin.id, method='force')
        self.client.post(
            reverse('publish_answer'),
            data={'answer_id': answer.id},
            HTTP_X_REQUESTED_WITH='XMLHttpRequest'
        )
        # TODO: test redirect

        answer = self.reload_object(answer)
        answer_groups = set(answer.groups.all())
        self.assertEqual(len(answer_groups & user_groups), 1)
Пример #5
0
    def test_load_empty_object_description_works(self):
        group = models.Group(name='somegroup')
        group.save()

        response = self.client.get(
            reverse('load_object_description'),
            data = {'object_id': group.id, 'model_name': 'Group'},
        )
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.content, '')
Пример #6
0
 def test_join_group(self):
     #create group
     group = models.Group(name='somegroup')
     group.openness = models.Group.OPEN
     group.save()
     #join
     self.u1 = self.create_user('user1')
     self.u1.join_group(group)
     #assert membership of askbot group object
     found_count = self.u1.get_groups().filter(name='somegroup').count()
     self.assertEqual(found_count, 1)
Пример #7
0
    def test_load_full_object_description_works(self):
        group = models.Group(name='somegroup')
        user = self.create_user('someuser')
        post_params = {'author': user, 'text':'some text'}
        post = models.Post.objects.create_new_tag_wiki(**post_params)
        group.description = post
        group.save()

        response = self.client.get(
            reverse('load_object_description'),
            data = {'object_id': group.id,'model_name': 'Group'},
        )
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.content, 'some text')
Пример #8
0
    def test_api_get_questions_groups_enabled(self):

        group = models.Group(name='secret group', openness=models.Group.OPEN)
        group.save()
        user = self.create_user('user')
        user.join_group(group)
        self.post_question(user=user, title='alibaba', group_id=group.id)

        query_data = {'query': 'alibaba'}
        response = self.client.get(reverse('api_get_questions'), query_data)
        response_data = simplejson.loads(response.content)
        self.assertEqual(len(response_data), 0)

        self.client.login(method='force', user_id=user.id)
        response = self.client.get(reverse('api_get_questions'), query_data)
        response_data = simplejson.loads(response.content)
        self.assertEqual(len(response_data), 1)
Пример #9
0
    def test_title_search_groups_enabled(self):

        group = models.Group(name='secret group', openness=models.Group.OPEN)
        group.save()
        user = self.create_user('user')
        user.join_group(group)
        question = self.post_question(user=user, title='alibaba', group_id=group.id)

        #ask for data anonymously - should get nothing
        query_data = {'query_text': 'alibaba'}
        response = self.client.get(reverse('api_get_questions'), query_data)
        response_data = simplejson.loads(response.content)
        self.assertEqual(len(response_data), 0)

        #log in - should get the question
        self.client.login(method='force', user_id=user.id)
        response = self.client.get(reverse('api_get_questions'), query_data)
        response_data = simplejson.loads(response.content)
        self.assertEqual(len(response_data), 1)
Пример #10
0
    def test_group_moderation(self):
        #create group
        group = models.Group(name='somegroup')
        #make it moderated
        group.openness = models.Group.MODERATED
        group.save()

        #add moderator to the group
        mod = self.create_user('mod', status='d')
        mod.join_group(group)

        #create a regular user
        reg = self.create_user('reg')
        reg.join_group(group)
        #assert that moderator has a notification
        acts = models.Activity.objects.filter(
            user=reg,
            activity_type=const.TYPE_ACTIVITY_ASK_TO_JOIN_GROUP,
            object_id=group.id)
        self.assertEqual(acts.count(), 1)
        self.assertEqual(acts[0].recipients.count(), 1)
        recipient = acts[0].recipients.all()[0]
        self.assertEqual(recipient, mod)
Пример #11
0
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 = models.Group.objects.get_global_group()
        group_slug = slugify(default_group.name)
        new_url = reverse('users_by_group',
                          kwargs={
                              'group_id': default_group.id,
                              'group_slug': group_slug
                          })
        return HttpResponseRedirect(new_url)

    users = models.User.objects.exclude(status='b')
    group = None
    group_email_moderation_enabled = False
    user_acceptance_level = 'closed'
    user_membership_level = 'none'
    if by_group == True:
        if askbot_settings.GROUPS_ENABLED == False:
            raise Http404
        if group_id:
            if all((group_id, group_slug)) == False:
                return HttpResponseRedirect('groups')
            else:
                try:
                    group = models.Group.objects.get(id=group_id)
                    group_email_moderation_enabled = \
                        (
                            askbot_settings.GROUP_EMAIL_ADDRESSES_ENABLED \
                            and askbot_settings.ENABLE_CONTENT_MODERATION
                        )
                    user_acceptance_level = group.get_openness_level_for_user(
                        request.user)
                except models.Group.DoesNotExist:
                    raise Http404
                if group_slug == slugify(group.name):
                    #filter users by full group memberships
                    #todo: refactor as Group.get_full_members()
                    full_level = models.GroupMembership.FULL
                    memberships = models.GroupMembership.objects.filter(
                        group=group, level=full_level)
                    user_ids = memberships.values_list('user__id', flat=True)
                    users = users.filter(id__in=user_ids)
                    if request.user.is_authenticated():
                        membership = request.user.get_group_membership(group)
                        if membership:
                            user_membership_level = membership.get_level_display(
                            )

                else:
                    group_page_url = reverse('users_by_group',
                                             kwargs={
                                                 'group_id': group.id,
                                                 'group_slug':
                                                 slugify(group.name)
                                             })
                    return HttpResponseRedirect(group_page_url)

    is_paginated = True

    sortby = request.GET.get('sort', 'reputation')
    if askbot_settings.KARMA_MODE == 'private' and sortby == 'reputation':
        sortby = 'newest'

    try:
        page = int(request.GET.get('page', '1'))
    except ValueError:
        page = 1

    search_query = request.GET.get('query', "")
    if search_query == "":
        if sortby == "newest":
            order_by_parameter = '-date_joined'
        elif sortby == "last":
            order_by_parameter = 'date_joined'
        elif sortby == "user":
            order_by_parameter = 'username'
        else:
            # default
            order_by_parameter = '-reputation'

        objects_list = Paginator(users.order_by(order_by_parameter),
                                 const.USERS_PAGE_SIZE)
        base_url = request.path + '?sort=%s&' % sortby
    else:
        sortby = "reputation"
        matching_users = models.get_users_by_text_query(search_query, users)
        objects_list = Paginator(matching_users.order_by('-reputation'),
                                 const.USERS_PAGE_SIZE)
        base_url = request.path + '?name=%s&sort=%s&' % (search_query,
                                                                 sortby)

    try:
        users_page = objects_list.page(page)
    except (EmptyPage, InvalidPage):
        users_page = objects_list.page(objects_list.num_pages)

    paginator_data = {
        'is_paginated': is_paginated,
        'pages': objects_list.num_pages,
        'page': page,
        'has_previous': users_page.has_previous(),
        'has_next': users_page.has_next(),
        'previous': users_page.previous_page_number(),
        'next': users_page.next_page_number(),
        'base_url': base_url
    }
    paginator_context = functions.setup_paginator(paginator_data)  #

    #todo: move to contexts
    #extra context for the groups
    if askbot_settings.GROUPS_ENABLED:
        #todo: cleanup this branched code after groups are migrated to auth_group
        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
        group_openness_choices = models.Group().get_openness_choices()
    else:
        user_groups = None
        group_openness_choices = None

    data = {
        'active_tab': 'users',
        'page_class': 'users-page',
        'users': users_page,
        'group': group,
        'search_query': search_query,
        'tab_id': sortby,
        'paginator_context': paginator_context,
        'group_email_moderation_enabled': group_email_moderation_enabled,
        'user_acceptance_level': user_acceptance_level,
        'user_membership_level': user_membership_level,
        'user_groups': user_groups,
        'group_openness_choices': group_openness_choices
    }

    return render(request, 'users.html', data)
Пример #12
0
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 = models.Group.objects.get_global_group()
        group_slug = slugify(default_group.name)
        new_url = reverse('users_by_group',
                          kwargs={
                              'group_id': default_group.id,
                              'group_slug': group_slug
                          })
        return HttpResponseRedirect(new_url)

    users = models.User.objects.exclude(askbot_profile__status='b').exclude(
        is_active=False).select_related('askbot_profile')

    if askbot.is_multilingual():
        users = users.filter(
            localized_askbot_profiles__language_code=get_language(),
            localized_askbot_profiles__is_claimed=True)

    group = None
    group_email_moderation_enabled = False
    user_acceptance_level = 'closed'
    user_membership_level = 'none'
    if by_group == True:
        if askbot_settings.GROUPS_ENABLED == False:
            raise Http404
        if group_id:
            if all((group_id, group_slug)) == False:
                return HttpResponseRedirect('groups')
            else:
                try:
                    group = models.Group.objects.get(id=group_id)
                    group_email_moderation_enabled = (
                        askbot_settings.GROUP_EMAIL_ADDRESSES_ENABLED \
                        and askbot_settings.CONTENT_MODERATION_MODE == 'premoderation'
                    )
                    user_acceptance_level = group.get_openness_level_for_user(
                        request.user)
                except models.Group.DoesNotExist:
                    raise Http404
                if group_slug == slugify(group.name):
                    #filter users by full group memberships
                    #todo: refactor as Group.get_full_members()
                    full_level = models.GroupMembership.FULL
                    memberships = models.GroupMembership.objects.filter(
                        group=group, level=full_level)
                    user_ids = memberships.values_list('user__id', flat=True)
                    users = users.filter(id__in=user_ids)
                    if request.user.is_authenticated():
                        membership = request.user.get_group_membership(group)
                        if membership:
                            user_membership_level = membership.get_level_display(
                            )

                else:
                    group_page_url = reverse('users_by_group',
                                             kwargs={
                                                 'group_id': group.id,
                                                 'group_slug':
                                                 slugify(group.name)
                                             })
                    return HttpResponseRedirect(group_page_url)

    is_paginated = True

    form = forms.ShowUsersForm(request.REQUEST)
    form.full_clean()  #always valid
    sort_method = form.cleaned_data['sort']
    page = form.cleaned_data['page']
    search_query = form.cleaned_data['query']

    if search_query == '':
        if sort_method == 'newest':
            order_by_parameter = '-date_joined'
        elif sort_method == 'last':
            order_by_parameter = 'date_joined'
        elif sort_method == 'name':
            order_by_parameter = 'username'
        else:
            # default
            if askbot.is_multilingual():
                order_by_parameter = '-localized_askbot_profiles__reputation'
            else:
                order_by_parameter = '-askbot_profile__reputation'

        objects_list = Paginator(users.order_by(order_by_parameter),
                                 askbot_settings.USERS_PAGE_SIZE)
        base_url = request.path + '?sort=%s&' % sort_method
    else:
        sort_method = 'reputation'
        matching_users = models.get_users_by_text_query(search_query, users)
        objects_list = Paginator(
            matching_users.order_by('-askbot_profile__reputation'),
            askbot_settings.USERS_PAGE_SIZE)
        base_url = request.path + '?name=%s&sort=%s&' % (search_query,
                                                         sort_method)

    try:
        users_page = objects_list.page(page)
    except (EmptyPage, InvalidPage):
        users_page = objects_list.page(objects_list.num_pages)

    paginator_data = {
        'is_paginated': is_paginated,
        'pages': objects_list.num_pages,
        'current_page_number': page,
        'page_object': users_page,
        'base_url': base_url
    }
    paginator_context = functions.setup_paginator(paginator_data)  #

    #todo: move to contexts
    #extra context for the groups
    if askbot_settings.GROUPS_ENABLED:
        #todo: cleanup this branched code after groups are migrated to auth_group
        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
        group_openness_choices = models.Group().get_openness_choices()
    else:
        user_groups = None
        group_openness_choices = None

    data = {
        'active_tab': 'users',
        'group': group,
        'group_email_moderation_enabled': group_email_moderation_enabled,
        'group_openness_choices': group_openness_choices,
        'page_class': 'users-page',
        'paginator_context': paginator_context,
        'search_query': search_query,
        'tab_id': sort_method,
        'user_acceptance_level': user_acceptance_level,
        'user_count': objects_list.count,
        'user_groups': user_groups,
        'user_membership_level': user_membership_level,
        'users': users_page,
    }

    return render(request, 'users.html', data)