Hide sensitive data in admin

This commit is contained in:
2023-03-05 02:30:42 -05:00
parent 9848d311c4
commit d8edad98b2
2 changed files with 28 additions and 3 deletions

View File

@ -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",
)

View File

@ -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