def as_sitemap_url(self, *args, **kwargs): target_name = self.__target.name with_name = kwargs.get('with_name', target_name) python_formatting = kwargs.get('python_formatting', False) url = self.__target.as_sitemap_url(*args, **kwargs) for key, value in self.__presets.items(): url = url.replace( ('%%(%s)s' if python_formatting else '{%s}') % key, value) if target_name == key and target_name != with_name: url = url.replace( ('%%(%s)s' if python_formatting else '{%s}') % with_name, value) return url
def _format_url(self, url, parent_id = None): return url.replace( '12345', '<b><id></b>' ).replace( '67890', '<b><%s></b>' % parent_id ).replace( 'xml', '<b><format></b>' )
def register_model(self, model, viewset=None, serializer=None, fields=None, queryset=None, filter=None, cache_filter=None, **kwargs): if isinstance(model, string_types) and '.' in model: from django.db.models import get_model model = get_model(*model.split('.')) if viewset: self.register_viewset(model, viewset) if queryset is not None: self.register_queryset(model, queryset) if filter: self.register_filter(model, filter) if cache_filter: self.register_cache_filter(model, cache_filter) kwargs['cache'] = 'filter' if 'name' not in kwargs: kwargs['name'] = model._meta.model_name if 'url' not in kwargs: url = force_text(model._meta.verbose_name_plural) kwargs['url'] = url.replace(' ', '') other_model = self._page_models.get(kwargs['name'], None) if other_model: raise ImproperlyConfigured( "Could not register %s: " "the name '%s' was already registered for %s" % (model, kwargs['name'], other_model)) other_model = self._url_models.get(kwargs['url'], None) if other_model: raise ImproperlyConfigured( "Could not register %s: " "the url '%s' was already registered for %s" % (model, kwargs['url'], other_model)) self.register_config(model, kwargs) if serializer: self.register_serializer(model, serializer) if fields: self.register_fields(model, fields) self._models.add(model) self._page_names[model] = kwargs['name'] self._page_models[kwargs['name']] = model self._url_models[kwargs['url']] = model
def register_model(self, model, viewset=None, serializer=None, fields=None, queryset=None, filter=None, cache_filter=None, **kwargs): if isinstance(model, string_types) and '.' in model: from django.db.models import get_model model = get_model(*model.split('.')) if viewset: self.register_viewset(model, viewset) if queryset is not None: self.register_queryset(model, queryset) if filter: self.register_filter(model, filter) if cache_filter: self.register_cache_filter(model, cache_filter) kwargs['cache'] = 'filter' if 'name' not in kwargs: kwargs['name'] = model._meta.model_name if 'url' not in kwargs: url = force_text(model._meta.verbose_name_plural) kwargs['url'] = url.replace(' ', '') other_model = self._page_models.get(kwargs['name'], None) if other_model: raise ImproperlyConfigured( "Could not register %s: " "the name '%s' was already registered for %s" % (model, kwargs['name'], other_model) ) other_model = self._url_models.get(kwargs['url'], None) if other_model: raise ImproperlyConfigured( "Could not register %s: " "the url '%s' was already registered for %s" % (model, kwargs['url'], other_model) ) self.register_config(model, kwargs) if serializer: self.register_serializer(model, serializer) if fields: self.register_fields(model, fields) self._models.add(model) self._page_names[model] = kwargs['name'] self._page_models[kwargs['name']] = model self._url_models[kwargs['url']] = model
def docs_define(url, params=None, headers=None, desc='', name=None, display=True): """ :param name: name of reverse generation url >>> from django.urls import reverse >>> reverse(name) :param url: api url :param params: request body content :param headers: request header content :param desc: API description :param display: display on the document :return: """ if not name: name = url.replace('/', '_').strip('_') docs_params = settings.DJANGO_DOCS_GLOBAL_PARAMS docs_headers = settings.DJANGO_DOCS_GLOBAL_HEADERS if params: docs_params.extend(list(params)) if headers: docs_headers.extend(list(headers)) docs_params = check_param(docs_params) docs_headers = check_param(docs_headers) def decorator(view): method = view.__name__ router.register(view=view, name=name, url=url, params=docs_params, headers=docs_headers, desc=desc, method=method, display=display) @functools.wraps(view) def handler(*args, **kwargs): return view(*args, **kwargs) return handler return decorator
def urls_to_html(urls, reverse=True, target="_blank", hide_protocol=True, classname=None, divider="<br />"): if not urls: return urls urls = [ u'<a %shref="%s" %s/>%s</a>' % ( ('target="%s" ' % target) if target else "", url, ('class="%s" ' % classname) if classname else "", (url.replace('https://', '').replace('http://', '') if hide_protocol else url).strip('/') ) for url in urls ] if reverse: urls.reverse() html = divider.join(urls) return html
def register_model(self, model, viewset=None, serializer=None, queryset=None, filter=None, **kwargs): if isinstance(model, basestring) and '.' in model: from django.db.models import get_model model = get_model(*model.split('.')) self._models.add(model) ct = get_ct(model) if viewset: self.register_viewset(model, viewset) if serializer: self.register_serializer(model, serializer) if queryset: self.register_queryset(model, queryset) if filter: self.register_filter(model, filter) if 'name' not in kwargs: kwargs['name'] = ct.identifier if 'url' not in kwargs: url = unicode(model._meta.verbose_name_plural) kwargs['url'] = url.replace(' ', '') self.register_config(model, kwargs)
def upload_album(request): """ Allow the artist to upload an album to the store. """ # Only needed to fill in currency info store = request.user.get_default_store() # if request.get_host() == "libertymusicstore.net": # upload_url = "https://upload.libertymusicstore.net" if "libertymusicstore.net" in request.get_host() else "" # logger.info("Processing") if request.method == "POST": logger.info("Got POST") form = AlbumUploadForm(request.POST, request.FILES, request=request) if form.is_valid(): try: store = form.cleaned_data["store"] upload = form.cleaned_data["zip_file"] album = zipupload.upload_album(store, form.cleaned_data["album_name"], upload, form.cleaned_data["album_price"], form.cleaned_data["song_price"], ) try: album.genre = int(form.cleaned_data["genre"]) except ValueError: album.genre = None album.store = form.cleaned_data["store"] album.description = form.cleaned_data["description"][0:40] album.save() messages.success(request, "The album is now uploaded. It might still take couple of minutes to process all songs and have them to appear.") # Succesfully loaded embed from the user website # TODO: Make sure we are the store owner wizard = models.WelcomeWizard(store) wizard.set_step_status("upload_album", True) # JavaScript redirect to this URL url = reverse('admin:tatianastore_album_change', args=(album.id,)) url = url.replace("upload.libertystoremusic.net", "libertystoremusic.net") return http.HttpResponse(url) except zipupload.BadAlbumContenException as e: # Handle b(ad upload content errors logger.error("Bad album content") logger.exception(e) errors = form._errors.setdefault("zip_upload", ErrorList()) errors.append(str(e)) return http.HttpResponseServerError(str(e)) else: logger.info("Not a POST request to upload album") form = AlbumUploadForm(request=request) form.fields["song_price"].label = "Song price for individual buys ({})".format(store.currency) form.fields["album_price"].label = "Album price ({})".format(store.currency) # Workaround for 100mb cloudflare upload limit logger.info("Upload host set to %s", request.get_host()) upload_url = "https://upload.libertymusicstore.net/storeadmin/upload-album/" if "libertymusicstore.net" in request.get_host() else "" return render_to_response("storeadmin/upload_album.html", locals(), context_instance=RequestContext(request))
def _format_url(self, url, parent_id=None): return url.replace('12345', '<b><id></b>').replace( '67890', '<b><%s></b>' % parent_id).replace('xml', '<b><format></b>')
def get_mapproxy(layer, seed=False, ignore_warnings=True, renderd=False): """Creates a mapproxy config for a given layer-like object. Compatible with django-registry and GeoNode. """ bbox = list(wkt2geom(layer.wkt_geometry)) # TODO: Check for correct url url = 'http://test.registry.org' # url = str(layer.service.url) layer_name = str(layer.title) srs = 'EPSG:4326' bbox_srs = 'EPSG:4326' grid_srs = 'EPSG:3857' default_source = { 'type': 'wms', 'coverage': { 'bbox': bbox, 'srs': srs, 'bbox_srs': bbox_srs, 'supported_srs': ['EPSG:4326', 'EPSG:900913', 'EPSG:3857'], }, 'req': { 'layers': str(layer.title), 'url': url, 'transparent': True, }, } if layer.type == 'ESRI:ArcGIS:MapServer' or layer.type == 'ESRI:ArcGIS:ImageServer': # blindly replace it with /arcgis/ url = url.replace("/ArcGIS/rest/", "/arcgis/") # same for uppercase url = url.replace("/arcgis/rest/", "/arcgis/") # and for old versions url = url.replace("ArcX/rest/services", "arcx/services") # in uppercase or lowercase url = url.replace("arcx/rest/services", "arcx/services") srs = 'EPSG:3857' bbox_srs = 'EPSG:3857' default_source = { 'type': 'arcgis', 'req': { 'url': url, 'grid': 'default_grid', 'transparent': True, }, } # A source is the WMS config sources = {'default_source': default_source} # A grid is where it will be projects (Mercator in our case) grids = { 'default_grid': { 'tile_size': [256, 256], 'srs': grid_srs, 'origin': 'nw', } } # A cache that does not store for now. It needs a grid and a source. caches = { 'default_cache': { 'disable_storage': True, 'grids': ['default_grid'], 'sources': ['default_source'] }, } # The layer is connected to the cache layers = [ { 'name': layer_name, 'sources': ['default_cache'], 'title': str(layer.title), }, ] # Services expose all layers. # WMS is used for reprojecting # TMS is used for easy tiles # Demo is used to test our installation, may be disabled in final version services = { 'wms': { 'image_formats': ['image/png'], 'md': { 'abstract': 'This is the Harvard HyperMap Proxy.', 'title': 'Harvard HyperMap Proxy' }, 'srs': ['EPSG:4326', 'EPSG:3857'], 'versions': ['1.1.1'] }, 'wmts': { 'restful': True, 'restful_template': '/{Layer}/{TileMatrixSet}/{TileMatrix}/{TileCol}/{TileRow}.png', }, 'tms': { 'origin': 'nw', }, 'demo': None, } global_config = { 'http': { 'ssl_no_cert_checks': True }, } # Start with a sane configuration using MapProxy's defaults conf_options = load_default_config() # Populate a dictionary with custom config changes extra_config = { 'caches': caches, 'grids': grids, 'layers': layers, 'services': services, 'sources': sources, 'globals': global_config, } yaml_config = yaml.dump(extra_config, default_flow_style=False) # If you want to test the resulting configuration. Turn on the next # line and use that to generate a yaml config. # assert False # Merge both load_config(conf_options, config_dict=extra_config) # TODO: Make sure the config is valid. errors, informal_only = validate_options(conf_options) for error in errors: LOGGER.warn(error) if not informal_only or (errors and not ignore_warnings): raise ConfigurationError('invalid configuration') errors = validate_references(conf_options) for error in errors: LOGGER.warn(error) conf = ProxyConfiguration(conf_options, seed=seed, renderd=renderd) # Create a MapProxy App app = MapProxyApp(conf.configured_services(), conf.base_config) return app, yaml_config
def uuidzy(url): return url.replace('%u', '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}')
def get_mapproxy(layer, seed=False, ignore_warnings=True, renderd=False, config_as_yaml=True): """Creates a mapproxy config for a given layer-like object. Compatible with django-registry and GeoNode. """ bbox = list(wkt2geom(layer.wkt_geometry)) bbox = ",".join([format(x, '.4f') for x in bbox]) url = str(layer.source) layer_name = str(layer.title) srs = 'EPSG:4326' bbox_srs = 'EPSG:4326' grid_srs = 'EPSG:3857' default_source = { 'type': 'wms', 'coverage': { 'bbox': bbox, 'srs': srs, 'bbox_srs': bbox_srs, 'supported_srs': ['EPSG:4326', 'EPSG:900913', 'EPSG:3857'], }, 'req': { 'layers': str(layer.title), 'url': url, 'transparent': True, }, } if layer.type == 'ESRI:ArcGIS:MapServer' or layer.type == 'ESRI:ArcGIS:ImageServer': # blindly replace it with /arcgis/ url = url.replace("/ArcGIS/rest/", "/arcgis/") # same for uppercase url = url.replace("/arcgis/rest/", "/arcgis/") # and for old versions url = url.replace("ArcX/rest/services", "arcx/services") # in uppercase or lowercase url = url.replace("arcx/rest/services", "arcx/services") srs = 'EPSG:3857' bbox_srs = 'EPSG:4326' default_source = { 'type': 'arcgis', 'req': { 'url': url.split('?')[0], 'grid': 'default_grid', 'transparent': True, }, } # A source is the WMS config sources = {'default_source': default_source} # A grid is where it will be projects (Mercator in our case) grids = { 'default_grid': { 'tile_size': [256, 256], 'srs': grid_srs, 'origin': 'nw', } } # A cache that does not store for now. It needs a grid and a source. caches = { 'default_cache': { 'disable_storage': True, 'grids': ['default_grid'], 'sources': ['default_source'] }, } # The layer is connected to the cache layers = [ { 'name': layer_name, 'sources': ['default_cache'], 'title': "%s" % layer.title, }, ] # Services expose all layers. # WMS is used for reprojecting # TMS is used for easy tiles # Demo is used to test our installation, may be disabled in final version services = { 'wms': { 'image_formats': ['image/png'], 'md': { 'abstract': 'This is the Harvard HyperMap Proxy.', 'title': 'Harvard HyperMap Proxy' }, 'srs': ['EPSG:4326', 'EPSG:3857'], 'srs_bbox': 'EPSG:4326', 'bbox': bbox, 'versions': ['1.1.1'] }, 'wmts': { 'restful': True, 'restful_template': '/{Layer}/{TileMatrixSet}/{TileMatrix}/{TileCol}/{TileRow}.png', }, 'tms': { 'origin': 'nw', }, 'demo': None, } global_config = { 'http': { 'ssl_no_cert_checks': True }, } # Populate a dictionary with custom config changes extra_config = { 'caches': caches, 'grids': grids, 'layers': layers, 'services': services, 'sources': sources, 'globals': global_config, } yaml_config = yaml.dump(extra_config, default_flow_style=False) # If you want to test the resulting configuration. Turn on the next # line and use that to generate a yaml config. # assert False conf = configure_mapproxy(extra_config) # Create a MapProxy App app = MapProxyApp(conf.configured_services(), conf.base_config) # Wrap it in an object that allows to get requests by path as a string. if (config_as_yaml): return app, yaml_config return app, extra_config
def handle_image_from_xhr_upload(request, instance, attr_name): inicio = time_module.time() if attr_name in request.FILES: image_file = request.FILES[attr_name] img_name = image_file.name elif 'url' in request.POST: # If image comes from url url = request.POST['url'] url = url.replace(" ", "%20") imgcontent = urllib.urlopen(url) # open the image contents image_file = StringIO(imgcontent.read()) # creates an in memory file object parsed_url = urlparse(url) img_name = parsed_url.path # gets the file name on url extension = img_name[img_name.rfind("."):] # gets the image extension # gets the ImageCrop attribute from the model image_crop_attr_obj = getattr(instance, attr_name) upload_to = image_crop_attr_obj.field.upload_to time = int(datetime.now().microsecond)# current time to add on the file name # Open the uploaded (or downloaded in case it's from url) image image_file = Image.open(image_file) img_attr_obj = getattr(instance, attr_name) try: os.remove(img_attr_obj.path) except: pass try: last_original_path = img_attr_obj.path last_slash = last_original_path.rfind("/") if last_slash == -1: last_slash = last_original_path.rfind("\\") last_original_name = last_original_path[last_slash+1:] last_original_name = last_original_name.replace(attr_name, attr_name+"_original") os.remove(os.path.join(settings.MEDIA_ROOT, upload_to, last_original_name)) except: pass ready_image_file = None ready_image_file = StringIO() image_file.save(ready_image_file , 'PNG', optimize = True) ready_image_file.seek(0) # Set the uploaded image to the file attribute of the imagefield img_attr_obj.file = ready_image_file # Seta o novo nome da imagem upada para o campo atual da iteracao (upload_to/nome.jpg) image_original_name = "%s%s_original_%s_%s%s" % (upload_to, attr_name, instance.id, time, extension) img_attr_obj.name = "%s%s_%s_%s%s" % (upload_to, attr_name, instance.id, time, extension) # Abre e salva a imagem atual no sistema de arquivos image_obj = Image.open( img_attr_obj ) #image_obj. image_obj.save( img_attr_obj.path ) # Dimensoes para usar como parametro na funcao adjust_img() dim = img_attr_obj.field.dimensions # Cria uma imagem cropavel ajustada para o tamanho especificado para o campo atual adjusted_image = adjust_img(img_attr_obj.path, dim[0], dim[1], dim[0], dim[1]) adjusted_image.save( os.path.join(settings.MEDIA_ROOT,image_original_name)) original_img_dim = adjusted_image.size box = getBox(adjusted_image, dim[0], dim[1]) cropped_img = adjusted_image.crop(box) # Salva a imagem cropada no lugar da anterior (a imagem upada não tratada) cropped_img.save( img_attr_obj.path ) cropped_img_dim = cropped_img.size instance.save() data = serializers.serialize("json", [instance]) json_as_python = json.loads(data) original_dict_key = { attr_name+'_original': { 'src':os.path.join("/media/", image_original_name), 'dimensions':"%i,%i"%(original_img_dim[0], original_img_dim[1]), } } cropped_imgage = { attr_name+'_cropped': { 'src': os.path.join("/media/", img_attr_obj.name), 'dimensions':"%i,%i"%(cropped_img_dim[0], cropped_img_dim[1]), } } json_as_python[0].update(original_dict_key) json_as_python[0].update(cropped_imgage) data = json.dumps(json_as_python) return data