예제 #1
0
def compile_scss(object, file="main.scss", fonts=True):
    sassdir = os.path.join(settings.STATIC_ROOT, 'pretixpresale/scss')

    def static(path):
        sp = _static(path)
        if not settings.MEDIA_URL.startswith("/") and sp.startswith("/"):
            domain = get_domain(
                object.organizer if isinstance(object, Event) else object)
            if domain:
                siteurlsplit = urlsplit(settings.SITE_URL)
                if siteurlsplit.port and siteurlsplit.port not in (80, 443):
                    domain = '%s:%d' % (domain, siteurlsplit.port)
                sp = urljoin('%s://%s' % (siteurlsplit.scheme, domain), sp)
            else:
                sp = urljoin(settings.SITE_URL, sp)
        return '"{}"'.format(sp)

    sassrules = []
    if object.settings.get('primary_color'):
        sassrules.append('$brand-primary: {};'.format(
            object.settings.get('primary_color')))
    if object.settings.get('theme_color_success'):
        sassrules.append('$brand-success: {};'.format(
            object.settings.get('theme_color_success')))
    if object.settings.get('theme_color_danger'):
        sassrules.append('$brand-danger: {};'.format(
            object.settings.get('theme_color_danger')))

    font = object.settings.get('primary_font')
    if font != 'Open Sans' and fonts:
        sassrules.append(get_font_stylesheet(font))
        sassrules.append(
            '$font-family-sans-serif: "{}", "Open Sans", "OpenSans", "Helvetica Neue", Helvetica, Arial, sans-serif '
            '!default'.format(font))

    if isinstance(object, Event):
        for recv, resp in sass_preamble.send(object, filename=file):
            sassrules.append(resp)

    sassrules.append('@import "{}";'.format(file))

    if isinstance(object, Event):
        for recv, resp in sass_postamble.send(object, filename=file):
            sassrules.append(resp)

    cf = dict(django_libsass.CUSTOM_FUNCTIONS)
    cf['static'] = static
    css = sass.compile(string="\n".join(sassrules),
                       include_paths=[sassdir],
                       output_style='nested',
                       custom_functions=cf)
    cssf = CSSCompressorFilter(css)
    css = cssf.output()
    checksum = hashlib.sha1(css.encode('utf-8')).hexdigest()
    return css, checksum
예제 #2
0
def compile_scss(object, file="main.scss", fonts=True):
    sassdir = os.path.join(settings.STATIC_ROOT, 'pretixpresale/scss')

    def static(path):
        sp = _static(path)
        if not settings.MEDIA_URL.startswith("/") and sp.startswith("/"):
            domain = get_domain(object.organizer if isinstance(object, Event) else object)
            if domain:
                siteurlsplit = urlsplit(settings.SITE_URL)
                if siteurlsplit.port and siteurlsplit.port not in (80, 443):
                    domain = '%s:%d' % (domain, siteurlsplit.port)
                sp = urljoin('%s://%s' % (siteurlsplit.scheme, domain), sp)
            else:
                sp = urljoin(settings.SITE_URL, sp)
        return '"{}"'.format(sp)

    sassrules = []
    if object.settings.get('primary_color'):
        sassrules.append('$brand-primary: {};'.format(object.settings.get('primary_color')))
    if object.settings.get('theme_color_success'):
        sassrules.append('$brand-success: {};'.format(object.settings.get('theme_color_success')))
    if object.settings.get('theme_color_danger'):
        sassrules.append('$brand-danger: {};'.format(object.settings.get('theme_color_danger')))

    font = object.settings.get('primary_font')
    if font != 'Open Sans' and fonts:
        sassrules.append(get_font_stylesheet(font))
        sassrules.append(
            '$font-family-sans-serif: "{}", "Open Sans", "OpenSans", "Helvetica Neue", Helvetica, Arial, sans-serif '
            '!default'.format(
                font
            ))

    if isinstance(object, Event):
        for recv, resp in sass_preamble.send(object, filename=file):
            sassrules.append(resp)

    sassrules.append('@import "{}";'.format(file))

    if isinstance(object, Event):
        for recv, resp in sass_postamble.send(object, filename=file):
            sassrules.append(resp)

    cf = dict(django_libsass.CUSTOM_FUNCTIONS)
    cf['static'] = static
    css = sass.compile(
        string="\n".join(sassrules),
        include_paths=[sassdir], output_style='nested',
        custom_functions=cf
    )
    cssf = CSSCompressorFilter(css)
    css = cssf.output()
    checksum = hashlib.sha1(css.encode('utf-8')).hexdigest()
    return css, checksum