def dashboard_activity_list_html(context, data_dict): '''Return the authorized (via login or API key) user's dashboard activity stream as HTML. The activity stream is rendered as a snippet of HTML meant to be included in an HTML page, i.e. it doesn't have any HTML header or footer. :param offset: where to start getting activity items from (optional, default: 0) :type offset: int :param limit: the maximum number of activities to return (optional, default: 31, the default value is configurable via the ckan.activity_list_limit setting) :type limit: int :rtype: string ''' activity_stream = dashboard_activity_list(context, data_dict) model = context['model'] offset = data_dict.get('offset', 0) extra_vars = { 'controller': 'user', 'action': 'dashboard', 'offset': offset, } return activity_streams.activity_list_to_html(context, activity_stream, extra_vars)
def user_activity_list_html(context, data_dict): '''Return a user's public activity stream as HTML. Override this action to filter out activities related to uploaded authorities and dataset agreement that are only shown for sysadmins and users that have updated their general activites. The activity stream is rendered as a snippet of HTML meant to be included in an HTML page, i.e. it doesn't have any HTML header or footer. :param id: The id or name of the user. :type id: string :param offset: where to start getting activity items from (optional, default: ``0``) :type offset: int :param limit: the maximum number of activities to return (optional, default: ``31``, the default value is configurable via the ckan.activity_list_limit setting) :type limit: int :rtype: string ''' activity_stream = toolkit.get_action('user_activity_list')(context, data_dict) offset = int(data_dict.get('offset', 0)) extra_vars = { 'controller': 'user', 'action': 'activity', 'id': data_dict['id'], 'offset': offset, } return activity_streams.activity_list_to_html( context, activity_stream, extra_vars)
def _activities(self, page): limit = 100 offset = (page - 1) * limit if c.userobj.sysadmin: users = model.Session.query(model.User) activity_queries = [] for user in users: activity_queries += self._get_activity(user) else: activity_queries = self._get_activity(c.userobj) sql_activities = model.activity._activities_union_all( *activity_queries) total_count = sql_activities.count() has_more = total_count > offset + limit raw_activities = model.activity._activities_at_offset( sql_activities, limit, offset) context = { 'model': model, 'session': model.Session, 'user': c.user or c.author, 'auth_user_obj': c.userobj, 'for_view': True } activities = model_dictize.activity_list_dictize( raw_activities, context) extra_vars = { 'controller': 'user', 'action': 'activity', 'offset': offset, 'user_history': True } return activity_streams.activity_list_to_html(context, activities, extra_vars), has_more
def recently_changed_packages_activity_list_html(context, data_dict): activity_stream = recently_changed_packages_activity_list( context, data_dict) offset = int(data_dict.get('offset', 0)) extra_vars = { 'controller': 'package', 'action': 'activity', 'offset': offset, } return activity_streams.activity_list_to_html(context, activity_stream, extra_vars)
def group_activity_list_html(context, data_dict): activity_stream = group_activity_list(context, data_dict) offset = int(data_dict.get('offset', 0)) extra_vars = { 'controller': 'group', 'action': 'activity', 'id': data_dict['id'], 'offset': offset, } return activity_streams.activity_list_to_html(context, activity_stream, extra_vars)
def dashboard(self): user_name = http_request.environ.get("REMOTE_USER") user = self.user_service.get_or_create_user(user_name) if user_name else None if not user: return self.login() try: context = {'model': model, 'session': model.Session, 'user': c.user or c.author} c.user_dict = get_action('user_show')(context, {'id': c.user}) except toolkit.ObjectNotFound: abort(404) profiles = self.location_service.get_user_profiles(c.userobj.id) indicators = self.community_profile_service.get_gallery_indicators_for_user(c.userobj.id, 'all') data_dict = {'am_member': True } groups = get_action('group_list_authz')(context, data_dict) c.group_dropdown = [[group['id'], group['display_name']] for group in groups ] c.followee_list = get_action('followee_list')(context, {'id': c.userobj.id}) activity_stream = logic.get_action('dashboard_activity_list')(context, data_dict) c.dataset_activity = filter(lambda a: self.is_dataset_activity(a), activity_stream) c.group_activity = filter(lambda a: len(a['data'].keys()) > 0 and a['data'].keys()[0] == 'group', activity_stream) extra_vars = { 'controller': 'user', 'action': 'dashboard', 'offset': 0, } for group in groups: activities = filter(lambda g: g['object_id'] == group['id'], c.group_activity) group['activities'] = activity_streams.activity_list_to_html(context, activities, extra_vars) c.dataset_activity_stream = activity_streams.activity_list_to_html(context, c.dataset_activity, extra_vars) c.group_activity_stream = activity_streams.activity_list_to_html(context, c.group_activity, extra_vars) self.session.close() return base.render('user/user_page.html', extra_vars={'profiles': profiles, 'gallery_indicators': indicators, 'groups': groups})
def dashboard_activity_list_html(context, data_dict): activity_stream = dashboard_activity_list(context, data_dict) model = context['model'] user_id = context['user'] offset = data_dict.get('offset', 0) extra_vars = { 'controller': 'user', 'action': 'dashboard', 'offset': offset, 'id': user_id } return activity_streams.activity_list_to_html(context, activity_stream, extra_vars)
def dashboard_activity_list_html(context, data_dict): '''Return the authorized (via login or API key) user's dashboard activity stream as HTML. Override this action to filter out activities related to uploaded authorities and dataset agreement that are only shown for sysadmins and users that have updated their general activites. ''' activity_stream = toolkit.get_action('dashboard_activity_list')(context, data_dict) model = context['model'] user_id = context['user'] offset = data_dict.get('offset', 0) extra_vars = { 'controller': 'user', 'action': 'dashboard', 'offset': offset, 'id': user_id } return activity_streams.activity_list_to_html(context, activity_stream, extra_vars)
def fetch_recent_package_activity_list_html( context, user=None, user_not=None, only_privatized=False, only_resourceful=False, limit=30): # Fetch recent revisions, store as list oredered by time recent_revisions_query = ( model.Session.query(model.PackageRevision, model.User.id) .join(model.Revision, model.PackageRevision.revision_id == model.Revision.id) .join(model.User, model.Revision.author == model.User.name) .distinct()) if only_resourceful: recent_revisions_query = ( recent_revisions_query .join(model.Resource, model.Resource.package_id == model.PackageRevision.id) .filter(model.Resource.state == "active")) if user is not None: recent_revisions_query = recent_revisions_query.filter( model.Revision.author == user) if user_not is not None: recent_revisions_query = recent_revisions_query.filter( model.Revision.author != user_not) if only_privatized: recent_revisions_query = recent_revisions_query.filter( model.PackageRevision.private) recent_revisions_query = ( recent_revisions_query .order_by(model.PackageRevision.metadata_modified.desc()) .limit(limit)) recent_revisions = [r for r in recent_revisions_query] # Fetch related packages, store by id packages = {r.id: None for r, uid in recent_revisions} packages_query = ( model.Session.query(model.Package) .filter(model.Package.id.in_(packages.keys()))) for package in packages_query: packages[package.id] = package # Fetch related packages' first revision timestamps packages_created = {} packages_created_query = ( model.Session.query( model.PackageRevision.id.label('id'), func.min(model.PackageRevision.metadata_modified).label('ts')) .filter(model.PackageRevision.id.in_(packages.keys())) .group_by(model.PackageRevision.id)) for package_id, created in packages_created_query: packages_created[package_id] = created # Fetch previous revisions for the recent revisions packages_previous = {} packages_previous_query = ( model.Session.query(model.PackageRevision.revision_id.label("rid"), model.PackageRevision) .from_statement(text(""" select p.revision_id as rid, r.* from package_revision r left join ( select l.revision_id, r.id, max(r.metadata_modified) as previous_timestamp from package_revision r join package_revision l on r.id = l.id where l.revision_id = ANY(:ids) and r.metadata_modified < l.metadata_modified group by l.revision_id, r.id, l.metadata_modified ) p on r.id = p.id where r.metadata_modified = p.previous_timestamp """)) .params(ids=[r.revision_id for r, uid in recent_revisions])) for rid, package in packages_previous_query: packages_previous[rid] = package # Add support for new color for privacy-changed packages activity_streams.activity_stream_string_icons['changed package privacy'] = 'sitemap' activity_streams.activity_stream_string_functions['changed package privacy'] = \ activity_streams.activity_stream_string_changed_package # Create activity objects based on revision data def revision_to_activity(r, uid): pr = packages_previous.get(r.revision_id) if only_privatized and (pr is None or (pr.private or not r.private)): return None privacy_changed = pr is not None and pr.private != r.private activity_type = None if r.state in ('active', 'draft'): if packages_created[r.id] == r.metadata_modified: activity_type = 'new package' elif privacy_changed: activity_type = 'changed package privacy' else: activity_type = 'changed package' elif r.state in ('deleted'): activity_type = 'deleted package' else: log.warning("Unknown package state, skipping: %s" % r.state) return None d = {'package': dictization.table_dictize( packages[r.id], context={'model': model})} activity = model.Activity(uid, r.id, r.revision_id, activity_type, d) activity.timestamp = r.metadata_modified return activity activity_objects = ( (r for r in (revision_to_activity(r, uid) for r, uid in recent_revisions) if r is not None)) # Render activity list snippet changed_packages = model_dictize.activity_list_dictize(activity_objects, context) return activity_streams.activity_list_to_html( context, changed_packages, {'offset': 0})
def fetch_recent_package_activity_list_html( context, user_id=None, user_id_not=None, only_privatized=False, only_resourceful=False, limit=30): # Fetch recent revisions, store as list oredered by time recent_revisions_query = model.Session.query(model.PackageRevision).distinct() if only_resourceful: recent_revisions_query = ( recent_revisions_query .join(model.Resource, model.Resource.package_id == model.PackageRevision.id) .filter(model.Resource.state == "active")) if user_id is not None: recent_revisions_query = recent_revisions_query.filter( model.PackageRevision.creator_user_id == user_id) if user_id_not is not None: recent_revisions_query = recent_revisions_query.filter( model.PackageRevision.creator_user_id != user_id_not) if only_privatized: recent_revisions_query = recent_revisions_query.filter( model.PackageRevision.private) recent_revisions_query = ( recent_revisions_query .order_by(model.PackageRevision.revision_timestamp.desc()) .limit(limit)) recent_revisions = [r for r in recent_revisions_query] # Fetch related packages, store by id packages = {r.id: None for r in recent_revisions} packages_query = ( model.Session.query(model.Package) .filter(model.Package.id.in_(packages.keys()))) for package in packages_query: packages[package.id] = package # Fetch related packages' first revision timestamps packages_created = {} packages_created_query = ( model.Session.query( model.PackageRevision.id.label('id'), func.min(model.PackageRevision.revision_timestamp).label('ts')) .filter(model.PackageRevision.id.in_(packages.keys())) .group_by(model.PackageRevision.id)) for package_id, created in packages_created_query: packages_created[package_id] = created # Fetch previous revisions for the recent revisions packages_previous = {} packages_previous_query = ( model.Session.query(model.PackageRevision.revision_id.label("rid"), model.PackageRevision) .from_statement(text(""" select p.revision_id as rid, r.* from package_revision r left join ( select l.revision_id, r.id, max(r.revision_timestamp) as previous_timestamp from package_revision r join package_revision l on r.id = l.id where l.revision_id = ANY(:ids) and r.revision_timestamp < l.revision_timestamp group by l.revision_id, r.id, l.revision_timestamp ) p on r.id = p.id where r.revision_timestamp = p.previous_timestamp """)) .params(ids=[r.revision_id for r in recent_revisions])) for rid, package in packages_previous_query: packages_previous[rid] = package # Add support for new color for privacy-changed packages activity_streams.activity_stream_string_icons['changed package privacy'] = 'sitemap' activity_streams.activity_stream_string_functions['changed package privacy'] = \ activity_streams.activity_stream_string_changed_package # Create activity objects based on revision data def revision_to_activity(r): pr = packages_previous.get(r.revision_id) if only_privatized and (pr is None or (pr.private or not r.private)): return None privacy_changed = pr is not None and pr.private != r.private activity_type = None if r.state in ('active', 'draft'): if packages_created[r.id] == r.revision_timestamp: activity_type = 'new package' elif privacy_changed: activity_type = 'changed package privacy' else: activity_type = 'changed package' elif r.state in ('deleted'): activity_type = 'deleted package' else: log.warning("Unknown package state, skipping: %s" % r.state) return None d = {'package': dictization.table_dictize( packages[r.id], context={'model': model})} activity = model.Activity(r.creator_user_id, r.id, r.revision_id, activity_type, d) activity.timestamp = r.revision_timestamp return activity activity_objects = ( (r for r in (revision_to_activity(r) for r in recent_revisions) if r is not None)) # Render activity list snippet changed_packages = model_dictize.activity_list_dictize(activity_objects, context) return activity_streams.activity_list_to_html( context, changed_packages, {'offset': 0})