def commitments(self, id): """ Shows all of the commitments for the specified publisher """ from ckanext.dgu.model.commitment import Commitment context = { 'model': model, 'session': model.Session, 'user': c.user or c.author, 'extras_as_string': True, 'save': 'save' in request.params } try: c.publisher_dict = get_action('organization_show')(context, { 'id': id }) except NotAuthorized: abort(401, _('Unauthorized to read commitments')) except ObjectNotFound: abort(404, _('Publisher not found')) c.publisher = context.get('group') c.commitments = Commitment.get_for_publisher(c.publisher.name)\ .order_by('commitment.dataset_name').all() return render('commitment/read.html')
def commitments(self, id): """ Shows all of the commitments for the specified publisher """ from ckanext.dgu.model.commitment import Commitment context = {'model': model, 'session': model.Session, 'user': c.user or c.author, 'extras_as_string': True, 'save': 'save' in request.params} try: c.publisher_dict = get_action('organization_show')(context, {'id': id}) except NotAuthorized: abort(401, _('Unauthorized to read commitments')) except ObjectNotFound: abort(404, _('Publisher not found')) c.publisher = context.get('group') c.commitments = Commitment.get_for_publisher(c.publisher.name).order_by('commitment.dataset_name').all() return render('commitment/read.html')
def edit(self, id): """ Allows editing of commitments for a specific publisher """ from ckanext.dgu.model.commitment import Commitment from ckanext.dgu.lib import helpers as dgu_helpers context = {'model': model, 'session': model.Session, 'user': c.user or c.author, 'extras_as_string': True, 'save': 'save' in request.params} if not dgu_helpers.is_sysadmin(): abort(401, "You are not allowed to edit the commitments for this publisher") try: c.publisher_dict = get_action('organization_show')(context, {'id': id}) except NotAuthorized: abort(401, _('Unauthorized to read commitments')) except ObjectNotFound: abort(404, _('Publisher not found')) c.publisher = context.get('group') c.publishers = model.Session.query(model.Group).filter(model.Group.state=='active')\ .order_by(model.Group.title).all() c.errors = {} if request.method == "POST": # need to flatten the request into some commitments, if there is an ID then # we should update them, if there isn't then add them. # TODO: Work out how to remove them. Perhaps get the current IDs and do a diff # If successful redirect to read() h.redirect_to(h.url_for(controller='ckanext.dgu.controllers.commitment:CommitmentController', action='commitments', id=c.publisher.name)) c.commitments = Commitment.get_for_publisher(c.publisher.name).order_by('commitment.commitment_text') return render('commitment/edit.html')
class CommitmentController(BaseController): def index(self): from ckanext.dgu.model.commitment import Commitment, ODS_ORGS c.publishers = model.Session.query(model.Group)\ .filter(model.Group.state=='active')\ .filter(model.Group.name.in_(ODS_ORGS.values()))\ .order_by(model.Group.title).all() c.commitments = model.Session.query(Commitment).filter( Commitment.state == 'active').all() return render('commitment/index.html') def commitments(self, id): """ Shows all of the commitments for the specified publisher """ from ckanext.dgu.model.commitment import Commitment context = { 'model': model, 'session': model.Session, 'user': c.user or c.author, 'extras_as_string': True, 'save': 'save' in request.params } try: c.publisher_dict = get_action('organization_show')(context, { 'id': id }) except NotAuthorized: abort(401, _('Unauthorized to read commitments')) except ObjectNotFound: abort(404, _('Publisher not found')) c.publisher = context.get('group') c.commitments = Commitment.get_for_publisher(c.publisher.name)\ .order_by('commitment.dataset_name').all() return render('commitment/read.html') def _save(self, context, data_dict, publisher): import ckan.model as model import ckanext.dgu.model.commitment as cmodel for _, items in data_dict.iteritems(): # items is now a list of dicts that can all be processed # individually if they have an ID, then we're updating, # if they don't they're new. for item in items: commitment = None if not item.get('commitment_text') and not item.get( 'dataset_name', ''): # TODO: We're ignoring empty dicts here as unfilled items but we # might well want to do some error checking too continue if item.get('id'): # Get the current commitment commitment = cmodel.Commitment.get(item.get('id')) commitment.source = item.get('source') else: commitment = cmodel.Commitment(source=item.get('source')) # text, notes, dataset (or url) commitment.commitment_text = item.get('commitment_text', '') commitment.notes = item.get('notes', '') commitment.dataset = item.get('dataset') or item.get('url') commitment.dataset_name = item.get('dataset_name', '') commitment.publisher = publisher.name commitment.author = c.user commitment.state = 'active' model.Session.add(commitment) # Commit each source to the database model.Session.commit() log.info("Commitments for {0} updated by {1}".format( publisher.name, c.user)) def edit(self, id): """ Allows editing of commitments for a specific publisher """ from ckanext.dgu.model.commitment import Commitment context = { 'model': model, 'session': model.Session, 'user': c.user, 'extras_as_string': True, 'save': 'save' in request.params } try: check_access('organization_update', {'id': id}) except Exception, e: abort(401, "Not authorised") try: c.publisher_dict = get_action('organization_show')(context, { 'id': id }) except NotAuthorized: abort(401, _('Unauthorised to read commitments')) except ObjectNotFound: abort(404, _('Publisher not found')) c.publisher = context.get('group') c.errors = {} if request.method == "POST": from ckan.logic import clean_dict, tuplize_dict, parse_params from ckan.lib.navl.dictization_functions import unflatten data_dict = clean_dict( unflatten(tuplize_dict(parse_params(request.params)))) self._save(context, data_dict, c.publisher) # We'll prefetch the available datasets for this publisher and add them to the drop-down # on the page so that we don't have to work out how to constrain an autocomplete. c.packages = model.Session.query(model.Package.name, model.Package.title)\ .filter(model.Package.state == 'active')\ .filter(model.Package.owner_org == c.publisher.id )\ .order_by(model.Package.title).all() if request.method == "POST": # need to flatten the request into some commitments, if there is an ID then # we should update them, if there isn't then add them. # TODO: Work out how to remove them. Perhaps get the current IDs and do a diff # If successful redirect to read() h.redirect_to( h.url_for( controller= 'ckanext.dgu.controllers.commitment:CommitmentController', action='commitments', id=c.publisher.name)) c.commitments = Commitment.get_for_publisher( c.publisher.name).order_by('commitment.commitment_text') return render('commitment/edit.html')
def process_row(row): """ Reads each row and tries to create a new commitment database entry after trying to determine if a matching one already exists. """ import ckan.model as model from ckanext.dgu.model.commitment import Commitment from ckanext.dgu.model.commitment import ODS_ORGS, ODS_LINKS short_org = row[0].strip() org_name = ODS_ORGS.get(short_org, None) if not org_name: raise ingest.IngestException("Failed to lookup group {0}".format(short_org), True) org = model.Group.get(org_name) if not org: raise ingest.IngestException("Failed to find group {0}".format(org_name), True) dataset = None # Handle multiple values in the URL field parts = row[6].strip().split() if parts: dataset_name = self._url_to_dataset_name(parts[0]) dataset = model.Session.query(model.Package)\ .filter(model.Package.name==dataset_name)\ .filter(model.Package.state=='active').first() if not dataset: if parts and parts[0].startswith('http'): dataset = parts[0] else: dataset = "" source = row[1] name = row[2] text = row[3] notes = row[4] or '' published = row[5] # Delete a record that matches based on source and name c = model.Session.query(Commitment)\ .filter(Commitment.source==source)\ .filter(Commitment.dataset_name==name)\ .filter(Commitment.commitment_text==text)\ .filter(Commitment.publisher==org.name).first() if not c: c = Commitment() log.info("Creating new commitment") else: log.info("Updating existing commitment") c.source = source c.commitment_text = text c.notes = notes c.publisher = org.name c.author = '' c.dataset_name = name if dataset and hasattr(dataset, 'name'): c.dataset = dataset.name else: c.dataset = dataset c.state = 'active' model.Session.add(c) model.Session.commit()