Пример #1
0
    def test_default_organization(self, db_session, routes, pyramid_request):
        organization = Organization.default(db_session)
        organization_resource = OrganizationResource(organization, pyramid_request)

        presenter = OrganizationJSONPresenter(organization_resource)
        presented = presenter.asdict()

        assert presented['default'] is True
Пример #2
0
    def test_default_organization(self, db_session, routes, pyramid_request):
        organization = Organization.default(db_session)
        organization_context = OrganizationContext(organization, pyramid_request)

        presenter = OrganizationJSONPresenter(organization_context)
        presented = presenter.asdict()

        assert presented["default"] is True
Пример #3
0
    def test_default_organization(self, db_session, routes, pyramid_request):
        organization = Organization.default(db_session)
        organization_context = OrganizationContext(organization,
                                                   pyramid_request)

        presenter = OrganizationJSONPresenter(organization_context)
        presented = presenter.asdict()

        assert presented["default"] is True
Пример #4
0
    def test_organization_asdict_with_logo(self, factories, routes, pyramid_request):
        organization = factories.Organization(name='My Org', logo='<svg>H</svg>')
        organization_resource = OrganizationResource(organization, pyramid_request)

        presenter = OrganizationJSONPresenter(organization_resource)

        assert presenter.asdict() == {
            'name': 'My Org',
            'id': organization_resource.id,
            'default': False,
            'logo': pyramid_request.route_url('organization_logo', pubid=organization.pubid)
        }
Пример #5
0
    def test_organization_asdict_no_logo(self, factories, pyramid_request):
        organization = factories.Organization(name='My Org', logo=None)
        organization_resource = OrganizationResource(organization, pyramid_request)

        presenter = OrganizationJSONPresenter(organization_resource)

        assert presenter.asdict() == {
            'name': 'My Org',
            'id': organization.pubid,
            'default': False,
            'logo': None,
        }
Пример #6
0
    def test_organization_asdict_no_logo(self, factories, pyramid_request):
        organization = factories.Organization(name="My Org", logo=None)
        organization_context = OrganizationContext(organization, pyramid_request)

        presenter = OrganizationJSONPresenter(organization_context)

        assert presenter.asdict() == {
            "name": "My Org",
            "id": organization.pubid,
            "default": False,
            "logo": None,
        }
Пример #7
0
    def test_organization_asdict_no_logo(self, factories, pyramid_request):
        organization = factories.Organization(name="My Org", logo=None)
        organization_context = OrganizationContext(organization,
                                                   pyramid_request)

        presenter = OrganizationJSONPresenter(organization_context)

        assert presenter.asdict() == {
            "name": "My Org",
            "id": organization.pubid,
            "default": False,
            "logo": None,
        }
Пример #8
0
    def test_organization_asdict_with_logo(self, factories, routes, pyramid_request):
        organization = factories.Organization(name="My Org", logo="<svg>H</svg>")
        organization_context = OrganizationContext(organization, pyramid_request)

        presenter = OrganizationJSONPresenter(organization_context)

        assert presenter.asdict() == {
            "name": "My Org",
            "id": organization_context.id,
            "default": False,
            "logo": pyramid_request.route_url(
                "organization_logo", pubid=organization.pubid
            ),
        }
Пример #9
0
    def test_organization_asdict_with_logo(self, factories, routes,
                                           pyramid_request):
        organization = factories.Organization(name="My Org",
                                              logo="<svg>H</svg>")
        organization_context = OrganizationContext(organization,
                                                   pyramid_request)

        presenter = OrganizationJSONPresenter(organization_context)

        assert presenter.asdict() == {
            "name":
            "My Org",
            "id":
            organization_context.id,
            "default":
            False,
            "logo":
            pyramid_request.route_url("organization_logo",
                                      pubid=organization.pubid),
        }
Пример #10
0
    def _expand(self, model, expand):
        if "organization" in expand and self.group.organization:
            model["organization"] = OrganizationJSONPresenter(
                self.group.organization, self.request).asdict()

        if "scopes" in expand:
            model["scopes"] = {
                # Groups in the DB have an `enforce_scope` property (default
                # True), but URL enforcement for annotations only happens if
                # there are scopes to restrict to. So the API value requires
                # both to be true.
                "enforced": bool(self.group.enforce_scope
                                 and self.group.scopes),
                # Format scopes to be the scope with a wild-card suffix so we
                # can make the scopes more granular later.
                "uri_patterns":
                [scope.scope + "*" for scope in self.group.scopes],
            }
Пример #11
0
 def _expand(self, model, expand):
     if "organization" in expand:
         if self.organization_context:
             model["organization"] = OrganizationJSONPresenter(
                 self.organization_context).asdict()
     if "scopes" in expand:
         model["scopes"] = {}
         # The API representation of scope enforcement differs from the DB
         # representation. All groups have an `enforce_scope` property, and
         # it defaults to True. However, URL enforcement for incoming
         # annotations only happens if there are 1 or more scopes to restrict
         # to. Therefore, the API representation of this property is False
         # if there are no scopes.
         model["scopes"]["enforced"] = (self.group.enforce_scope
                                        if self.group.scopes else False)
         # At this presentation layer, format scopes to look like
         # patterns—currently a simple wildcarded prefix—to give us more
         # flexibility in making scope more granular later
         model["scopes"]["uri_patterns"] = [
             scope.scope + "*" for scope in self.group.scopes
         ]
     return model
Пример #12
0
    def search(self):  # pylint: disable=too-complex
        result = self._check_access_permissions()
        if result is not None:
            return result

        check_slug(self.group, self.request)

        result = super().search()

        result["opts"] = {"search_groupname": self.group.name}

        # If the group has read access only for members  and the user is not in that list
        # return without extra info.
        if self.group.readable_by == ReadableBy.members and (
                self.request.user not in self.group.members):
            return result

        def user_annotation_count(aggregation, userid):
            for user in aggregation:
                if user["user"] == userid:
                    return user["count"]
            return 0

        members = []
        moderators = []
        users_aggregation = result["search_results"].aggregations.get(
            "users", [])
        # If the group has members provide a list of member info,
        # otherwise provide a list of moderator info instead.
        if self.group.members:
            members = [{
                "username":
                u.username,
                "userid":
                u.userid,
                "count":
                user_annotation_count(users_aggregation, u.userid),
                "faceted_by":
                _faceted_by_user(self.request, u.username,
                                 self.parsed_query_params),
            } for u in self.group.members]
            members = sorted(members, key=lambda k: k["username"].lower())
        else:
            moderators = []
            if self.group.creator:
                # Pass a list of moderators, anticipating that [self.group.creator]
                # will change to an actual list of moderators at some point.
                moderators = [{
                    "username":
                    u.username,
                    "userid":
                    u.userid,
                    "count":
                    user_annotation_count(users_aggregation, u.userid),
                    "faceted_by":
                    _faceted_by_user(self.request, u.username,
                                     self.parsed_query_params),
                } for u in [self.group.creator]]
                moderators = sorted(moderators,
                                    key=lambda k: k["username"].lower())

        group_annotation_count = self._get_total_annotations_in_group(
            result, self.request)

        result["stats"] = {"annotation_count": group_annotation_count}
        result["group"] = {
            "created":
            utc_us_style_date(self.group.created),
            "description":
            self.group.description,
            "name":
            self.group.name,
            "pubid":
            self.group.pubid,
            "url":
            self.request.route_url("group_read",
                                   pubid=self.group.pubid,
                                   slug=self.group.slug),
            "share_subtitle":
            _("Share group"),
            "share_msg":
            _("Sharing the link lets people view this group:"),
        }
        if self.group.organization:
            result["group"]["organization"] = OrganizationJSONPresenter(
                self.group.organization, self.request).asdict(summary=True)
        else:
            result["group"]["organization"] = None

        if self.group.type == "private":
            result["group"]["share_subtitle"] = _("Invite new members")
            result["group"]["share_msg"] = _(
                "Sharing the link lets people join this group:")

        result["group_users_args"] = [
            _("Members"),
            moderators if self.group.type == "open" else members,
            self.group.creator.userid if self.group.creator else None,
        ]

        if self.request.has_permission(Permission.Group.EDIT,
                                       context=self.context):
            result["group_edit_url"] = self.request.route_url(
                "group_edit", pubid=self.group.pubid)

        result["more_info"] = "more_info" in self.request.params

        if not result.get("q"):
            result["zero_message"] = Markup(
                _("The group “{name}” has not made any annotations yet.").
                format(name=Markup.escape(self.group.name)))

        result["show_leave_button"] = self.request.user in self.group.members

        return result
Пример #13
0
 def _expand(self, model, expand=[]):
     if 'organization' in expand:
         model['organization'] = OrganizationJSONPresenter(
           self.organization_resource
         ).asdict()
     return model
Пример #14
0
 def presenter(self, organization, pyramid_request):
     return OrganizationJSONPresenter(organization, pyramid_request)
Пример #15
0
 def _expand(self, model, expand):
     if 'organization' in expand:
         if self.organization_context:
             model['organization'] = OrganizationJSONPresenter(
                 self.organization_context).asdict()
     return model