def visit(self, context): if IProfile.providedBy(context): users = find_users(context) person = self._get_person(context.__name__) person['first_last'] = ' '.join( (context.firstname, context.lastname)) person['create_date'] = context.created person['is_staff'] = users.member_of_group( context.__name__, 'group.KarlStaff') person['location'] = context.location person['department'] = context.department tags = find_tags(context) person['tags'] = len(tags.getTags(users=[context.__name__])) elif ICommunity.providedBy(context): for id in context.member_names: self._get_person(id)['membership'] += 1 for id in context.moderator_names: self._get_person(id)['communities_moderator'] += 1 else: creator = getattr(context, 'creator', None) if creator is not None: person = self._get_person(creator) person['content_created'] += 1 if context.created > self.one_month_ago: person['created_this_month'] += 1
def get_groups(obj, default): if not IProfile.providedBy(obj): return default users = find_users(obj) user = users.get_by_id(obj.__name__) if user: return user.get('groups', default) return default
def __call__(self, obj, default): if not IProfile.providedBy(obj): return default categories = getattr(obj, 'categories', None) if not categories: return default values = categories.get(self.catid) if not values: return default return values
def reindex_peopledirectory(peopledir): catalog = peopledir.catalog document_map = catalog.document_map profiles = find_profiles(peopledir) for obj in profiles.values(): if IProfile.providedBy(obj): path = model_path(obj) docid = document_map.docid_for_address(path) if not docid: docid = document_map.add(path) catalog.index_doc(docid, obj) else: catalog.reindex_doc(docid, obj)
def reindex_peopledirectory(peopledir): catalog = peopledir.catalog document_map = catalog.document_map profiles = find_profiles(peopledir) for obj in profiles.values(): if IProfile.providedBy(obj): path = resource_path(obj) docid = document_map.docid_for_address(path) if not docid: docid = document_map.add(path) catalog.index_doc(docid, obj) else: catalog.reindex_doc(docid, obj)
def index_profile(obj, event): """ Index profile (an IObjectAddedEvent subscriber) """ catalog = find_peopledirectory_catalog(obj) if catalog is not None: for node in postorder(obj): if IProfile.providedBy(node): path = resource_path(node) docid = getattr(node, 'docid', None) if docid is None: docid = node.docid = catalog.document_map.add(path) else: catalog.document_map.add(path, docid) catalog.index_doc(docid, node)
def get_lastname_firstletter(obj, default): if not IProfile.providedBy(obj): return default return obj.lastname[:1].upper()
def manage_communities_view(context, request): assert IProfile.providedBy(context) page_title = 'Manage Communities' api = TemplateAPI(context, request, page_title) users = find_users(context) communities_folder = find_communities(context) userid = context.__name__ # Handle cancel if request.params.get("form.cancel", False): return HTTPFound(location=resource_url(context, request)) # Handle form submission if request.params.get("form.submitted", False): for key in request.params: if key.startswith("leave_"): community_name = key[6:] community = communities_folder[community_name] # Not concerned about catching this exception, since checkbox # should not have been displayed in form unless user allowed # to leave. Assert merely guards integrity of the system. assert may_leave(userid, community) if userid in community.moderator_names: users.remove_group(userid, community.moderators_group_name) if userid in community.member_names: users.remove_group(userid, community.members_group_name) elif key.startswith("alerts_pref_"): community_name = key[12:] preference = int(request.params.get(key)) context.set_alerts_preference(community_name, preference) context.alert_attachments = request.params.get('attachments', 'link') path = resource_url(context, request) msg = '?status_message=Community+preferences+updated.' return HTTPFound(location=path+msg) # XXX Iterating over every community in the system isn't a particularly # efficient solution. Should use catalog. communities = [] for community in communities_folder.values(): if (userid in community.member_names or userid in community.moderator_names): alerts_pref = context.get_alerts_preference(community.__name__) display_community = { 'name': community.__name__, 'title': community.title, 'alerts_pref': [ { "value": IProfile.ALERT_IMMEDIATELY, "label": "Immediately", "selected": alerts_pref == IProfile.ALERT_IMMEDIATELY, }, { "value": IProfile.ALERT_DIGEST, "label": "Digest", "selected": alerts_pref == IProfile.ALERT_DIGEST, }, { "value": IProfile.ALERT_NEVER, "label": "Never", "selected": alerts_pref == IProfile.ALERT_NEVER, }], 'may_leave': may_leave(userid, community), } communities.append(display_community) if len(communities) > 1: communities.sort(key=lambda x: x["title"]) return render_to_response( 'karl.views:templates/manage_communities.pt', dict(api=api, communities=communities, post_url=request.url, formfields=api.formfields, attachments=context.alert_attachments), request=request, )
def get_member_name(object, default): if not IProfile.providedBy(object): return default return ('%s %s' % (object.firstname, object.lastname)).lower()
def batch_images( context, request, get_image_info=get_image_info, # unittest get_images_batch=get_images_batch): # unittest include_image_url = request.params.get('include_image_url', None) # include_image_url is a special case. include_info = None if include_image_url is not None: # Note, we must use the path only, as IE submits the full domain # and without the next line IE would fail. path = urlparse.urlparse(include_image_url)[2] include_context = traverse(context, path)['context'] if IImage.providedBy(include_context): # We have a good image to include. include_info = get_image_info(include_context, request) # Find query parameters based on the 'source' param, # which signifies the selection index of the source button # in the imagedrawer dialog. source = request.params.get('source') assert source in ('myrecent', 'thiscommunity', 'allkarl') if source == 'myrecent': creator = authenticated_userid(request) community_path = None elif source == 'thiscommunity': # If we're editing a profile, show that user's images if IProfile.providedBy(context): creator = context.__name__ community = None else: creator = None community = find_community(context) # batching api requires the community path if community: community_path = resource_path(community) else: community_path = None else: # All Karl creator = None community_path = None # batching # Decide start and size here, don't let the lower levels # apply their default. This allows us to enforce # a MINIMAL_BATCH size. batch_start = int(request.params.get('start', '0')) batch_size = int(request.params.get('limit', '0')) # there is a minimal batch size to enforce, if the client # does not ask for one # Just pass the values to lower levels where sensible # defaults will be applied. sort_index = request.params.get('sort_on', None) reverse = request.params.get('reverse', None) # XXX include_image will now be inserted in the first # position, as extra image. insert_extra = False if include_info is not None: if batch_start == 0: batch_size -= 1 insert_extra = True else: batch_start -= 1 # Enforce the minimal batch size batch_size = max(batch_size, MINIMAL_BATCH) search_params = dict( creator=creator, community=community_path, batch_start=batch_start, batch_size=batch_size, ) if sort_index: search_params['sort_index'] = sort_index if reverse: search_params['reverse'] = bool(int(reverse)) batch_info = get_images_batch(context, request, **search_params) records = [ get_image_info(image, request) for image in batch_info['entries'] ] start = batch_info['batch_start'] totalRecords = batch_info['total'] # add the fake included image if include_info is not None: totalRecords += 1 if insert_extra: records.insert(0, include_info) else: start += 1 return dict( records=records, start=start, totalRecords=totalRecords, )
def get_lastfirst(object, default): if not IProfile.providedBy(object): return default return ("%s, %s" % (object.lastname, object.firstname)).lower()
def batch_images(context, request, get_image_info=get_image_info, # unittest get_images_batch=get_images_batch): # unittest include_image_url = request.params.get('include_image_url', None) # include_image_url is a special case. include_info = None if include_image_url is not None: # Note, we must use the path only, as IE submits the full domain # and without the next line IE would fail. path = urlparse.urlparse(include_image_url)[2] include_context = traverse(context, path)['context'] if IImage.providedBy(include_context): # We have a good image to include. include_info = get_image_info(include_context, request) # Find query parameters based on the 'source' param, # which signifies the selection index of the source button # in the imagedrawer dialog. source = request.params.get('source') assert source in ('myrecent', 'thiscommunity', 'allkarl') if source == 'myrecent': creator = authenticated_userid(request) community_path = None elif source == 'thiscommunity': # If we're editing a profile, show that user's images if IProfile.providedBy(context): creator = context.__name__ community = None else: creator = None community = find_community(context) # batching api requires the community path if community: community_path = resource_path(community) else: community_path = None else: # All Karl creator = None community_path = None # batching # Decide start and size here, don't let the lower levels # apply their default. This allows us to enforce # a MINIMAL_BATCH size. batch_start = int(request.params.get('start', '0')) batch_size = int(request.params.get('limit', '0')) # there is a minimal batch size to enforce, if the client # does not ask for one # Just pass the values to lower levels where sensible # defaults will be applied. sort_index = request.params.get('sort_on', None) reverse = request.params.get('reverse', None) # XXX include_image will now be inserted in the first # position, as extra image. insert_extra = False if include_info is not None: if batch_start == 0: batch_size -= 1 insert_extra = True else: batch_start -= 1 # Enforce the minimal batch size batch_size = max(batch_size, MINIMAL_BATCH) search_params = dict( creator=creator, community=community_path, batch_start=batch_start, batch_size=batch_size, ) if sort_index: search_params['sort_index'] = sort_index if reverse: search_params['reverse'] = bool(int(reverse)) batch_info = get_images_batch( context, request, **search_params ) records = [get_image_info(image, request) for image in batch_info['entries']] start = batch_info['batch_start'] totalRecords = batch_info['total'] # add the fake included image if include_info is not None: totalRecords += 1 if insert_extra: records.insert(0, include_info) else: start += 1 return dict( records = records, start = start, totalRecords = totalRecords, )