Add user profile with a timezone
This commit is contained in:
8
poetry.lock
generated
8
poetry.lock
generated
@ -1100,7 +1100,7 @@ postgresql = ["psycopg2"]
|
||||
|
||||
[[package]]
|
||||
name = "pytz"
|
||||
version = "2022.7"
|
||||
version = "2022.7.1"
|
||||
description = "World timezone definitions, modern and historical"
|
||||
category = "main"
|
||||
optional = false
|
||||
@ -1487,7 +1487,7 @@ testing = ["flake8 (<5)", "func-timeout", "jaraco.functools", "jaraco.itertools"
|
||||
[metadata]
|
||||
lock-version = "1.1"
|
||||
python-versions = "^3.8"
|
||||
content-hash = "f2e10b828fa5d1a92dd93e7549c06425b4b70a7910f57d22f56e4c6ad9d80a00"
|
||||
content-hash = "d17f83d59ef4f4e588d10a40d71090833fa7604880abde498d37771d8ca8b698"
|
||||
|
||||
[metadata.files]
|
||||
amqp = [
|
||||
@ -2224,8 +2224,8 @@ python3-openid = [
|
||||
{file = "python3_openid-3.2.0-py3-none-any.whl", hash = "sha256:6626f771e0417486701e0b4daff762e7212e820ca5b29fcc0d05f6f8736dfa6b"},
|
||||
]
|
||||
pytz = [
|
||||
{file = "pytz-2022.7-py2.py3-none-any.whl", hash = "sha256:93007def75ae22f7cd991c84e02d434876818661f8df9ad5df9e950ff4e52cfd"},
|
||||
{file = "pytz-2022.7.tar.gz", hash = "sha256:7ccfae7b4b2c067464a6733c6261673fdb8fd1be905460396b97a073e9fa683a"},
|
||||
{file = "pytz-2022.7.1-py2.py3-none-any.whl", hash = "sha256:78f4f37d8198e0627c5f1143240bb0206b8691d8d7ac6d78fee88b78733f8c4a"},
|
||||
{file = "pytz-2022.7.1.tar.gz", hash = "sha256:01a0681c4b9684a28304615eba55d1ab31ae00bf68ec157ec3708a8182dbbcd0"},
|
||||
]
|
||||
pyyaml = [
|
||||
{file = "PyYAML-6.0-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:d4db7c7aef085872ef65a8fd7d6d09a14ae91f691dec3e87ee5ee0539d516f53"},
|
||||
|
||||
@ -31,6 +31,7 @@ musicbrainzngs = "^0.7.1"
|
||||
cinemagoer = "^2022.12.27"
|
||||
pysportsdb = "^0.1.0"
|
||||
django-cachalot = "^2.5.2"
|
||||
pytz = "^2022.7.1"
|
||||
|
||||
[tool.poetry.dev-dependencies]
|
||||
Werkzeug = "2.0.3"
|
||||
|
||||
0
vrobbler/apps/profiles/__init__.py
Normal file
0
vrobbler/apps/profiles/__init__.py
Normal file
9
vrobbler/apps/profiles/admin.py
Normal file
9
vrobbler/apps/profiles/admin.py
Normal file
@ -0,0 +1,9 @@
|
||||
from django.contrib import admin
|
||||
|
||||
from profiles.models import UserProfile
|
||||
|
||||
|
||||
@admin.register(UserProfile)
|
||||
class UserProfileAdmin(admin.ModelAdmin):
|
||||
date_hierarchy = "created"
|
||||
ordering = ("-created",)
|
||||
17
vrobbler/apps/profiles/constants.py
Normal file
17
vrobbler/apps/profiles/constants.py
Normal file
@ -0,0 +1,17 @@
|
||||
from datetime import datetime
|
||||
|
||||
import pytz
|
||||
|
||||
ALL_TIMEZONE_CHOICES = tuple(zip(pytz.all_timezones, pytz.all_timezones))
|
||||
COMMON_TIMEZONE_CHOICES = tuple(
|
||||
zip(pytz.common_timezones, pytz.common_timezones)
|
||||
)
|
||||
PRETTY_TIMEZONE_CHOICES = []
|
||||
|
||||
for tz in pytz.common_timezones:
|
||||
now = datetime.now(pytz.timezone(tz))
|
||||
ofs = now.strftime("%z")
|
||||
PRETTY_TIMEZONE_CHOICES.append((int(ofs), tz, "(GMT%s) %s" % (ofs, tz)))
|
||||
PRETTY_TIMEZONE_CHOICES.sort()
|
||||
for i in range(len(PRETTY_TIMEZONE_CHOICES)):
|
||||
PRETTY_TIMEZONE_CHOICES[i] = PRETTY_TIMEZONE_CHOICES[i][1:]
|
||||
1035
vrobbler/apps/profiles/migrations/0001_initial.py
Normal file
1035
vrobbler/apps/profiles/migrations/0001_initial.py
Normal file
File diff suppressed because it is too large
Load Diff
0
vrobbler/apps/profiles/migrations/__init__.py
Normal file
0
vrobbler/apps/profiles/migrations/__init__.py
Normal file
18
vrobbler/apps/profiles/models.py
Normal file
18
vrobbler/apps/profiles/models.py
Normal file
@ -0,0 +1,18 @@
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.db import models
|
||||
from django_extensions.db.models import TimeStampedModel
|
||||
from profiles.constants import PRETTY_TIMEZONE_CHOICES
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
|
||||
class UserProfile(TimeStampedModel):
|
||||
user = models.OneToOneField(
|
||||
User, on_delete=models.CASCADE, related_name="profile"
|
||||
)
|
||||
timezone = models.CharField(
|
||||
max_length=255, choices=PRETTY_TIMEZONE_CHOICES
|
||||
)
|
||||
|
||||
def __str__(self):
|
||||
return f"User profile for {self.user}"
|
||||
13
vrobbler/apps/profiles/signals.py
Normal file
13
vrobbler/apps/profiles/signals.py
Normal file
@ -0,0 +1,13 @@
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.db.models.base import post_save
|
||||
from django.dispatch import receiver
|
||||
|
||||
from profiles.models import UserProfile
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
|
||||
@receiver(post_save, sender=User)
|
||||
def create_profile(sender, instance, created, **kwargs):
|
||||
if created:
|
||||
UserProfile.objects.create(user=instance)
|
||||
25
vrobbler/apps/profiles/utils.py
Normal file
25
vrobbler/apps/profiles/utils.py
Normal file
@ -0,0 +1,25 @@
|
||||
import datetime
|
||||
import settings
|
||||
import pytz
|
||||
|
||||
# need to translate to a non-naive timezone, even if timezone == settings.TIME_ZONE, so we can compare two dates
|
||||
def to_user_timezone(date, profile):
|
||||
timezone = profile.timezone if profile.timezone else settings.TIME_ZONE
|
||||
return date.replace(tzinfo=pytz.timezone(settings.TIME_ZONE)).astimezone(
|
||||
pytz.timezone(timezone)
|
||||
)
|
||||
|
||||
|
||||
def to_system_timezone(date, profile):
|
||||
timezone = profile.timezone if profile.timezone else settings.TIME_ZONE
|
||||
return date.replace(tzinfo=pytz.timezone(timezone)).astimezone(
|
||||
pytz.timezone(settings.TIME_ZONE)
|
||||
)
|
||||
|
||||
|
||||
def now_timezone():
|
||||
return (
|
||||
datetime.datetime.now()
|
||||
.replace(tzinfo=pytz.timezone(settings.TIME_ZONE))
|
||||
.astimezone(pytz.timezone(settings.TIME_ZONE))
|
||||
)
|
||||
@ -84,6 +84,7 @@ INSTALLED_APPS = [
|
||||
"django_extensions",
|
||||
'rest_framework.authtoken',
|
||||
"cachalot",
|
||||
"profiles",
|
||||
"scrobbles",
|
||||
"videos",
|
||||
"music",
|
||||
|
||||
Reference in New Issue
Block a user