Commit 45cbd8a3 authored by David Read's avatar David Read
Browse files

[noticket] No real change - just PEP8 and CKAN style imports.

parent 0dba6527
......@@ -10,12 +10,12 @@ from ckan.lib.navl.validators import (ignore_missing,
missing,
not_missing,
keep_extras,
)
)
import ckan.logic.schema as default_schema
import ckan.logic.validators as val
from ckan.plugins import implements, IDatasetForm, SingletonPlugin
import ckan.plugins as p
import ckan.plugins.toolkit as toolkit
from ckanext.dgu.lib import publisher as publib
from ckanext.dgu.lib import helpers as dgu_helpers
......@@ -46,15 +46,16 @@ update_frequency = [('', ''),
('monthly', 'monthly'),
('other', 'other - please specify')]
temporal_granularity = [("",""),
("year","year"),
("quarter","quarter"),
("month","month"),
("week","week"),
("day","day"),
("hour","hour"),
("point","point"),
("other","other - please specify")]
temporal_granularity = [("", ""),
("year", "year"),
("quarter", "quarter"),
("month", "month"),
("week", "week"),
("day", "day"),
("hour", "hour"),
("point", "point"),
("other", "other - please specify")]
def resources_schema():
schema = default_schema.default_resource_schema()
......@@ -69,6 +70,7 @@ def resources_schema():
})
return schema
def additional_resource_schema():
schema = resources_schema()
schema['format'] = [not_empty, unicode]
......@@ -77,6 +79,7 @@ def additional_resource_schema():
schema['description'] = [not_empty]
return schema
def individual_resource_schema():
schema = resources_schema()
schema['format'] = [not_empty, unicode]
......@@ -85,6 +88,7 @@ def individual_resource_schema():
schema['description'] = [not_empty]
return schema
def timeseries_resource_schema():
schema = resources_schema()
schema['date'] = [not_empty, unicode, convert_to_extras]
......@@ -94,19 +98,19 @@ def timeseries_resource_schema():
schema['description'] = [not_empty]
return schema
class DatasetForm(SingletonPlugin):
implements(IDatasetForm, inherit=True)
class DatasetForm(p.SingletonPlugin):
p.implements(p.IDatasetForm, inherit=True)
# We don't customize the schema here - instead it is done in the validate
# function, because there it has the context.
#def create_package_schema(self):
#def update_package_schema(self):
def show_package_schema(self):
return self.db_to_form_schema()
def new_template(self):
return 'package/new.html'
......@@ -206,8 +210,7 @@ class DatasetForm(SingletonPlugin):
for resources in ('additional_resources',
'timeseries_resources',
'individual_resources'):
schema[resources]['format'] = [unicode] # i.e. optional
schema[resources]['format'] = [unicode] # i.e. optional
def _ons_sysadmin_schema_updates(self, schema):
schema.update(
......@@ -217,7 +220,7 @@ class DatasetForm(SingletonPlugin):
for resources in ('additional_resources',
'timeseries_resources',
'individual_resources'):
schema[resources]['format'] = [unicode] # i.e. optional
schema[resources]['format'] = [unicode] # i.e. optional
@property
def _resource_format_optional(self):
......@@ -225,19 +228,9 @@ class DatasetForm(SingletonPlugin):
'theme-primary': [ignore_missing, unicode, convert_to_extras],
}
def _ons_sysadmin_schema_updates(self, schema):
schema.update(
{
'theme-primary': [ignore_missing, unicode, convert_to_extras],
})
for resources in ('additional_resources',
'timeseries_resources',
'individual_resources'):
schema[resources]['format'] = [unicode] # i.e. optional
def db_to_form_schema_options(self, options={}):
context = options.get('context', {})
schema = context.get('schema',None)
schema = context.get('schema', None)
if schema:
return schema
else:
......@@ -341,8 +334,8 @@ class DatasetForm(SingletonPlugin):
'__extras': [keep_extras]
},
'organization' : [],
'owner_org' : [],
'organization': [],
'owner_org': [],
'groups': {
'name': [not_empty, unicode]
......@@ -396,11 +389,11 @@ class DatasetForm(SingletonPlugin):
editor_groups = c.userobj.get_groups('organization', 'editor')
groups = list(admin_groups) + editor_groups
else: # anonymous user shouldn't have access to this page anyway.
else: # anonymous user shouldn't have access to this page anyway.
groups = []
# Be explicit about which fields we make available in the template
groups = [ {
groups = [{
'name': g.name,
'id': g.id,
'title': g.title,
......@@ -411,9 +404,10 @@ class DatasetForm(SingletonPlugin):
'foi-email': g.extras.get('foi-email', ''),
'foi-phone': g.extras.get('foi-phone', ''),
'foi-web': g.extras.get('foi-name', ''),
} for g in groups ]
} for g in groups]
return dict((g['name'], g) for g in groups)
return dict( (g['name'], g) for g in groups )
def date_to_db(value, context):
try:
......@@ -422,6 +416,7 @@ def date_to_db(value, context):
raise Invalid(str(e))
return value
def date_to_form(value, context):
try:
value = DateType.db_to_form(value)
......@@ -429,14 +424,16 @@ def date_to_form(value, context):
raise Invalid(str(e))
return value
def convert_to_extras(key, data, errors, context):
current_index = max( [int(k[1]) for k in data.keys() \
if len(k) == 3 and k[0] == 'extras'] + [-1] )
current_index = max([int(k[1]) for k in data.keys()
if len(k) == 3 and k[0] == 'extras'] + [-1])
data[('extras', current_index+1, 'key')] = key[-1]
data[('extras', current_index+1, 'value')] = data[key]
def convert_from_extras(key, data, errors, context):
for data_key, data_value in data.iteritems():
......@@ -445,6 +442,7 @@ def convert_from_extras(key, data, errors, context):
and data_value == key[-1]):
data[key] = data[('extras', data_key[1], 'value')]
def use_other(key, data, errors, context):
other_key = key[-1] + '-other'
......@@ -452,6 +450,7 @@ def use_other(key, data, errors, context):
if other_value:
data[key] = other_value
def extract_other(option_list):
def other(key, data, errors, context):
......@@ -467,6 +466,7 @@ def extract_other(option_list):
data[(other_key,)] = value
return other
def convert_geographic_to_db(value, context):
if isinstance(value, list):
......@@ -478,15 +478,18 @@ def convert_geographic_to_db(value, context):
return GeoCoverageType.get_instance().form_to_db(regions)
def convert_geographic_to_form(value, context):
return GeoCoverageType.get_instance().db_to_form(value)
def validate_group_id_or_name_exists_if_not_blank(value, context):
if not value.strip():
return True
return val.group_id_or_name_exists(value, context)
def tags_schema():
schema = {
'name': [not_missing,
......@@ -494,7 +497,7 @@ def tags_schema():
unicode,
val.tag_length_validator,
val.tag_name_validator,
],
],
'revision_timestamp': [ignore],
'state': [ignore],
}
......
import os
from logging import getLogger
from urllib import quote
from logging import getLogger
from pylons import config
import sqlalchemy.orm
from ckan.lib.helpers import flash_notice
from ckanext.dgu.plugins_toolkit import ObjectNotFound
from ckan.plugins import implements, SingletonPlugin
from ckan.plugins import IRoutes
from ckan.plugins import IConfigurer
from ckan.plugins import ITemplateHelpers
from ckan.plugins import IMiddleware
from ckan.plugins import IAuthFunctions
from ckan.plugins import IPackageController
from ckan.plugins import ISession
from ckan.plugins import IActions
from ckan.plugins import IDomainObjectModification
from ckan.plugins import IResourceUrlChange
from ckan.plugins import IActions
from ckan.plugins import ICachedReport
import ckan.plugins as p
import ckan.plugins.toolkit as toolkit
from ckanext.dgu.authentication.drupal_auth import DrupalAuthMiddleware
from ckanext.dgu.authorize import (
......@@ -34,7 +18,6 @@ from ckanext.dgu.authorize import (
from ckan.lib.helpers import url_for
from ckanext.dgu.lib.helpers import dgu_linked_user
from ckanext.dgu.lib.search import solr_escape
import ckanext.dgu
from ckanext.dgu.search_indexing import SearchIndexing
from ckan.config.routing import SubMapper
from ckan.exceptions import CkanUrlException
......@@ -54,8 +37,8 @@ def not_found(self, url):
def _guess_package_type(self, expecting_name=False):
return 'dataset'
class ReportsPlugin(SingletonPlugin):
implements(IRoutes, inherit=True)
class ReportsPlugin(p.SingletonPlugin):
p.implements(p.IRoutes, inherit=True)
def before_map(self, map):
"""
......@@ -64,8 +47,8 @@ class ReportsPlugin(SingletonPlugin):
report_ctlr = 'ckanext.dgu.controllers.reports:ReportsController'
map.connect('reports', '/data/reports', controller=report_ctlr, action='index')
# Resource reports
map.connect('resources_report','/data/reports/resources', controller=report_ctlr, action='resources')
map.connect('resources_report_org','/data/reports/resources/:id', controller=report_ctlr, action='resources')
map.connect('resources_report', '/data/reports/resources', controller=report_ctlr, action='resources')
map.connect('resources_report_org', '/data/reports/resources/:id', controller=report_ctlr, action='resources')
# QA
qa_home = 'ckanext.qa.controllers.qa_home:QAHomeController'
......@@ -91,11 +74,11 @@ class ReportsPlugin(SingletonPlugin):
# Feedback reports
map.connect('feedback_reports', '/data/reports/feedback',
controller=report_ctlr, action='feedback')
map.connect('feedback_reports_csv','/data/reports/feedback/:id.{format}',
map.connect('feedback_reports_csv', '/data/reports/feedback/:id.{format}',
controller=report_ctlr, action='feedback')
map.connect('feedback_report_org','/data/reports/feedback/:id',
map.connect('feedback_report_org', '/data/reports/feedback/:id',
controller=report_ctlr, action='feedback')
map.connect('feedback_report_org_csv','/data/reports/feedback.{format}',
map.connect('feedback_report_org_csv', '/data/reports/feedback.{format}',
controller=report_ctlr, action='feedback')
# Activity reports
......@@ -106,14 +89,13 @@ class ReportsPlugin(SingletonPlugin):
# Commitment reports
c_ctlr = 'ckanext.dgu.controllers.commitment:CommitmentController'
map.connect('commitments','/data/reports/commitments',
map.connect('commitments', '/data/reports/commitments',
controller=c_ctlr, action='index')
map.connect('commitments_publisher','/data/reports/commitments/:id',
map.connect('commitments_publisher', '/data/reports/commitments/:id',
controller=c_ctlr, action='commitments')
map.connect('/data/reports/commitments/:id/edit',
controller=c_ctlr, action='edit')
# Redirecting these so as to not break existing links
map.redirect('/data/feedback/report/{id}.{format}', '/data/reports/feedback/{id}.format')
map.redirect('/data/feedback/report/{id}', '/data/reports/feedback/{id}')
......@@ -126,13 +108,13 @@ class ReportsPlugin(SingletonPlugin):
return map
class ThemePlugin(SingletonPlugin):
class ThemePlugin(p.SingletonPlugin):
'''
DGU Visual Theme for a CKAN install embedded in dgu/Drupal.
'''
implements(IConfigurer)
implements(IRoutes, inherit=True)
implements(ITemplateHelpers, inherit=True)
p.implements(p.IConfigurer)
p.implements(p.IRoutes, inherit=True)
p.implements(p.ITemplateHelpers, inherit=True)
from ckan.lib.base import h, BaseController
# [Monkey patch] Replace h.linked_user with a version to hide usernames
......@@ -180,8 +162,6 @@ class ThemePlugin(SingletonPlugin):
return helper_dict
def before_map(self, map):
"""
Make "/data" the homepage.
......@@ -247,6 +227,7 @@ class ThemePlugin(SingletonPlugin):
def after_map(self, map):
return map
def ensure_package_major_time_remains(package):
'''A write to the package may remove the last_major_modification extra,
so make sure it remains.'''
......@@ -257,7 +238,6 @@ def ensure_package_major_time_remains(package):
model.Session.flush()
def update_package_major_time(package):
import datetime
import ckan.model as model
try:
......@@ -275,8 +255,9 @@ def update_package_major_time(package):
log.exception(e)
model.Session.remove()
class LastMajorModificationPlugin1(SingletonPlugin):
implements(IDomainObjectModification, inherit=True)
class LastMajorModificationPlugin1(p.SingletonPlugin):
p.implements(p.IDomainObjectModification, inherit=True)
def notify(self, entity, operation):
from ckan import model
......@@ -310,26 +291,27 @@ class LastMajorModificationPlugin1(SingletonPlugin):
ensure_package_major_time_remains(pkg)
class LastMajorModificationPlugin2(SingletonPlugin):
implements(IResourceUrlChange, inherit=True)
class LastMajorModificationPlugin2(p.SingletonPlugin):
p.implements(p.IResourceUrlChange, inherit=True)
def notify(self, resource):
log.debug("URL for resource %s has changed" % resource.id)
update_package_major_time(resource.resource_group.package)
class DrupalAuthPlugin(SingletonPlugin):
class DrupalAuthPlugin(p.SingletonPlugin):
'''Reads Drupal login cookies to log user in.'''
implements(IMiddleware, inherit=True)
p.implements(p.IMiddleware, inherit=True)
def make_middleware(self, app, config):
return DrupalAuthMiddleware(app, config)
class AuthApiPlugin(SingletonPlugin):
class AuthApiPlugin(p.SingletonPlugin):
'''Adds functions that work out if the user is allowed to do
certain edits.'''
implements(IAuthFunctions, inherit=True)
p.implements(p.IAuthFunctions, inherit=True)
def get_auth_functions(self):
return {
......@@ -343,19 +325,19 @@ class AuthApiPlugin(SingletonPlugin):
}
class DguForm(SingletonPlugin):
class DguForm(p.SingletonPlugin):
# NB the actual form (IDatasetForm) is in forms/dataset_form.py
implements(IRoutes, inherit=True)
p.implements(p.IRoutes, inherit=True)
# IRoutes
def before_map(self, map):
dgu_package_controller = 'ckanext.dgu.controllers.package:PackageController'
map.connect('dataset_new','/dataset/new', controller=dgu_package_controller, action='new')
map.connect('dataset_edit','/dataset/edit/{id}', controller=dgu_package_controller, action='edit')
map.connect('dataset_new', '/dataset/new', controller=dgu_package_controller, action='new')
map.connect('dataset_edit', '/dataset/edit/{id}', controller=dgu_package_controller, action='edit')
map.connect('/dataset/delete/{id}', controller=dgu_package_controller, action='delete')
map.connect('dataset_history','/dataset/history/{id}', controller=dgu_package_controller, action='history')
map.connect('dataset_history', '/dataset/history/{id}', controller=dgu_package_controller, action='history')
map.connect('/dataset/{id}.{format}', controller=dgu_package_controller, action='read')
map.connect('/dataset/{id}', controller=dgu_package_controller, action='read')
map.connect('/dataset/{id}/resource/{resource_id}', controller=dgu_package_controller, action='resource_read')
......@@ -363,11 +345,11 @@ class DguForm(SingletonPlugin):
return map
class PublisherPlugin(SingletonPlugin):
class PublisherPlugin(p.SingletonPlugin):
implements(IRoutes, inherit=True)
implements(ISession, inherit=True)
implements(ICachedReport)
p.implements(p.IRoutes, inherit=True)
p.implements(p.ISession, inherit=True)
p.implements(p.ICachedReport)
def before_commit(self, session):
"""
......@@ -382,7 +364,7 @@ class PublisherPlugin(SingletonPlugin):
return
pubctlr = 'ckanext.dgu.controllers.publisher:PublisherController'
for obj in set( session._object_cache['new'] ):
for obj in set(session._object_cache['new']):
if isinstance(obj, (User)):
try:
url = url_for(controller=pubctlr, action='apply')
......@@ -401,7 +383,6 @@ class PublisherPlugin(SingletonPlugin):
#log.debug('Did not add a flash message due to a missing session: %s' % msg)
pass
def before_map(self, map):
pub_ctlr = 'ckanext.dgu.controllers.publisher:PublisherController'
......@@ -412,30 +393,30 @@ class PublisherPlugin(SingletonPlugin):
controller=pub_ctlr, action='index')
map.connect('publisher_edit',
'/publisher/edit/:id',
controller=pub_ctlr, action='edit' )
controller=pub_ctlr, action='edit')
map.connect('publisher_apply',
'/publisher/apply/:id',
controller=pub_ctlr, action='apply' )
controller=pub_ctlr, action='apply')
map.connect('publisher_apply_empty',
'/publisher/apply',
controller=pub_ctlr, action='apply' )
controller=pub_ctlr, action='apply')
map.connect('publisher_users',
'/publisher/users/:id',
controller=pub_ctlr, action='users' )
controller=pub_ctlr, action='users')
map.connect('publisher_new',
'/publisher/new',
controller=pub_ctlr, action='new' )
controller=pub_ctlr, action='new')
map.connect('/publisher/report_groups_without_admins',
controller=pub_ctlr, action='report_groups_without_admins' )
controller=pub_ctlr, action='report_groups_without_admins')
map.connect('/publisher/report_publishers_and_users',
controller=pub_ctlr, action='report_publishers_and_users' )
controller=pub_ctlr, action='report_publishers_and_users')
map.connect('/publisher/report_users',
controller=pub_ctlr, action='report_users' )
controller=pub_ctlr, action='report_users')
map.connect('/publisher/report_users_not_assigned_to_groups',
controller=pub_ctlr, action='report_users_not_assigned_to_groups' )
controller=pub_ctlr, action='report_users_not_assigned_to_groups')
map.connect('publisher_read',
'/publisher/:id',
controller=pub_ctlr, action='read' )
controller=pub_ctlr, action='read')
return map
......@@ -465,8 +446,8 @@ class PublisherPlugin(SingletonPlugin):
from ckanext.dgu.lib.publisher import cached_openness_scores
from ckanext.dgu.lib.reports import cached_reports
return { 'Cached Openness Scores': cached_openness_scores,
'Cached reports': cached_reports }
return {'Cached Openness Scores': cached_openness_scores,
'Cached reports': cached_reports}
def list_report_keys(self):
"""
......@@ -477,17 +458,17 @@ class PublisherPlugin(SingletonPlugin):
'feedback-report', 'publisher-activity-report']
class InventoryPlugin(SingletonPlugin):
class InventoryPlugin(p.SingletonPlugin):
implements(IRoutes, inherit=True)
implements(IConfigurer)
implements(ISession, inherit=True)
implements(IAuthFunctions, inherit=True)
p.implements(p.IRoutes, inherit=True)
p.implements(p.IConfigurer)
p.implements(p.ISession, inherit=True)
p.implements(p.IAuthFunctions, inherit=True)
def get_auth_functions(self):
return {
'feedback_update' : dgu_feedback_update,
'feedback_create' : dgu_feedback_create,
'feedback_update': dgu_feedback_update,
'feedback_create': dgu_feedback_create,
'feedback_delete': dgu_feedback_delete,
}
......@@ -511,7 +492,6 @@ class InventoryPlugin(SingletonPlugin):
map.connect('/dataset/:id/feedback/add',
controller=fb_ctlr, action='add')
# As users have been sent out a direct link to /inventory/publisher-name/edit
# we will (at least for a short while) allow /inventory to redirect to
# /unpublished
......@@ -522,18 +502,17 @@ class InventoryPlugin(SingletonPlugin):
map.connect('/unpublished/edit-item/:id',
controller=inv_ctlr, action='edit_item')
map.connect('/unpublished/:id/edit',
controller=inv_ctlr, action='edit' )
controller=inv_ctlr, action='edit')
map.connect('/unpublished/:id/edit/download',
controller=inv_ctlr, action='download' )
controller=inv_ctlr, action='download')
map.connect('/unpublished/:id/edit/template',
controller=inv_ctlr, action='template' )
controller=inv_ctlr, action='template')
map.connect('/unpublished/:id/edit/upload',
controller=inv_ctlr, action='upload' )
controller=inv_ctlr, action='upload')
map.connect('/unpublished/:id/edit/upload_complete',
controller=inv_ctlr, action='upload_complete' )
controller=inv_ctlr, action='upload_complete')
map.connect('/unpublished/:id/edit/upload/:upload_id',
controller=inv_ctlr, action='upload_status' )
controller=inv_ctlr, action='upload_status')
return map
......@@ -544,7 +523,7 @@ class InventoryPlugin(SingletonPlugin):
pass
class SearchPlugin(SingletonPlugin):
class SearchPlugin(p.SingletonPlugin):
"""
DGU-specific searching.
......@@ -562,7 +541,7 @@ class SearchPlugin(SingletonPlugin):
well as the group name.
"""
implements(IPackageController, inherit=True)
p.implements(p.IPackageController, inherit=True)
def read(self, entity):
pass
......@@ -652,17 +631,17 @@ class SearchPlugin(SingletonPlugin):
# Extract multiple theme values (concatted with ' ') into one multi-value schema field
all_themes = set()
for value in ( pkg_dict.get('theme-primary',''), pkg_dict.get('theme-secondary','') ):
for value in (pkg_dict.get('theme-primary', ''), pkg_dict.get('theme-secondary', '')):
for theme in value.split(' '):
if theme:
all_themes.add(theme)
pkg_dict['all_themes'] = list(all_themes)
return pkg_dict
class ApiPlugin(SingletonPlugin):
class ApiPlugin(p.SingletonPlugin):
'''DGU-specific API'''
implements(IRoutes, inherit=True)
implements(IActions)
p.implements(p.IRoutes, inherit=True)
p.implements(p.IActions)
def before_map(self, map):
api_controller = 'ckanext.dgu.controllers.api:DguApiController'
......@@ -685,5 +664,6 @@ class ApiPlugin(SingletonPlugin):
'publisher_show': publisher_show,
}
def is_plugin_enabled(plugin_name):
return plugin_name in config.get('ckan.plugins', '').split()
Markdown is supported
0% or .