def livegeojson(request): if request.is_ajax(): message = StreamingHttpResponse(staticfiles_storage.open('live.geojson'), content_type="application/json") return message else: message = StreamingHttpResponse(staticfiles_storage.open('live.geojson'), content_type="application/json") return message
def register_fonts(self): """Register fonts so reportlab can use them.""" pdfmetrics.registerFont( TTFont("Raleway Bold", staticfiles_storage.open("fonts/Raleway-Bold.ttf"))) pdfmetrics.registerFont( TTFont("Raleway Medium", staticfiles_storage.open("fonts/Raleway-Medium.ttf")))
def getRouteFromAPI(input): lat = round(float(input[0]), 9) long = round(float(input[1]), 9) length = input[2] seed = input[3] imageName = input[4] type = input[5] baseURL = "http://*****:*****@2x?access_token=pk.eyJ1IjoiZ2FveXVzaGkwMDEiLCJhIjoiY2tubGM0cmV1MGY5aTJucGVtMHAwZGtpNyJ9.xApcEalgtGPF4fQc4to1DA" res = requests.get(requestUrl) if res.status_code != 200: return with staticfiles_storage.open( os.path.join(django_settings.STATIC_ROOT, imageName), 'wb') as out_file: out_file.write(res.content) del res return instructionsList, realDistance, time, imageName, risk, encodedCoordinatesList, polyline
def stream(request): """Proxies the kiltiskamera stream to logged in Prodeko users. Retrieves the kiltiskamera stream using a GET request and returns it with a StreamingHttpResponse. Args: request: HttpRequest object from Django. Returns: StreamingHttpResponse of the camera stream. If the user isn't logged in, they are redirected to the login url. """ try: response = StreamingHttpResponse( get(settings.KILTISKAMERA_URL, stream=True), content_type="multipart/x-mixed-replace; boundary=BoundaryString", ) response["Cache-Control"] = "no-cache" response["Cache-Control"] = "private" return response except Exception as e: img = staticfiles_storage.open("images/kiltiskamera/webcam_offline.jpg") response = HttpResponse(img, content_type="image/jpeg") return response
def fetch(self, locale): if not locale: locale = settings.LANGUAGE_CODE filename = get_filename(locale, settings.STATICI18N_DOMAIN) path = os.path.join(settings.STATICI18N_OUTPUT_DIR, filename) with staticfiles_storage.open(path) as i18n_file: return mark_safe(i18n_file.read())
def build_diabetes_pima(): diab = pd.read_csv(staticfiles_storage.open('dataset/diabetes.csv')) diab2 = diab[[ 'Glucose', 'BMI', 'Age', 'BloodPressure', 'Pregnancies', 'DiabetesPedigreeFunction', 'Outcome' ]] features = diab2[diab2.columns[:6]] train1, test1 = train_test_split(diab2, test_size=0.25, random_state=0) train_X1 = train1[train1.columns[:6]] test_X1 = test1[test1.columns[:6]] train_Y1 = train1['Outcome'] test_Y1 = test1['Outcome'] # print(train_Y1.head(5)) model = LogisticRegression() model.fit(train_X1, train_Y1) prediction = model.predict(test_X1) # print('The accuracy of the Model : ', metrics.accuracy_score(prediction, test_Y1)) # print('\nConfusion Matrix - \n') # print(confusion_matrix(prediction, test_Y1)) # print('\n\n') return model
def get_buster_json(buster_file=BUSTER_FILE): """ Returns json data either from cache or from the busters file from staticfiles storage. """ # First check for cached version if BUSTER_CACHE: buster_json = cache.get(BUSTER_CACHE_KEY) if buster_json is not None: return buster_json # Look for busters file in staticfiles storage buster_json = '' if staticfiles_storage.exists(buster_file): with staticfiles_storage.open(buster_file) as file_: contents = file_.read() file_.flush() # Try to load the json from file try: buster_json = json.loads(contents) except ValueError: pass # cache the json cache[BUSTER_CACHE_KEY] = buster_json return buster_json
def handle_starttag(self, tag, attrs): """ :param tag : link, div... :param attrs : [('href', '/static/dist/css/cms-x.x.x.css'), ('rel', 'stylesheet')] """ if tag == 'link': attrs_dict = dict(attrs) try: href = attrs_dict['href'] rel = attrs_dict['rel'] except NameError: raise LinkAttributeError() if rel == 'stylesheet': # External if href.startswith('https://') or href.startswith('//'): try: style_file = urllib.request.urlopen(href) style_content = style_file.read() try: self.style_content += style_content.decode("utf-8") except UnicodeDecodeError: # Gzip file self.style_content += gzip.decompress( style_content).decode("utf-8") except urllib.error.HTTPError: pass # Django Static elif href.startswith(settings.STATIC_URL): style_path_static = href.replace(settings.STATIC_URL, '') style_file = staticfiles_storage.open(style_path_static) self.style_content += style_file.read().decode("utf-8")
def inlinei18n(locale): key = "statici18n:%s" % locale path = cache.get(key) if path is None: path = os.path.join(settings.STATICI18N_OUTPUT_DIR, get_filename(locale, settings.STATICI18N_DOMAIN)) cache.set(key, path, 60 * 60 * 24 * 30) with staticfiles_storage.open(path) as i18n_file: return mark_safe(i18n_file.read())
def cid_static(context, path, mimetype=None): ''' Embed a file from static files. ''' fin = staticfiles_storage.open(path) if mimetype is None: mimetype, _ = guess_type(path) return _embed_cid(context, fin, mimetype)
def inline_css(path): if getattr(settings, 'DEBUG', False): full_path = finders.find(path) with open(full_path, 'rb') as file: return file.read() else: with staticfiles_storage.open(path) as file: return file.read()
def favicon(request): try: site = get_current_site(request) # must explicitly open ImageField objects favicon = site.page.branding.favicon_file.open() except Exception: favicon = staticfiles_storage.open("favicon.ico") with favicon: return HttpResponse(favicon.read(), content_type="image/x-icon")
def inlinei18n(locale): """ A template tag that returns the Javascript catalog content for the selected locale to be inlined in a <script></script> block. Behind the scenes, this is a thin wrapper around staticfiles's configred storage """ return mark_safe(staticfiles_storage.open(get_path(locale)).read().decode())
def inlinei18n(locale): key = 'statici18n:%s' % locale path = memcache.get(key) if path is None: path = os.path.join(settings.STATICI18N_OUTPUT_DIR, get_filename(locale, settings.STATICI18N_DOMAIN)) memcache.set(key, path, 60 * 60 * 24 * 30) with staticfiles_storage.open(path) as i18n_file: return mark_safe(i18n_file.read())
def inlinei18n(locale): """ A template tag that returns the Javascript catalog content for the selected locale to be inlined in a <script></script> block. Behind the scenes, this is a thin wrapper around staticfiles's configred storage """ return mark_safe(staticfiles_storage.open(get_path(locale)).read())
def handle(self, *args, **options): translation.activate('en') submission = PlanSubmission.objects.get(pk=options['submission_id']) output_path = '{}.html'.format(submission.pk) if os.path.exists(output_path): print('Report file already exists!') return template = loader.get_template('submission_summary.html') # We're going to reuse the summary panel from the main map editing page score_panel = ScorePanel.objects.filter( name='plan_submission_summary_17')[0] districts = [ d for d in submission.plan.district_set.all() if not d.is_unassigned ] # The above Score Display is split into two ScorePanels: the top summary panel and the # bottom panel of per-district scores. We want the summary panel. # The type field is apparently limited to three options: 1) plan 2) plan_summary 3) district scores_html = score_panel.render(submission.plan) GeoJSONSerializer = serializers.get_serializer('geojson') serializer = GeoJSONSerializer() # is_unassigned is a property so we can't use queryset filtering # The unassigned district is a catch-all for geounits that haven't been assigned to a real # district. We don't want to display this on the submission map, so filter it out here. geojson = serializer.serialize(districts, geometry_field='geom', fields=('short_label', 'long_label')) leaflet_css = staticfiles_storage.open('leaflet/leaflet.css').read() leaflet_js = staticfiles_storage.open('leaflet/leaflet.js').read() context = dict(plan_url='https://{host}{path}'.format( host='map.drawthelinespa.org', path=reverse('plan-view', args=[submission.plan_id])), submission=submission, scores_html=scores_html, leaflet_css=leaflet_css, leaflet_js=leaflet_js, geojson=geojson) print('Writing report to {}'.format(output_path)) with codecs.open(output_path, 'wb', 'UTF-8') as outfile: outfile.write(template.render(context)) print('Done.')
def raw_include(path): if settings.DEBUG: absolute_path = finders.find(path) if absolute_path is None: raise Exception("raw_include: couldn't find file {}".format(path)) f = codecs.open(absolute_path, 'r', 'utf-8') else: f = staticfiles_storage.open(path) content = f.read() return mark_safe(content)
def _test_compiler(self, compiler_cls_str, infile, expected): compiler_cls = to_class(compiler_cls_str) compiler = compiler_cls(verbose=False, storage=staticfiles_storage) infile_path = staticfiles_storage.path(infile) outfile_path = compiler.output_path(infile_path, compiler.output_extension) compiler.compile_file(_(infile_path), _(outfile_path), force=True) with open(outfile_path) as f: result = f.read() with staticfiles_storage.open(expected) as f: expected = f.read() self.assertEqual(smart_bytes(result), expected)
def raw_include(path): if settings.DEBUG: absolute_path = finders.find(path) if absolute_path is None: raise Exception("raw_include: couldn't find file {}".format(path)) f = codecs.open(absolute_path, 'r', 'utf-8') content = f.read() else: f = staticfiles_storage.open(path) content = f.read().decode('utf-8') return mark_safe(content)
def thumbnail_static(file_, geometry, **options): from sorl.thumbnail.shortcuts import get_thumbnail from sorl.thumbnail.images import ImageFile, DummyImageFile storage = SafeStaticFilesStorage() absolute_path = finders.find(file_) try: file_ = ImageFile(staticfiles_storage.open(file_)) except: file_ = ImageFile(open(absolute_path)) file_.storage = storage return get_thumbnail(file_, geometry, **options)
def verbatim_include(path): """ Includes and scape a file from the static files :param path: :return: """ if settings.DEBUG: absolute_path = finders.find(path) with open(absolute_path) as fd: content = fd.read() else: content = staticfiles_storage.open(path).read() return escape(content)
def raw_include(path): """ Includes a file as-is from the static files. WARNING: this is dangerous and this means that the source is trusty :param path: :return: """ if settings.DEBUG: absolute_path = finders.find(path) with open(absolute_path) as fd: content = fd.read() else: content = staticfiles_storage.open(path).read() return mark_safe(content) # dangerous, we trust the source file
def record_invitation_email_opened(self): # record opening of the email if(self.GET.get('id')): invitation_id = self.GET.get('id') invitation = Invitation.objects.get(id=invitation_id) if invitation: if not invitation.read_email_timestamp: invitation.read_email_timestamp = timezone.now() invitation.save() # get the image to return file_path = staticfiles_storage.open('api_v1/empty.png') image = Image.open(file_path) response = HttpResponse(content_type="image/png") image.save(response, "PNG") return response
def compile_file(self, infile, outfile, outdated=False, force=False): for path in settings.STATICFILES_DIRS: outfile = outfile.replace(path, "").strip("/") import sass out_value = sass.compile( filename=infile, output_style="compressed", include_paths=settings.SASS_INCLUDE_PATHS, ) if type(out_value) == bytes: out_value = out_value.decode("utf8") with staticfiles_storage.open(outfile, "w") as out: out.write(out_value) return out_value
def get_thumbnail_object(image): if isinstance(image, ImageFieldFile): return get_thumbnailer(image) elif image.startswith('http'): response = requests.get(image, stream=True, timeout=1) file_name = os.path.join('url_files', "%s.%s" % (hashlib.md5(image.encode()).hexdigest(), image.rsplit('.', 1)[1])) path = os.path.join(settings.MEDIA_ROOT, file_name) if not os.path.exists(path): storage = FileSystemStorage() storage.save(path, ContentFile(response.content)) return get_thumbnailer(default_storage.open(path), relative_name=file_name) else: try: return get_thumbnailer(staticfiles_storage.open(image), relative_name=image) except FileNotFoundError: return None
def compile_file(self, infile, outfile, outdated=False, force=False): if not outdated: return open(outfile).read() import sass out_value = sass.compile( filename=infile, output_style="compressed", include_paths=settings.SASS_INCLUDE_PATHS, ) if type(out_value) == bytes: out_value = out_value.decode("utf8") with staticfiles_storage.open(outfile, "w") as out: out.write(out_value) return out_value
def render(self, context): rendered_contents = self.nodelist.render(context) css = '' for expression in self.filter_expressions: path = expression.resolve(context, True) if path is not None: path = smart_unicode(path) if settings.DEBUG: expanded_path = finders.find(path) with open(expanded_path) as css_file: css = ''.join((css, css_file.read())) else: with staticfiles_storage.open(path) as css_file: css = ''.join((css, css_file.read())) engine = conf.get_engine()(html=rendered_contents, css=css) return engine.render()
def render(self, context): rendered_contents = self.nodelist.render(context) css = '' for expression in self.filter_expressions: path = expression.resolve(context, True) if path is not None: path = smart_unicode(path) if settings.DEBUG or not getattr(settings, 'INLINECSS_USE_STATIC_FINDER', False): expanded_path = finders.find(path) css_file = open(expanded_path) else: css_file = staticfiles_storage.open(path) with css_file: css = ''.join((css, css_file.read())) engine = conf.get_engine()(html=rendered_contents, css=css) return engine.render()
def ignore(wordCountDict, ignoreList): DEFAULT = "__DEFAULT__" PATH_TO_IGNORE_FILE = 'text/assets/txt/commonWords.txt' for wordToIgnore in set(ignoreList): if wordToIgnore == DEFAULT: # staticfiles_storage.url('txt/file.txt') # staticfiles_storage.path('txt/file.txt') toIgnoreFile = staticfiles_storage.open(PATH_TO_IGNORE_FILE) for defaultIgnore in toIgnoreFile.readlines(): if isinstance(defaultIgnore, (bytes, bytearray)): defaultIgnore = defaultIgnore.decode() defaultIgnore = defaultIgnore.strip() wordCountDict.pop(defaultIgnore, None) else: wordCountDict.pop(wordToIgnore, None) return wordCountDict
def read_static_file(path, mode="r"): """ Return the contents of a static file. """ if settings.DEBUG: # Lookup file in using Django's static finder, e.g. when in # local development mode. filename = find(path) if filename: return open(filename, mode=mode).read() elif staticfiles_storage.exists(path): # Look up file in collectstatic target directory (regular # deployment). return staticfiles_storage.open(path, mode=mode).read() message = 'Unable to include inline static file "%s", file not found.' logger.warning(message, path) raise ValueError(message % path)
def render(self, context): rendered_contents = self.nodelist.render(context) css = '' for expression in self.filter_expressions: path = expression.resolve(context, True) if path is not None: path = smart_text(path) if settings.DEBUG or not getattr( settings, 'INLINECSS_USE_STATIC_FINDER', False): expanded_path = finders.find(path) css_file = open(expanded_path) else: css_file = staticfiles_storage.open(path) with css_file: css = ''.join((css, css_file.read())) engine = conf.get_engine()(html=rendered_contents, css=css) return engine.render()
def getMapImage(input): path = input[0] lat = input[1] long = input[2] name = input[3].replace(' ', '') imageName = str(name) + 'mapImage.png' if staticfiles_storage.exists( os.path.join(django_settings.STATIC_ROOT, imageName)): return imageName encodedCoordinatesList = urllib.parse.quote(path, safe='') requestUrl = "https://api.mapbox.com/styles/v1/mapbox/streets-v11/static/pin-s+ff2600(" + str( long ) + "," + str( lat ) + ")," + "path-3+0061ff-0.55(" + encodedCoordinatesList + ")/auto/300x200@2x?access_token=pk.eyJ1IjoiZ2FveXVzaGkwMDEiLCJhIjoiY2tubGM0cmV1MGY5aTJucGVtMHAwZGtpNyJ9.xApcEalgtGPF4fQc4to1DA" res = requests.get(requestUrl) with staticfiles_storage.open( os.path.join(django_settings.STATIC_ROOT, imageName), 'wb') as out_file: out_file.write(res.content) del res return imageName
def build_cardiovascular_organized(): df = pd.read_csv(staticfiles_storage.open('dataset/cardiovascular.csv'), sep=';') train1, test1 = train_test_split(df, test_size=0.25, random_state=0) train_X1 = train1[train1.columns[1:12]] test_X1 = test1[test1.columns[1:12]] train_Y1 = train1['cardio'] test_Y1 = test1['cardio'] # print(train_Y1.head(5)) # print(train_X1.head(5)) model = RandomForestClassifier(n_estimators=100, random_state=0) model.fit(train_X1, train_Y1) prediction = model.predict(test_X1) # print('The accuracy of the Model : ', metrics.accuracy_score(prediction, test_Y1)) # print('\nConfusion Matrix - \n') # print(confusion_matrix(prediction, test_Y1)) # print('\n\n') return model
def crop_pic(uploaded_img, x, y, w, h): if not uploaded_img: img_url = staticfiles_storage.open("images/misc/anonymous_prodeko.jpg") x, y, w, h = 0, 0, 150, 150 img = Image.open(img_url) else: img = Image.open(uploaded_img.file) area = (x, y, x + w, y + h) cropped_img = img.crop(area) img_io = BytesIO() # Have to use because people might upload them anyways... # We get an error if format='JPEG' because png's have alpha channel cropped_img.save(fp=img_io, format="PNG") buff_val = img_io.getvalue() content_file = ContentFile(buff_val) # If no image was provided use anonymous_prodeko.jpg name = uploaded_img.name if uploaded_img else "anonymous_prodeko.jpg" ret = InMemoryUploadedFile( content_file, None, name, "image/png", cropped_img.tell, None ) img.close() img_io.close() return ret
def _render(self, context): storage = SafeStaticFilesStorage() file_ = self.file_.resolve(context) absolute_path = finders.find(file_) try: file_ = ImageFile(staticfiles_storage.open(file_)) except: file_ = ImageFile(open(absolute_path)) file_.storage = storage geometry = self.geometry.resolve(context) options = {} for key, expr in self.options: noresolve = {'True': True, 'False': False, 'None': None} value = noresolve.get(text_type(expr), expr.resolve(context)) if key == 'options': options.update(value) else: options[key] = value thumbnail = get_thumbnail(file_, geometry, **options) if not thumbnail or (isinstance(thumbnail, DummyImageFile) and self.nodelist_empty): if self.nodelist_empty: return self.nodelist_empty.render(context) else: return '' if self.as_var: context.push() context[self.as_var] = thumbnail output = self.nodelist_file.render(context) context.pop() else: output = thumbnail.url return output
def importer(path, prev=None): # import pudb; pu.db # if not prev: # result = finders.find(path) # if result: # return load(path, result) # else: # return None n = Path(path) p = Path(prev) for name in ( n.with_suffix(".scss"), n.with_name(f"_{n.name}").with_suffix(".scss"), ): if p.is_absolute(): search = str(name) else: search = str(p.parent / name) if not staticfiles_storage.exists(search): continue with staticfiles_storage.open(search) as content: return [(search, content.read())]
def read_bytes(self, path): """Read file content in binary mode""" file = staticfiles_storage.open(path) content = file.read() file.close() return content
def image(self): return staticfiles_storage.open('js-lover.png')