aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site
diff options
context:
space:
mode:
authorGravatar Lucas Lindström <[email protected]>2020-10-06 21:29:34 +0200
committerGravatar Lucas Lindström <[email protected]>2020-10-06 21:58:57 +0200
commitc8c6cb8754bd0917e35eb157925028a9b6f1dcb9 (patch)
tree7da1d95e76a21eca127b1239ed4f4e042d272da4 /pydis_site
parentMerge pull request #402 from python-discord/joseph/reduce-discord-scopes (diff)
Added metricity db connection and user bot API
Diffstat (limited to 'pydis_site')
-rw-r--r--pydis_site/apps/api/viewsets/bot/user.py46
-rw-r--r--pydis_site/settings.py3
2 files changed, 48 insertions, 1 deletions
diff --git a/pydis_site/apps/api/viewsets/bot/user.py b/pydis_site/apps/api/viewsets/bot/user.py
index 9571b3d7..0eeacbb3 100644
--- a/pydis_site/apps/api/viewsets/bot/user.py
+++ b/pydis_site/apps/api/viewsets/bot/user.py
@@ -1,3 +1,10 @@
+import json
+
+from django.db import connections
+from rest_framework import status
+from rest_framework.decorators import action
+from rest_framework.request import Request
+from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from rest_framework_bulk import BulkCreateModelMixin
@@ -53,6 +60,29 @@ class UserViewSet(BulkCreateModelMixin, ModelViewSet):
- 200: returned on success
- 404: if a user with the given `snowflake` could not be found
+ ### GET /bot/users/<snowflake:int>/metricity_data
+ Gets metricity data for a single user by ID.
+
+ #### Response format
+ >>> {
+ ... "id": "0",
+ ... "name": "foo",
+ ... "avatar_hash": "bar",
+ ... "joined_at": "2020-10-06T18:17:30.101677",
+ ... "created_at": "2020-10-06T18:17:30.101677",
+ ... "is_staff": False,
+ ... "opt_out": False,
+ ... "bot": False,
+ ... "is_guild": True,
+ ... "is_verified": False,
+ ... "public_flags": {},
+ ... "verified_at": null
+ ...}
+
+ #### Status codes
+ - 200: returned on success
+ - 404: if a user with the given `snowflake` could not be found
+
### POST /bot/users
Adds a single or multiple new users.
The roles attached to the user(s) must be roles known by the site.
@@ -115,7 +145,23 @@ class UserViewSet(BulkCreateModelMixin, ModelViewSet):
#### Status codes
- 204: returned on success
- 404: if a user with the given `snowflake` does not exist
+
+
"""
serializer_class = UserSerializer
queryset = User.objects
+
+ @action(detail=True)
+ def metricity_data(self, request: Request, pk: str = None) -> Response:
+ """Request handler for metricity_data endpoint."""
+ user = self.get_object()
+ column_keys = ["id", "name", "avatar_hash", "joined_at", "created_at", "is_staff",
+ "opt_out", "bot", "is_guild", "is_verified", "public_flags", "verified_at"]
+ with connections['metricity'].cursor() as cursor:
+ query = f"SELECT {','.join(column_keys)} FROM users WHERE id = '%s'"
+ cursor.execute(query, [user.id])
+ values = cursor.fetchone()
+ data = dict(zip(column_keys, values))
+ data["public_flags"] = json.loads(data["public_flags"])
+ return Response(data, status=status.HTTP_200_OK)
diff --git a/pydis_site/settings.py b/pydis_site/settings.py
index 3769fa25..2e78e458 100644
--- a/pydis_site/settings.py
+++ b/pydis_site/settings.py
@@ -172,7 +172,8 @@ WSGI_APPLICATION = 'pydis_site.wsgi.application'
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases
DATABASES = {
- 'default': env.db()
+ 'default': env.db(),
+ 'metricity': env.db('METRICITY_DB_URL'),
}
# Password validation