def save_extra_package_multilang(pkg, lang, field_type): try: from ckanext.multilang.model import PackageMultilang except ImportError: log.warn('DCAT-AP_IT: multilang extension not available.') return log.debug('Creating create_loc_field for package ID: %r', str(pkg.get('id'))) PackageMultilang.persist(pkg, lang, field_type) log.info('Localized field created successfully')
def persist_package_multilang_field(self, package_id, field_name, text, locale, field_type): record = PackageMultilang.get(package_id, field_name, locale, field_type) if record: log.info('::::::::: Updating the localized {0} package field in the package_multilang table :::::::::'.format(field_name)) record.text = text record.save() log.info('::::::::: PACKAGE MULTILANG FIELD UPDATED SUCCESSFULLY :::::::::') else: log.info('::::::::: Adding new localized {0} package field in the package_multilang table :::::::::'.format(field_name)) PackageMultilang.persist({'id': package_id, 'text': text, 'field': field_name}, locale, field_type) log.info('::::::::: PACKAGE MULTILANG FIELD PERSISTED SUCCESSFULLY :::::::::')
def upsert_package_multilang(pkg_id, field_name, field_type, lang, text): try: from ckanext.multilang.model import PackageMultilang except ImportError: log.warn('DCAT-AP_IT: multilang extension not available.') return pml = PackageMultilang.get(pkg_id, field_name, lang, field_type) if not pml and text: PackageMultilang.persist({'id':pkg_id, 'field':field_name, 'text':text}, lang, field_type) elif pml and not text: pml.purge() elif pml and not pml.text == text: pml.text = text pml.save()
def after_import_stage(self, package_dict): log.info( '::::::::: Performing after_import_stage persist operation for localised dataset content :::::::::' ) if bool(self._package_dict): session = Session package_id = package_dict.get('id') # Persisting localized packages try: # rows = session.query(PackageMultilang).filter(PackageMultilang.package_id == package_id).all() rows = PackageMultilang.get_for_package(package_id) if not rows: log.info( '::::::::: Adding new localised object to the package_multilang table :::::::::' ) log.debug('::::: Persisting default metadata locale :::::') loc_titles = self._package_dict.get('localised_titles') if loc_titles: log.debug('::::: Persisting title locales :::::') for title in loc_titles: PackageMultilang.persist( { 'id': package_id, 'text': title.get('text'), 'field': 'title' }, title.get('locale')) loc_abstracts = self._package_dict.get( 'localised_abstracts') if loc_abstracts: log.debug('::::: Persisting abstract locales :::::') for abstract in loc_abstracts: PackageMultilang.persist( { 'id': package_id, 'text': abstract.get('text'), 'field': 'notes' }, abstract.get('locale')) log.info( '::::::::: OBJECT PERSISTED SUCCESSFULLY :::::::::') else: log.info( '::::::::: Updating localised object in the package_multilang table :::::::::' ) for row in rows: if row.field == 'title': titles = self._package_dict.get('localised_titles') if titles: for title in titles: if title.get('locale') == row.lang: row.text = title.get('text') elif row.field == 'notes': abstracts = self._package_dict.get( 'localised_abstracts') if abstracts: for abstract in abstracts: if abstract.get('locale') == row.lang: row.text = abstract.get('text') row.save() log.info('::::::::: OBJECT UPDATED SUCCESSFULLY :::::::::') pass except Exception, e: # on rollback, the same closure of state # as that of commit proceeds. session.rollback() log.error('Exception occurred while persisting DB objects: %s', e) raise # Persisting localized Tags loc_tags = self._package_dict.get('localized_tags') if loc_tags: log.debug('::::: Persisting tag locales :::::') for tag in loc_tags: tag_name = tag.get('text') tag_lang = tag.get('locale') tag_localized_name = tag.get('localized_text') tag = TagMultilang.by_name(tag_name, tag_lang) if tag: # Update the existing record if tag_localized_name and tag_localized_name != tag.text: tag.text = tag_localized_name try: tag.save() log.info( '::::::::: OBJECT TAG UPDATED SUCCESSFULLY :::::::::' ) pass except Exception, e: # on rollback, the same closure of state # as that of commit proceeds. session.rollback() log.error( 'Exception occurred while persisting DB objects: %s', e) raise else: # Create a new localized record existing_tag = model.Tag.by_name(tag_name) if existing_tag: TagMultilang.persist( { 'id': existing_tag.id, 'name': tag_name, 'text': tag_localized_name }, tag_lang) log.info( '::::::::: OBJECT TAG PERSISTED SUCCESSFULLY :::::::::' )
def _save_new(self, context, package_type=None): # The staged add dataset used the new functionality when the dataset is # partially created so we need to know if we actually are updating or # this is a real new. is_an_update = False ckan_phase = request.params.get('_ckan_phase') from ckan.lib.search import SearchIndexError try: data_dict = clean_dict( dict_fns.unflatten(tuplize_dict(parse_params(request.POST)))) if ckan_phase: # prevent clearing of groups etc context['allow_partial_update'] = True # sort the tags if 'tag_string' in data_dict: data_dict['tags'] = self._tag_string_to_list( data_dict['tag_string']) if data_dict.get('pkg_name'): is_an_update = True # This is actually an update not a save data_dict['id'] = data_dict['pkg_name'] del data_dict['pkg_name'] # don't change the dataset state data_dict['state'] = 'draft' # this is actually an edit not a save pkg_dict = get_action('package_update')(context, data_dict) if request.params['save'] == 'go-metadata': # redirect to add metadata url = h.url_for(controller='package', action='new_metadata', id=pkg_dict['name']) else: # redirect to add dataset resources url = h.url_for(controller='package', action='new_resource', id=pkg_dict['name']) redirect(url) # Make sure we don't index this dataset if request.params['save'] not in [ 'go-resource', 'go-metadata' ]: data_dict['state'] = 'draft' # allow the state to be changed context['allow_state_change'] = True data_dict['type'] = package_type context['message'] = data_dict.get('log_message', '') # MULTILANG - retrieving dict for localized tag's strings extra_tag = None if data_dict.get('extra_tag'): extra_tag = data_dict.get('extra_tag') # After saving in memory the extra_tag dict this must be removed because not present in the schema del data_dict['extra_tag'] pkg_dict = get_action('package_create')(context, data_dict) lang = helpers.getLanguage() # MULTILANG - persisting tags self.localized_tags_persist(extra_tag, pkg_dict, lang) # MULTILANG - persisting the localized package dict log.info('::::: Persisting localised metadata locale :::::') for field in self.pkg_localized_fields: if pkg_dict.get(field): PackageMultilang.persist( { 'id': pkg_dict.get('id'), 'field': field, 'text': pkg_dict.get(field) }, lang) if ckan_phase: # redirect to add dataset resources url = h.url_for(controller='package', action='new_resource', id=pkg_dict['name']) redirect(url) self._form_save_redirect(pkg_dict['name'], 'new', package_type=package_type) except NotAuthorized: abort(401, _('Unauthorized to read package %s') % '') except NotFound, e: abort(404, _('Dataset not found'))
def _save_edit(self, name_or_id, context, package_type=None): from ckan.lib.search import SearchIndexError log.debug('Package save request name: %s POST: %r', name_or_id, request.POST) try: data_dict = clean_dict( dict_fns.unflatten(tuplize_dict(parse_params(request.POST)))) if '_ckan_phase' in data_dict: # we allow partial updates to not destroy existing resources context['allow_partial_update'] = True if 'tag_string' in data_dict: data_dict['tags'] = self._tag_string_to_list( data_dict['tag_string']) del data_dict['_ckan_phase'] del data_dict['save'] context['message'] = data_dict.get('log_message', '') data_dict['id'] = name_or_id # MULTILANG - retrieving dict for localized tag's strings extra_tag = None if data_dict.get('extra_tag'): extra_tag = data_dict.get('extra_tag') # After saving in memory the extra_tag dict this must be removed because not present in the schema del data_dict['extra_tag'] pkg = get_action('package_update')(context, data_dict) c.pkg = context['package'] c.pkg_dict = pkg lang = helpers.getLanguage() # MULTILANG - persisting tags self.localized_tags_persist(extra_tag, c.pkg_dict, lang) # MULTILANG - persisting package dict log.info( ':::::::::::: Saving the corresponding localized title and abstract :::::::::::::::' ) # q_results = model.Session.query(PackageMultilang).filter(PackageMultilang.package_id == c.pkg_dict.get('id'), PackageMultilang.lang == lang).all() q_results = PackageMultilang.get_for_package_id_and_lang( c.pkg_dict.get('id'), lang) if q_results: pkg_processed_field = [] for result in q_results: # do not update multilang field if original pkg dict doesn't # have this field anymore. otherwise IntegrityError will raise # because text will be None if c.pkg_dict.has_key(result.field): pkg_processed_field.append(result.field) log.debug('::::::::::::::: value before %r', result.text) result.text = c.pkg_dict.get(result.field) log.debug('::::::::::::::: value after %r', result.text) result.save() # Check for missing localized fields in DB for field in self.pkg_localized_fields: if field not in pkg_processed_field: if c.pkg_dict.get(field): PackageMultilang.persist( { 'id': c.pkg_dict.get('id'), 'field': field, 'text': c.pkg_dict.get(field) }, lang) else: log.info( ':::::::::::: Localised fields are missing in package_multilang table, persisting defaults using values in the table package :::::::::::::::' ) for field in self.pkg_localized_fields: if c.pkg_dict.get(field): PackageMultilang.persist( { 'id': c.pkg_dict.get('id'), 'field': field, 'text': c.pkg_dict.get(field) }, lang) self._form_save_redirect(pkg['name'], 'edit', package_type=package_type) except NotAuthorized: abort(401, _('Unauthorized to read package %s') % id) except NotFound, e: abort(404, _('Dataset not found'))