Преглед на файлове

Hide sensitive data in admin

Colin Powell преди 2 години
родител
ревизия
d8edad98b2
променени са 2 файла, в които са добавени 28 реда и са изтрити 3 реда
  1. 5 0
      vrobbler/apps/profiles/admin.py
  2. 23 3
      vrobbler/apps/profiles/models.py

+ 5 - 0
vrobbler/apps/profiles/admin.py

@@ -7,3 +7,8 @@ from profiles.models import UserProfile
 class UserProfileAdmin(admin.ModelAdmin):
     date_hierarchy = "created"
     ordering = ("-created",)
+    exclude = (
+        "twitch_token",
+        "twitch_client_secret",
+        "lastfm_password",
+    )

+ 23 - 3
vrobbler/apps/profiles/models.py

@@ -1,11 +1,14 @@
-import pytz
+from datetime import timedelta
 
+import pytz
+from django.utils import timezone
 from django.contrib.auth import get_user_model
 from django.db import models
 from django_extensions.db.models import TimeStampedModel
+from encrypted_field import EncryptedField
 from profiles.constants import PRETTY_TIMEZONE_CHOICES
 
-from encrypted_field import EncryptedField
+from vrobbler.apps.videogames.igdb import refresh_igdb_api_token
 
 User = get_user_model()
 BNULL = {"blank": True, "null": True}
@@ -16,10 +19,16 @@ class UserProfile(TimeStampedModel):
         User, on_delete=models.CASCADE, related_name="profile"
     )
     timezone = models.CharField(
-        max_length=255, choices=PRETTY_TIMEZONE_CHOICES, default=pytz.UTC
+        max_length=255,
+        choices=PRETTY_TIMEZONE_CHOICES,
+        **BNULL,
     )
     lastfm_username = models.CharField(max_length=255, **BNULL)
     lastfm_password = EncryptedField(**BNULL)
+    twitch_client_id = models.CharField(max_length=255, **BNULL)
+    twitch_client_secret = EncryptedField(**BNULL)
+    twitch_token = EncryptedField(**BNULL)
+    twitch_token_expires = models.DateTimeField(**BNULL)
 
     def __str__(self):
         return f"User profile for {self.user}"
@@ -27,3 +36,14 @@ class UserProfile(TimeStampedModel):
     @property
     def tzinfo(self):
         return pytz.timezone(self.timezone)
+
+    def get_twitch_token(self):
+        now = timezone.now()
+        token = self.twitch_token
+        if not token or self.twitch_token_expires < now:
+            self.twitch_token, expires_in = refresh_igdb_api_token(
+                self.user_id
+            )
+            self.twitch_token_expires = now + timedelta(seconds=expires_in)
+            self.save(update_fields=["twitch_token", "twitch_token_expires"])
+        return self.twitch_token