Esempio n. 1
0
    def change_css(self,
                   content,
                   parsed,
                   prev=None,
                   reason=None,
                   author=None,
                   force=False):
        from r2.models import ModAction
        author = author if author else c.user.name
        if content is None:
            content = ''
        try:
            wiki = WikiPage.get(self, 'config/stylesheet')
        except tdb_cassandra.NotFound:
            wiki = WikiPage.create(self, 'config/stylesheet')
        wr = wiki.revise(content,
                         previous=prev,
                         author=author,
                         reason=reason,
                         force=force)

        minified = cssmin(parsed)
        if minified:
            if g.static_stylesheet_bucket:
                digest = hashlib.sha1(minified).digest()
                self.stylesheet_hash = (
                    base64.urlsafe_b64encode(digest).rstrip("="))

                s3cp.send_file(
                    g.static_stylesheet_bucket,
                    self.static_stylesheet_name,
                    minified,
                    content_type="text/css",
                    never_expire=True,
                    replace=False,
                )

                self.stylesheet_contents = ""
                self.stylesheet_modified = None
            else:
                self.stylesheet_hash = hashlib.md5(minified).hexdigest()
                self.stylesheet_contents = minified
                self.stylesheet_modified = datetime.datetime.now(g.tz)
        else:
            self.stylesheet_contents = ""
            self.stylesheet_hash = ""
            self.stylesheet_modified = datetime.datetime.now(g.tz)
        self.stylesheet_contents_user = ""  # reads from wiki; ensure pg clean
        self._commit()

        ModAction.create(self,
                         c.user,
                         action='wikirevise',
                         details='Updated subreddit stylesheet')
        return wr
Esempio n. 2
0
    def change_css(self, content, parsed, prev=None, reason=None, author=None, force=False):
        from r2.models import ModAction

        author = author if author else c.user.name
        if content is None:
            content = ""
        try:
            wiki = WikiPage.get(self, "config/stylesheet")
        except tdb_cassandra.NotFound:
            wiki = WikiPage.create(self, "config/stylesheet")
        wr = wiki.revise(content, previous=prev, author=author, reason=reason, force=force)

        minified = cssmin(parsed)
        if minified:
            if g.static_stylesheet_bucket:
                digest = hashlib.sha1(minified).digest()
                self.stylesheet_hash = base64.urlsafe_b64encode(digest).rstrip("=")

                s3cp.send_file(
                    g.static_stylesheet_bucket,
                    self.static_stylesheet_name,
                    minified,
                    content_type="text/css",
                    never_expire=True,
                    replace=False,
                )

                self.stylesheet_contents = ""
                self.stylesheet_modified = None
            else:
                self.stylesheet_hash = hashlib.md5(minified).hexdigest()
                self.stylesheet_contents = minified
                self.stylesheet_modified = datetime.datetime.now(g.tz)
        else:
            self.stylesheet_contents = ""
            self.stylesheet_hash = ""
            self.stylesheet_modified = datetime.datetime.now(g.tz)
        self.stylesheet_contents_user = ""  # reads from wiki; ensure pg clean
        self._commit()

        ModAction.create(self, c.user, action="wikirevise", details="Updated subreddit stylesheet")
        return wr
Esempio n. 3
0
    def parse_and_validate(self, stylesheet_source):
        if len(stylesheet_source) > (MAX_SIZE_KIB * 1024):
            return "", [ValidationError(0, "TOO_BIG", {"size": MAX_SIZE_KIB})]

        nodes = tinycss2.parse_stylesheet(stylesheet_source)

        source_lines = stylesheet_source.splitlines()

        backslash_errors = self.check_for_evil_codepoints(source_lines)
        validation_errors = self.validate_rule_list(nodes)

        errors = []
        for error in itertools.chain(backslash_errors, validation_errors):
            error._source_lines = source_lines
            errors.append(error)
        errors.sort(key=lambda e: e.line)

        if not errors:
            serialized = rcssmin.cssmin(tinycss2.serialize(nodes))
        else:
            serialized = ""

        return serialized.encode("utf-8"), errors
Esempio n. 4
0
    def parse_and_validate(self, stylesheet_source):
        if len(stylesheet_source) > (MAX_SIZE_KIB * 1024):
            return "", [ValidationError(0, "TOO_BIG", {"size": MAX_SIZE_KIB})]

        nodes = tinycss2.parse_stylesheet(stylesheet_source)

        source_lines = stylesheet_source.splitlines()

        backslash_errors = self.check_for_evil_codepoints(source_lines)
        validation_errors = self.validate_rule_list(nodes)

        errors = []
        for error in itertools.chain(backslash_errors, validation_errors):
            error._source_lines = source_lines
            errors.append(error)
        errors.sort(key=lambda e: e.line)

        if not errors:
            serialized = rcssmin.cssmin(tinycss2.serialize(nodes))
        else:
            serialized = ""

        return serialized.encode("utf-8"), errors