diff options
| author | 2018-07-21 04:48:54 +0200 | |
|---|---|---|
| committer | 2018-07-21 04:48:54 +0200 | |
| commit | 3a9ceaccb5326ff3b569e3f948b94107b7c26ee2 (patch) | |
| tree | 9fd4e017965e20467b551193284c55533fc5161c /pysite | |
| parent | Merge branch 'update-dev-env' into 'master' (diff) | |
Completed the clean API and the clean frontend.
Diffstat (limited to 'pysite')
| -rw-r--r-- | pysite/tables.py | 8 | ||||
| -rw-r--r-- | pysite/views/api/bot/clean.py | 44 | ||||
| -rw-r--r-- | pysite/views/main/bot/cleanlog.py | 65 | 
3 files changed, 117 insertions, 0 deletions
diff --git a/pysite/tables.py b/pysite/tables.py index 8f849664..adc8c409 100644 --- a/pysite/tables.py +++ b/pysite/tables.py @@ -16,6 +16,14 @@ TABLES = {          ])      ), +    "clean_logs": Table(  # Logs of cleanups done by the clean bot commands +        primary_key="id", +        keys=sorted([ +            "id", +            "log_data" +        ]) +    ), +      "hiphopify": Table(  # Users in hiphop prison          primary_key="user_id",          keys=sorted([ diff --git a/pysite/views/api/bot/clean.py b/pysite/views/api/bot/clean.py new file mode 100644 index 00000000..216261ce --- /dev/null +++ b/pysite/views/api/bot/clean.py @@ -0,0 +1,44 @@ +from flask import jsonify +from schema import Schema + +from pysite.base_route import APIView +from pysite.constants import ValidationTypes +from pysite.decorators import api_key, api_params +from pysite.mixins import DBMixin + +POST_SCHEMA = Schema({ +    'log_data': [ +        { +            "author": str, +            "content": str, +            "timestamp": str +        } +    ] +}) + + +class CleanView(APIView, DBMixin): +    path = '/bot/clean' +    name = 'bot.clean' +    table_name = 'clean_logs' + +    @api_key +    @api_params(schema=POST_SCHEMA, validation_type=ValidationTypes.json) +    def post(self, data): +        """ +        Receive some log_data from a bulk deletion, +        and store it in the database. + +        Returns an ID which can be used to get the data +        from the /bot/clean_logs/<id> endpoint. +        """ + +        # Insert and return the id to use for GET +        insert = self.db.insert( +            self.table_name, +            { +                "log_data": data["log_data"] +            } +        ) + +        return jsonify({"log_id": insert['generated_keys'][0]}) diff --git a/pysite/views/main/bot/cleanlog.py b/pysite/views/main/bot/cleanlog.py new file mode 100644 index 00000000..abd1ee2e --- /dev/null +++ b/pysite/views/main/bot/cleanlog.py @@ -0,0 +1,65 @@ +import logging +import random + +from pysite.base_route import RouteView +from pysite.constants import ALL_STAFF_ROLES +from pysite.decorators import require_roles +from pysite.mixins import DBMixin, OAuthMixin + +log = logging.getLogger(__name__) + + +class CleanLogView(RouteView, DBMixin, OAuthMixin): +    path = "/bot/clean_logs/<log_id>" +    name = "bot.clean_logs" + +    table_name = "clean_logs" +    template = "main/bot/clean_logs.html" + +    # Colors +    author_colors = {} +    all_colors = [ +        "#afcfff", +        "#93ff91", +        "#ffe559", +        "#ff9036", +        "#ff6c5e", +        "#ff65be", +        "#9298ff", +    ] +    color_pool = all_colors + +    def _assign_color(self, author): +        """ +        Assign a color to a specific author. +        """ + +        if not self.color_pool: +            self.color_pool = self.all_colors + +        if author not in self.author_colors: +            random_index = random.randint(0, len(self.color_pool)) +            color = self.color_pool.pop(random_index) +            self.author_colors[author] = color +            return color +        else: +            return self.author_colors[author] + +    @require_roles(ALL_STAFF_ROLES) +    def get(self, log_id): +        """ +        Get the requested clean log and spit it out +        in a beautiful template. +        """ + +        data = self.db.get(self.table_name, log_id) + +        if data is None: +            return "ID could not be found in the database", 404 + +        messages = data["log_data"] + +        for message in messages: +            message['color'] = self._assign_color(message['author']) + +        return self.render(self.template, messages=messages)  |