def menu_links(section=None): drupal_url = config.get('drupal.site_url') if drupal_url == None or drupal_url == h.full_current_url().split('?')[0][:-1]: return None # request links from Drupal r = None section_menu = [] try: r = requests.get(drupal_url + '/menu_export', verify=False, timeout=10) except requests.exceptions.Timeout: log.warning(drupal_url + '/menu_export connection timeout') except requests.exceptions.TooManyRedirects: log.warning(drupal_url + '/menu_export too many redirects') except requests.exceptions.RequestException as e: log.error(e.message) if r: links = r.json() else: return None if links: if section in links: if section == 'main': for item in links[section]: if item['link'] == '<front>': item['link'] = drupal_url section_menu.extend(links[section]) return section_menu else: return None
def proxy_solr(self, action): url = urlparse.urlparse(h.full_current_url()) if url.query != '': data = urlparse.parse_qs(urllib.unquote(url.query).decode('utf-8')) else: data = ast.literal_eval(p.toolkit.request.body) content_type = data.get('wt', 'xml') if isinstance(content_type, list): content_type = content_type[0] ckan_response = p.toolkit.response ckan_response.content_type = CONTENT_TYPES[content_type] solr_response = '' if content_type == 'csv': ckan_response.headers[ 'Content-Disposition'] = 'attachment; filename=query.csv' solr_response = str(codecs.BOM_UTF8) conn = make_connection() try: solr_response += conn.raw_query(**data) ckan_response.body = solr_response except SolrException, e: ckan_response.status_int = e.httpcode ckan_response.status = str(e.httpcode) + ' ' + e.reason ckan_response.body = e.body
def proxy_solr(self, action): url = urlparse.urlparse(h.full_current_url()) if url.query != '': data = urlparse.parse_qs(urllib.unquote(url.query).decode('utf-8')) else: data = ast.literal_eval(p.toolkit.request.body) content_type = data.get('wt', 'xml') if isinstance(content_type, list): content_type = content_type[0] ckan_response = p.toolkit.response ckan_response.content_type = CONTENT_TYPES[content_type] solr_response = '' if content_type == 'csv': ckan_response.headers['Content-Disposition'] = 'attachment; filename=query.csv' solr_response = str(codecs.BOM_UTF8) conn = make_connection() try: solr_response += conn.raw_query(**data) ckan_response.body = solr_response except SolrException, e: ckan_response.status_int = e.httpcode ckan_response.status = str(e.httpcode) + ' ' + e.reason ckan_response.body = e.body
def create_report(ctx, report_name, report_type, csv_str): # Save report to table report = db.Report(report_name, csv_str) report.save() # Create dataset ctx['return_id_only'] = True dataset_id = tk.get_action('package_create')(ctx, { 'name': uuid.uuid4(), 'title': report_name, 'type': 'saabreport', 'schema': 'saabreport', 'author': ctx['user'] }) # Create resource new_path = '/get_report?report_id=%s' % report.id tk.get_action('resource_create')(ctx, { 'url': full_current_url().replace('/create_report', new_path).replace( '/create_chapter_report', new_path), 'package_id': dataset_id, 'description': 'Generated %s report' % report_type, 'type': 'text/csv', 'format': 'csv' }) tk.redirect_to(controller='package', action='read', id=dataset_id)
def menu_links(section=None): drupal_url = config.get('drupal.site_url') if drupal_url is None or (drupal_url == h.full_current_url().split('?')[0][:-1]): return None # request links from Drupal r = None section_menu = [] try: r = requests.get(drupal_url + '/menu_export', verify=False, timeout=10) except requests.exceptions.Timeout: log.warning(drupal_url + '/menu_export connection timeout') except requests.exceptions.TooManyRedirects: log.warning(drupal_url + '/menu_export too many redirects') except requests.exceptions.RequestException as e: log.error(e.message) if r: links = r.json() else: return None if links: if section in links: if section == 'main': for item in links[section]: if item['link'] == '<front>': item['link'] = drupal_url section_menu.extend(links[section]) return section_menu else: return None
def export_contentpackage(self, dataset_list): context = { 'model': model, 'session': model.Session, 'user': toolkit.c.user, } r = toolkit.response r.content_type = 'application/zip' query_str = h.full_current_url() query_str_s = query_str.split("dataset/contentpackage.zip?search_query%3D%252Fdataset%253F") if len(query_str_s) == 1: query_str_s = query_str_s[0].split("dataset/contentpackage.zip?search_query%3D%252Fdataset") search_json = "tags:map" search_json = query_str_s[0] + "api/action/package_search?fq=" + search_json + "&rows=1000" catalog_url = config.get('ckan.site_url', '') if not catalog_url[-1] == "/": catalog_url += "/" re = requests.get(catalog_url + "api/action/package_search?fq=tags:map&rows=2", verify=False) response = json.loads(re.content) dat_list = [] dat_str = "[" + dataset_list[3:-2].replace("', u'", ", ") + "]" da_str = dataset_list[3:-2].split("', u'") dd_str = [] for ddd in da_str: dd_str.append(ddd) try: content_package = toolkit.get_action('search_list_show_as_contentpackage')(context,{'dataset_list': dd_str}) except toolkit.ObjectNotFound: toolkit.abort(404, 'Dataset not found') r.content_disposition = 'attachment; filename=CKAN_CONTENTPACKAGE.zip' return content_package
def is_dataset_page(): current_url = helpers.full_current_url() dataset_page = app_globals.site_url + '/dataset' if current_url == dataset_page: return True else: return False
def get_required_items_missing(pkg_dict): if not pkg_dict: return [] if not pkg_dict.get("id"): m = re.search(r"/edit/([^/]*)", help.full_current_url()) pkg_dict = toolkit.get_action("package_show")(data_dict={ "id": m.group(1) }) if not pkg_dict.get("type"): m = re.search(r"/([^/]*?)/new", help.full_current_url()) type = m.group(1) else: type = pkg_dict['type'] required_dict = get_required_fields_name_label_dict(type) missing = [] for e in required_dict.keys(): if e in pkg_dict and not pkg_dict[e]: missing.append('{0}: Missing value'.format(required_dict[e])) if not pkg_dict['resources'] and resource_required(type): missing.append("At least one resource must exist") return missing
def abort(self, status_code, detail, headers, comment): """ HTTP Status 401 causes a login redirect. We need to prevent this unless we are actually trying to login. """ if (status_code == 401 and p.toolkit.request.environ['PATH_INFO'] != '/user/login'): if not p.toolkit.c.user: if NATIVE_LOGIN_ENABLED: h.flash_error(_('Requires authentication')) h.redirect_to('login', came_from=h.full_current_url()) h.redirect_to('saml2_unauthorized') return (status_code, detail, headers, comment)
def check_if_active(parent_menu, menu): current_url = helpers.full_current_url().replace('https', '').replace('http', '') menu_url = menu.get('url').replace('https', '').replace('http', '') # Set menu to active if the menu url matches the full current page url active = current_url == menu_url # Check menus that have no parent in the WP menu api structure, usually a CKAN submenu which has not been # configured through WordPress if active == False and parent_menu is None: active = menu_url in current_url return active
def proxy_solr(self, action): url = urlparse.urlparse(h.full_current_url()) query = urlparse.parse_qs(urllib.unquote(url.query).decode('utf-8')) content_type = query.get('wt', ['xml'])[0] ckan_response = p.toolkit.response ckan_response.content_type = CONTENT_TYPES[content_type] conn = make_connection() try: solr_response = conn.raw_query(**query) ckan_response.body = solr_response except SolrException, e: ckan_response.status_int = e.httpcode ckan_response.status = str(e.httpcode) + ' ' + e.reason ckan_response.body = e.body
def validate_access_application_url(key, data, errors, context): ''' Validate dataset's `access_application_URL`. Dummy value _must_ be added for a new form so that it can be overwritten in the same session in iPackageController `edit` hook. For REMS. ''' if data.get(('availability',)) == 'access_application': if data.get(('access_application_new_form',)) in [u'True', u'on']: data[key] = h.full_current_url().replace('/edit/', '/') else: not_empty(key, data, errors, context) url_validator(key, data, errors, context) else: data.pop(key, None) raise StopOnError
def app_add(self): if c.userobj is None: tk.redirect_to( tk.url_for(controller='user', action='login', came_from=full_current_url())) form = CreateAppForm(tk.request.POST) data_dict = clean_dict( dict_fns.unflatten(tuplize_dict(parse_params(tk.request.params)))) upload = uploader.get_uploader('apps') if tk.request.POST: if form.validate(): # Upload[load image upload.update_data_dict(data_dict, 'image_url', 'image_upload', 'clear_upload') try: upload.upload(uploader.get_max_image_size()) except logic.ValidationError as err: flash_error(err.error_dict['image_upload'][0]) else: app = App() form.populate_obj(app) app.author_id = c.userobj.id app.content = strip_tags(app.content) app.status = "pending" app.image_url = data_dict.get('image_url') app.save() log.debug("App data is valid. Content: %s", strip_tags(app.name)) flash_success(tk._('You successfully create app')) jobs.enqueue( send_notifications_on_change_app_status, [app, 'pending', tk.request.environ.get('CKAN_LANG')]) tk.redirect_to(app.get_absolute_url()) else: flash_error(tk._('You have errors in form')) log.info("Validate errors: %s", form.errors) context = {'form': form, 'active_boards': Board.filter_active()} log.debug('ForumController.thread_add context: %s', context) return self.__render('create_app.html', context)
def validate_access_application_url(key, data, errors, context): ''' Validate dataset's `access_application_URL`. Dummy value _must_ be added if user chooses either reetta option so that its value can be overwritten in ckanext-rems when it knows what the access_application_URL will be. If user has chosen the option to which access application URL is input directly, then validation checks its not empty and that it is an url. ''' if data.get(('availability',)) == 'access_application_rems' or \ data.get(('availability',)) == 'access_application_other': if data.get(('availability',)) == 'access_application_rems': data[key] = h.full_current_url().replace('/edit/', '/') elif data.get(('availability',)) == 'access_application_other': not_empty(key, data, errors, context) url_validator(key, data, errors, context) else: data.pop(key, None) raise StopOnError
def validate_access_application_url(key, data, errors, context): ''' Validate dataset's `access_application_URL`. Dummy value _must_ be added if user chooses either reetta option so that its value can be overwritten in ckanext-rems when it knows what the access_application_URL will be. If user has chosen the option to which access application URL is input directly, then validation checks its not empty and that it is an url. ''' if data.get(('availability',)) == 'access_application_rems' or \ data.get(('availability',)) == 'access_application_other': if data.get(('availability', )) == 'access_application_rems': data[key] = h.full_current_url().replace('/edit/', '/') elif data.get(('availability', )) == 'access_application_other': not_empty(key, data, errors, context) url_validator(key, data, errors, context) else: data.pop(key, None) raise StopOnError
def _spc_get_footer_from_drupal(drupal_url=None): if drupal_url is None or drupal_url == h.full_current_url( ).split('?')[0][:-1]: return None r = None try: r = requests.get( drupal_url + '/footer_export', verify=False, timeout=10 ) except requests.exceptions.Timeout: logger.warning(drupal_url + '/footer_export connection timeout') except requests.exceptions.TooManyRedirects: logger.warning(drupal_url + '/footer_export too many redirects') except requests.exceptions.RequestException as e: logger.error(e.message) if r: footer = r.json() else: return None if footer and 'main' in footer: return footer['main'][0]
def thread_add(self): if not c.userobj: tk.redirect_to( tk.url_for(controller='user', action='login', came_from=full_current_url())) if BannedUser.check_by_id(c.userobj): flash_error(tk._('You are banned')) tk.redirect_to(tk.url_for('forum_index')) form = CreateThreadForm(tk.request.POST) if tk.request.POST: if form.validate(): thread = tk.get_action('forum_create_thread')( { 'auth_user_obj': c.userobj }, form.data) log.debug("Form data is valid. Content: %s", thread.content) flash_success(tk._('You successfully create thread')) tk.redirect_to(thread.get_absolute_url()) else: flash_error(tk._('You have errors in form')) log.error("Validate errors: %s", form.errors) context = {'form': form, 'board_can_post': Board.filter_can_post()} return self.__render('create_thread.html', context)
def submit_feedback(self): """ Retrieves the necessary data and sends a feedback email to the appropriate recipient. """ context = { 'model': model, 'session': model.Session, 'user': c.user, 'for_view': True, 'auth_user_obj': c.userobj } protocol, host = h.get_site_protocol_and_host() full_current_url = h.full_current_url() if protocol is not None and host is not None and host in full_current_url: package = get_action('package_show')(context, { 'id': request.GET['id'] }) if 'error' not in package: data_dict = {} not_provided = 'Not provided' if 'name' not in request.GET: data_dict['name'] = not_provided else: data_dict['name'] = request.GET['name'].encode('utf8') if 'email' not in request.GET: data_dict['email'] = not_provided else: data_dict['email'] = request.GET['email'].encode('utf8') if 'comments' not in request.GET: data_dict['comments'] = not_provided else: data_dict['comments'] = request.GET['comments'].encode( 'utf8') data_dict['resource_id'] = request.GET.get('resource_id', '') data_dict['captcha'] = request.GET.get('captcha', '') if (data_dict.get('captcha', '') or request.GET.get( 'captchaCatch', 'none') not in ['dev', 'prod']): # Do not indicate failure or success since captcha was filled likely bot; # 7 is the expected arguments in the query string; # captchaCatch is serverside generated value hence can either be 'dev' or 'prod' h.redirect_to('/') return package # If there is value for either maintenance_email or author_email, use that. # If both of them null then send the email to [email protected] # Logic written to maintain legacy data # Once all the records in database have 'maintainer_email', # remove this and feedback_email = package.get('maintainer_email', '') feedback_email = package.get('maintainer_email', '') if not feedback_email: feedback_email = package.get('author_email', '') if not feedback_email: feedback_email = '*****@*****.**' if 'organization' in package and package['organization']: feedback_organisation = _strip_non_ascii( package['organization'].get('title', '')) else: feedback_organisation = 'None' feedback_resource_name = '' feedback_dataset = _strip_non_ascii(package.get('title', '')) package_name = _strip_non_ascii(package.get('name', '')) feedback_origins = "{0}/dataset/{1}".format(host, package_name) if data_dict['resource_id'] != '': feedback_origins = "{0}/resource/{1}".format( feedback_origins, data_dict['resource_id']) package_resources = package.get('resources', []) for resource in package_resources: if data_dict['resource_id'] == resource.get('id'): feedback_resource_name = _strip_non_ascii( resource.get('name', '')) email_subject = '{0} Feedback {1} {2}'.format( host, feedback_dataset, feedback_resource_name) email_recipient_name = 'All' email_to = (config.get('feedback_form_recipients', '')).split(',') if feedback_email != '' and feedback_email: email_to.append(feedback_email) else: feedback_email = '' email_to = [e for e in email_to if e is not None] email_to = [i.strip() for i in email_to if i.strip() != ''] if email_to: email_body = "Name: {0} \r\nEmail: {1} \r\nComments: {2} \r\nFeedback Organisation: {3} \r\n" \ "Feedback Email: {4} \r\nFeedback Dataset: {5} \r\nFeedback Resource: {6} \r\n" \ "Feedback URL: {7}://{8}".format( cgi.escape(_strip_non_ascii(data_dict['name'])), cgi.escape(_strip_non_ascii(data_dict['email'])), cgi.escape(_strip_non_ascii(data_dict['comments'])), cgi.escape(feedback_organisation), cgi.escape(_strip_non_ascii(feedback_email)), cgi.escape(feedback_dataset), cgi.escape(feedback_resource_name), cgi.escape(protocol), cgi.escape(feedback_origins) ) try: _feedback_mail_recipient(email_recipient_name, email_to, g.site_title, g.site_url, email_subject, email_body) except Exception: abort( 404, 'This form submission is invalid or CKAN mail is not configured.' ) # Redirect to home page if no thanks page is found success_redirect = config.get('feedback_redirection', '/') req = requests.get(protocol + '://' + host + success_redirect, verify=False) if req.status_code == requests.codes.ok: h.redirect_to(success_redirect) else: h.redirect_to('/') else: abort(404, 'Form submission is invalid no recipients.') return package else: abort(404, 'Invalid request source')