geonode.utils ============= .. py:module:: geonode.utils Attributes ---------- .. autoapisummary:: geonode.utils.MAX_EXTENT geonode.utils.FULL_ROTATION_DEG geonode.utils.HALF_ROTATION_DEG geonode.utils.DEFAULT_TITLE geonode.utils.DEFAULT_ABSTRACT geonode.utils.INVALID_PERMISSION_MESSAGE geonode.utils.ALPHABET geonode.utils.ALPHABET_REVERSE geonode.utils.BASE geonode.utils.SIGN_CHARACTER geonode.utils.SQL_PARAMS_RE geonode.utils.FORWARDED_HEADERS geonode.utils.XML_PARSER geonode.utils.signalnames geonode.utils.signals_store geonode.utils.id_none geonode.utils.logger geonode.utils.http_client geonode.utils.json_serializer_k_map Exceptions ---------- .. autoapisummary:: geonode.utils.ServerDoesNotExist Classes ------- .. autoapisummary:: geonode.utils.OGC_Server geonode.utils.OGC_Servers_Handler geonode.utils.DisableDjangoSignals geonode.utils.HttpClient Functions --------- .. autoapisummary:: geonode.utils.mkdtemp geonode.utils.unzip_file geonode.utils.extract_tarfile geonode.utils.get_dataset_name geonode.utils.get_dataset_workspace geonode.utils.get_headers geonode.utils._get_basic_auth_info geonode.utils.batch_delete geonode.utils._split_query geonode.utils.bbox_swap geonode.utils.bbox_to_wkt geonode.utils._v geonode.utils.bbox_to_projection geonode.utils.bounds_to_zoom_level geonode.utils.llbbox_to_mercator geonode.utils.mercator_to_llbbox geonode.utils.forward_mercator geonode.utils.inverse_mercator geonode.utils.resolve_object geonode.utils.json_response geonode.utils.num_encode geonode.utils.num_decode geonode.utils.format_urls geonode.utils.build_abstract geonode.utils.build_caveats geonode.utils.build_social_links geonode.utils.check_shp_columnnames geonode.utils.clone_shp_field_defn geonode.utils.rename_shp_columnnames geonode.utils.fixup_shp_columnnames geonode.utils.id_to_obj geonode.utils.printsignals geonode.utils.run_subprocess geonode.utils.parse_datetime geonode.utils._convert_sql_params geonode.utils.raw_sql geonode.utils.get_client_ip geonode.utils.get_client_host geonode.utils.check_ogc_backend geonode.utils.get_dir_time_suffix geonode.utils.zip_dir geonode.utils.copy_tree geonode.utils.extract_archive geonode.utils.chmod_tree geonode.utils.slugify_zh geonode.utils.get_legend_url geonode.utils.set_resource_default_links geonode.utils.add_url_params geonode.utils.json_serializer_producer geonode.utils.is_monochromatic_image geonode.utils.find_by_attr geonode.utils.build_absolute_uri geonode.utils.remove_credentials_from_url geonode.utils.extract_ip_or_domain geonode.utils.get_xpath_value geonode.utils.get_geonode_app_types geonode.utils.get_supported_datasets_file_types geonode.utils.get_allowed_extensions geonode.utils.safe_path_leaf Module Contents --------------- .. py:data:: MAX_EXTENT :value: 20037508.34 .. py:data:: FULL_ROTATION_DEG :value: 360.0 .. py:data:: HALF_ROTATION_DEG :value: 180.0 .. py:data:: DEFAULT_TITLE :value: '' .. py:data:: DEFAULT_ABSTRACT :value: '' .. py:data:: INVALID_PERMISSION_MESSAGE .. py:data:: ALPHABET .. py:data:: ALPHABET_REVERSE .. py:data:: BASE .. py:data:: SIGN_CHARACTER :value: '$' .. py:data:: SQL_PARAMS_RE .. py:data:: FORWARDED_HEADERS :value: ['content-type', 'content-disposition'] .. py:data:: XML_PARSER :type: Final .. py:data:: signalnames :value: ['class_prepared', 'm2m_changed', 'post_delete', 'post_init', 'post_save', 'post_syncdb',... .. py:data:: signals_store .. py:data:: id_none .. py:data:: logger .. py:exception:: ServerDoesNotExist Bases: :py:obj:`Exception` Common base class for all non-exit exceptions. .. py:class:: OGC_Server(ogc_server, alias) Bases: :py:obj:`object` OGC Server object. .. py:attribute:: alias .. py:attribute:: server .. py:method:: __getattr__(item) .. py:property:: credentials Returns a tuple of the server's credentials. .. py:property:: datastore_db Returns the server's datastore dict or None. .. py:property:: ows The Open Web Service url for the server. .. py:property:: rest The REST endpoint for the server. .. py:property:: public_url The global public endpoint for the server. .. py:property:: internal_ows The Open Web Service url for the server used by GeoNode internally. .. py:property:: hostname .. py:property:: netloc .. py:method:: __str__() .. py:class:: OGC_Servers_Handler(ogc_server_dict) OGC Server Settings Convenience dict. .. py:attribute:: servers .. py:attribute:: _servers .. py:method:: ensure_valid_configuration(alias) Ensures the settings are valid. .. py:method:: ensure_defaults(alias) Puts the defaults into the settings dictionary for a given connection where no settings is provided. .. py:method:: __getitem__(alias) .. py:method:: __setitem__(key, value) .. py:method:: __iter__() .. py:method:: all() .. py:function:: mkdtemp(dir=settings.MEDIA_ROOT) .. py:function:: unzip_file(upload_file, extension='.shp', tempdir=None) Unzips a zipfile into a temporary directory and returns the full path of the .shp file inside (if any) .. py:function:: extract_tarfile(upload_file, extension='.shp', tempdir=None) Extracts a tarfile into a temporary directory and returns the full path of the .shp file inside (if any) .. py:function:: get_dataset_name(dataset) Get the workspace where the input layer belongs .. py:function:: get_dataset_workspace(dataset) Get the workspace where the input layer belongs .. py:function:: get_headers(request, url, raw_url, allowed_hosts=[]) .. py:function:: _get_basic_auth_info(request) grab basic auth info .. py:function:: batch_delete(request) .. py:function:: _split_query(query) split and strip keywords, preserve space separated quoted blocks. .. py:function:: bbox_swap(bbox) .. py:function:: bbox_to_wkt(x0, x1, y0, y1, srid='4326', include_srid=True) .. py:function:: _v(coord, x, source_srid=4326, target_srid=3857) .. py:function:: bbox_to_projection(native_bbox, target_srid=4326) native_bbox must be in the form ('-81.3962935', '-81.3490249', '13.3202891', '13.3859614', 'EPSG:4326') .. py:function:: bounds_to_zoom_level(bounds, width, height) .. py:function:: llbbox_to_mercator(llbbox) .. py:function:: mercator_to_llbbox(bbox) .. py:function:: forward_mercator(lonlat) Given geographic coordinates, return a x,y tuple in spherical mercator. If the lat value is out of range, -inf will be returned as the y value .. py:function:: inverse_mercator(xy) Given coordinates in spherical mercator, return a lon,lat tuple. .. py:function:: resolve_object(request, model, query, permission='base.view_resourcebase', user=None, permission_required=True, permission_msg=None) Resolve an object using the provided query and check the optional permission. Model views should wrap this function as a shortcut. query - a dict to use for querying the model permission - an optional permission to check permission_required - if False, allow get methods to proceed permission_msg - optional message to use in 403 .. py:function:: json_response(body=None, errors=None, url=None, redirect_to=None, exception=None, content_type=None, status=None) Create a proper JSON response. If body is provided, this is the response. If errors is not None, the response is a success/errors json object. If redirect_to is not None, the response is a success=True, redirect_to object If the exception is provided, it will be logged. If body is a string, the exception message will be used as a format option to that string and the result will be a success=False, errors = body % exception .. py:function:: num_encode(n) .. py:function:: num_decode(s) .. py:function:: format_urls(a, values) .. py:function:: build_abstract(resourcebase, url=None, includeURL=True) .. py:function:: build_caveats(resourcebase) .. py:function:: build_social_links(request, resourcebase) .. py:function:: check_shp_columnnames(layer) Check if shapefile for a given layer has valid column names. If not, try to fix column names and warn the user .. py:function:: clone_shp_field_defn(srcFieldDefn, name) Clone an existing ogr.FieldDefn with a new name .. py:function:: rename_shp_columnnames(inLayer, fieldnames) Rename columns in a layer to those specified in the given mapping .. py:function:: fixup_shp_columnnames(inShapefile, charset, tempdir=None) Try to fix column names and warn the user .. py:function:: id_to_obj(id_) .. py:function:: printsignals() .. py:class:: DisableDjangoSignals(disabled_signals=None, skip=False) Python3 class temporarily disabling django signals on model creation. usage: with DisableDjangoSignals(): .. py:attribute:: skip .. py:attribute:: stashed_signals .. py:attribute:: disabled_signals .. py:method:: __enter__() .. py:method:: __exit__(exc_type, exc_val, exc_tb) .. py:method:: disconnect(signal) .. py:method:: reconnect(signal) .. py:function:: run_subprocess(*cmd, **kwargs) .. py:function:: parse_datetime(value) .. py:function:: _convert_sql_params(cur, query) .. py:function:: raw_sql(query, params=None, ret=True) Execute raw query param ret=True returns data from cursor as iterator .. py:function:: get_client_ip(request) .. py:function:: get_client_host(request) .. py:function:: check_ogc_backend(backend_package) Check that geonode use a particular OGC Backend integration :param backend_package: django app of backend to use :type backend_package: str :return: bool :rtype: bool .. py:class:: HttpClient .. py:attribute:: timeout :value: 5 .. py:attribute:: retries :value: 1 .. py:attribute:: pool_maxsize :value: 10 .. py:attribute:: backoff_factor :value: 0.3 .. py:attribute:: pool_connections :value: 10 .. py:attribute:: status_forcelist :value: (500, 502, 503, 504) .. py:attribute:: username :value: 'admin' .. py:attribute:: password :value: 'admin' .. py:method:: request(url, method='GET', data=None, headers={}, stream=False, timeout=None, retries=None, user=None, verify=False) .. py:method:: get(url, data=None, headers={}, stream=False, timeout=None, user=None, verify=False) .. py:method:: post(url, data=None, headers={}, stream=False, timeout=None, user=None, verify=False) .. py:data:: http_client .. py:function:: get_dir_time_suffix() Returns the name of a folder with the 'now' time as suffix .. py:function:: zip_dir(basedir, archivename) .. py:function:: copy_tree(src, dst, symlinks=False, ignore=None) .. py:function:: extract_archive(zip_file, dst) .. py:function:: chmod_tree(dst, permissions=511) .. py:function:: slugify_zh(text, separator='_') Make a slug from the given text, which is simplified from slugify. It remove the other args and do not convert Chinese into Pinyin :param text (str): initial text :param separator (str): separator between words :return (str): .. py:function:: get_legend_url(instance, style_name, /, service_url=None, dataset_name=None, version='1.3.0', sld_version='1.1.0', width=20, height=20, params=None) .. py:function:: set_resource_default_links(instance, layer, prune=False, **kwargs) .. py:function:: add_url_params(url, params) Add GET params to provided URL being aware of existing. :param url: string of target URL :param params: dict containing requested params to be added :return: string with updated URL >> url = 'http://stackoverflow.com/test?answers=true' >> new_params = {'answers': False, 'data': ['some','values']} >> add_url_params(url, new_params) 'http://stackoverflow.com/test?data=some&data=values&answers=false' .. py:data:: json_serializer_k_map .. py:function:: json_serializer_producer(dictionary) - usage: serialized_obj = json_serializer_producer(model_to_dict(instance)) - dump to file: with open('data.json', 'w') as outfile: json.dump(serialized_obj, outfile) - read from file: with open('data.json', 'r') as infile: serialized_obj = json.load(infile) .. py:function:: is_monochromatic_image(image_url, image_data=None) .. py:function:: find_by_attr(lst, val, attr='id') Returns an object if the id matches in any list of objects .. py:function:: build_absolute_uri(url) .. py:function:: remove_credentials_from_url(url) .. py:function:: extract_ip_or_domain(url) .. py:function:: get_xpath_value(element: lxml.etree.Element, xpath_expression: str, nsmap: Optional[dict] = None) -> Optional[str] .. py:function:: get_geonode_app_types() .. py:function:: get_supported_datasets_file_types() .. py:function:: get_allowed_extensions() .. py:function:: safe_path_leaf(path) A view that is not vulnerable to malicious file access.