def create_datarequest(context, data_dict):
    '''
    Action to create a new data request. The function checks the access rights
    of the user before creating the data request. If the user is not allowed
    a NotAuthorized exception will be risen.

    In addition, you should note that the parameters will be checked and an
    exception (ValidationError) will be risen if some of these parameters are
    not valid.

    :param title: The title of the data request
    :type title: string

    :param description: A brief description for your data request
    :type description: string

    :param organiztion_id: The ID of the organization you want to asign the
        data request (optional).
    :type organization_id: string

    :returns: A dict with the data request (id, user_id, title, description,
        organization_id, open_time, accepted_dataset, close_time, closed,
        followers)
    :rtype: dict
    '''

    model = context['model']
    session = context['session']

    # Init the data base
    db.init_db(model)

    # Check access
    tk.check_access(constants.CREATE_DATAREQUEST, context, data_dict)

    # Validate data
    validator.validate_datarequest(context, data_dict)

    # Store the data
    data_req = db.DataRequest()
    _undictize_datarequest_basic(data_req, data_dict)
    data_req.user_id = context['auth_user_obj'].id
    data_req.open_time = datetime.datetime.utcnow()

    session.add(data_req)
    session.commit()

    datarequest_dict = _dictize_datarequest(data_req)

    if datarequest_dict['organization']:
        users = {
            user['id']
            for user in datarequest_dict['organization']['users']
        }
        users.discard(context['auth_user_obj'].id)
        _send_mail(users, 'new_datarequest', datarequest_dict)

    return datarequest_dict
def datarequest_create(context, data_dict):
    '''
    Action to create a new dara request. The function checks the access rights
    of the user before creating the data request. If the user is not allowed
    a NotAuthorized exception will be risen.

    In addition, you should note that the parameters will be checked and an
    exception (ValidationError) will be risen if some of these parameters are
    not valid.

    :param title: The title of the data request
    :type title: string

    :param description: A brief description for your data request
    :type description: string

    :param organiztion_id: If you want to create the data request in a specific
        organization.
    :type organization_id: string

    :returns: A dict with the data request (id, user_id, title, description,
        organization_id, open_time, accepted_dataset, close_time, closed)
    :rtype: dict
    '''

    model = context['model']
    session = context['session']

    # Init the data base
    db.init_db(model)

    # Check access
    tk.check_access(constants.DATAREQUEST_CREATE, context, data_dict)

    # Validate data
    validator.validate_datarequest(context, data_dict)

    # Store the data
    data_req = db.DataRequest()
    _undictize_datarequest_basic(data_req, data_dict)
    data_req.user_id = context['auth_user_obj'].id if context['auth_user_obj'] else 'anonymous'
    data_req.open_time = datetime.datetime.now()

    data_req.visibility = constants.DataRequestState.visible.value
    context['ignore_auth'] = config.get('ckan.datarequests.ignore_auth', False)
    user = context['user']
    if context['ignore_auth'] == False and not authz.is_sysadmin(user):
        data_req.visibility = constants.DataRequestState.hidden.value

    session.add(data_req)
    session.commit()

    return _dictize_datarequest(data_req)
def datarequest_create(context, data_dict):
    '''
    Action to create a new dara request. The function checks the access rights
    of the user before creating the data request. If the user is not allowed
    a NotAuthorized exception will be risen.

    In addition, you should note that the parameters will be checked and an
    exception (ValidationError) will be risen if some of these parameters are
    not valid.

    :param title: The title of the data request
    :type title: string

    :param description: A brief description for your data request
    :type description: string

    :param organiztion_id: If you want to create the data request in a specific
        organization.
    :type organization_id: string

    :returns: A dict with the data request (id, user_id, title, description,
        organization_id, open_time, accepted_dataset, close_time, closed)
    :rtype: dict
    '''

    model = context['model']
    session = context['session']

    # Init the data base
    db.init_db(model)

    # Check access
    tk.check_access(constants.DATAREQUEST_CREATE, context, data_dict)

    # Validate data
    validator.validate_datarequest(context, data_dict)

    # Store the data
    data_req = db.DataRequest()
    _undictize_datarequest_basic(data_req, data_dict)
    data_req.user_id = context['auth_user_obj'].id
    data_req.open_time = datetime.datetime.now()

    session.add(data_req)
    session.commit()

    return _dictize_datarequest(data_req)
def datarequest_update(context, data_dict):
    '''
    Action to update a dara request. The function checks the access rights of
    the user before updating the data request. If the user is not allowed
    a NotAuthorized exception will be risen.

    In addition, you should note that the parameters will be checked and an
    exception (ValidationError) will be risen if some of these parameters are
    not valid.

    :param id: The id of the data request to be updated
    :type id: string

    :param title: The title of the data request
    :type title: string

    :param description: A brief description for your data request
    :type description: string

    :param organiztion_id: If you want to create the data request in a specific
        organization.
    :type organization_id: string

    :returns: A dict with the data request (id, user_id, title, description, 
        organization_id, open_time, accepted_dataset, close_time, closed)
    :rtype: dict
    '''

    model = context['model']
    session = context['session']
    datarequest_id = data_dict.get('id', '')

    if not datarequest_id:
        raise tk.ValidationError('Data Request ID has not been included')

    # Init the data base
    db.init_db(model)

    # Check access
    tk.check_access(constants.DATAREQUEST_UPDATE, context, data_dict)
    user = context['user']
    if not authz.is_sysadmin(user):
        # only sysadmins can update visiblity
        data_dict.pop('visibility', None)

    # Get the initial data
    result = db.DataRequest.get(id=datarequest_id)
    if not result:
        raise tk.ObjectNotFound('Data Request %s not found in the data base' % datarequest_id)

    data_req = result[0]

    # Avoid the validator to return an error when the user does not change the title
    context['avoid_existing_title_check'] = data_req.title == data_dict['title']

    # Validate data
    validator.validate_datarequest(context, data_dict)

    # Set the data provided by the user in the data_red
    _undictize_datarequest_basic(data_req, data_dict)

    session.add(data_req)
    session.commit()

    return _dictize_datarequest(data_req)
Exemple #5
0
def datarequest_update(context, data_dict):
    '''
    Action to update a dara request. The function checks the access rights of
    the user before updating the data request. If the user is not allowed
    a NotAuthorized exception will be risen.

    In addition, you should note that the parameters will be checked and an
    exception (ValidationError) will be risen if some of these parameters are
    not valid.

    :param id: The id of the data request to be updated
    :type id: string

    :param title: The title of the data request
    :type title: string

    :param description: A brief description for your data request
    :type description: string

    :param organiztion_id: If you want to create the data request in a specific
        organization.
    :type organization_id: string

    :returns: A dict with the data request (id, user_id, title, description,
        organization_id, open_time, accepted_dataset, close_time, closed)
    :rtype: dict
    '''

    model = context['model']
    session = context['session']
    datarequest_id = data_dict.get('id', '')

    if not datarequest_id:
        raise tk.ValidationError('Data Request ID has not been included')

    # Init the data base
    db.init_db(model)

    # Check access
    tk.check_access(constants.DATAREQUEST_UPDATE, context, data_dict)

    # Get the initial data
    result = db.DataRequest.get(id=datarequest_id)
    if not result:
        raise tk.ObjectNotFound('Data Request %s not found in the data base' %
                                datarequest_id)

    data_req = result[0]

    # Avoid the validator to return an error when the user does not change the title
    context['avoid_existing_title_check'] = data_req.title == data_dict[
        'title']

    # Validate data
    validator.validate_datarequest(context, data_dict)

    # Set the data provided by the user in the data_red
    _undictize_datarequest_basic(data_req, data_dict)

    session.add(data_req)
    session.commit()

    return _dictize_datarequest(data_req)
Exemple #6
0
def datarequest_create(context, data_dict):
    '''
    Action to create a new data request. The function checks the access rights
    of the user before creating the data request. If the user is not allowed
    a NotAuthorized exception will be risen.

    In addition, you should note that the parameters will be checked and an
    exception (ValidationError) will be risen if some of these parameters are
    not valid.

    :param title: The title of the data request
    :type title: string

    :param description: A brief description for your data request
    :type description: string

    :param organization_id: If you want to create the data request in a specific
        organization.
    :type organization_id: string

    :returns: A dict with the data request (id, user_id, title, description,
        organization_id, open_time, accepted_dataset, close_time, closed)
    :rtype: dict
    '''

    model = context['model']
    session = context['session']

    # Init the data base
    db.init_db(model)

    # Check access
    tk.check_access(constants.DATAREQUEST_CREATE, context, data_dict)

    # Validate data
    validator.validate_datarequest(context, data_dict)

    # Store the data
    data_req = db.DataRequest()
    _undictize_datarequest_basic(data_req, data_dict)
    data_req.user_id = context['auth_user_obj'].id
    data_req.open_time = datetime.datetime.now()

    session.add(data_req)
    session.commit()

    # SEND NOTIFICATION EMAIL
    group_dict = tk.get_action('organization_show')(
        {}, {
            'id': data_dict['organization_id']
        })

    # set up a data dict for filling out the email template
    data = {
        "email": context['auth_user_obj'].email,
        "user": context['auth_user_obj'].name,
        "request_title": data_dict.get("title"),
        "request_description": data_dict.get("description"),
        "request_id": data_req.id,
        "organization": group_dict.get("display_name"),
        "organization_lang": group_dict.get("original_language")
    }

    admins = _get_organization_admins(data_dict.get('organization_id', ''))
    for admin in admins:
        if admin:
            lang = data.get(
                "organization_lang",
                "en")  # send the mail in organization's default lang
            _send_comment_notification_mail(admin.display_name, admin.email,
                                            data, lang)

    # Always send a notification mail to website admin
    admin_email = config.get(
        "ckanext-datarequests.datarequest_notifications_admin_email", None)
    if admin_email:
        # Add 4th parameter lang (fi/sv/en) for internationalizerd email templates
        log.debug("Sending datarequest notification email to avoindata-admin")
        _send_comment_notification_mail("Avoindata-admin", admin_email, data)

    return _dictize_datarequest(data_req)
def create_datarequest(context, data_dict):
    '''
    Action to create a new data request. The function checks the access rights
    of the user before creating the data request. If the user is not allowed
    a NotAuthorized exception will be risen.

    In addition, you should note that the parameters will be checked and an
    exception (ValidationError) will be risen if some of these parameters are
    not valid.

    :param title: The title of the data request
    :type title: string

    :param description: A brief description for your data request
    :type description: string

    :param organiztion_id: The ID of the organization you want to asign the
        data request (optional).
    :type organization_id: string

    :returns: A dict with the data request (id, user_id, title, description,
        organization_id, open_time, accepted_dataset, close_time, closed,
        followers)
    :rtype: dict
    '''

    model = context['model']
    session = context['session']

    # Init the data base
    db.init_db(model)

    # Check access
    tk.check_access(constants.CREATE_DATAREQUEST, context, data_dict)

    # Validate data
    validator.validate_datarequest(context, data_dict)

    # Store the data
    data_req = db.DataRequest()
    _undictize_datarequest_basic(data_req, data_dict)
    data_req.user_id = context['auth_user_obj'].id
    data_req.open_time = datetime.datetime.now()

    session.add(data_req)
    session.commit()

    # SEND NOTIFICATION EMAIL
    group_dict = tk.get_action('organization_show')({}, {'id': data_dict['organization_id']})

    # set up a data dict for filling out the email template
    data = {
        "email": context['auth_user_obj'].email,
        "user": context['auth_user_obj'].name,
        "request_title": data_dict.get("title"),
        "request_description": data_dict.get("description"),
        "request_id": data_req.id,
        "organization": group_dict.get("display_name"),
        "organization_lang": group_dict.get("original_language")
    }

    admins = _get_organization_admins(data_dict.get('organization_id', ''))
    for admin in admins:
        if admin:
            lang = data.get("organization_lang", "en")  # send the mail in organization's default lang
            _send_comment_notification_mail(admin.display_name, admin.email, data, lang)

    # Always send a notification mail to website admin
    admin_email = config.get("ckanext-datarequests.datarequest_notifications_admin_email", None)
    if admin_email:
        # Add 4th parameter lang (fi/sv/en) for internationalizerd email templates
        log.debug("Sending datarequest notification email to avoindata-admin")
        _send_comment_notification_mail("Avoindata-admin", admin_email, data)

    datarequest_dict = _dictize_datarequest(data_req)

    if datarequest_dict['organization']:
        users = set([user['id'] for user in datarequest_dict['organization']['users']])
        users.discard(context['auth_user_obj'].id)
        _send_mail(users, 'new_datarequest', datarequest_dict)

    return datarequest_dict