Пример #1
0
def ficha_atividade(request, v1, v2):
    '''Ficha de Atividade Coletiva da Equipe'''
    message = 'Ficha usada apenas pelas equipes PSF'
    if Equipe.objects.filter(ine=v2).exists():
        response = HttpResponse(content_type='application/pdf')
        response['Content-Disposition'] = 'inline;\
            filename="%s_ficha_atividade.pdf"' % v2
        pdf = canvas.Canvas(response, pagesize=A4)
        pdf.drawImage(find('core/img/Ficha_de_Atividade_Coletiva-0.png'),
                      0, 0, 21*cm, 29.7*cm)
        pdf.setFillColor(white)
        pdf.rect(10.45*cm, 21.4*cm, 9.83*cm, 3.5*cm, stroke=False,
                 fill=True)
        alt = 24.7*cm
        for value in Profissional.objects.filter(equipe__ine=v2).order_by('nome'):
            pdf.setFillColor(black)
            pdf.setFontSize(0.2*cm)
            pdf.drawString(12.4*cm, alt, value.nome)
            pdf.drawString(10.6*cm, alt, value.cns)
            pdf.drawString(17*cm, alt, value.cbo.cbo)
            pdf.drawString(17.7*cm, alt, value.cbo.nome)
            alt -= 6.0
        pdf.showPage()
        pdf.drawImage(find('core/img/Ficha_de_Atividade_Coletiva-1.png'),
                      0, 0, 21*cm, 29.7*cm)
        pdf.showPage()
        pdf.save()
        return response
    else:
        return HttpResponse(message)
Пример #2
0
    def register_fonts():
        from reportlab.pdfbase.ttfonts import TTFont
        from reportlab.pdfbase import pdfmetrics

        pdfmetrics.registerFont(TTFont('OpenSans', finders.find('fonts/OpenSans-Regular.ttf')))
        pdfmetrics.registerFont(TTFont('OpenSansIt', finders.find('fonts/OpenSans-Italic.ttf')))
        pdfmetrics.registerFont(TTFont('OpenSansBd', finders.find('fonts/OpenSans-Bold.ttf')))
Пример #3
0
def get_media_source_from_url(url):
    """
    For a given media/static URL, return the matching full path in the media/static directory
    """
    # Media
    if MEDIA_URL and url.startswith(MEDIA_URL):
        return os.path.join(MEDIA_ROOT, url[len(MEDIA_URL):].lstrip('/'))

    elif MEDIA_URL and url.startswith('/media/'):
        return os.path.join(MEDIA_ROOT, url[len('/media/'):].lstrip('/'))

    # Static
    elif STATIC_URL and url.startswith(STATIC_URL):
        return find(url[len(STATIC_URL):].lstrip('/'))

    elif STATIC_URL and url.startswith('/static/'):
        return find(url[len('/static/'):].lstrip('/'))


    # External URLs
    elif is_remote_url(url):
        return url

    else:
        raise Exception('Invalid media/static url given: %s' % url)
Пример #4
0
 def test_images(self):
     abs_path = finders.find('blogango/images/agiliq_blog_logo.png')
     self.assertIsNotNone(abs_path)
     abs_path = finders.find('blogango/images/person_default.jpg')
     self.assertIsNotNone(abs_path)
     abs_path = finders.find('logango/images/person_default.jpg')
     self.assertIsNone(abs_path)
Пример #5
0
 def test_css(self):
     abs_path = finders.find('blogango/css/as_blog_styles.css')
     self.assertIsNotNone(abs_path)
     abs_path = finders.find('blogango/css/prettify.css')
     self.assertIsNotNone(abs_path)
     abs_path = finders.find('logango/css/prettify.css')
     self.assertIsNone(abs_path)
Пример #6
0
def get_resized_image(filename, final_resolution):
    fullname = finders.find(filename)
    filename = filename.split("/").pop()
    filename, extension = filename.split(".")
    i = Image.open(fullname)
    aspect_ratio = i.size[1]/float(i.size[0])
    final_width = final_resolution
    final_height = int(final_resolution*aspect_ratio)

    if i.size[0] < final_resolution:
        return fullname, extension
    filename = "%s_%s_%s.%s" % (filename, final_width, final_height,
                             extension)

    cache_dir_name = getattr(settings, 'RESPONSIVE_IMAGES_CACHE_DIR',
                             'responsive_images_cache')
    cache_path = os.path.join(settings.MEDIA_ROOT, cache_dir_name)
    if not finders.find(cache_dir_name):
        os.mkdir(cache_path)

    resized_image_full_path = os.path.join(cache_path, filename)

    if not finders.find(cache_dir_name + "/" + filename):
        size = (final_width, final_height)
        resized_image = i.resize(size, Image.ANTIALIAS)
        resized_image.save(resized_image_full_path, extension,
            quality=getattr('settings', 'RESPONSIVE_IMAGE_QUALITY', 75)) 
    return resized_image_full_path, extension
def fetch_resources(uri, rel):
    """
    Retrieves embeddable resource from given ``uri``.

    For now only local resources (images, fonts) are supported.

    :param str uri: path or url to image or font resource
    :returns: path to local resource file.
    :rtype: str
    :raises: :exc:`~easy_pdf.exceptions.UnsupportedMediaPathException`
    """
    # For protocol relative, http and https URLs, we need to short circuit to skip 
    # path checking at the end of function
    if uri.startswith('//') or uri.startswith('http://') or uri.startswith('https://'):
        return uri
    elif settings.STATIC_URL and uri.startswith(settings.STATIC_URL):
        path = finders.find(uri.replace(settings.STATIC_URL, ""))
    elif settings.MEDIA_URL and uri.startswith(settings.MEDIA_URL):
        path = os.path.join(settings.MEDIA_ROOT, uri.replace(settings.MEDIA_URL, ""))
    else:
        path = os.path.join(settings.STATIC_ROOT, uri)

    if not os.path.isfile(path):
        path = finders.find(uri.replace(settings.STATIC_URL, ""))

    if not os.path.isfile(path):
        raise UnsupportedMediaPathException(
            "media urls must start with {} or {}".format(
                settings.MEDIA_ROOT, settings.STATIC_ROOT
            )
        )

    return path.replace("\\", "/")
Пример #8
0
def _create_scan_png(scan, filename, scan_number):
    """
    Create standard png with afm and summary information.
    """
    image = Image.fromarray(scan.colorize())
    if image.size != (512, 512):
        logger.debug('Image not correct size: {}, should be 512 x 512'.format(image.size))
        image = image.resize((512, 512), resample=Image.NEAREST)
        logger.debug('Resized image to 512 x 512')

    processed_image = Image.new(image.mode, (654, 580), 'white')
    processed_image.paste(image,
                          (20, 20, image.size[0] + 20, image.size[1] + 20))

    calibri = ImageFont.truetype(finders.find('afm/fonts/calibrib.ttf'), 20)
    draw = ImageDraw.Draw(processed_image)

    scale_image = Image.open(finders.find('afm/img/scale_12.png'))
    processed_image.paste(scale_image,
                         (28 + image.size[0],
                          30,
                          28 + image.size[0] + scale_image.size[0],
                          30 + scale_image.size[1]))
    draw.text(
        (28 + image.size[0] + scale_image.size[0] + 7, 25),
        '{0:.0f} {1}'.format(scan.height_scale, scan.unit), 'black', calibri)
    draw.text(
        (28 + image.size[0] + scale_image.size[0] + 7, 20 + scale_image.size[1]),
        '0.0 {}'.format(scan.unit), 'black', calibri)

    zrange_str = 'Z-Range: {0:.2f} nm'.format(scan.zrange)
    rms_str = 'RMS: {0:.2f} nm'.format(scan.rms)
    size_str = 'Area: {0} \u03bcm X {0} \u03bcm'.format(math.sqrt(scan.scan_area))

    draw.text((20, 25 + image.size[1]), filename, 'black', calibri)
    draw.text(
        (20 + image.size[0] - calibri.getsize(size_str)[0], 25 + image.size[1]),
        size_str, 'black', calibri)
    if scan.unit == 'nm':
        draw.text(
            (20, 50 + image.size[1]), zrange_str, 'black', calibri)
        draw.text(
            (20 + image.size[0] - calibri.getsize(rms_str)[0], 50 + image.size[1]),
            rms_str, 'black', calibri)
    else:
        type_str = '{} AFM'.format(scan.type)
        draw.text(
            (20 + image.size[0] - calibri.getsize(type_str)[0], 50 + image.size[1]),
            type_str, 'black', calibri)

    tempio = six.BytesIO()
    processed_image.save(tempio, format='PNG')
    if scan.type == 'Amplitude':
        filename += 'a'
    elif scan.type == 'Phase':
        filename += 'p'
    return InMemoryUploadedFile(
        tempio, field_name=None, name=filename + '.png',
        content_type='image/png', size=len(tempio.getvalue()), charset=None)
    def test_finder_non_jspm_package_file(self):
        # assert that it's not found with the standard finders
        location = finders.find('node_modules/system.js', all=False)
        self.assertIsNone(location)

        # but it is found with the custom finder...
        with override_settings(STATICFILES_FINDERS=('systemjs.finders.SystemFinder',)):
            location = finders.find('node_modules/system.js', all=False)
        self.assertIsNone(location)
Пример #10
0
    def render(self, context, instance, placeholder):
        context = super(SlickCarouselPlugin, self).render(
            context, instance, placeholder)
        
        slick_dict = {'slide': ':not(template)'}
        breakpoints = {}
        if instance.slick_preset:
            slick_dict.update({
                'infinite': instance.slick_preset.infinite,
                'speed': instance.slick_preset.speed,
                'dots': instance.slick_preset.dots,
                'arrows': instance.slick_preset.arrows,
                'slidesToShow': instance.slick_preset.slides_to_show,
                'slidesToScroll': instance.slick_preset.slides_to_scroll,
                'autoplay': instance.slick_preset.autoplay,
                'autoplaySpeed': instance.slick_preset.autoplay_speed,
                'pauseOnHover': instance.slick_preset.pause_on_hover,
                'pauseOnDotsHover': instance.slick_preset.pause_on_dots_hover,
                'fade': instance.slick_preset.fade,
                'rows': instance.slick_preset.rows,
                'slidesPerRow': instance.slick_preset.slides_per_row,
                'centerMode': instance.slick_preset.center_mode,
                'centerPadding': instance.slick_preset.center_padding,
                'variableWidth': instance.slick_preset.variable_width,
                'adaptiveHeight': instance.slick_preset.adaptive_height,
                'vertical': instance.slick_preset.vertical,
                'rtl': instance.slick_preset.rigth_to_left,
                'respondTo': instance.slick_preset.respond_to,
                'mobileFirst': instance.slick_preset.mobile_first,
            })
        
        if instance.breakpoints:
            breakpoints, responsive = self._get_breakpoints(instance)
            slick_dict.update({'responsive': responsive})

        breakpoints = json.dumps(breakpoints)
        slick_settings = json.dumps(slick_dict)

        try:
            os.path.isfile(finders.find('cmsplugin_slick/slick/slick.min.js'))
            os.path.isfile(finders.find('cmsplugin_slick/slick/slick.css'))
            
            slick_static_path = os.path.dirname(static('cmsplugin_slick/slick/slick.min.js'))
        except:
            try:
                slick_static_path = settings.SLICK_CDN
            except:
                slick_static_path = '//cdn.jsdelivr.net/jquery.slick/1.6.0/'

        context.update({
            'slick_settings': slick_settings,
            'breakpoints': breakpoints,
            'slick_static_path': slick_static_path,
        })

        return context
Пример #11
0
    def test_font_awesome(self):
        path = 'plugins/font-awesome/css/font-awesome.min.css'
        absolute_path = finders.find(path)
        assert absolute_path is not None
        assert os.path.exists(absolute_path)

        path = 'plugins/font-awesome/fonts/FontAwesome.otf'
        absolute_path = finders.find(path)
        assert absolute_path is not None
        assert os.path.exists(absolute_path)
Пример #12
0
 def _register_fonts(self):
     """
     Register fonts with reportlab. By default, this registers the OpenSans font family
     """
     pdfmetrics.registerFont(TTFont('OpenSans', finders.find('fonts/OpenSans-Regular.ttf')))
     pdfmetrics.registerFont(TTFont('OpenSansIt', finders.find('fonts/OpenSans-Italic.ttf')))
     pdfmetrics.registerFont(TTFont('OpenSansBd', finders.find('fonts/OpenSans-Bold.ttf')))
     pdfmetrics.registerFont(TTFont('OpenSansBI', finders.find('fonts/OpenSans-BoldItalic.ttf')))
     pdfmetrics.registerFontFamily('OpenSans', normal='OpenSans', bold='OpenSansBd',
                                   italic='OpenSansIt', boldItalic='OpenSansBI')
Пример #13
0
def dados_utilizados(request):
    if finders.find('db-dump/radar.sql.bz2'):
        dump_file_path = finders.find('db-dump/radar.sql.bz2')
        time = os.path.getmtime(dump_file_path)
        dt = datetime.datetime.fromtimestamp(time)
        dt_str = dt.strftime('%d/%m/%Y')
        arquivo = True
        return render_to_response('dados_utilizados.html', {'dumpdate': dt_str, 'arquivo_dump': arquivo},context_instance=RequestContext(request))
    else:
        arquivo = False
        return render_to_response('dados_utilizados.html', {'_arquivo_dump': arquivo})
def populate_player_icons(apps, schema_editor):
    Player = apps.get_model("bughouse", "Player")

    for player in Player.objects.all():
        static_file_name = "{0}.jpg".format(player.name.lower())
        static_image_path = find(os.path.join('images', 'player-icons', static_file_name))
        if static_image_path:
            static_file = open(static_image_path, 'r')
        else:
            static_file = open(find(os.path.join('images', 'player-icons', 'default.jpg')), 'r')
        image_file = File(static_file)
        player.icon.save(static_file_name, image_file, save=True)
Пример #15
0
    def test_create_email_msg_attachments(self):
        class TestMail(BaseMail):
            params = []
            template_name = 'test'

        test_mail = TestMail()
        attachments = [
            (finders.find('admin/img/nav-bg.gif'), 'nav-bg.gif', 'image/png'),
            (finders.find('admin/css/base.css'), 'base.css', 'plain/text')]
        msg = test_mail.create_email_msg([], attachments=attachments)
        self.assertEqual(len(msg.attachments), 1)  # base.css
        self.assertEqual(len(msg.related_attachments), 1)  # nav-bg.gif
Пример #16
0
def static_mtime_b64(*staticfile_paths):
	"""
	Return the b64 encoded most recent changed date among staticfile_paths, for cache invalidation.
	"""
	latest = None
	for path in staticfile_paths:
		found = finders.find(path)
		if found and (not latest or latest < getmtime(finders.find(path))):
			latest = getmtime(finders.find(path))
	if latest:
		return float_b64(latest)
	return None
Пример #17
0
    def _collect_static_file(self, file_type, base_file):
        a,b,c = self._get_prefix_triple()

        file_list = base_file['file_name']
        if not isinstance(file_list, (list, tuple)):
            file_list=[file_list]
        
        for f in file_list:
            ra,rb,rc = (finders.find(a+f) and a+f, 
                    finders.find(b+f) and b+f, 
                    finders.find(c+f) and c+f)
            if ra or rb or rc: break
        return ra,rb,rc
    def render(self, context):
        output = self.nodelist.render(context)
        filenames = output.strip().split("\n")

        from django.contrib.staticfiles import finders
        from django.conf import settings
        from django.core.files.storage import default_storage
        from django.core.files.base import ContentFile
        import os
        import re
        import sass

        foundation_path = finders.find('scss/foundation.scss')
        foundation_dir = os.path.dirname(foundation_path)

        # include_paths = [os.path.join(d, 'scss') for d in settings.STATICFILES_DIRS] + [foundation_dir.encode('utf-8'),]
        include_paths = []
        for finder in finders.get_finders():
            for path, storage in finder.list([]):
                path_dir = os.path.dirname(storage.path(path)).encode('utf-8')

                if path_dir not in include_paths:
                    include_paths.append(path_dir)

        for f in filenames:
            full_href = re.search('href="(.*)"', f).groups()[0]
            if full_href.endswith('.scss'):
                cache_key = "sass_%s" % full_href
                cached = cache.get(cache_key)

                if not cached or settings.DEBUG:
                    if full_href.startswith(settings.STATIC_URL):
                        href = full_href.replace(settings.STATIC_URL, '')
                        destination = href.replace('scss', 'css')
                        absolute_path = finders.find(href)

                    elif full_href.startswith(settings.MEDIA_ROOT):
                        destination = full_href.replace(settings.MEDIA_ROOT, '').replace('scss', 'css')
                        absolute_path = full_href

                    content = sass.compile(filename=absolute_path.encode('utf-8'), include_paths=include_paths)
                    if default_storage.exists(destination):
                        default_storage.delete(destination)
                    default_storage.save(destination, ContentFile(content))
                    cached = os.path.join(settings.MEDIA_URL, destination)
                    cache.set(cache_key, cached, None)

                output = output.replace(full_href, cached)

        return output
Пример #19
0
def dokuman_convert(instance):
    if True:
        dosya = instance.dosya
        klasor = dosya.filename_root + '-katalog-gorunumu-dosyalari'
        swfKlasor = os.path.dirname(dosya.path) + '/' + klasor
        default_storage.open(dosya.path).file.save_to_filename('/tmp/pdf')
        pdf = file('/tmp/pdf', 'rb')
        pdfPy = PdfFileReader(pdf)
        sayfa = pdfPy.getNumPages()
        size_x, size_y = pdfPy.getPage(0).mediaBox.upperRight
        if size_x > size_y:
            base = file(finders.find('yan.pdf'), 'rb')
        else:
            base = file(finders.find('portre.pdf'),'rb')
        pre_second_page = PdfFileReader(base).getPage(0)
        pre_second_page.mediaBox.upperRight = (size_x, size_y)
        second_page = PdfFileWriter()
        second_page.addPage(pre_second_page)
        stream = file('/tmp/fitted_page','wb')
        second_page.write(stream)
        stream.close()
        base.close()
        pdf.close()
        for i in range(0, sayfa + 1):
            args = ['/usr/local/bin/pdf2swf',
                    '/tmp/pdf', '-o',
                    '/tmp/' + '%d.swf' % (i + 1,),
                    '-f', '-T', '9', '-t', '-p',
                    '%d' % i, '-s', 'storeallcharacters']
            if i==0:
                args[1] = '/tmp/fitted_page'
                args[3] = '/tmp/2.swf'
                args[9] = '1'
                i = 1
            elif i == 1:
                args[3] = '/tmp/1.swf'
                i = 0
            # p = subprocess.Popen(args, stdout=subprocess.PIPE)
            # for line in iter(p.stdout.readline, ''):
            #     print line
            #     if line[:5] == 'ERROR' or line[:5] == 'FATAL':
            args += ['-s', 'poly2bitmap']
            p = subprocess.Popen(args)
            p.wait()
            name = swfKlasor + '/' + '%d.swf' % (i + 1,)
            swf_file = open('/tmp/%d.swf' % (i + 1,), 'rb')
            swf = swf_file.read()
            content = ContentFile(swf)
            default_storage.save(name, content)
            swf_file.close()
Пример #20
0
def get_i18n_name(lang_code):
    """Ensure lang_code is supported by Select2."""
    lower_lang = lang_code.lower()
    split_lang = lang_code.split('-')[0]
    # Use the SELECT2_TRANSLATIONS if available
    if SELECT2_TRANSLATIONS:
        if lower_lang in SELECT2_TRANSLATIONS:
            return SELECT2_TRANSLATIONS.get(lower_lang)
        elif split_lang in SELECT2_TRANSLATIONS:
            return SELECT2_TRANSLATIONS.get(split_lang)
    # Otherwise fallback to manually checking if the static file exists
    if finders.find('admin/js/vendor/select2/i18n/%s.js' % lang_code):
        return lang_code
    elif finders.find('admin/js/vendor/select2/i18n/%s.js' % split_lang):
        return lang_code.split('-')[0]
Пример #21
0
    def import_pdf_font(self, base_name, face_name):
        if self.fonts.get(face_name, None) is None:
            afm = find(base_name + '.afm')
            pfb = find(base_name + '.pfb')

            try:
                face = pdfmetrics.EmbeddedType1Face(afm, pfb)

                pdfmetrics.registerTypeFace(face)
                font = pdfmetrics.Font(face_name, face_name, 'WinAnsiEncoding')
                pdfmetrics.registerFont(font)
            except:
                pass
        else:
            self.fonts[face_name] = True
Пример #22
0
def translate_static(value, language):

    # Split language and country code
    codes = language.split('-')
    if len(codes) > 1:
        codes[1] = codes[1].upper()
        language = '_'.join(codes)
    base = codes[0]

    if finders.find('locale/%s/%s' % (language, value)):  # Looks for language and country code
        return 'locale/%s/%s' % (language, value)
    elif finders.find('locale/%s/%s' % (base, value)):  # Looks for language code
        return 'locale/%s/%s' % (base, value)
    else:
        return value
Пример #23
0
 def getChild(self, path, request):
     # trim static/ from beginning of path
     path = '/'.join(request.prepath[1:] + request.postpath)
     file_path = find(path)
     if not file_path:
         return self.childNotFound
     return self.createSimilarFile(file_path)
Пример #24
0
def serve(request, path, document_root=None, 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)
Пример #25
0
def download_anki_backup(request):
    from django.contrib.staticfiles import finders
    result = finders.find('resources/anki_backup.apkg')
    fsock = open(result, 'rb')
    response = HttpResponse(fsock, content_type='binary/octet-stream')
    response['Content-Disposition'] = "attachment; filename=backup-0.apkg"
    return response
Пример #26
0
def static_proxy(request):
    """
    Serves TinyMCE plugins inside the inline popups and the uploadify
    SWF, as these are normally static files, and will break with
    cross-domain JavaScript errors if ``STATIC_URL`` is an external
    host. URL for the file is passed in via querystring in the inline
    popup plugin template.
    """
    # Get the relative URL after STATIC_URL.
    url = request.GET["u"]
    protocol = "http" if not request.is_secure() else "https"
    host = protocol + "://" + request.get_host()
    for prefix in (host, settings.STATIC_URL):
        if url.startswith(prefix):
            url = url.replace(prefix, "", 1)
    response = ""
    mimetype = ""
    path = finders.find(url)
    if path:
        if isinstance(path, (list, tuple)):
            path = path[0]
        with open(path, "rb") as f:
            response = f.read()
        mimetype = "application/octet-stream"
        if url.endswith(".htm"):
            # Inject <base href="{{ STATIC_URL }}"> into TinyMCE
            # plugins, since the path static files in these won't be
            # on the same domain.
            mimetype = "text/html"
            static_url = settings.STATIC_URL + os.path.split(url)[0] + "/"
            if not urlparse(static_url).scheme:
                static_url = urljoin(host, static_url)
            base_tag = "<base href='%s'>" % static_url
            response = response.replace("<head>", "<head>" + base_tag)
    return HttpResponse(response, mimetype=mimetype)
Пример #27
0
    def render(self, context):
        path = self.path.resolve(context)

        if not re.search(r'\.svg$', path):
            raise template.TemplateSyntaxError(
                "SVG template nodes must be given a path to a SVG file to return.")  # NOQA

        fullpath = finders.find(path)

        if fullpath is None:
            raise template.TemplateSyntaxError(
                "SVG file ({}) not found.".format(fullpath))

        content = open(fullpath, 'r').read()

        # Test if it is valid XML
        ET.fromstring(content)

        if self.kwargs:
            kwargs = {x: y.resolve(context) for x, y in self.kwargs.items()}
            content = re.sub(
                r'(<svg[^>]*)', r'\1{}'.format(flatatt(kwargs)), content,
                flags=re.MULTILINE)

        if self.varname is None:
            return content
        context[self.varname] = mark_safe(content)
        return ''
Пример #28
0
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

        path('<path: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(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_locale_js_url(lang):
    url = 'datepicker/js/locales/bootstrap-datepicker.%s.js' % lang
    if finders.find(url):
        return settings.STATIC_URL + url
    if '-' in lang:
        return get_locale_js_url(lang.split('-')[0].lower())
    return ''
Пример #30
0
def serve(request, path, document_root=None, 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>.*)$', 'assetfiles.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 the --insecure '
                                   "option of 'runserver' is used")
    normalized_path = posixpath.normpath(unquote(path)).lstrip('/')

    static_path = finders.find(normalized_path)
    if static_path:
        document_root, path = os.path.split(static_path)
        return static.serve(request, path, document_root=document_root, **kwargs)

    asset_path, filter = assets.find(normalized_path)
    if asset_path:
        content = filter.filter(asset_path)
        mimetype, encoding = mimetypes.guess_type(normalized_path)
        return HttpResponse(content, content_type=mimetype)

    if path.endswith('/') or path == '':
        raise Http404('Directory indexes are not allowed here.')
    raise Http404("'%s' could not be found" % path)
Пример #31
0
@csrf_exempt
def template(request, name, val):
    template_conf_missing = False

    try:
        config.check()
    except OSError, e:
        if e.errno == errno.ENOENT:
            template_conf_missing = True
        else:
            raise

    initialError = None
    debug = request.GET.get('debug', False)
    theme = request.GET.get('theme', config.ui_config['theme'])
    css_file = finders.find('css/dashboard-%s.css' % theme)
    if css_file is None:
        initialError = "Invalid theme '%s'" % theme
        theme = config.ui_config['theme']

    context = {
        'schemes_json': json.dumps(config.schemes),
        'ui_config_json': json.dumps(config.ui_config),
        'jsdebug': debug or settings.JAVASCRIPT_DEBUG,
        'debug': debug,
        'theme': theme,
        'initialError': initialError,
        'querystring': json.dumps(dict(request.GET.items())),
        'template_conf_missing': template_conf_missing,
        'userName': '',
        'permissions': json.dumps(getPermissions(request.user)),
Пример #32
0
def static_file_exists(path):
    """Return True/False based on whether a static file exists"""
    return True if finders.find(path) else False
Пример #33
0
def home(request):
    # pandas_gbq.context.credentials = credentials
    # pandas_gbq.context.project = "bigdata-253023"

    state_table_prefix = "home_value_byState_"
    county_table_prefix = "home_value_byCounty_"
    tables = ["all", "singleFamily", "condo", "topTier", "bottomTier"]

    # # Query and save data as dataframes
    # data = {}
    # hist = {}
    #  # Query data from BigQuery and cache into pickles
    # for i, table in enumerate(tables):
    #     # State data
    #     SQL = "SELECT * FROM HomeViz.home_value_byState_" + table
    #     df = pandas_gbq.read_gbq(SQL)
    #     data["state_" + table] = df.set_index('RegionName').iloc[:,-1].rename("state_" + table).dropna().to_dict()
    #     hist["state_" + table] = df.drop(columns=['RegionID', 'SizeRank']).set_index("RegionName").fillna(0).to_dict(orient='index')

    #     # County data
    #     SQL = "SELECT * FROM HomeViz.home_value_byCounty_" + table
    #     df = pandas_gbq.read_gbq(SQL)
    #     # Create new column "id" by concatenating "StateCodeFIPS" and "MunicipalCodeFIPS" columns
    #     df["StateCodeFIPS"] = df["StateCodeFIPS"].astype(str).apply(lambda x: x.zfill(2))
    #     df["MunicipalCodeFIPS"] = df["MunicipalCodeFIPS"].astype(str).apply(lambda x: x.zfill(3))
    #     df["id"] = df["StateCodeFIPS"] + df["MunicipalCodeFIPS"]
    #     data["county_" + table] = df.set_index('id').iloc[:,-2].rename("county_" + table).dropna().to_dict()
    #     hist["county_" + table] = df.drop(columns=['RegionID', 'RegionName', 'State', 'Metro', 'StateCodeFIPS', 'MunicipalCodeFIPS', 'SizeRank']).set_index("id").fillna(0).to_dict(orient='index')

    # # Query data from BigQuery and cache into pickles
    # for i, table in enumerate(tables):
    #     # State data
    #     SQL = "SELECT * FROM HomeViz.home_value_byState_" + table
    #     df = pandas_gbq.read_gbq(SQL)
    #     df.to_pickle("./static/dfs{}.pkl".format(i))

    #     # County data
    #     SQL = "SELECT * FROM HomeViz.home_value_byCounty_" + table
    #     df = pandas_gbq.read_gbq(SQL)
    #     # Create new column "id" by concatenating "StateCodeFIPS" and "MunicipalCodeFIPS" columns
    #     df["StateCodeFIPS"] = df["StateCodeFIPS"].astype(str).apply(lambda x: x.zfill(2))
    #     df["MunicipalCodeFIPS"] = df["MunicipalCodeFIPS"].astype(str).apply(lambda x: x.zfill(3))
    #     df["id"] = df["StateCodeFIPS"] + df["MunicipalCodeFIPS"]
    #     df.to_pickle("./static/dfc{}.pkl".format(i))

    # data = {}
    # # Consolidated data for heat map (state)
    # for cat in state_col_label:
    #     data[cat] = df1.set_index("RegionName")[cat].dropna().to_dict()

    # # Consolidated data for heat map (county)
    # for cat in county_col_label:
    #     data[cat] = df2.set_index("id")[cat].dropna().to_dict()

    # # Historical home value data for plots (state, all)
    # # Reformat df to dict
    # # Format: {category --> {state/county -> {column -> value}}}
    # hist = {}
    # for i, cat in enumerate(state_col_label):
    #     hist[cat] = state_dfs[i].drop(columns=['RegionID', 'SizeRank']).set_index("RegionName").fillna(0).to_dict(orient='index')

    # for i, cat in enumerate(county_col_label):
    #     hist[cat] = county_dfs[i].drop(columns=['RegionID', 'RegionName', 'State', 'Metro', 'StateCodeFIPS', 'MunicipalCodeFIPS', 'SizeRank']).set_index("id").fillna(0).to_dict(orient='index')

    # # Cache files
    # with open("./static/data.pkl", "wb") as handle:
    #     pickle.dump(data, handle, protocol=pickle.HIGHEST_PROTOCOL)
    # with open("./static/hist.pkl", "wb") as handle:
    #     pickle.dump(hist, handle, protocol=pickle.HIGHEST_PROTOCOL)

    # income = {}
    # # Get income data
    # SQL = "SELECT FIPS, Area_name, Median_Household_income_2017 AS income, Unemployment_rate_2018 AS unemployment FROM HomeViz.income"
    # df = pandas_gbq.read_gbq(SQL)
    # df["FIPS"] = df["FIPS"].astype(str).apply(lambda x: x.zfill(5))
    # income['income'] = df.set_index('FIPS')['income'].dropna().to_dict()
    # income['unemployment'] = df.set_index('FIPS')['unemployment'].dropna().to_dict()
    # with open("./static/income.pkl", "wb") as handle:
    #     pickle.dump(income, handle, protocol=pickle.HIGHEST_PROTOCOL)

    # summary = {}
    # # Get income data
    # SQL = "SELECT * FROM HomeViz.home_value_byState_summaryAll"
    # df = pandas_gbq.read_gbq(SQL)
    # summary['state'] = df.set_index('RegionName').drop(columns=['Date', 'RegionID']).dropna().to_dict(orient='index')
    # SQL = "SELECT index.id, summary.state, summary.SizeRank, summary.zhvi, summary.MoM, summary.QoQ, summary.YoY, summary._5Year, summary._10Year, summary.PeakMonth, summary.PeakQuarter, summary.PeakZHVI, summary.PctFallFromPeak, summary.LastTimeAtCurrZHVI FROM HomeViz.home_value_byCounty_summaryAll AS summary JOIN HomeViz.county_index as index ON summary.RegionID = index.RegionID and summary.state = index.state"
    # df = pandas_gbq.read_gbq(SQL)
    # df["id"] = df["id"].astype(str).apply(lambda x: x.zfill(5))
    # summary['county'] = df.set_index('id').dropna().to_dict(orient='index')
    # with open("./static/summary.pkl", "wb") as handle:
    #     pickle.dump(summary, handle, protocol=pickle.HIGHEST_PROTOCOL)

    # Retrieve Cache files
    with open(find("data.pkl"), "rb") as handle:
        data = pickle.load(handle)
    with open(find("hist.pkl"), "rb") as handle:
        hist = pickle.load(handle)
    with open(find("income.pkl"), "rb") as handle:
        income = pickle.load(handle)
    with open(find("summary.pkl"), "rb") as handle:
        summary = pickle.load(handle)

    return render(request, 'home.html', {
        "data": data,
        "hist": hist,
        "income": income,
        "summary": summary
    })
Пример #34
0
def get_all_photos():
    """This function is not used; it can be used to view all photos available."""
    with open(find("animal_photo_urls.txt")) as f:
        urls = f.readlines()
    return [url.strip() + "?w=400" for url in urls]
Пример #35
0
 def find_file(self, url):
     if self.use_finders and url.startswith(self.static_prefix):
         path = finders.find(url[len(self.static_prefix):])
         if path:
             return self.get_static_file(path, url)
     return super(DjangoWhiteNoise, self).find_file(url)
Пример #36
0
    Enum,
    get_random_string,
    resize_image,
    roman_numeral,
    safe_file_name,
    url_with_query_in_data
)
from lib.remote_page import RemotePage, RemotePageException
from lib.models import UrlMixin
from lib.validators import generate_url_key_validator
from userprofile.models import User, UserProfile, GraderUser

logger = logging.getLogger('aplus.course')

# Read pseudonymization data from file
with open(finders.find('pseudonym.json')) as json_file:
    DATA = json.load(json_file)

class Course(UrlMixin, models.Model):
    """
    Course model represents a course in a university. A course has a name and an
    identification number. It also has a URL which is included in the addresses
    of pages under the course.
    """
    name = models.CharField(max_length=255)
    code = models.CharField(max_length=255)
    url = models.CharField(unique=True, max_length=255, blank=False,
        validators=[generate_url_key_validator()],
        help_text=_("Input an URL identifier for this course."))
    teachers = models.ManyToManyField(UserProfile,
        related_name="teaching_courses", blank=True)
Пример #37
0
def get_all_photos():
    with open(find("candidate/animal_photo_urls.txt")) as f:
        urls = f.readlines()
    return [url.strip() + "?w=400" for url in urls]
Пример #38
0
def get_fallback_avatar(size):
    """Return fallback avatar."""
    filename = finders.find(f"weblate-{size}.png")
    with open(filename, "rb") as handle:
        return handle.read()
Пример #39
0
def HeaderFooterPersona(canvas, doc):
    canvas.saveState()
    title = "Reporte de Personas"
    canvas.setTitle(title)

    Story = []
    styles = getSampleStyleSheet()

    archivo_imagen = finders.find('assets/img/logo.jpg')
    canvas.drawImage(archivo_imagen,
                     30,
                     720,
                     width=540,
                     height=100,
                     preserveAspectRatio=True)

    fecha = datetime.now().strftime('%d/%m/%Y ')
    # Estilos de Párrafos
    ta_c = ParagraphStyle(
        'parrafos',
        alignment=TA_CENTER,
        fontSize=10,
        fontName="Helvetica",
    )
    ta_l = ParagraphStyle(
        'parrafos',
        alignment=TA_LEFT,
        fontSize=13,
        fontName="Helvetica-Bold",
    )
    ta_l7 = ParagraphStyle(
        'parrafos',
        alignment=TA_LEFT,
        fontSize=7,
        fontName="Helvetica-Bold",
    )
    ta_r = ParagraphStyle(
        'parrafos',
        alignment=TA_RIGHT,
        fontSize=13,
        fontName="Helvetica-Bold",
    )

    # Header
    header = Paragraph(
        "REPORTE DE PERSONAS ",
        ta_l,
    )
    w, h = header.wrap(doc.width + 250, doc.topMargin)
    header.drawOn(canvas, 215, doc.height - 10 + doc.topMargin - 80)

    #header1 = Paragraph("<u>DIRECCIÓN DE AGRICULTURA Y TIERRA</u> ",ta_r,)
    #w,h = header1.wrap(doc.width-115, doc.topMargin)
    #header1.drawOn(canvas, 140, doc.height -10 + doc.topMargin - 2)

    P1 = Paragraph('''N°''', ta_c)
    P2 = Paragraph('''CEDULA''', ta_c)
    P3 = Paragraph('''NOMBRE''', ta_c)
    P4 = Paragraph('''APELLIDO''', ta_c)
    P5 = Paragraph('''CORREO''', ta_c)
    P6 = Paragraph('''ROL''', ta_c)
    data = [[P1, P2, P3, P4, P5, P6]]
    header2 = Table(data, colWidths=[35, 85, 80, 80, 150, 80])
    header2.setStyle(
        TableStyle([
            ('GRID', (0, -1), (-1, -1), 1, colors.black),
            ('BACKGROUND', (0, 0), (-1, 0), '#50b7e6'),
            ('ALIGN', (0, 0), (-1, -3), "CENTER"),
        ]))
    w, h = header2.wrap(doc.width - 115, doc.topMargin)
    header2.drawOn(canvas, 42.5, doc.height - 40 + doc.topMargin - 93)

    # FOOTER

    footer4 = Paragraph("Fecha: " + str(fecha), ta_l7)
    w, h = footer4.wrap(doc.width - 200, doc.bottomMargin)
    footer4.drawOn(canvas, doc.height - 105, doc.topMargin + 620, h)

    canvas.restoreState()
Пример #40
0
def HeaderFooterTrans(canvas, doc):
    canvas.saveState()
    title = "Reporte de Transacciones"
    canvas.setTitle(title)

    Story = []
    styles = getSampleStyleSheet()

    archivo_imagen = finders.find('assets/img/logo.jpg')
    canvas.drawImage(archivo_imagen,
                     30,
                     720,
                     width=540,
                     height=100,
                     preserveAspectRatio=True)

    fecha = datetime.now().strftime('%d/%m/%Y ')
    # Estilos de Párrafos
    ta_c = ParagraphStyle(
        'parrafos',
        alignment=TA_CENTER,
        fontSize=10,
        fontName="Helvetica",
    )
    ta_l = ParagraphStyle(
        'parrafos',
        alignment=TA_LEFT,
        fontSize=13,
        fontName="Helvetica-Bold",
    )
    ta_l7 = ParagraphStyle(
        'parrafos',
        alignment=TA_LEFT,
        fontSize=7,
        fontName="Helvetica-Bold",
    )
    ta_r = ParagraphStyle(
        'parrafos',
        alignment=TA_CENTER,
        fontSize=11,
        fontName="Helvetica-Bold",
    )

    # Header
    header = Paragraph(
        "REPORTE DE TRANSACCIONES ",
        ta_l,
    )
    w, h = header.wrap(doc.width + 250, doc.topMargin)
    header.drawOn(canvas, 215, doc.height - 10 + doc.topMargin - 80)

    total1 = Paragraph(
        "Total: " + str(total) + " bs.",
        ta_l,
    )
    w, h = total1.wrap(doc.width + 250, doc.topMargin)
    total1.drawOn(canvas, 425, doc.height - 10 + doc.topMargin - 60)

    meses = Paragraph(
        "Mes: " + str(mes1) + ".",
        ta_l,
    )
    w, h = meses.wrap(doc.width + 250, doc.topMargin)
    meses.drawOn(canvas, 75, doc.height - 10 + doc.topMargin - 60)

    #header1 = Paragraph("<u>DIRECCIÓN DE AGRICULTURA Y TIERRA</u> ",ta_r,)
    #w,h = header1.wrap(doc.width-115, doc.topMargin)
    #header1.drawOn(canvas, 140, doc.height -10 + doc.topMargin - 2)

    P1 = Paragraph('''N°''', ta_c)
    P2 = Paragraph('''FECHA''', ta_c)
    P3 = Paragraph('''TIPO''', ta_c)
    P4 = Paragraph('''MONTO''', ta_c)
    data = [[P1, P2, P3, P4]]
    header2 = Table(data, colWidths=[35, 85, 80, 250])
    header2.setStyle(
        TableStyle([
            ('GRID', (0, -1), (-1, -1), 1, colors.black),
            ('BACKGROUND', (0, 0), (-1, 0), '#50b7e6'),
            ('ALIGN', (0, 0), (-1, -3), "CENTER"),
        ]))
    w, h = header2.wrap(doc.width - 115, doc.topMargin)
    header2.drawOn(canvas, 72.5, doc.height - 40 + doc.topMargin - 93)

    # FOOTER

    footer4 = Paragraph("Fecha: " + str(fecha), ta_l7)
    w, h = footer4.wrap(doc.width - 200, doc.bottomMargin)
    footer4.drawOn(canvas, doc.height - 105, doc.topMargin + 620, h)

    canvas.restoreState()
def test_mode_extra_css():
    w = CodeMirrorEditor(options={"mode": "tiki"})
    js, css = w.media._js, w.media._css
    assert "codemirror2/mode/tiki/tiki.css" in css["screen"]
    for f in chain(js, css["screen"]):
        assert find(f)
Пример #42
0
 def test_searched_locations(self):
     finders.find('spam')
     self.assertEqual(finders.searched_locations,
                      [os.path.join(TEST_ROOT, 'project', 'documents')])
Пример #43
0
def _handle_github_contributions(org, repo, timedelta=3600, weeks_back=8):
    """
    This function returns the url of an image of recent GitHub contributions
    If the image is not present or outdated it will be reconstructed
    """
    path = "GitHub_{0}_{1}_Contribution.png".format(org, repo)
    full_path = os.path.join(djangoSettings.MEDIA_ROOT, path)

    # We have to replot the image
    # Set plot font
    font = {"family": "normal"}
    matplotlib.rc("font", **font)

    # Query GitHub API for contributions
    user_agent = {"user-agent": "oeplatform"}
    http = urllib3.PoolManager(headers=user_agent)
    try:
        reply = http.request(
            "GET",
            "https://api.github.com/repos/{0}/{1}/stats/commit_activity".
            format(org, repo),
        ).data.decode("utf8")
    except:
        pass

    reply = json.loads(reply)

    if not reply:
        return None

    # If there are more weeks than nessecary, truncate
    if weeks_back < len(reply):
        reply = reply[-weeks_back:]

    # GitHub API returns a JSON dict with w: weeks, c: contributions
    (times, commits) = zip(*[(
        datetime.datetime.fromtimestamp(int(week["week"])).strftime("%m-%d"),
        sum(map(int, week["days"])),
    ) for week in reply])
    max_c = max(commits)

    # generate a distribution wrt. to the commit numbers
    commits_ids = [i for i in range(len(commits)) for _ in range(commits[i])]

    # transform the contribution distribution into a density function
    # using a gaussian kernel estimator
    if commits_ids:
        density = stats.kde.gaussian_kde(commits_ids, bw_method=0.2)
    else:
        # if there are no commits the density is a constant 0
        density = lambda x: 0
    # plot this distribution
    x = numpy.arange(0.0, len(commits), 0.01)
    c_real_max = max(density(xv) for xv in x)
    fig1 = plt.figure(figsize=(4, 2))  # facecolor='white',

    # replace labels by dates and numbers of commits
    ax1 = plt.axes(frameon=False)
    plt.fill_between(x, density(x), 0)
    ax1.set_frame_on(False)
    ax1.axes.get_xaxis().tick_bottom()
    ax1.axes.get_yaxis().tick_left()
    plt.yticks(numpy.arange(c_real_max - 0.001, c_real_max), [max_c],
               size="small")
    plt.xticks(numpy.arange(0.0, len(times)), times, size="small", rotation=45)

    # save the figure
    plt.savefig(full_path, transparent=True, bbox_inches="tight")
    url = finders.find(path)
    return url
Пример #44
0
    },
)

USE_SPELLCHECKER = getattr(settings, "TINYMCE_SPELLCHECKER", False)

USE_COMPRESSOR = getattr(settings, "TINYMCE_COMPRESSOR", False)

INCLUDE_JQUERY = getattr(settings, "TINYMCE_INCLUDE_JQUERY", True)

USE_EXTRA_MEDIA = getattr(settings, "TINYMCE_EXTRA_MEDIA", None)

USE_FILEBROWSER = getattr(settings, "TINYMCE_FILEBROWSER", "filebrowser"
                          in settings.INSTALLED_APPS)

JS_URL = getattr(
    #settings, "TINYMCE_JS_URL", os.path.join(settings.STATIC_URL, "tinymce/tinymce.min.js"),
    settings,
    "TINYMCE_JS_URL",
    os.path.join(settings.STATIC_URL, "tinymce/static/tinymce/tinymce.min.js"),
)
try:
    from django.contrib.staticfiles import finders

    JS_ROOT = getattr(settings, "TINYMCE_JS_ROOT",
                      finders.find("tinymce", all=False))
except AppRegistryNotReady:
    JS_ROOT = getattr(settings, "TINYMCE_JS_ROOT",
                      os.path.join(settings.STATIC_ROOT, "tinymce"))

JS_BASE_URL = JS_URL[:JS_URL.rfind("/")]
Пример #45
0
    def replace_static_url(original, prefix, quote, rest):
        """
        Replace a single matched url.
        """
        original_uri = "".join([prefix, rest])
        # Don't mess with things that end in '?raw'
        if rest.endswith('?raw'):
            static_paths_out.append((original_uri, original_uri))
            return original

        # In debug mode, if we can find the url as is,
        if settings.DEBUG and finders.find(rest, True):
            static_paths_out.append((original_uri, original_uri))
            return original

        # if we're running with a MongoBacked store course_namespace is not None, then use studio style urls
        elif (not static_asset_path) and course_id:
            # first look in the static file pipeline and see if we are trying to reference
            # a piece of static content which is in the edx-platform repo (e.g. JS associated with an xmodule)

            exists_in_staticfiles_storage = False
            try:
                exists_in_staticfiles_storage = staticfiles_storage.exists(
                    rest)
            except Exception as err:
                log.warning(
                    "staticfiles_storage couldn't find path {0}: {1}".format(
                        rest, str(err)))

            if exists_in_staticfiles_storage:
                url = staticfiles_storage.url(rest)
            else:
                # if not, then assume it's courseware specific content and then look in the
                # Mongo-backed database
                # Import is placed here to avoid model import at project startup.
                from common.djangoapps.static_replace.models import AssetBaseUrlConfig, AssetExcludedExtensionsConfig
                base_url = AssetBaseUrlConfig.get_base_url()
                excluded_exts = AssetExcludedExtensionsConfig.get_excluded_extensions(
                )
                url = StaticContent.get_canonicalized_asset_path(
                    course_id, rest, base_url, excluded_exts)

                if AssetLocator.CANONICAL_NAMESPACE in url:
                    url = url.replace('block@', 'block/', 1)

        # Otherwise, look the file up in staticfiles_storage, and append the data directory if needed
        else:
            course_path = "/".join((static_asset_path or data_directory, rest))

            try:
                if staticfiles_storage.exists(rest):
                    url = staticfiles_storage.url(rest)
                else:
                    url = staticfiles_storage.url(course_path)
            # And if that fails, assume that it's course content, and add manually data directory
            except Exception as err:
                log.warning(
                    "staticfiles_storage couldn't find path {0}: {1}".format(
                        rest, str(err)))
                url = "".join([prefix, course_path])

        static_paths_out.append((original_uri, url))
        return "".join([quote, url, quote])
Пример #46
0
 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)
Пример #47
0
def global_settings(request):
    last_instance = get_last_visited_instance(request)
    if hasattr(request, 'user') and request.user.is_authenticated():
        last_effective_instance_user =\
            request.user.get_effective_instance_user(last_instance)
        _update_last_seen(last_effective_instance_user)
    else:
        if hasattr(request, 'instance'):
            instance = request.instance
            default_role = instance.default_role

            last_effective_instance_user = InstanceUser(
                role=default_role, instance=instance)
        else:
            last_effective_instance_user = None

    if hasattr(request, 'instance') and request.instance.logo:
        logo_url = request.instance.logo.url
    else:
        logo_url = settings.STATIC_URL + "img/logo.png"

    try:
        comment_file_path = finders.find('version.txt')
        with open(comment_file_path, 'r') as f:
            header_comment = f.read()
    except:
        header_comment = "Version information not available\n"

    term = copy.copy(REPLACEABLE_TERMS)
    if hasattr(request, 'instance'):
        term.update(request.instance.config.get('terms', {}))
        # config.get('terms') above populates the term context variable with
        # model terminology provided it has been customized for the treemap
        # instance, but fails to populate it with the default terminology. The
        # for loop below ensures that term is populated with model terminology
        # whether it has been customized or not.

        # Convertible is the base class where the terminology class property is
        # defined, so its leaf subclasses are the ones with default terminology
        # we might care about.

        # leaf_models_of_class uses recursive descent through the
        # clz.__subclasses__ attributes, but it only iterates through a total
        # of around ten nodes at present, so it is unlikely to be a performance
        # problem.
        for clz in leaf_models_of_class(Convertible):
            term.update({
                clz.__name__: clz.terminology(request.instance)})

    ctx = {
        'SITE_ROOT': settings.SITE_ROOT,
        'settings': settings,
        'last_instance': last_instance,
        'last_effective_instance_user': last_effective_instance_user,
        'logo_url': logo_url,
        'header_comment': header_comment,
        'term': term,
        'embed': request_is_embedded(request),
        'datepicker_start_date': datetime.min.replace(year=1900),
    }

    return ctx
Пример #48
0
    def test_static_files(self):

        # If using static media correctly the result is not NONE

        result = finders.find('img/logo.jpg')
        self.assertIsNotNone(result)
Пример #49
0
from django.conf import settings
from django.core.exceptions import AppRegistryNotReady

DEFAULT_CONFIG = getattr(
    settings, "TINYMCE_DEFAULT_CONFIG", {"theme": "silver", "relative_urls": False}
)

USE_SPELLCHECKER = getattr(settings, "TINYMCE_SPELLCHECKER", False)

USE_COMPRESSOR = getattr(settings, "TINYMCE_COMPRESSOR", False)

INCLUDE_JQUERY = getattr(settings, "TINYMCE_INCLUDE_JQUERY", True)

USE_EXTRA_MEDIA = getattr(settings, "TINYMCE_EXTRA_MEDIA", None)

USE_FILEBROWSER = getattr(
    settings, "TINYMCE_FILEBROWSER", "filebrowser" in settings.INSTALLED_APPS
)

JS_URL = getattr(
    settings, "TINYMCE_JS_URL", os.path.join(settings.STATIC_URL, "tinymce/tinymce.min.js"),
)
try:
    from django.contrib.staticfiles import finders

    JS_ROOT = getattr(settings, "TINYMCE_JS_ROOT", finders.find("tinymce", all=False))
except AppRegistryNotReady:
    JS_ROOT = getattr(settings, "TINYMCE_JS_ROOT", os.path.join(settings.STATIC_ROOT, "tinymce"))

JS_BASE_URL = JS_URL[: JS_URL.rfind("/")]
Пример #50
0
def get_rand_photo(width=400):
    with open(find("animal_photo_urls.txt")) as f:
        urls = f.readlines()
    return urls[randint(0, len(urls) - 1)].strip() + "?w=" + str(width)
Пример #51
0
def animation_view(request):
    if request.method == 'POST':
        text = request.POST.get('sen')
        #tokenizing the sentence
        text.lower()
        #tokenizing the sentence
        words = word_tokenize(text)

        tagged = nltk.pos_tag(words)
        tense = {}
        tense["future"] = len([word for word in tagged if word[1] == "MD"])
        tense["present"] = len(
            [word for word in tagged if word[1] in ["VBP", "VBZ", "VBG"]])
        tense["past"] = len(
            [word for word in tagged if word[1] in ["VBD", "VBN"]])
        tense["present_continuous"] = len(
            [word for word in tagged if word[1] in ["VBG"]])

        #stopwords that will be removed
        stop_words = set([
            "mightn't", 're', 'wasn', 'wouldn', 'be', 'has', 'that', 'does',
            'shouldn', 'do', "you've", 'off', 'for', "didn't", 'm', 'ain',
            'haven', "weren't", 'are', "she's", "wasn't", 'its', "haven't",
            "wouldn't", 'don', 'weren', 's', "you'd", "don't", 'doesn',
            "hadn't", 'is', 'was', "that'll", "should've", 'a', 'then', 'the',
            'mustn', 'i', 'nor', 'as', "it's", "needn't", 'd', 'am', 'have',
            'hasn', 'o', "aren't", "you'll", "couldn't", "you're", "mustn't",
            'didn', "doesn't", 'll', 'an', 'hadn', 'whom', 'y', "hasn't",
            'itself', 'couldn', 'needn', "shan't", 'isn', 'been', 'such',
            'shan', "shouldn't", 'aren', 'being', 'were', 'did', 'ma', 't',
            'having', 'mightn', 've', "isn't", "won't"
        ])

        #removing stopwords and applying lemmatizing nlp process to words
        lr = WordNetLemmatizer()
        filtered_text = []
        for w, p in zip(words, tagged):
            if w not in stop_words:
                if p[1] == 'VBG' or p[1] == 'VBD' or p[1] == 'VBZ' or p[
                        1] == 'VBN' or p[1] == 'NN':
                    filtered_text.append(lr.lemmatize(w, pos='v'))
                elif p[1] == 'JJ' or p[1] == 'JJR' or p[1] == 'JJS' or p[
                        1] == 'RBR' or p[1] == 'RBS':
                    filtered_text.append(lr.lemmatize(w, pos='a'))

                else:
                    filtered_text.append(lr.lemmatize(w))

        #adding the specific word to specify tense
        words = filtered_text
        temp = []
        for w in words:
            if w == 'I':
                temp.append('Me')
            else:
                temp.append(w)
        words = temp
        probable_tense = max(tense, key=tense.get)

        if probable_tense == "past" and tense["past"] >= 1:
            temp = ["Before"]
            temp = temp + words
            words = temp
        elif probable_tense == "future" and tense["future"] >= 1:
            if "Will" not in words:
                temp = ["Will"]
                temp = temp + words
                words = temp
            else:
                pass
        elif probable_tense == "present":
            if tense["present_continuous"] >= 1:
                temp = ["Now"]
                temp = temp + words
                words = temp

        filtered_text = []
        for w in words:
            path = w + ".mp4"
            f = finders.find(path)
            #splitting the word if its animation is not present in database
            if not f:
                for c in w:
                    filtered_text.append(c)
            #otherwise animation of word
            else:
                filtered_text.append(w)
        words = filtered_text

        return render(request, 'animation.html', {
            'words': words,
            'text': text
        })
    else:
        return render(request, 'animation.html')
Пример #52
0
 def _get_default_background(self):
     return open(finders.find('pretixpresale/pdf/ticket_default_a4.pdf'), "rb")
Пример #53
0
# Copyright (C) 2017-2019  The Software Heritage developers
# See the AUTHORS file at the top-level directory of this distribution
# License: GNU Affero General Public License version 3, or any later version
# See top-level LICENSE file for more information

import functools
import json
from typing import Dict

from pygments.lexers import get_all_lexers, get_lexer_for_filename
import sentry_sdk

from django.contrib.staticfiles.finders import find

with open(str(find("json/highlightjs-languages.json")),
          "r") as _hljs_languages_file:
    _hljs_languages_data = json.load(_hljs_languages_file)

# set of languages ids that can be highlighted by highlight.js library
_hljs_languages = set(_hljs_languages_data["languages"])

# languages aliases defined in highlight.js
_hljs_languages_aliases = {
    **_hljs_languages_data["languages_aliases"],
    "ml": "ocaml",
    "bsl": "1c",
    "ep": "mojolicious",
    "lc": "livecode",
    "p": "parser3",
    "pde": "processing",
    "rsc": "routeros",
Пример #54
0
def serve(request,
          path,
          document_root=None,
          show_indexes=False,
          insecure=False):
    """
    Serve static files below a given point in the directory structure or
    from locations inferred from the static files finders.

    To use, put a URL pattern such as::

        (r'^(?P<path>.*)$', 'django.contrib.staticfiles.views.serve')

    in your URLconf.

    If you provide the ``document_root`` parameter, the file won't be looked
    up with the staticfiles finders, but in the given filesystem path, e.g.::

    (r'^(?P<path>.*)$', 'django.contrib.staticfiles.views.serve', {'document_root' : '/path/to/my/files/'})

    You may also set ``show_indexes`` to ``True`` if you'd like to serve a
    basic index of the directory.  This index view will use the
    template hardcoded below, but if you'd like to override it, you can create
    a template called ``static/directory_index.html``.
    """
    if not settings.DEBUG and not insecure:
        raise ImproperlyConfigured("The view to serve static files can only "
                                   "be used if the DEBUG setting is True or "
                                   "the --insecure option of 'runserver' is "
                                   "used")
    if not document_root:
        absolute_path = finders.find(path)
        if not absolute_path:
            raise Http404('"%s" could not be found' % path)
        document_root, path = os.path.split(absolute_path)
    # Clean up given path to only allow serving files below document_root.
    path = posixpath.normpath(urllib.unquote(path))
    path = path.lstrip('/')
    newpath = ''
    for part in path.split('/'):
        if not part:
            # Strip empty path components.
            continue
        drive, part = os.path.splitdrive(part)
        head, part = os.path.split(part)
        if part in (os.curdir, os.pardir):
            # Strip '.' and '..' in path.
            continue
        newpath = os.path.join(newpath, part).replace('\\', '/')
    if newpath and path != newpath:
        return HttpResponseRedirect(newpath)
    fullpath = os.path.join(document_root, newpath)
    if os.path.isdir(fullpath):
        if show_indexes:
            return directory_index(newpath, fullpath)
        raise Http404("Directory indexes are not allowed here.")
    if not os.path.exists(fullpath):
        raise Http404('"%s" does not exist' % fullpath)
    # Respect the If-Modified-Since header.
    statobj = os.stat(fullpath)
    mimetype, encoding = mimetypes.guess_type(fullpath)
    mimetype = mimetype or 'application/octet-stream'
    if not was_modified_since(request.META.get('HTTP_IF_MODIFIED_SINCE'),
                              statobj[stat.ST_MTIME], statobj[stat.ST_SIZE]):
        return HttpResponseNotModified(mimetype=mimetype)
    contents = open(fullpath, 'rb').read()
    response = HttpResponse(contents, mimetype=mimetype)
    response["Last-Modified"] = http_date(statobj[stat.ST_MTIME])
    response["Content-Length"] = len(contents)
    if encoding:
        response["Content-Encoding"] = encoding
    return response
Пример #55
0
def get_fallback_avatar(size):
    """Return fallback avatar."""
    filename = finders.find('weblate-{0}.png'.format(size))
    with open(filename, 'rb') as handle:
        return handle.read()
Пример #56
0
 def test_static_finder(self):
 	'''Gets correctly the static files'''
     result = finders.find('images/favicon.ico')
     self.assertIsNotNone(result)
Пример #57
0
def find(path, *args, **kwargs):
    if DJANGO_CONFIGURED:
        return finders.find(path, *args, **kwargs)
    if path and os.path.isabs(path) and os.path.exists(path):
        return path
Пример #58
0
 def real_path(self):
     return finders.find(self.path)
Пример #59
0
 def get(self, path, include_body=True):
     from django.contrib.staticfiles import finders
     normalized_path = posixpath.normpath(unquote(path)).lstrip('/')
     absolute_path = finders.find(normalized_path)
     return super(DjangoStaticFileHandler,
                  self).get(absolute_path, include_body)
Пример #60
0
 def find_placeholder(self):
     placeholder = 'documents/{0}-placeholder.png'
     return finders.find(placeholder.format(self.extension), False) or \
         finders.find(placeholder.format('generic'), False)