def before_search(self, search_params): search_string = search_params.get("q") or "" # for search cloud we don't make any changes to the search_params, # just log the search string to the database for later analysis. # do some clean up of the search string so that the analysis # will be easier later search_string = searchcloud.unify_terms(search_string, max_length=200) if not search_string: return search_params lang = str(helpers.current_locale()) try: # Unfortunately a nested session doesn't behave the way we want, # failing to actually commit the change made. # We can either create a separate connection for this # functionality on each request (potentially costly), # or just commit at this point on the basis that for a search # request, no changes that can't be committed will have been # saved to the database. For now, we choose the latter. ## model.Session.begin_nested() # establish a savepoint searchcloud.track_term(model.Session, lang, search_string) except sqlalchemy.exc.ProgrammingError, e: # We don't want the non-existence of the search_query table to # crash searches, we just won't log queries log.error(e) if 'relation "search_query" does not exist' in str(e): log.error( "Please run the paster searchcloud-install-tables " "command to set up the correct tables for " "search logging" ) model.Session.rollback() else: raise
def setup_template_variables(self, context, data_dict=None, package_type=None): c = tk.c ckan_lang = str(helpers.current_locale()) ckan_lang_fallback = str(helpers.fallback_locale()) c.is_sysadmin = new_authz.is_sysadmin(c.user) factory = ControlledVocabularyFactory() #self.license = factory.get_controlled_vocabulary_util(ControlledVocabularyFactory.LICENSE) #type: ControlledVocabularyUtil # Initialize cache if needed self.license = controlled_vocabulary_util.retrieve_all_licenses(ckan_lang) # Initialize cache if needed self.geographical_coverage = controlled_vocabulary_util.retrieve_all_geographical_coverage(ckan_lang) self.controlled_keyword = controlled_vocabulary_util.retrieve_all_controlled_keyword(ckan_lang) self.domains_eurovoc = controlled_vocabulary_util.retrieve_all_themes(ckan_lang) self.status = controlled_vocabulary_util.retrieve_all_datasets_status(ckan_lang) self.type_of_dataset = controlled_vocabulary_util.retrieve_all_datasets_types(ckan_lang) self.languages = controlled_vocabulary_util.retrieve_all_languages(ckan_lang) self.frequency = controlled_vocabulary_util.retrieve_all_frequencies(ckan_lang) self.temporal_granularity = controlled_vocabulary_util.retrieve_all_time_periodicity(ckan_lang) #self.notation_skos = controlled_vocabulary_util.retrieve_all_notation_skos(ckan_lang) self.notation_skos = controlled_vocabulary_util.retrieve_all_notation_types(ckan_lang) self.formats = factory.get_controlled_vocabulary_util(ControlledVocabularyFactory.FILE_TYPE) #type: ControlledVocabularyUtil if (c.action in (u'edit', u'new', u'editresources', u'manage_package', u'update', 'bulk_update')): c.license = self.license c.geographical_coverage = self.geographical_coverage c.controlled_keyword = self.controlled_keyword c.domains_eurovoc = self.domains_eurovoc c.publishers = helpers.organizations_available(c.user) c.status = controlled_vocabulary_util.retrieve_all_datasets_status(ckan_lang) c.type_of_dataset = controlled_vocabulary_util.retrieve_all_datasets_types(ckan_lang) c.languages = controlled_vocabulary_util.retrieve_all_languages(ckan_lang) c.temporal_granularity = controlled_vocabulary_util.retrieve_all_time_periodicity(ckan_lang) c.groups = homepage.get_groups('display_name') c.frequency = self.frequency c.notation_skos = self.notation_skos c.formats = self.formats.get_all_values_for_form(ckan_lang) # datasets_titles_dict = self.retrieve_all_datasets_titles(c) # c.datasets = datasets_titles_dict if c.action in (u'dashboard'): c.formats = FORMATS # get new group name if group ID in query string new_group_id = pylons.request.params.get('groups__0__id') if new_group_id: try: data = {'id': new_group_id} new_group = p.toolkit.get_action('group_show')(context, data) c.new_group = new_group['name'] except p.toolkit.ObjectNotFound: c.new_group = None # find extras that are not part of our schema c.additional_extras = [] schema_keys = self.create_package_schema().keys() if c.pkg_dict: extras = c.pkg_dict.get('extras', []) if extras: for extra in extras: if not extra['key'] in schema_keys: c.additional_extras.append(extra) # This is messy as auths take domain object not data_dict context_pkg = context.get('package', None) pkg = context_pkg or c.pkg if pkg: try: if not context_pkg: context['package'] = pkg logic.check_access('package_update', context) c.auth_for_change_state = True except logic.NotAuthorized: c.auth_for_change_state = False
def index(self): context = {'for_view': True} start = time.time() locale = ckanext_helpers.current_locale().language c.package_count = ckanext_helpers.get_package_count() c.approved_search_terms = ckanext_helpers.approved_search_terms() c.most_viewed_datasets = ckanext_helpers.most_viewed_datasets() c.recent_updates = ckanext_helpers.recent_updates(10) publishers = get_action('get_skos_hierarchy')(context, None) factory = ControlledVocabularyFactory() publ_mdr = factory.get_controlled_vocabulary_util(ControlledVocabularyFactory.CORPORATE_BODY) #type: CorporateBodiesUtil for top_level, item in publishers.items(): translation = publ_mdr.get_translation_for_language(top_level, locale) item['name'] = top_level.split('/')[-1].lower() item['label'] = translation interim = [] for child in item.get('children', []): translation = publ_mdr.get_translation_for_language(child[0], locale) interim.append((child[0].split('/')[-1].lower(), translation, child[1])) item['children'] = sorted(interim, key=lambda child: child[1]) c.get_skos_hierarchy = publishers c.most_common_themes = get_action('theme_list')(context, {'mode': 'most_common'}) c.less_common_themes = get_action('theme_list')(context, {'mode': 'less_common'}) #c.get_eurovoc_domains_by_packages_with_cache_most =homepage.get_eurovoc_domains_by_packages_with_cache('most_common', locale) #c.get_eurovoc_domains_by_packages_with_cache_less= homepage.get_eurovoc_domains_by_packages_with_cache('less_common', locale) duration = time.time()-start log.info("Build all cache took {0}".format(duration)) if c.userobj is not None: msg = None url = h.url_for(controller='user', action='edit') is_google_id = \ c.userobj.name.startswith( 'https://www.google.com/accounts/o8/id') if not c.userobj.email and (is_google_id and not c.userobj.fullname): msg = _(u'Please <a href="{link}">update your profile</a>' u' and add your email address and your full name. ' u'{site} uses your email address' u' if you need to reset your password.'.format( link=url, site=g.site_title)) elif not c.userobj.email: msg = _('Please <a href="%s">update your profile</a>' ' and add your email address. ') % url + \ _('%s uses your email address' ' if you need to reset your password.') \ % g.site_title elif is_google_id and not c.userobj.fullname: msg = _('Please <a href="%s">update your profile</a>' ' and add your full name.') % (url) if msg: h.flash_notice(msg, allow_html=True) log.info("Use Home extension") start = time.time() response_str = base.render('home/index.html', cache_force=True) duration = time.time() - start log.info("Build Render index took {0}".format(duration)) # request.environ['CKAN_PAGE_CACHABLE'] = True # response.headers["Cache-Control"] = "max-age=604800" return response_str # return base.render('home/static.html', cache_force=True)
def setup_template_variables(self, context, data_dict=None, package_type=None): c = p.toolkit.c ckan_lang = str(helpers.current_locale()) ckan_lang_fallback = str(helpers.fallback_locale()) c.licences = sorted(model.Package.get_license_options(), key=operator.itemgetter(1)) default_licence = ( "Europa Legal Notice", "http://open-data.europa.eu/kos/licence/EuropeanCommission") c.licences = filter(lambda l: l != default_licence, c.licences) c.licences.insert(0, default_licence) c.is_sysadmin = Authorizer().is_sysadmin(c.user) c.status = _tags_and_translations( context, STATUS_VOCAB_NAME, ckan_lang, ckan_lang_fallback) c.interoperability_levels = _tags_and_translations( context, INTEROP_VOCAB_NAME, ckan_lang, ckan_lang_fallback) c.type_of_dataset = _tags_and_translations( context, DATASET_TYPE_VOCAB_NAME, ckan_lang, ckan_lang_fallback) c.geographical_coverage = _tags_and_translations( context, GEO_VOCAB_NAME, ckan_lang, ckan_lang_fallback) c.languages = _tags_and_translations( context, LANGUAGE_VOCAB_NAME, ckan_lang, ckan_lang_fallback) c.temporal_granularity = [(u'', u'')] + _tags_and_translations( context, TEMPORAL_VOCAB_NAME, ckan_lang, ckan_lang_fallback) # publisher IDs and name translations c.publishers = helpers.groups_available(c.user) # get new group name if group ID in query string new_group_id = pylons.request.params.get('groups__0__id') if new_group_id: try: data = {'id': new_group_id} new_group = p.toolkit.get_action('group_show')(context, data) c.new_group = new_group['name'] except p.toolkit.ObjectNotFound: c.new_group = None # find extras that are not part of our schema c.additional_extras = [] schema_keys = self.form_to_db_schema().keys() if c.pkg_dict: extras = c.pkg_dict.get('extras', []) for extra in extras: if not extra['key'] in schema_keys: c.additional_extras.append(extra) # This is messy as auths take domain object not data_dict context_pkg = context.get('package', None) pkg = context_pkg or c.pkg if pkg: try: if not context_pkg: context['package'] = pkg logic.check_access('package_change_state', context) c.auth_for_change_state = True except logic.NotAuthorized: c.auth_for_change_state = False