def create(self, model_obj): otype = model_obj.type lang = helpers.getLanguage() ## CREATE GROUP OR ORGANIZATION if otype == 'group' or otype == 'organization' and lang: log.info('::::: Persisting localised metadata locale :::::') lang = helpers.getLanguage() group = model_dictize.group_dictize(model_obj, {'model': model, 'session': model.Session}) GroupMultilang.persist(group, lang)
def get_localized_pkg(pkg_dict): if pkg_dict != '' and 'type' in pkg_dict: # MULTILANG - Localizing package dict lang = getLanguage() # MULTILANG - Localizing Tags display names in Facet list tags = pkg_dict.get('tags') if tags: for tag in tags: localized_tag = TagMultilang.by_name(tag.get('name'), lang) if localized_tag: tag['display_name'] = localized_tag.text # q_results = model.Session.query(PackageMultilang).filter(PackageMultilang.package_id == pkg_dict.get('id'), PackageMultilang.lang == lang).all() q_results = PackageMultilang.get_for_package_id_and_lang(pkg_dict.get('id'), lang) if q_results: for result in q_results: pkg_dict[result.field] = result.text # MULTILANG - Localizing organization sub dict for the dataset edit page organization = pkg_dict.get('organization') if organization: # q_results = model.Session.query(GroupMultilang).filter(GroupMultilang.group_id == organization.get('id'), GroupMultilang.lang == lang).all() q_results = GroupMultilang.get_for_group_id_and_lang(organization.get('id'), lang) if q_results: for result in q_results: organization[result.field] = result.text pkg_dict['organization'] = organization return pkg_dict
def edit(self, model_obj): otype = model_obj.type lang = helpers.getLanguage() ## EDIT GROUP OR ORGANIZATION if otype == 'group' or otype == 'organization' and lang: group = model_dictize.group_dictize(model_obj, { 'model': model, 'session': model.Session }) # q_results = model.Session.query(GroupMultilang).filter(GroupMultilang.group_id == group.get('id')).all() q_results = GroupMultilang.get_for_group_id(group.get('id')) create_new = False if q_results: available_db_lang = [] for result in q_results: if result.lang not in available_db_lang: available_db_lang.append(result.lang) # check if the group identifier name has been changed if result.name != group.get('name'): result.name = group.get('name') result.save() if lang not in available_db_lang: create_new = True else: for result in q_results: if result.lang == lang: result.text = group.get(result.field) result.save() else: create_new = True if create_new == True: log.info( ':::::::::::: Localized fields are missing in package_multilang table, persisting defaults using values in the table group :::::::::::::::' ) GroupMultilang.persist(group, lang)
def after_search(self, search_results, search_params): search_facets = search_results.get('search_facets') lang = helpers.getLanguage() if search_facets and lang: if 'tags' in search_facets: # MULTILANG - Localizing Tags display names in Facet list tags = search_facets.get('tags') for tag in tags.get('items'): localized_tag = TagMultilang.by_name(tag.get('name'), lang) if localized_tag: tag['display_name'] = localized_tag.text if 'organization' in search_facets: # MULTILANG - Localizing Organizations display names in Facet list organizations = search_facets.get('organization') for org in organizations.get('items'): # q_results = model.Session.query(GroupMultilang).filter(GroupMultilang.name == org.get('name'), GroupMultilang.lang == lang).all() q_results = GroupMultilang.get_for_group_name_and_lang( org.get('name'), lang) if q_results: for result in q_results: if result.field == 'title': org['display_name'] = result.text if 'groups' in search_facets: # MULTILANG - Localizing Groups display names in Facet list groups = search_facets.get('groups') for group in groups.get('items'): # q_results = model.Session.query(GroupMultilang).filter(GroupMultilang.name == group.get('name'), GroupMultilang.lang == lang).all() q_results = GroupMultilang.get_for_group_name_and_lang( group.get('name'), lang) if q_results: for result in q_results: if result.field == 'title': group['display_name'] = result.text search_results['search_facets'] = search_facets return search_results
def get_localized_group(org_dict): # MULTILANG - Localizing group dict if org_dict != '' and 'type' in org_dict: lang = getLanguage() # q_results = model.Session.query(GroupMultilang).filter(GroupMultilang.group_id == org_dict.get('id'), GroupMultilang.lang == lang).all() q_results = GroupMultilang.get_for_group_id_and_lang(org_dict.get('id'), lang) if q_results: for result in q_results: org_dict[result.field] = result.text if result.field == 'title': org_dict['display_name'] = result.text return org_dict
def get_for_group_or_organization(pkg_id): ''' Returns all the localized fields of group (or organization), in a dict of dicts, i.e.: {FIELDNAME:{LANG:label,...},...} Returns None if multilang extension not loaded. ''' try: from ckanext.multilang.model import GroupMultilang except ImportError: log.warn('DCAT-AP_IT: multilang extension not available.') # TODO: if no multilang, return the dataset in a single language in the same format of the multilang data return None records = GroupMultilang.get_for_group_id(pkg_id) return _multilang_to_dict(records)
def handle_organization(self, harvest_object, organisation_mapping, values): citedResponsiblePartys = values["cited-responsible-party"] organisation = None for party in citedResponsiblePartys: if party["role"] == "owner": for org in organisation_mapping: if org.get("value") in party["organisation-name"]: existing_org = model.Session.query(model.Group).filter( model.Group.name == org.get("key")).first() if not existing_org: log.warning( '::::::::: Organisation not found in CKAN: %r: assigning default :::::::::', org.get("key")) else: organisation = existing_org.id log.debug( '::::: Collecting localized data from the organization name :::::' ) localized_org = [] localized_org.append({ 'text': org.get("value_" + self._ckan_locales_mapping[ values["metadata-language"].lower()]) or org.get("value"), 'locale': self._ckan_locales_mapping[ values["metadata-language"].lower()] }) for entry in party["organisation-name-localized"]: if entry['text'] and entry['locale'].lower( )[1:]: if self._ckan_locales_mapping[ entry['locale'].lower()[1:]]: localized_org.append({ 'text': org.get("value_" + self._ckan_locales_mapping[ entry['locale'].lower( )[1:]]) or entry['text'], 'locale': self._ckan_locales_mapping[ entry['locale'].lower()[1:]] }) else: log.warning( 'Locale Mapping not found for organization name, entry skipped!' ) else: log.warning( 'TextGroup data not available for organization name, entry skipped!' ) log.debug( "::::::::::::: Persisting localized ORG :::::::::::::" ) # rows = session.query(GroupMultilang).filter(GroupMultilang.group_id == organisation).all() rows = GroupMultilang.get_for_group_id( organisation) for localized_entry in localized_org: insert = True for row in rows: if row.lang == localized_entry.get( "locale") and row.field == 'title': # Updating the org localized record row.text = localized_entry.get("text") row.save() insert = False log.debug( "::::::::::::: ORG locale successfully updated :::::::::::::" ) if insert: # Inserting the missing org localized record org_dict = { 'id': organisation, 'name': existing_org.name, 'title': localized_entry.get("text"), 'description': localized_entry.get("text") } GroupMultilang.persist( org_dict, localized_entry.get("locale")) log.debug( "::::::::::::: ORG locale successfully added :::::::::::::" ) return organisation
def before_view(self, odict): otype = odict.get('type') lang = helpers.getLanguage() if lang: if otype == 'group' or otype == 'organization': # MULTILANG - Localizing Group/Organizzation names and descriptions in search list # q_results = model.Session.query(GroupMultilang).filter(GroupMultilang.group_id == odict.get('id'), GroupMultilang.lang == lang).all() q_results = GroupMultilang.get_for_group_id_and_lang( odict.get('id'), lang) if q_results: for result in q_results: odict[result.field] = result.text if result.field == 'title': odict['display_name'] = result.text elif otype == 'dataset': # MULTILANG - Localizing Datasets names and descriptions in search list # MULTILANG - Localizing Tags display names in Facet list tags = odict.get('tags') if tags: for tag in tags: localized_tag = TagMultilang.by_tag_id( tag.get('id'), lang) if localized_tag: tag['display_name'] = localized_tag.text # MULTILANG - Localizing package sub dict for the dataset read page # q_results = model.Session.query(PackageMultilang).filter(PackageMultilang.package_id == odict['id'], PackageMultilang.lang == lang).all() q_results = PackageMultilang.get_for_package_id_and_lang( odict.get('id'), lang) if q_results: for result in q_results: if odict.get(result.field, None): odict[result.field] = result.text else: extras = odict.get('extras', None) if extras and len(extras) > 0: for extra in extras: extra_key = extra.get('key', None) if extra_key and extra_key == result.field: extra['value'] = result.text #if result.field == 'notes': # odict['notes'] = result.text # MULTILANG - Localizing organization sub dict for the dataset read page organization = odict.get('organization') if organization: # q_results = model.Session.query(GroupMultilang).filter(GroupMultilang.group_id == organization.get('id'), GroupMultilang.lang == lang).all() q_results = GroupMultilang.get_for_group_id_and_lang( organization.get('id'), lang) if q_results: for result in q_results: organization[result.field] = result.text odict['organization'] = organization # MULTILANG - Localizing resources dict resources = odict.get('resources') if resources: for resource in resources: # q_results = model.Session.query(ResourceMultilang).filter(ResourceMultilang.resource_id == resource.get('id'), ResourceMultilang.lang == lang).all() q_results = ResourceMultilang.get_for_resource_id_and_lang( resource.get('id'), lang) if q_results: for result in q_results: resource[result.field] = result.text return odict