Skip to content

Commit

Permalink
Add pubid to User model
Browse files Browse the repository at this point in the history
  • Loading branch information
mtomilov committed Jan 14, 2025
1 parent dac0c3f commit 125adcc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
17 changes: 17 additions & 0 deletions h/migrations/versions/3bae642f2b36_add_pubid_to_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""Add pubid to user."""

import sqlalchemy as sa
from alembic import op

revision = "3bae642f2b36"
down_revision = "9c0efdf762a6"


def upgrade():
op.add_column("user", sa.Column("pubid", sa.Text(), nullable=True))
op.create_unique_constraint(op.f("uq__user__pubid"), "user", ["pubid"])


def downgrade():
op.drop_constraint(op.f("uq__user__pubid"), "user", type_="unique")
op.drop_column("user", "pubid")
11 changes: 11 additions & 0 deletions h/models/user.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
import re
from functools import partial
from typing import TYPE_CHECKING

import sqlalchemy as sa
Expand All @@ -9,6 +10,7 @@
from h.db import Base
from h.exceptions import InvalidUserId
from h.models import helpers
from h.pubid import generate
from h.util.user import format_userid, split_user

if TYPE_CHECKING:
Expand All @@ -20,6 +22,7 @@
USERNAME_PATTERN = "(?i)^[A-Z0-9._]+$"
EMAIL_MAX_LENGTH = 100
DISPLAY_NAME_MAX_LENGTH = 30
USER_PUBID_LENGTH = 12


def _normalise_username(username):
Expand Down Expand Up @@ -158,6 +161,14 @@ def __table_args__(cls): # pylint:disable=no-self-argument

id = sa.Column(sa.Integer, autoincrement=True, primary_key=True)

#: A publicly visible unique identifier for the user
pubid = sa.Column(
sa.Text(),
default=partial(generate, USER_PUBID_LENGTH),
unique=True,
nullable=True,
)

#: Username as chosen by the user on registration
_username = sa.Column("username", sa.UnicodeText(), nullable=False)

Expand Down

0 comments on commit 125adcc

Please sign in to comment.