Ejemplo n.º 1
0
 def test_set_preferred_communities(self):
     from zope.interface import Interface
     from opencore.models.interfaces import ICommunityInfo
     from opencore.views.communities import get_preferred_communities
     context = testing.DummyModel()
     yo = testing.DummyModel()
     yo.title = 'Yo'
     yi = testing.DummyModel()
     yi.title = 'Yi'
     context['yo'] = yo
     context['yi'] = yi
     profiles = context['profiles'] = testing.DummyModel()
     foo = profiles['foo'] = testing.DummyModel()
     foo.preferred_communities = None
     request = testing.DummyRequest()
     testing.registerDummySecurityPolicy(
         'foo',
         groupids=[
         'group.community:yo:members',
         'group.community:yo:moderators',
         'group.community:yi:moderators',
         'group.community:yang:moderators'
         ]
         )
     testing.registerAdapter(DummyAdapter, (Interface, Interface),
                             ICommunityInfo)
     communities = ['yi']
     self._callFUT(context, request, communities)
     result = get_preferred_communities(context, request)
     self.assertEqual(len(result), 1)
     self.assertEqual(result[0], 'yi')
Ejemplo n.º 2
0
 def _callFUT(self, context, request):
     from opencore.views.communities import get_preferred_communities
     return get_preferred_communities(context, request)
Ejemplo n.º 3
0
    def __call__(self):

        profile_details = getUtility(IProfileDict, name='profile-details')
        profile_details.update_details(self.context, self.request, self.api,
                                       self.photo_thumb_size)

        context = self.context
        request = self.request
        api = self.api

        # provide client data for rendering current tags in the tagbox
        client_json_data = dict(
            tagbox = get_tags_client_data(context, request),
            )

        # Get communities this user is a member of, along with moderator info
        #
        communities = {}
        communities_folder = find_communities(context)
        user_info = find_users(context).get_by_id(context.__name__)
        if user_info is not None:
            for group in user_info["groups"]:
                if group.startswith("group.community:"):
                    unused, community_name, role = group.split(":")
                    if (communities.has_key(community_name) and
                        role != "moderators"):
                        continue

                    community = communities_folder.get(community_name, None)
                    if community is None:
                        continue

                    if has_permission('view', community, request):
                        communities[community_name] = {
                            "title": community.title,
                            "moderator": role == "moderators",
                            "url": model_url(community, request),
                        }

        communities = communities.values()
        communities.sort(key=lambda x:x["title"])

        preferred_communities = []
        my_communities = None
        name = context.__name__
        # is this the current user's profile?
        if authenticated_userid(request) == name:
            preferred_communities = get_preferred_communities(communities_folder,
                                                              request)
            my_communities = get_my_communities(communities_folder, request)

        tagger = find_tags(context)
        if tagger is None:
            tags = ()
        else:
            tags = []
            names = tagger.getTags(users=[context.__name__])
            for name, count in sorted(tagger.getFrequency(names,
                                                          user=context.__name__),
                                      key=lambda x: x[1],
                                      reverse=True,
                                     )[:10]:
                tags.append({'name': name, 'count': count})



        self.profile = profile_details
        self.communities = communities
        self.my_communities = my_communities or []
        self.preferred_communities = preferred_communities
        self.tags = tags
        self.actions = get_profile_actions(context,request)
        self.head_data = convert_to_script(client_json_data)
        return self.make_response()