Skip to content

Commit 4d618e6

Browse files
committed
orcid validation, migration edits
1 parent a04f023 commit 4d618e6

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Generated by Django 4.2.14 on 2025-09-04 12:52
22

33
from django.db import migrations, models
4+
from ..validators import validate_orcid_id
45

56

67
class Migration(migrations.Migration):
@@ -13,11 +14,11 @@ class Migration(migrations.Migration):
1314
migrations.AddField(
1415
model_name='user',
1516
name='orcid_id',
16-
field=models.CharField(blank=True, max_length=255, verbose_name='ORCID ID'),
17-
),
18-
migrations.AlterField(
19-
model_name='user',
20-
name='is_approved',
21-
field=models.BooleanField(default=True, help_text='Designates whether this user has been approved to use the API by an Admin.', verbose_name='Approved'),
17+
field=models.CharField(
18+
blank=True,
19+
max_length=19,
20+
validators=[validate_orcid_id],
21+
verbose_name='ORCID ID'
22+
),
2223
),
2324
]

gateway/sds_gateway/users/models.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from .managers import APIKeyUserManager
1515
from .managers import UserManager
16+
from .validators import validate_orcid_id
1617

1718

1819
class User(AbstractUser):
@@ -28,7 +29,9 @@ class User(AbstractUser):
2829
username: str | None = None
2930
name = models.CharField(_("Name of User"), blank=True, max_length=255)
3031
email = models.EmailField(_("Email address"), unique=True)
31-
orcid_id = models.CharField(_("ORCID ID"), blank=True, max_length=255)
32+
orcid_id = models.CharField(
33+
_("ORCID ID"), blank=True, max_length=19, validators=[validate_orcid_id]
34+
)
3235
is_approved = models.BooleanField(
3336
_("Approved"),
3437
default=settings.SDS_NEW_USERS_APPROVED_ON_CREATION,
@@ -38,7 +41,7 @@ class User(AbstractUser):
3841
)
3942

4043
USERNAME_FIELD = "email"
41-
REQUIRED_FIELDS = []
44+
REQUIRED_FIELDS: list[str] = []
4245

4346
objects = UserManager()
4447

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""Custom validators for the users app."""
2+
3+
import re
4+
5+
from django.core.exceptions import ValidationError
6+
from django.utils.translation import gettext_lazy as _
7+
8+
9+
def validate_orcid_id(value):
10+
"""Validate the ORCID ID format."""
11+
if not value:
12+
return value
13+
14+
if not re.match(r"^\d{4}-\d{4}-\d{4}-\d{4}$", value):
15+
raise ValidationError(_("ORCID ID must be in the format 0000-0000-0000-0000."))
16+
17+
return value

0 commit comments

Comments
 (0)