コード例 #1
0
def react_questions(context, obj):
    request = context['request']

    user = request.user
    is_moderator = user.has_perm('a4_candy_questions.moderate_questions', obj)
    categories = [category.name for category in obj.category_set.all()]
    questions_api_url = reverse('questions-list', kwargs={'module_pk': obj.pk})
    present_url = \
        reverse('question-present',
                kwargs={'module_slug': obj.slug,
                        'organisation_slug': obj.project.organisation.slug})

    like_permission = 'a4_candy_likes.add_like_model'
    has_liking_permission = user.has_perm(like_permission, obj)
    would_have_liking_permission = NormalUser().would_have_perm(
        like_permission, obj)

    ask_permissions = 'a4_candy_questions.propose_question'
    has_ask_questions_permissions = user.has_perm(ask_permissions, obj)
    would_have_ask_questions_permission = NormalUser().would_have_perm(
        ask_permissions, obj)

    attributes = {
        'information':
        obj.description,
        'questions_api_url':
        questions_api_url,
        'present_url':
        present_url,
        'isModerator':
        is_moderator,
        'categories':
        categories,
        'hasLikingPermission': (has_liking_permission
                                or would_have_liking_permission),
        'hasAskQuestionsPermission': (has_ask_questions_permissions
                                      or would_have_ask_questions_permission),
        'askQuestionUrl':
        reverse('question-create',
                kwargs={
                    'slug': obj.slug,
                    'organisation_slug': obj.project.organisation.slug
                })
    }

    return format_html(
        '<div data-speakup-widget="questions" '
        'data-attributes="{attributes}"></div>',
        attributes=json.dumps(attributes))
コード例 #2
0
ファイル: react_questions.py プロジェクト: liqd/a4-speakup
def react_questions(context, obj):
    request = context['request']

    user = request.user
    is_moderator = user.has_perm('a4_candy_questions.moderate_questions', obj)
    categories = [category.name for category in obj.category_set.all()]
    questions_api_url = reverse('questions-list', kwargs={'module_pk': obj.pk})

    permission = 'a4_candy_likes.add_like_model'
    has_liking_permission = user.has_perm(permission, obj)
    would_have_liking_permission = NormalUser().would_have_perm(
        permission, obj)

    attributes = {
        'questions_api_url':
        questions_api_url,
        'isModerator':
        is_moderator,
        'categories':
        categories,
        'hasLikingPermission': (has_liking_permission
                                or would_have_liking_permission)
    }

    return format_html(
        '<div data-speakup-widget="questions" '
        'data-attributes="{attributes}"></div>',
        attributes=json.dumps(attributes))
コード例 #3
0
 def is_cta_enabled(self):
     if self.cta_object:
         has_or_would_have_perm = (self.request.user.has_perm(
             self.cta_permission,
             self.cta_object) or NormalUser().would_have_perm(
                 self.cta_permission, self.cta_object))
         return has_or_would_have_perm
     else:
         return False
コード例 #4
0
    def get_is_read_only(self, question):
        if 'request' in self.context:
            user = self.context['request'].user
            has_poll_permission = user.has_perm('meinberlin_polls.add_vote',
                                                question.poll.module)
            would_have_poll_permission = NormalUser().would_have_perm(
                'meinberlin_polls.add_vote', question.poll.module)
            return not has_poll_permission and not would_have_poll_permission

        return True
コード例 #5
0
def react_comments(context, obj):
    request = context['request']

    serializer = ThreadSerializer(
        obj.comments.all().order_by('-created'),
        many=True, context={'request': request})
    comments = serializer.data

    user = request.user
    is_authenticated = bool(user.is_authenticated)
    is_moderator = user.is_superuser or user in obj.project.moderators.all()
    user_name = user.username

    contenttype = ContentType.objects.get_for_model(obj)
    permission = '{ct.app_label}.comment_{ct.model}'.format(ct=contenttype)
    has_comment_permission = user.has_perm(permission, obj)

    would_have_comment_permission = NormalUser().would_have_perm(
        permission, obj
    )

    comments_contenttype = ContentType.objects.get_for_model(Comment)
    pk = obj.pk

    attributes = {
        'comments': comments,
        'comments_contenttype': comments_contenttype.pk,
        'subjectType': contenttype.pk,
        'subjectId': pk,
        'isAuthenticated': is_authenticated,
        'isModerator': is_moderator,
        'user_name': user_name,
        'isReadOnly': (not has_comment_permission and
                       not would_have_comment_permission),
        'isContextMember': (is_context_member(user, obj)
                            or is_context_member(NormalUser(), obj))
    }

    return format_html(
        '<div data-a4-widget="comment" data-attributes="{attributes}"></div>',
        attributes=json.dumps(attributes)
    )
コード例 #6
0
def has_or_would_have_perm(perm, user, obj=None):
    """
    Check if the NormalUser has the given permission.
    This checks only permissions defined with django-rules.
    """
    if not hasattr(user, 'has_perm'):  # pragma: no cover
        return False  # swapped user model that doesn't support permissions
    elif user.is_authenticated:
        return user.has_perm(perm, obj)
    else:
        return NormalUser().would_have_perm(perm, obj)
コード例 #7
0
def react_ratings(context, obj):
    request = context['request']
    user = request.user

    contenttype = ContentType.objects.get_for_model(obj)
    permission = '{ct.app_label}.rate_{ct.model}'.format(ct=contenttype)
    has_rate_permission = user.has_perm(permission, obj)

    would_have_rate_permission = NormalUser().would_have_perm(
        permission, obj
    )

    if user.is_authenticated:
        authenticated_as = user.username
    else:
        authenticated_as = None
    user_rating = rating_models.Rating.objects.filter(
        content_type=contenttype, object_pk=obj.pk, creator=user.pk).first()
    if user_rating:
        user_rating_value = user_rating.value
        user_rating_id = user_rating.pk
    else:
        user_rating_value = None
        user_rating_id = -1

    attributes = {
        'contentType': contenttype.pk,
        'objectId': obj.pk,
        'authenticatedAs': authenticated_as,
        'positiveRatings': obj.positive_rating_count,
        'negativeRatings': obj.negative_rating_count,
        'userRating': user_rating_value,
        'userRatingId': user_rating_id,
        'isReadOnly': (not has_rate_permission and
                       not would_have_rate_permission),
        'style': 'ideas',
    }

    return format_html(
        '<div data-a4-widget="ratings" data-attributes="{attributes}"></div>',
        attributes=json.dumps(attributes)
    )
コード例 #8
0
def react_comments_async(context, obj, with_categories=False):
    request = context['request']
    user = request.user
    is_authenticated = bool(user.is_authenticated)
    is_moderator = user.is_superuser or user in obj.project.moderators.all()
    user_name = str(user.id)

    anchoredCommentId = request.GET.get('comment', '')

    contenttype = ContentType.objects.get_for_model(obj)
    permission = '{ct.app_label}.comment_{ct.model}'.format(ct=contenttype)
    has_comment_permission = user.has_perm(permission, obj)

    would_have_comment_permission = NormalUser().would_have_perm(
        permission, obj)

    comments_contenttype = ContentType.objects.get_for_model(Comment)
    pk = obj.pk

    comments_api_url = reverse('comments-list',
                               kwargs={
                                   'content_type': contenttype.pk,
                                   'object_pk': obj.pk
                               })

    with_categories = bool(with_categories)

    comment_category_choices = {}
    if with_categories:
        comment_category_choices = getattr(settings, 'A4_COMMENT_CATEGORIES',
                                           None)
        if comment_category_choices:
            comment_category_choices = dict(
                (x, str(y)) for x, y in comment_category_choices)
        else:
            raise ImproperlyConfigured('set A4_COMMENT_CATEGORIES in settings')
    '''
    isReadOnly - true if phase does not allow comment or project is non-public
                 and user is not participant or project is draft
                 (negation of modules.predicates.is_allowed_comment_item)
    isContextMember - true if project is public or user is
                      participant/moderator/org_member
    '''
    attributes = {
        'commentsApiUrl':
        comments_api_url,
        'comments_contenttype':
        comments_contenttype.pk,
        'subjectType':
        contenttype.pk,
        'subjectId':
        pk,
        'isAuthenticated':
        is_authenticated,
        'isModerator':
        is_moderator,
        'user_name':
        user_name,
        'isReadOnly': (not has_comment_permission
                       and not would_have_comment_permission),
        'commentCategoryChoices':
        comment_category_choices,
        'anchoredCommentId':
        anchoredCommentId,
        'withCategories':
        with_categories,
        'isContextMember': (is_context_member(user, obj)
                            or is_context_member(NormalUser(), obj))
    }

    return format_html(
        '<div data-a4-widget="comment_async" '
        'data-attributes="{attributes}"></div>',
        attributes=json.dumps(attributes))
コード例 #9
0
def would_have_perm(perm, obj=None):
    """
    Check if the NormalUser has the given permission.
    This checks only permissions defined with django-rules.
    """
    return NormalUser().would_have_perm(perm, obj)
コード例 #10
0
def react_interactiveevents(context, obj):
    request = context['request']

    user = request.user
    is_moderator = \
        user.has_perm('a4_candy_interactive_events.moderate_livequestions',
                      obj)
    categories = [category.name for category in obj.category_set.all()]
    category_dict = {category.pk: category.name
                     for category in obj.category_set.all()}
    questions_api_url = reverse('interactiveevents-list',
                                kwargs={'module_pk': obj.pk})

    private_policy_label = str(_('I confirm that I have read and accepted the '
                                 '{}terms of use{} and the {}data protection '
                                 'policy{}.'))

    terms_of_use_url = get_important_page_url('terms_of_use')
    data_protection_policy_url = \
        get_important_page_url('data_protection_policy')

    likes_api_url = '/api/livequestions/LIVEQUESTIONID/likes/'
    present_url = \
        reverse('question-present',
                kwargs={'module_slug': obj.slug,
                        'organisation_slug': obj.project.organisation.slug})

    like_permission = 'a4_candy_interactive_events.add_like_model'
    has_liking_permission = user.has_perm(
        like_permission, obj)
    would_have_liking_permission = NormalUser().would_have_perm(
        like_permission, obj
    )

    ask_permissions = 'a4_candy_interactive_events.add_livequestion'
    has_ask_questions_permissions = user.has_perm(ask_permissions, obj)
    would_have_ask_questions_permission = NormalUser().would_have_perm(
        ask_permissions, obj)

    attributes = {
        'information': obj.description,
        'questions_api_url': questions_api_url,
        'likes_api_url': likes_api_url,
        'present_url': present_url,
        'isModerator': is_moderator,
        'categories': categories,
        'category_dict': category_dict,
        'hasLikingPermission': (has_liking_permission
                                or would_have_liking_permission),
        'hasAskQuestionsPermission': (has_ask_questions_permissions
                                      or would_have_ask_questions_permission),
        'privatePolicyLabel': private_policy_label,
        'termsOfUseUrl': terms_of_use_url,
        'dataProtectionPolicyUrl': data_protection_policy_url
    }

    return format_html(
        '<div data-aplus-widget="questions" '
        'data-attributes="{attributes}"></div>',
        attributes=json.dumps(attributes)
    )