我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用django.contrib.staticfiles.finders.find()。
def serve(request, path, insecure=False, **kwargs): """ Serve static files below a given point in the directory structure or from locations inferred from the staticfiles finders. To use, put a URL pattern such as:: from django.contrib.staticfiles import views url(r'^(?P<path>.*)$', views.serve) in your URLconf. It uses the django.views.static.serve() view to serve the found files. """ if not settings.DEBUG and not insecure: raise Http404 normalized_path = posixpath.normpath(unquote(path)).lstrip('/') absolute_path = finders.find(normalized_path) if not absolute_path: if path.endswith('/') or path == '': raise Http404("Directory indexes are not allowed here.") raise Http404("'%s' could not be found" % path) document_root, path = os.path.split(absolute_path) return static.serve(request, path, document_root=document_root, **kwargs)
def file_hash(filename): if not filename: return None def _get_full_path(path): rel_source_path = path.lstrip("/") if settings.STATIC_ROOT: full_path = os.path.join(settings.STATIC_ROOT, rel_source_path) if os.path.exists(full_path): return full_path try: full_path = finders.find(rel_source_path) except Exception: full_path = None return full_path full_path = _get_full_path(filename) return None if full_path is None \ else hashlib.md5(open(full_path, 'rb').read()).hexdigest()[:7]
def money_format(amount, format='+/'): """ ?????, ????????. '+'??????????'+' '/'????????100 '0'?????????? 'a'??????????,??????(?????0) """ i = format.find('0') >= 0 a = format.find('a') >= 0 if amount is None or amount is '': return i and '0' or '0.00' if isinstance(amount, str): amount = float(amount) if format.find('/') >= 0: amount /= 100 sign = '' if format.find('+') >= 0 and amount > 0: sign = '+' if a: return sign+"%0.9g"%amount elif i: return sign+"%.f"%amount else: return sign+"%.2f"%amount
def test_ceap_datasets(self, mock_urlretrieve): # backup existing file if exists original = path.join(settings.CORE_STATIC_DIR, 'ceap-datasets.html') backup = original + '.bkp' if path.exists(original): rename(original, backup) # test self.assertEqual(None, finders.find('ceap-datasets.html')) call_command('ceapdatasets', stdout=StringIO()) self.assertTrue(finders.find('ceap-datasets.html')) # restore existing file backup if path.exists(backup): remove(backup, original)
def serve_static(request, path, insecure=False, **kwargs): """ This is copied from Django's contrib.staticfiles.views to remove the DEBUG check. We don't need to check DEBUG since we never actually serve the site with Django. Serve static files below a given point in the directory structure or from locations inferred from the staticfiles finders. To use, put a URL pattern such as:: from django.contrib.staticfiles import views url(r'^(?P<path>.*)$', views.serve) in your URLconf. It uses the django.views.static.serve() view to serve the found files. """ normalized_path = posixpath.normpath(unquote(path)).lstrip('/') absolute_path = finders.find(normalized_path) if not absolute_path: if path.endswith('/') or path == '': raise Http404("Directory indexes are not allowed here.") raise Http404("'%s' could not be found" % path) document_root, path = os.path.split(absolute_path) return static.serve(request, path, document_root=document_root, **kwargs)
def get_attachments(self): attachments = super().get_attachments() filename = ( finders.find('images/email_logo.png') or finders.find('images/email_logo.svg') ) if filename: if filename.endswith('.png'): imagetype = 'png' else: imagetype = 'svg+xml' with open(filename, 'rb') as f: logo = MIMEImage(f.read(), imagetype) logo.add_header('Content-ID', '<{}>'.format('logo')) return attachments + [logo] return attachments
def serve(request, path, insecure=False, **kwargs): """ Serve static files below a given point in the directory structure or from locations inferred from the staticfiles finders. To use, put a URL pattern such as:: (r'^(?P<path>.*)$', 'django.contrib.staticfiles.views.serve') in your URLconf. It uses the django.views.static view to serve the found files. """ if not settings.DEBUG and not insecure: raise ImproperlyConfigured("The staticfiles view can only be used in " "debug mode or if the --insecure " "option of 'runserver' is used") normalized_path = posixpath.normpath(unquote(path)).lstrip('/') absolute_path = finders.find(normalized_path) if not absolute_path: if path.endswith('/') or path == '': raise Http404("Directory indexes are not allowed here.") raise Http404("'%s' could not be found" % path) document_root, path = os.path.split(absolute_path) return static.serve(request, path, document_root=document_root, **kwargs)
def handle_label(self, path, **options): verbosity = int(options.get('verbosity', 1)) result = finders.find(path, all=options['all']) path = force_text(path) if result: if not isinstance(result, (list, tuple)): result = [result] result = (force_text(os.path.realpath(path)) for path in result) if verbosity >= 1: output = '\n '.join(result) return "Found '%s' here:\n %s" % (path, output) else: return '\n'.join(result) else: if verbosity >= 1: self.stderr.write("No matching file found for '%s'." % path)
def handle_label(self, path, **options): verbosity = options['verbosity'] result = finders.find(path, all=options['all']) path = force_text(path) if verbosity >= 2: searched_locations = ("Looking in the following locations:\n %s" % "\n ".join(force_text(location) for location in finders.searched_locations)) else: searched_locations = '' if result: if not isinstance(result, (list, tuple)): result = [result] result = (force_text(os.path.realpath(path)) for path in result) if verbosity >= 1: file_list = '\n '.join(result) return ("Found '%s' here:\n %s\n%s" % (path, file_list, searched_locations)) else: return '\n'.join(result) else: message = ["No matching file found for '%s'." % path] if verbosity >= 2: message.append(searched_locations) if verbosity >= 1: self.stderr.write('\n'.join(message))
def handle_label(self, path, **options): verbosity = options['verbosity'] result = finders.find(path, all=options['all']) path = force_text(path) if verbosity >= 2: searched_locations = ( "\nLooking in the following locations:\n %s" % "\n ".join(force_text(location) for location in finders.searched_locations) ) else: searched_locations = '' if result: if not isinstance(result, (list, tuple)): result = [result] result = (force_text(os.path.realpath(path)) for path in result) if verbosity >= 1: file_list = '\n '.join(result) return ("Found '%s' here:\n %s%s" % (path, file_list, searched_locations)) else: return '\n'.join(result) else: message = ["No matching file found for '%s'." % path] if verbosity >= 2: message.append(searched_locations) if verbosity >= 1: self.stderr.write('\n'.join(message))
def inline_static_file(path, minify=None): """ Outputs the [minified] contents of a given static file. For example, to display the minified CSS file "``inline.css``":: <style> {% inline_static_file 'inline.css' 'css' %} </style> The optional ``minify`` argument can be one of css, js, or html. """ p = finders.find(path) if not p: raise RuntimeError('path=%s not found' % path) elif os.path.isdir(p): raise RuntimeError('path=%s is not a file' % path) with open(p, encoding='utf-8') as f: if minify == 'js': return mark_safe(js_minify(f.read())) elif minify == 'css': return mark_safe(css_minify(f.read())) elif minify == 'html': return mark_safe(html_minify(f.read())) else: return mark_safe(f.read())
def find(value, substr): return value.find(substr)
def test_digitalocean(self): self.assertTrue(finders.find('digitalocean.png'))
def test_serving_static_files(self): # If using static media properly result is not NONE once it finds rango.jpg result = finders.find('images/rango.jpg') self.assertIsNotNone(result)