Exemplo n.º 1
0
def querytool_list_by_group(context, data_dict):
    '''Returns a list of query tools that
     belong to a specified group.
    :rtype: list of dictionaries
    '''

    # check_access('',
    #            context, data_dict)
    group = data_dict.get('group')

    # get a user's organizations:
    user_orgs = ch.organizations_available('read')
    querytools = []

    if c.userobj is not None:
        if c.userobj.sysadmin is not None:
            querytools += CkanextQueryTool.search(group=group)
        else:
            for org in user_orgs:
                querytools += CkanextQueryTool.search(group=group,
                                                      owner_org=org['id'])
    else:
        querytools += CkanextQueryTool.search(group=group, private=False)

    out = []

    for querytool in querytools:
        querytool = table_dictize(querytool, context)
        out.append(querytool)

    return out
Exemplo n.º 2
0
def querytool_delete(context, data_dict):
    '''Delete querytool.
    :param name: the name of query
    :type name: string
    :rtype: dictionary
    '''
    check_access('querytool_delete', context, data_dict)
    CkanextQueryTool.delete(id=data_dict['name'])
Exemplo n.º 3
0
def querytool_delete(context, data_dict):
    '''Delete querytool.
    :param id: the id of query
    :type id: string
    :rtype: dictionary
    '''
    check_access('querytool_delete', context)
    id = toolkit.get_or_bust(data_dict, 'id')
    CkanextQueryTool.delete(id=id)
Exemplo n.º 4
0
def querytool_visualizations_update(context, data_dict):
    '''
        Create new query tool visualizations
    :param name
    :param charts
    :param
    '''

    session = context['session']
    # data, errors = df.validate(data_dict, schema.querytool_schema(),
    #                          context)

    # if errors:
    #    raise toolkit.ValidationError(errors)
    querytool = CkanextQueryTool.get(name=data_dict['name'])
    visualizations = CkanextQueryToolVisualizations.get(name=data_dict['name'])
    if not visualizations:
        visualizations = CkanextQueryToolVisualizations()

    visualizations.name = data_dict['name']
    visualizations.charts = data_dict['charts']
    visualizations.ckanext_querytool_id = querytool.id
    visualizations.save()
    session.add(visualizations)
    session.commit()
Exemplo n.º 5
0
def querytool_visualizations_update(context, data_dict):
    '''
        Create new query tool visualizations
    :param name
    :param visualizations
    :param
    '''

    session = context['session']
    # data, errors = df.validate(data_dict, schema.querytool_schema(),
    #                          context)

    # if errors:
    #    raise toolkit.ValidationError(errors)
    querytool = CkanextQueryTool.get(name=data_dict['name'])
    visualizations = CkanextQueryToolVisualizations.get(name=data_dict['name'])

    images = []
    if visualizations:
        items = json.loads(visualizations.visualizations)
        for image in items:
            if image['type'] == 'image':
                images.append(image)

    new_items = json.loads(data_dict['visualizations'])
    if new_items:
        new_images = []
        for new in new_items:
            if new['type'] == 'image':
                new_images.append(new['url'])

    if new_images or images:
        for old in images:
            old_img_url = old['url']
            if old_img_url not in new_images:
                upload = uploader.get_uploader('vs', old_img_url)
                new_data = {
                    'image_url': old_img_url,
                    'image_upload': 'true',
                    'clear_upload': 'true'
                }
                upload.update_data_dict(new_data, 'image_url', 'image_upload',
                                        'clear_upload')
                upload.upload(uploader)

    if not visualizations:
        visualizations = CkanextQueryToolVisualizations()

    visualizations.name = data_dict['name']
    visualizations.visualizations = data_dict['visualizations']
    visualizations.y_axis_column = data_dict['y_axis_column']
    visualizations.ckanext_querytool_id = querytool.id
    visualizations.save()
    session.add(visualizations)
    session.commit()
Exemplo n.º 6
0
def querytool_update(context, data_dict):
    '''
        Authorization check for updating querytool or visualizations
    '''
    querytool = data_dict.get('name')
    if querytool:
        # check if user has an edit permission for given querytool:
        querytool = CkanextQueryTool.get(name=querytool)
        return {'success': user_in_org_or_group(querytool.owner_org)}
    # if querytool is None then we need to create one:
    # check if user has a edit permission for any org:
    orgs = organizations_available()
    return {'success': any([org['capacity'] != 'member' for org in orgs])}
Exemplo n.º 7
0
def querytool_delete(context, data_dict):
    '''
        Authorization check for querytool delete
    '''
    # sysadmins only
    # check if user has a delete permission for an org:
    querytool = CkanextQueryTool.get(name=data_dict['name'])
    owner_org = querytool.owner_org
    user_has_permission = has_user_permission_for_group_or_org(
        owner_org, context.get('user'), 'delete_dataset')
    if not user_has_permission:
        return {'success': False}
    return {'success': True}
Exemplo n.º 8
0
def querytool_list_other(context, data_dict):
    '''Returns a list of query tools that
     don't belong to any of the existing groups.
    :rtype: list of dictionaries
    '''
    groups = data_dict.get('groups')
    querytools = CkanextQueryTool.search()
    out = []

    for querytool in querytools:
        if querytool.group not in groups:
            querytool = table_dictize(querytool, context)
            ch.user_in_org_or_group(querytool.get('owner_org'))
            out.append(querytool)
    return out
Exemplo n.º 9
0
def querytool_get(context, data_dict):
    '''Returns  query tool.
    :param name: querytool name
    :rtype: query tool object
    '''

    # check_access('',
    #            context, data_dict)

    log.info('Querytool : %r', data_dict)
    name = data_dict['name']

    querytool = CkanextQueryTool.get(name=name)
    if querytool:
        querytool = table_dictize(querytool, context)
    return querytool
Exemplo n.º 10
0
def querytool_update(context, data_dict):
    '''
        Authorization check for updating querytool or visualizations
    '''
    querytool = data_dict.get('name')
    if querytool:
        # check if user an edit permission for given querytool:
        querytool = CkanextQueryTool.get(name=querytool)
        user_in_org = user_in_org_or_group(querytool.owner_org)
        if user_in_org:
            return {'success': True}
        return {'success': False}
    # if querytool is None then we need to create one:
    # check if user has a read permission for any org:
    orgs = organizations_available('read')
    if len(orgs) == 0:
        return {'success': False}
    return {'success': True}
Exemplo n.º 11
0
def querytool_update(context, data_dict):
    '''
        Create new query tool
    :param title
    :param description
    :param dataset
    :param filters
    :param created
    :param map_resource
    :param chart_resource
    :param y_axis_columns
    '''

    # we need the querytool name in the context for name validation
    context['querytool'] = data_dict['querytool']
    session = context['session']
    data, errors = df.validate(data_dict, schema.querytool_schema(), context)

    if errors:
        raise toolkit.ValidationError(errors)

    querytool = CkanextQueryTool.get(name=data_dict['querytool'])
    visualizations = \
        CkanextQueryToolVisualizations.get(name=data_dict['querytool'])

    # if name is not changed don't insert in visualizations table
    is_changed = False
    if visualizations:
        is_changed = (querytool.name == visualizations.name)

    if visualizations and is_changed:
        visualizations.name = data.get('name')
        visualizations.save()
        session.add(querytool)
        session.commit()

    if not querytool:
        querytool = CkanextQueryTool()

    items = [
        'title', 'description', 'name', 'private', 'type', 'group',
        'dataset_name', 'owner_org', 'filters', 'sql_string',
        'related_querytools', 'chart_resource', 'y_axis_columns'
    ]
    for item in items:
        setattr(querytool, item, data.get(item))

    querytool.modified = datetime.datetime.utcnow()
    querytool.save()
    session.add(querytool)
    session.commit()
Exemplo n.º 12
0
def querytool_list(context, data_dict):
    '''Returns a list of all query tools.
    :rtype: list of dictionaries
    '''

    # check_access('',
    #            context, data_dict)

    log.info('Querytool list: %r', data_dict)

    querytools = CkanextQueryTool.search()

    out = []

    for querytool in querytools:
        querytool = table_dictize(querytool, context)
        out.append(querytool)

    return out
Exemplo n.º 13
0
def querytool_update(context, data_dict):
    '''
        Create new query tool
    :param title
    :param description
    :param dataset
    :param filters
    :param created
    :param map_resource
    :param chart_resource
    :param y_axis_columns
    '''

    check_access('querytool_update', context)

    # we need the querytool name in the context for name validation
    context['querytool'] = data_dict['querytool']
    session = context['session']
    data, errors = df.validate(data_dict, schema.querytool_schema(), context)

    if errors:
        raise toolkit.ValidationError(errors)

    querytool = CkanextQueryTool.get(name=data_dict['querytool'])

    if not querytool:
        querytool = CkanextQueryTool()

    items = [
        'title', 'description', 'name', 'dataset_name', 'filters',
        'sql_string', 'map_resource', 'chart_resource', 'y_axis_columns'
    ]
    for item in items:
        setattr(querytool, item, data.get(item))

    querytool.modified = datetime.datetime.utcnow()
    querytool.save()
    session.add(querytool)
    session.commit()
Exemplo n.º 14
0
def querytool_update(context, data_dict):
    '''
        Create new query tool
    :param title
    :param description
    :param dataset
    :param filters
    :param created
    :param map_resource
    :param chart_resource
    :param y_axis_columns
    :param selection_label
    :param report_caption
    '''

    # we need the querytool name in the context for name validation
    context['querytool'] = data_dict['querytool']
    session = context['session']
    data, errors = df.validate(data_dict, schema.querytool_schema(), context)

    if errors:
        raise toolkit.ValidationError(errors)

    querytool = CkanextQueryTool.get(name=data_dict['querytool'])
    visualizations = \
        CkanextQueryToolVisualizations.get(name=data_dict['querytool'])

    # if name is not changed don't insert in visualizations table
    is_changed = False
    if visualizations:
        is_changed = (querytool.name == visualizations.name)

    if visualizations and is_changed:
        visualizations.name = data.get('name')
        visualizations.save()
        session.add(querytool)
        session.commit()

    if not querytool:
        querytool = CkanextQueryTool()

    items = [
        'title', 'description', 'name', 'private', 'type', 'group',
        'dataset_name', 'owner_org', 'icon', 'image_url', 'image_display_url',
        'filters', 'sql_string', 'related_querytools', 'chart_resource',
        'y_axis_columns', 'additional_description', 'selection_label',
        'report_caption'
    ]

    dataset_name = data.get('dataset_name')
    dataset = _get_action('package_show')(context, {'id': dataset_name})
    dataset['groups'] = [{'name': str(data.get('group'))}]
    _get_action('package_patch')(context, dataset)

    image_url = data_dict['image_url']

    if h.uploads_enabled():
        image_upload = data_dict['image_upload']
        if isinstance(image_upload, cgi.FieldStorage):
            upload = uploader.get_uploader('querytool', image_url)
            upload.update_data_dict(data_dict, 'image_url', 'image_upload',
                                    'clear_upload')
            upload.upload(uploader)
            data_dict['image_display_url'] = upload.filename
            data['image_display_url'] = upload.filename
        else:
            data['image_display_url'] = querytool.image_display_url

    for item in items:
        setattr(querytool, item, data.get(item))

    querytool.modified = datetime.datetime.utcnow()
    querytool.save()

    session.add(querytool)
    session.commit()