aboutsummaryrefslogtreecommitdiffstats
path: root/pysite/views
diff options
context:
space:
mode:
Diffstat (limited to 'pysite/views')
-rw-r--r--pysite/views/api/bot/clean.py48
-rw-r--r--pysite/views/api/bot/off_topic_names.py47
-rw-r--r--pysite/views/main/bot/cleanlog.py35
3 files changed, 119 insertions, 11 deletions
diff --git a/pysite/views/api/bot/clean.py b/pysite/views/api/bot/clean.py
new file mode 100644
index 00000000..82d1e735
--- /dev/null
+++ b/pysite/views/api/bot/clean.py
@@ -0,0 +1,48 @@
+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,
+ "user_id": str,
+ "content": str,
+ "role_id": str,
+ "timestamp": str,
+ "embeds": object,
+ "attachments": [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/api/bot/off_topic_names.py b/pysite/views/api/bot/off_topic_names.py
index f353ab02..1c75428e 100644
--- a/pysite/views/api/bot/off_topic_names.py
+++ b/pysite/views/api/bot/off_topic_names.py
@@ -9,18 +9,24 @@ from pysite.decorators import api_key, api_params
from pysite.mixins import DBMixin
-POST_SCHEMA = Schema({
- 'name': And(
- str,
- len,
- lambda name: all(c.isalnum() or c == '-' for c in name),
- str.islower,
- lambda name: len(name) <= 96,
- error=(
- "The channel name must be a non-blank string consisting only of"
- " lowercase regular characters and '-' with a maximum length of 96"
- )
+OFF_TOPIC_NAME = And(
+ str,
+ len,
+ lambda name: all(c.isalnum() or c == '-' for c in name),
+ str.islower,
+ lambda name: len(name) <= 96,
+ error=(
+ "The channel name must be a non-blank string consisting only of"
+ " lowercase regular characters and '-' with a maximum length of 96"
)
+)
+
+DELETE_SCHEMA = Schema({
+ 'name': OFF_TOPIC_NAME
+})
+
+POST_SCHEMA = Schema({
+ 'name': OFF_TOPIC_NAME
})
@@ -30,6 +36,25 @@ class OffTopicNamesView(APIView, DBMixin):
table_name = "off_topic_names"
@api_key
+ @api_params(schema=DELETE_SCHEMA, validation_type=ValidationTypes.params)
+ def delete(self, params):
+ """
+ Removes a single off-topic name from the database.
+ Returns the result of the deletion call.
+
+ API key must be provided as header.
+ Name to delete must be provided as the `name` query argument.
+ """
+
+ result = self.db.delete(
+ self.table_name,
+ params['name'],
+ return_changes=True
+ )
+
+ return jsonify(result)
+
+ @api_key
def get(self):
"""
Fetch all known off-topic channel names from the database.
diff --git a/pysite/views/main/bot/cleanlog.py b/pysite/views/main/bot/cleanlog.py
new file mode 100644
index 00000000..9c719b3e
--- /dev/null
+++ b/pysite/views/main/bot/cleanlog.py
@@ -0,0 +1,35 @@
+import logging
+
+from pysite.base_route import RouteView
+from pysite.constants import ALL_STAFF_ROLES, DEVELOPERS_ROLE, ROLE_COLORS
+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"
+
+ @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'] = ROLE_COLORS.get(message['role_id'], ROLE_COLORS[DEVELOPERS_ROLE])
+
+ return self.render(self.template, messages=messages)