Exemple #1
0
def activity_create(context, activity_dict, ignore_auth=False):
    '''Create a new activity stream activity and return a dictionary
    representation of it.

    '''
    model = context['model']
    user = context['user']

    # Any revision_id that the caller attempts to pass in the activity_dict is
    # ignored and overwritten here.
    if getattr(model.Session, 'revision', None):
        activity_dict['revision_id'] = model.Session.revision.id
    else:
        activity_dict['revision_id'] = None

    if not ignore_auth:
        check_access('activity_create', context, activity_dict)

    schema = context.get(
        'schema') or ckan.logic.schema.default_create_activity_schema()
    data, errors = validate(activity_dict, schema, context)
    if errors:
        raise ValidationError(errors)

    activity = model_save.activity_dict_save(activity_dict, context)

    if not context.get('defer_commit'):
        model.repo.commit()

    log.debug("Created '%s' activity" % activity.activity_type)
    return model_dictize.activity_dictize(activity, context)
Exemple #2
0
def activity_create(context, activity_dict, ignore_auth=False):
    """Create a new activity stream activity and return a dictionary
    representation of it.

    """
    model = context["model"]
    user = context["user"]

    # Any revision_id that the caller attempts to pass in the activity_dict is
    # ignored and overwritten here.
    if getattr(model.Session, "revision", None):
        activity_dict["revision_id"] = model.Session.revision.id
    else:
        activity_dict["revision_id"] = None

    if not ignore_auth:
        check_access("activity_create", context, activity_dict)

    schema = context.get("schema") or ckan.logic.schema.default_create_activity_schema()
    data, errors = validate(activity_dict, schema, context)
    if errors:
        raise ValidationError(errors)

    activity = model_save.activity_dict_save(activity_dict, context)

    if not context.get("defer_commit"):
        model.repo.commit()

    log.debug("Created '%s' activity" % activity.activity_type)
    return model_dictize.activity_dictize(activity, context)
Exemple #3
0
def activity_create(context, activity_dict, ignore_auth=False):
    '''Create a new activity stream activity and return a dictionary
    representation of it.

    '''
    model = context['model']
    user = context['user']

    # Any revision_id that the caller attempts to pass in the activity_dict is
    # ignored and overwritten here.
    if getattr(model.Session, 'revision', None):
        activity_dict['revision_id'] = model.Session.revision.id
    else:
        activity_dict['revision_id'] = None

    if not ignore_auth:
        check_access('activity_create', context, activity_dict)

    schema = context.get('schema') or default_create_activity_schema()
    data, errors = validate(activity_dict, schema, context)
    if errors:
        raise ValidationError(errors)

    activity = activity_dict_save(activity_dict, context)

    if not context.get('defer_commit'):
        model.repo.commit()

    log.debug("Created '%s' activity" % activity.activity_type)
    return activity_dictize(activity, context)
    def _create(cls, target_class, *args, **kwargs):
        if args:
            assert False, "Positional args aren't supported, use keyword args."

        context = {
            'user': ckan_factories._get_action_user_name(kwargs),
            'model': model
        }

        if not kwargs.get('user_id'):
            kwargs['user_id'] = ckan_factories.User()['id']

        activity_dict = \
            p.toolkit.get_action('activity_create')(context, kwargs)

        # to set the timestamp we need to edit the object
        activity = model.Session.query(model.Activity).get(activity_dict['id'])
        if kwargs.get('timestamp'):
            activity.timestamp = kwargs['timestamp']
            model.repo.commit()

        if kwargs.get('return_object'):
            return activity

        return model_dictize.activity_dictize(activity, context)
Exemple #5
0
def activity_create(context, activity_dict, **kw):
    '''Create a new activity stream activity.

    You must be a sysadmin to create new activities.

    :param user_id: the name or id of the user who carried out the activity,
        e.g. ``'seanh'``
    :type user_id: string
    :param object_id: the name or id of the object of the activity, e.g.
        ``'my_dataset'``
    :param activity_type: the type of the activity, this must be an activity
        type that CKAN knows how to render, e.g. ``'new package'``,
        ``'changed user'``, ``'deleted group'`` etc. (for a full list see
        ``activity_renderers`` in ``ckan/logic/action/get.py``
    :type activity_type: string
    :param data: any additional data about the activity
    :type data: dictionary

    :returns: the newly created activity
    :rtype: dictionary

    '''

    # this action had a ignore_auth param which has been removed
    # removed in 2.2
    if 'ignore_auth' in kw:
        raise Exception('Activity Stream calling parameters have changed '
                        'ignore_auth must be passed in the context not as '
                        'a param')

    if not paste.deploy.converters.asbool(
            config.get('ckan.activity_streams_enabled', 'true')):
        return

    model = context['model']

    # Any revision_id that the caller attempts to pass in the activity_dict is
    # ignored and overwritten here.
    if getattr(model.Session, 'revision', None):
        activity_dict['revision_id'] = model.Session.revision.id
    else:
        activity_dict['revision_id'] = None

    _check_access('activity_create', context, activity_dict)

    schema = context.get(
        'schema') or ckan.logic.schema.default_create_activity_schema()
    data, errors = _validate(activity_dict, schema, context)
    if errors:
        raise ValidationError(errors)

    activity = model_save.activity_dict_save(data, context)

    if not context.get('defer_commit'):
        model.repo.commit()

    log.debug("Created '%s' activity" % activity.activity_type)
    return model_dictize.activity_dictize(activity, context)
Exemple #6
0
def activity_create(context, activity_dict, **kw):
    '''Create a new activity stream activity.

    You must be a sysadmin to create new activities.

    :param user_id: the name or id of the user who carried out the activity,
        e.g. ``'seanh'``
    :type user_id: string
    :param object_id: the name or id of the object of the activity, e.g.
        ``'my_dataset'``
    :param activity_type: the type of the activity, this must be an activity
        type that CKAN knows how to render, e.g. ``'new package'``,
        ``'changed user'``, ``'deleted group'`` etc.
    :type activity_type: string
    :param data: any additional data about the activity
    :type data: dictionary

    :returns: the newly created activity
    :rtype: dictionary

    '''

    _check_access('activity_create', context, activity_dict)

    # this action had a ignore_auth param which has been removed
    # removed in 2.2
    if 'ignore_auth' in kw:
        raise Exception('Activity Stream calling parameters have changed '
                        'ignore_auth must be passed in the context not as '
                        'a param')

    if not paste.deploy.converters.asbool(
            config.get('ckan.activity_streams_enabled', 'true')):
        return

    model = context['model']

    # Any revision_id that the caller attempts to pass in the activity_dict is
    # ignored and overwritten here.
    if getattr(model.Session, 'revision', None):
        activity_dict['revision_id'] = model.Session.revision.id
    else:
        activity_dict['revision_id'] = None

    schema = context.get('schema') or \
        ckan.logic.schema.default_create_activity_schema()

    data, errors = _validate(activity_dict, schema, context)
    if errors:
        raise ValidationError(errors)

    activity = model_save.activity_dict_save(data, context)

    if not context.get('defer_commit'):
        model.repo.commit()

    log.debug("Created '%s' activity" % activity.activity_type)
    return model_dictize.activity_dictize(activity, context)
Exemple #7
0
def activity_create(context, activity_dict, **kw):
    """Create a new activity stream activity.

    You must be a sysadmin to create new activities.

    :param user_id: the name or id of the user who carried out the activity,
        e.g. ``'seanh'``
    :type user_id: string
    :param object_id: the name or id of the object of the activity, e.g.
        ``'my_dataset'``
    :param activity_type: the type of the activity, this must be an activity
        type that CKAN knows how to render, e.g. ``'new package'``,
        ``'changed user'``, ``'deleted group'`` etc. (for a full list see
        ``activity_renderers`` in ``ckan/logic/action/get.py``
    :type activity_type: string
    :param data: any additional data about the activity
    :type data: dictionary

    :returns: the newly created activity
    :rtype: dictionary

    """

    # this action had a ignore_auth param which has been removed
    # removed in 2.2
    if "ignore_auth" in kw:
        raise Exception(
            "Activity Stream calling parameters have changed "
            "ignore_auth must be passed in the context not as "
            "a param"
        )

    if not paste.deploy.converters.asbool(config.get("ckan.activity_streams_enabled", "true")):
        return

    model = context["model"]

    # Any revision_id that the caller attempts to pass in the activity_dict is
    # ignored and overwritten here.
    if getattr(model.Session, "revision", None):
        activity_dict["revision_id"] = model.Session.revision.id
    else:
        activity_dict["revision_id"] = None

    _check_access("activity_create", context, activity_dict)

    schema = context.get("schema") or ckan.logic.schema.default_create_activity_schema()
    data, errors = _validate(activity_dict, schema, context)
    if errors:
        raise ValidationError(errors)

    activity = model_save.activity_dict_save(data, context)

    if not context.get("defer_commit"):
        model.repo.commit()

    log.debug("Created '%s' activity" % activity.activity_type)
    return model_dictize.activity_dictize(activity, context)
def metadata_record_activity_dictize(activity, context):
    model = context['model']
    session = context['session']
    activity_dict = ckan_model_dictize.activity_dictize(activity, context)
    activity_details = session.query(model.ActivityDetail) \
        .filter(model.ActivityDetail.activity_id == activity.id) \
        .all()
    activity_dict['details'] = ckan_model_dictize.activity_detail_list_dictize(
        activity_details, context)
    return activity_dict
def export_(activity_query):
    num_activities = activity_query.count()
    if not num_activities:
        print(u'  No activities')

    global args
    with open(args.output, 'wb') as f:
        for activity in query.yield_per(100):
            # from activity_show
            activity_dict = model_dictize.activity_dictize(activity,
                                                           context,
                                                           include_data=True)
def activity_create(context, activity_dict, ignore_auth=False):
    '''Create a new activity stream activity.

    You must be a sysadmin to create new activities.

    :param user_id: the name or id of the user who carried out the activity,
        e.g. ``'seanh'``
    :type user_id: string
    :param object_id: the name or id of the object of the activity, e.g.
        ``'my_dataset'``
    :param activity_type: the type of the activity, this must be an activity
        type that CKAN knows how to render, e.g. ``'new package'``,
        ``'changed user'``, ``'deleted group'`` etc. (for a full list see
        ``activity_renderers`` in ``ckan/logic/action/get.py``
    :type activity_type: string
    :param data: any additional data about the activity
    :type data: dictionary

    :returns: the newly created activity
    :rtype: dictionary

    '''
    if not paste.deploy.converters.asbool(
            config.get('ckan.activity_streams_enabled', 'true')):
        return

    model = context['model']

    # Any revision_id that the caller attempts to pass in the activity_dict is
    # ignored and overwritten here.
    if getattr(model.Session, 'revision', None):
        activity_dict['revision_id'] = model.Session.revision.id
    else:
        activity_dict['revision_id'] = None

    if not ignore_auth:
        _check_access('activity_create', context, activity_dict)

    schema = context.get('schema') or ckan.logic.schema.default_create_activity_schema()
    data, errors = _validate(activity_dict, schema, context)
    if errors:
        raise ValidationError(errors)

    activity = model_save.activity_dict_save(data, context)

    if not context.get('defer_commit'):
        model.repo.commit()

    log.debug("Created '%s' activity" % activity.activity_type)
    return model_dictize.activity_dictize(activity, context)
Exemple #11
0
def activity_create(context, activity_dict, ignore_auth=False):
    '''Create a new activity stream activity.

    You must be a sysadmin to create new activities.

    :param user_id: the name or id of the user who carried out the activity,
        e.g. ``'seanh'``
    :type user_id: string
    :param object_id: the name or id of the object of the activity, e.g.
        ``'my_dataset'``
    :param activity_type: the type of the activity, this must be an activity
        type that CKAN knows how to render, e.g. ``'new package'``,
        ``'changed user'``, ``'deleted group'`` etc. (for a full list see
        ``activity_renderers`` in ``ckan/logic/action/get.py``
    :type activity_type: string
    :param data: any additional data about the activity
    :type data: dictionary

    :returns: the newly created activity
    :rtype: dictionary

    '''
    model = context['model']

    # Any revision_id that the caller attempts to pass in the activity_dict is
    # ignored and overwritten here.
    if getattr(model.Session, 'revision', None):
        activity_dict['revision_id'] = model.Session.revision.id
    else:
        activity_dict['revision_id'] = None

    if not ignore_auth:
        _check_access('activity_create', context, activity_dict)

    schema = context.get(
        'schema') or ckan.logic.schema.default_create_activity_schema()
    data, errors = _validate(activity_dict, schema, context)
    if errors:
        raise ValidationError(errors)

    activity = model_save.activity_dict_save(activity_dict, context)

    if not context.get('defer_commit'):
        model.repo.commit()

    log.debug("Created '%s' activity" % activity.activity_type)
    return model_dictize.activity_dictize(activity, context)
Exemple #12
0
 def test_include_data(self):
     dataset = factories.Dataset()
     user = factories.User()
     activity = factories.Activity(
         user_id=user["id"],
         object_id=dataset["id"],
         activity_type="new package",
         data={"package": copy.deepcopy(dataset), "actor": "Mr Someone"},
     )
     activity_obj = model.Activity.get(activity["id"])
     context = {"model": model, "session": model.Session}
     dictized = model_dictize.activity_dictize(activity_obj, context)
     assert dictized["user_id"] == user["id"]
     assert dictized["activity_type"] == "new package"
     assert dictized["data"]["package"]["title"] == dataset["title"]
     assert dictized["data"]["package"]["id"] == dataset["id"]
     assert dictized["data"]["actor"] == "Mr Someone"
Exemple #13
0
 def test_include_data(self):
     dataset = factories.Dataset()
     user = factories.User()
     activity = factories.Activity(user_id=user['id'],
                                   object_id=dataset['id'],
                                   activity_type='new package',
                                   data={
                                       'package': copy.deepcopy(dataset),
                                       'actor': 'Mr Someone',
                                   })
     activity_obj = model.Activity.get(activity['id'])
     context = {'model': model, 'session': model.Session}
     dictized = model_dictize.activity_dictize(activity_obj,
                                               context,
                                               include_data=True)
     assert_equal(dictized['user_id'], user['id'])
     assert_equal(dictized['activity_type'], 'new package')
     assert_equal(dictized['data']['package']['title'], dataset['title'])
     assert_equal(dictized['data']['package']['id'], dataset['id'])
     assert_equal(dictized['data']['actor'], 'Mr Someone')
 def test_dont_include_data(self):
     dataset = factories.Dataset()
     user = factories.User()
     activity = factories.Activity(
         user_id=user['id'],
         object_id=dataset['id'],
         revision_id=None,
         activity_type='new package',
         data={
             'package': copy.deepcopy(dataset),
             'actor': 'Mr Someone',
         })
     activity_obj = model.Activity.get(activity['id'])
     context = {'model': model, 'session': model.Session}
     dictized = model_dictize.activity_dictize(activity_obj, context,
                                               include_data=False)
     assert_equal(dictized['user_id'], user['id'])
     assert_equal(dictized['activity_type'], 'new package')
     assert_equal(dictized['data'],
                  {'package': {'title': dataset['title']}})
def package_activity_list_dictize(activity_list,
                                  package_name,
                                  package_title,
                                  context,
                                  include_data=False):
    '''all the activities are to do with one specific package, given by
    package_name/title.
    Similar to model_dictize.activity_list_dictize, but provides the package
    name and title
    '''
    import ckan.lib.dictization.model_dictize as model_dictize
    dictized_activity_list = []
    for activity in activity_list:
        dictized_activity = \
            model_dictize.activity_dictize(activity, context, include_data)
        dictized_activity['package'] = dict(id=dictized_activity['object_id'],
                                            name=package_name,
                                            title=package_title)
        dictized_activity['object_type'] = 'package'
        dictized_activity_list.append(dictized_activity)
    return dictized_activity_list
def export(activity_query):
    import ckan.lib.dictization.model_dictize as model_dictize
    num_activities = activity_query.count()
    if not num_activities:
        print(u'  No activities')

    global args
    with open(args.output, 'wb') as f:
        for activity_tuple in activity_query.yield_per(25):
            activity, group_name, group_title, group_is_org, package_name, \
                package_title = activity_tuple
            # from activity_show
            activity_dict = model_dictize.activity_dictize(activity,
                                                           get_context(),
                                                           include_data=True)

            # e.g.
            # {'activity_type': u'new package',
            #  'data': {u'actor': u'boston_ckan_sandbox',
            #           u'package': {u'author': None,
            #                        u'author_email': None,
            #                        u'btype': [u'geospatial'],
            #                        u'classification': u'public',
            #                        u'contact_point': u'GIS Team',
            #                        u'contact_point_email': u'*****@*****.**',
            #                        u'creator_user_id': u'be45c2f4-8960-4f26-83c8-6b7ecc8364f8',
            #                        u'extras': [{u'key': u'spatial',
            #                                     u'value': u'{"type":"Polygon","coordinates": [[[-71.1789,42.2313],[-71.1789,42.3944],[-70.9951,42.3944],[-70.9951,42.2313],[-71.1789,42.2313]]]}'}],
            #                        u'groups': [],
            #                        u'id': u'82992437-d3a9-4aa2-8c56-ac6aa5b51233',
            #                        u'isopen': True,
            #                        u'license_id': u'odc-pddl',
            #                        u'license_title': u'Open Data Commons Public Domain Dedication and License (PDDL)',
            #                        u'license_url': u'http://www.opendefinition.org/licenses/odc-pddl',
            #                        u'location': [],
            #                        u'maintainer': None,
            #                        u'maintainer_email': None,
            #                        u'metadata_created': u'2018-02-09T17:28:35.461622',
            #                        u'metadata_modified': u'2018-02-09T17:28:35.461631',
            #                        u'modified': u'2017-03-02',
            #                        u'name': u'trash-collection-days',
            #                        u'notes': u'City of Boston Public Works (PWD) department trash collection days.\xa0',
            #                        u'notes_translated': {u'en': u'City of Boston Public Works (PWD) department trash collection days.\xa0'},
            #                        u'num_resources': 6,
            #                        u'num_tags': 7,
            #                        u'open': u'open',
            #                        u'organization': {u'approval_status': u'approved',
            #                                          u'created': u'2016-10-05T09:08:53.173506',
            #                                          u'description': u'One location for both spatial and non-spatial datasets in a variety of forms.\r\n',
            #                                          u'id': u'f8d82bea-34ab-4328-8adf-a5b1c331b50c',
            #                                          u'image_url': u'2016-10-05-090853.162217BostonMaps.png',
            #                                          u'is_organization': True,
            #                                          u'name': u'boston-maps',
            #                                          u'revision_id': u'aafd194b-61ee-491e-ba41-7b5d70c3bac3',
            #                                          u'state': u'active',
            #                                          u'title': u'Boston Maps',
            #                                          u'type': u'organization'},
            #                        u'owner_org': u'f8d82bea-34ab-4328-8adf-a5b1c331b50c',
            #                        u'private': False,
            #                        u'publisher': u'Department of Innovation and Technology',
            #                        u'relationships_as_object': [],
            #                        u'relationships_as_subject': [],
            #                        u'released': u'2015-10-14',
            #                        u'resources': [{u'Created': u'2018-02-09T17:28:35.465904',
            #                                        u'Language': [],
            #                                        u'Media type': None,
            #                                        u'Size': None,
            #                                        u'cache_last_updated': None,
            #                                        u'cache_url': None,
            #                                        u'data_dictionary': [],
            #                                        u'datastore_active': False,
            #                                        u'description': u'',
            #                                        u'description_translated': {},
            #                                        u'format': u'GeoJSON',
            #                                        u'hash': u'',
            #                                        u'id': u'0c22f570-e634-42ad-829c-a90d58d1610d',
            #                                        u'last_modified': None,
            #                                        u'mimetype_inner': None,
            #                                        u'name': u'GeoJSON',
            #                                        u'name_translated': {},
            #                                        u'package_id': u'82992437-d3a9-4aa2-8c56-ac6aa5b51233',
            #                                        u'position': 0,
            #                                        u'resource_type': None,
            #                                        u'revision_id': u'f7a99013-b951-48ee-b347-7ce5b489b1ef',
            #                                        u'state': u'active',
            #                                        u'tracking_summary': {u'recent': 0,
            #                                                              u'total': 0},
            #                                        u'url': u'http://bostonopendata-boston.opendata.arcgis.com/datasets/b09b9dd54c1241369080c0ee48895e85_10.geojson',
            #                                        u'url_type': None}],
            #                        u'revision_id': u'f7a99013-b951-48ee-b347-7ce5b489b1ef',
            #                        u'source': [],
            #                        u'state': u'active',
            #                        u'tags': [{u'display_name': u'boston',
            #                                   u'id': u'2fa281c5-3431-4bc0-8d47-3e64c6b480ac',
            #                                   u'name': u'boston',
            #                                   u'state': u'active',
            #                                   u'vocabulary_id': None},
            #                                  {u'display_name': u'ckan',
            #                                   u'id': u'87f84b6f-e129-4750-be0d-ba0c030b7650',
            #                                   u'name': u'ckan',
            #                                   u'state': u'active',
            #                                   u'vocabulary_id': None}],
            #                        u'temporal_notes': {},
            #                        u'theme': [],
            #                        u'title': u'Trash Collection Days',
            #                        u'title_translated': {u'en': u'Trash Collection Days'},
            #                        u'tracking_summary': {u'recent': 0, u'total': 0},
            #                        u'type': u'dataset',
            #                        u'url': u'http://bostonopendata-boston.opendata.arcgis.com/datasets/b09b9dd54c1241369080c0ee48895e85_10',
            #                        u'version': None}},
            #  'id': u'2fa19192-19af-4697-8038-8eb722c66d31',
            #  'object_id': u'82992437-d3a9-4aa2-8c56-ac6aa5b51233',
            #  'revision_id': u'f7a99013-b951-48ee-b347-7ce5b489b1ef',
            #  'timestamp': '2018-02-09T17:28:35.785088',
            #  'user_id': u'be45c2f4-8960-4f26-83c8-6b7ecc8364f8'}

            # -- logging format: json --

            log_line = dict(
                activity_type=activity_dict['activity_type'],
                timestamp=activity_dict[u'timestamp'],
                dataset_name=package_name,
                dataset_title=package_title,
            )

            if u'package' in activity_dict[u'data']:
                pkg = activity_dict[u'data'][u'package']
                log_line[u'num_resources'] = pkg[u'num_resources']
                log_line[u'resources'] = pkg[u'resources']
                log_line[u'tags'] = \
                    [tag[u'display_name'] for tag in pkg[u'tags']]

            if not group_name:
                pass
            elif group_is_org:
                log_line[u'organization_name'] = group_name
                log_line[u'organization_title'] = group_title
            else:
                log_line[u'group_name'] = group_name
                log_line[u'group_title'] = group_title
            log_line = json.dumps(log_line)

            # -- logging format: text --
            if 'package' in activity_dict['data']:
                pkg = activity_dict['data']['package']
                package_log = '{package[name]} "{package[title]}"' \
                    .format(package=pkg)
            else:
                package_log = '"" ""'

            if not group_name:
                group_type = ''
            elif group_is_org:
                group_type = 'org'
            else:
                group_type = 'group'
            group_type = ''
            log_line = '{timestamp} "{package_name}" "{package_title}" "{group_name}" "{group_title}" "{group_type}" "{package_log}"'.format(
                group_name=group_name,
                group_title=group_title,
                group_type=group_type,
                package_name=package_name,
                package_title=package_title,
                package_log=package_log,
                **activity_dict)

            # -- end of logging formats --

            print(log_line)
            f.write(log_line + '\n')

    print(u'Saved: {} ({} items)'.format(args.output, num_activities))