diff options
| -rw-r--r-- | generalresearch/managers/thl/user_manager/user_metadata_manager.py | 28 | ||||
| -rw-r--r-- | generalresearch/models/thl/user_profile.py | 20 | ||||
| -rw-r--r-- | generalresearch/thl_django/common/models.py | 5 |
3 files changed, 36 insertions, 17 deletions
diff --git a/generalresearch/managers/thl/user_manager/user_metadata_manager.py b/generalresearch/managers/thl/user_manager/user_metadata_manager.py index ffa8b44..4f394f1 100644 --- a/generalresearch/managers/thl/user_manager/user_metadata_manager.py +++ b/generalresearch/managers/thl/user_manager/user_metadata_manager.py @@ -22,9 +22,9 @@ class UserMetadataManager(PostgresManager): email_sha1s, email_md5s, ]: - assert arg is None or isinstance( - arg, (set, list) - ), "must pass a collection of objects" + assert arg is None or isinstance(arg, (set, list)), ( + "must pass a collection of objects" + ) filters = [] params = {} @@ -48,7 +48,7 @@ class UserMetadataManager(PostgresManager): filter_str = "WHERE " + " AND ".join(filters) if filters else "" res = self.pg_config.execute_sql_query( f""" - SELECT user_id, email_address, email_sha256, email_sha1, email_md5 + SELECT user_id, email_address, email_sha256, email_sha1, email_md5, display_name FROM thl_usermetadata {filter_str} """, @@ -126,8 +126,12 @@ class UserMetadataManager(PostgresManager): c.execute( """ UPDATE thl_usermetadata - SET email_address = %(email_address)s, email_sha256 = %(email_sha256)s, - email_sha1 = %(email_sha1)s, email_md5 = %(email_md5)s + SET + email_address = %(email_address)s, + email_sha256 = %(email_sha256)s, + email_sha1 = %(email_sha1)s, + email_md5 = %(email_md5)s, + display_name = %(display_name)s WHERE user_id = %(user_id)s; """, params=user_metadata.to_db(), @@ -139,10 +143,14 @@ class UserMetadataManager(PostgresManager): def _create(self, user_metadata: UserMetadata) -> int: return self.pg_config.execute_write( query=""" - INSERT INTO thl_usermetadata - (user_id, email_address, email_sha256, email_sha1, email_md5) - VALUES (%(user_id)s, %(email_address)s, %(email_sha256)s, - %(email_sha1)s, %(email_md5)s); + INSERT INTO thl_usermetadata ( + user_id, email_address, email_sha256, + email_sha1, email_md5, display_name + ) + VALUES ( + %(user_id)s, %(email_address)s, %(email_sha256)s, + %(email_sha1)s, %(email_md5)s, %(display_name)s + ); """, params=user_metadata.to_db(), ) diff --git a/generalresearch/models/thl/user_profile.py b/generalresearch/models/thl/user_profile.py index e96266a..064036f 100644 --- a/generalresearch/models/thl/user_profile.py +++ b/generalresearch/models/thl/user_profile.py @@ -1,7 +1,7 @@ from __future__ import annotations import hashlib -from typing import Any +from typing import Annotated, Any from pydantic import ( BaseModel, @@ -12,7 +12,7 @@ from pydantic import ( computed_field, ) from pydantic.json_schema import SkipJsonSchema -from typing_extensions import Annotated, Self +from typing_extensions import Self from generalresearch.models import MAX_INT32, Source from generalresearch.models.custom_types import UUIDStr @@ -23,12 +23,12 @@ from generalresearch.models.thl.user_streak import UserStreak class UserMetadata(BaseModel): model_config = ConfigDict(extra="forbid", validate_assignment=True) - user_id: SkipJsonSchema[PositiveInt | None] = Field( - exclude=True, default=None, lt=MAX_INT32 - ) + user_id: SkipJsonSchema[PositiveInt] = Field(exclude=True, lt=MAX_INT32) email_address: EmailStr | None = Field(default=None, examples=["contact@mail.com"]) + display_name: str | None = Field(default=None, max_length=255) + @computed_field def email_md5( self, @@ -86,9 +86,15 @@ class UserMetadata(BaseModel): return res @classmethod - def from_db(cls, user_id, email_address, **kwargs) -> Self: + def from_db(cls, user_id, email_address, display_name, **kwargs) -> Self: # If the hashes are passed, just validate that they match - obj = cls.model_validate({"user_id": user_id, "email_address": email_address}) + obj = cls.model_validate( + { + "user_id": user_id, + "email_address": email_address, + "display_name": display_name, + } + ) if kwargs.get("email_md5") is not None: assert obj.email_md5 == kwargs["email_md5"], "email_md5 mismatch" diff --git a/generalresearch/thl_django/common/models.py b/generalresearch/thl_django/common/models.py index a41d2eb..ffd9662 100644 --- a/generalresearch/thl_django/common/models.py +++ b/generalresearch/thl_django/common/models.py @@ -374,6 +374,11 @@ class THLUserMetadata(models.Model): email_sha1 = models.CharField(max_length=40, null=True) email_md5 = models.CharField(max_length=32, null=True) + # Not unique within a BP, or anything like that. A user + # can set this to whatever they like. No index + # as we will not ever look up a user by their name. + display_name = models.CharField(max_length=255, null=True) + class Meta: db_table = "thl_usermetadata" indexes = [ |
