def share_default_templates(portal, templateType, groupOwner, groupTitle): func_results = {'success': True} # Get a list of template ids for items shared with the "default" template group printStatement = '\t-Retrieving list of items in default {} group...' if templateType == 'WEBAPPS': templateTypeStr = 'web app templates' print printStatement.format(templateTypeStr) templateIDs = unpack(portal.webmap_templates(['id'])) elif templateType == 'GALLERY': templateTypeStr = 'gallery templates' print printStatement.format(templateTypeStr) templateIDs = unpack(portal.gallery_templates(['id'])) else: # Template type value is invalid func_results['success'] = False func_results[ 'message'] = 'Invalid templateType value "{}"; must be WEBAPPS or GALLERY.'.format( templateType) return func_results if not templateIDs: func_results['success'] = False func_results[ 'message'] = '{} portal property not set to use "Default" group.'.format( templateTypeStr.capitalize()) return func_results # Get the group id for the group to share the template items groupID = getGroupID(portal, groupOwner, groupTitle) if not groupID: func_results['success'] = False func_results[ 'message'] = 'Could not find group where owner = "{}" and title = "{}"'.format( groupOwner, groupTitle) return func_results # Share templates with group print '\t-Sharing web templates with group {} ({})...'.format( groupTitle, groupOwner) results = share_template_items(portal, templateIDs, [groupID]) if not results['success']: func_results['success'] = False print results['message'] return func_results
def feature_groups(portal, group_ids, clear_existing=False): featured_groups = [] group_ids = unpack(group_ids, 'id') # If we're not clearing all existing groups, get the current set of featured # groups and check to see which groups are already featured. If a group is # already featured, remove it from the list. if not clear_existing: featured_groups = portal.properties().get('featuredGroups') already_featured_ids = [] if featured_groups: for featured_group in featured_groups: if featured_group['id'] in group_ids: _log.info('Group ' + featured_group['id'] + ' is already featured') already_featured_ids.append(featured_group['id']) group_ids = [id for id in group_ids if id not in already_featured_ids] # Add the featured group entries to the array of featured groups (requires # fetching owner and title from the portal) for id in group_ids: group = portal.group(id) featured_groups.append({ 'owner': group['owner'], 'id': id, 'title': group['title'] }) # Update the featured groups property if featured_groups: portal.update_property('featuredGroups', featured_groups)
def feature_groups(portal, group_ids, clear_existing=False): featured_groups = [] group_ids = unpack(group_ids, 'id') # If we're not clearing all existing groups, get the current set of featured # groups and check to see which groups are already featured. If a group is # already featured, remove it from the list. if not clear_existing: featured_groups = portal.properties().get('featuredGroups') already_featured_ids = [] if featured_groups: for featured_group in featured_groups: if featured_group['id'] in group_ids: _log.info('Group ' + featured_group['id'] + ' is already featured') already_featured_ids.append(featured_group['id']) group_ids = [id for id in group_ids if id not in already_featured_ids] # Add the featured group entries to the array of featured groups (requires # fetching owner and title from the portal) for id in group_ids: group = portal.group(id) featured_groups.append({'owner': group['owner'], 'id': id, 'title': group['title']}) # Update the featured groups property if featured_groups: portal.update_property('featuredGroups', featured_groups)
def count_tags(): portal = Portal('http://www.geoplatform.gov') results = portal.search(['tags']) tags = unpack(results, flatten=True) counts = dict((tag, tags.count(tag)) for tag in tags) sorted_counts = sorted(counts.iteritems(), key=itemgetter(1), reverse=True) pprint(sorted_counts, indent=2)
def share_default_templates(portal, templateType, groupOwner, groupTitle): func_results = {'success': True} # Get a list of template ids for items shared with the "default" template group printStatement = '\t-Retrieving list of items in default {} group...' if templateType == 'WEBAPPS': templateTypeStr = 'web app templates' print printStatement.format(templateTypeStr) templateIDs = unpack(portal.webmap_templates(['id'])) elif templateType == 'GALLERY': templateTypeStr = 'gallery templates' print printStatement.format(templateTypeStr) templateIDs = unpack(portal.gallery_templates(['id'])) else: # Template type value is invalid func_results['success'] = False func_results['message'] = 'Invalid templateType value "{}"; must be WEBAPPS or GALLERY.'.format(templateType) return func_results if not templateIDs: func_results['success'] = False func_results['message'] = '{} portal property not set to use "Default" group.'.format(templateTypeStr.capitalize()) return func_results # Get the group id for the group to share the template items groupID = getGroupID(portal, groupOwner, groupTitle) if not groupID: func_results['success'] = False func_results['message'] = 'Could not find group where owner = "{}" and title = "{}"'.format(groupOwner, groupTitle) return func_results # Share templates with group print '\t-Sharing web templates with group {} ({})...'.format(groupTitle, groupOwner) results = share_template_items(portal, templateIDs, [groupID]) if not results['success']: func_results['success'] = False print results['message'] return func_results
def feature_items_query(portal, q, gallery=True, home_page=True, clear_existing=False): # Query for the items to feature items = portal.search(['id'], q) item_ids = unpack(items) # Call the feature_items function using with the query results return feature_items(portal, item_ids, gallery, home_page, clear_existing)
def clear_featured_items(portal, gallery=True, home_page=True): # Retrieve the IDs of the gallery groups to unshare with group_ids = _get_featured_group_ids(portal, gallery, home_page) # If there are gallery group(s) to unshare with, get the list of items in # those group(s) and then unshare each item from the gallery group(s) if group_ids: item_query = ' OR '.join('group:"%s"' % id for id in group_ids) items = portal.search(['id'], item_query) item_ids = unpack(items) for item_id in item_ids: portal.share_item(item_id, group_ids)
def create_basemap_gallery_group(portal, title, desc=None, snippet=None, tags='Basemap', phone=None, access='org', invitation_only=True, thumbnail=None, copy=True, copy_filter=None): # If it's a single tenant portal change 'org' access to 'public' access if not portal.is_multitenant() and access == 'org': access = 'public' # Prepare the group object group = { 'title': title, 'tags': tags, 'access': access, 'isinvitationonly': invitation_only } if desc: group['description'] = desc if snippet: group['snippet'] = snippet if phone: group['phone'] = phone # Create the group group_id = portal.create_group(group, thumbnail) if not group_id: raise PortalError('Unable to create basemap group: ' + title) # Share the contents of the current basemap group, if directed to do so if copy: old_group_id = _prop_to_group_id(portal, 'basemapGalleryGroupQuery') if old_group_id: item_query = 'group:' + old_group_id if copy_filter: item_query += ' ' + copy_filter item_ids = unpack( portal.search(['id'], item_query, scope='public')) for item_id in item_ids: portal.share_item(item_id, [group_id]) # Update the portal to use the new basemap gallery group portal.update_property('basemapGalleryGroupQuery', 'id:' + group_id) return group_id
def feature_items(portal, item_ids, gallery=True, home_page=True, clear_existing=False): item_ids = unpack(item_ids, 'id') # Clear existing featured items (if directed to) if clear_existing: clear_featured_items(portal, gallery, home_page) # Retrieve the IDs of the gallery groups to share with group_ids = _get_featured_group_ids(portal, gallery, home_page) # If there are gallery groups to share with, share each item with the # gallery group(s) if group_ids: for item_id in item_ids: portal.share_item(item_id, group_ids)
def group_item_stats(portal, sample_size=100): results = portal.groups(['id']) group_ids = unpack(results) group_ids_sample = random.sample(group_ids, min(sample_size, len(group_ids))) # Find the item counts (max of sample_size groups) item_counts = [] for index, group_id in enumerate(group_ids_sample): if index > sample_size: break item_count = len(portal.search(['id'], 'group:' + group_id)) item_counts.append(item_count) # Calculate and return statistics if len(item_counts): mean = sum(item_counts) / len(item_counts) stdv = _stdv(item_counts, mean) return min(item_counts), mean, max(item_counts), stdv return 0, 0, 0, 0
def group_member_stats(portal, sample_size=100): results = portal.groups(['id']) group_ids = unpack(results) group_ids_sample = random.sample(group_ids, min(sample_size, len(group_ids))) # Find the member counts (max of sample_size groups) member_counts = [] for index, group_id in enumerate(group_ids_sample): if index > sample_size: break group_members = portal.group_members(group_id) member_count = len(group_members['admins']) + len(group_members['users']) member_counts.append(member_count) # Calculate and return statistics if len(member_counts): mean = sum(member_counts) / len(member_counts) stdv = _stdv(member_counts, mean) return min(member_counts), mean, max(member_counts), stdv return 0, 0, 0, 0
def group_member_stats(portal, sample_size=100): results = portal.groups(['id']) group_ids = unpack(results) group_ids_sample = random.sample(group_ids, min(sample_size, len(group_ids))) # Find the member counts (max of sample_size groups) member_counts = [] for index, group_id in enumerate(group_ids_sample): if index > sample_size: break group_members = portal.group_members(group_id) member_count = len(group_members['admins']) + len( group_members['users']) member_counts.append(member_count) # Calculate and return statistics if len(member_counts): mean = sum(member_counts) / len(member_counts) stdv = _stdv(member_counts, mean) return min(member_counts), mean, max(member_counts), stdv return 0, 0, 0, 0
def create_basemap_gallery_group(portal, title, desc=None, snippet=None, tags='Basemap', phone=None, access='org', invitation_only=True, thumbnail=None, copy=True, copy_filter=None): # If it's a single tenant portal change 'org' access to 'public' access if not portal.is_multitenant() and access == 'org': access = 'public' # Prepare the group object group = { 'title': title, 'tags': tags, 'access': access, 'isinvitationonly': invitation_only } if desc: group['description'] = desc if snippet: group['snippet'] = snippet if phone: group['phone'] = phone # Create the group group_id = portal.create_group(group, thumbnail) if not group_id: raise PortalError('Unable to create basemap group: ' + title) # Share the contents of the current basemap group, if directed to do so if copy: old_group_id = _prop_to_group_id(portal, 'basemapGalleryGroupQuery') if old_group_id: item_query = 'group:' + old_group_id if copy_filter: item_query += ' ' + copy_filter item_ids = unpack(portal.search(['id'], item_query, scope='public')) for item_id in item_ids: portal.share_item(item_id, [group_id]) # Update the portal to use the new basemap gallery group portal.update_property('basemapGalleryGroupQuery', 'id:' + group_id) return group_id