Skip to content

Commit

Permalink
Merge pull request #23 from OVINC-CN/feat_username_regex
Browse files Browse the repository at this point in the history
feat(account): support for username extra regex check
  • Loading branch information
OrenZhang authored Dec 27, 2024
2 parents 9143158 + b3b9e80 commit ddeb2ba
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
19 changes: 13 additions & 6 deletions apps/account/serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re

from adrf.serializers import ModelSerializer, Serializer
from channels.db import database_sync_to_async
from django.contrib.auth import get_user_model
from django.utils.translation import gettext, gettext_lazy
from ovinc_client.core.async_tools import SyncRunner
Expand All @@ -14,6 +15,8 @@
)
from apps.account.exceptions import PhoneVerifyCodeInvalid
from apps.account.models import User
from apps.home.constants import BuildInKeys
from apps.home.models import MetaConfig

USER_MODEL: User = get_user_model()

Expand Down Expand Up @@ -91,15 +94,19 @@ def validate(self, attrs: dict) -> dict:
raise PhoneVerifyCodeInvalid()
return data

def validate_username(self, username: str) -> str:
username_extra_regex = MetaConfig.objects.filter(key=BuildInKeys.USERNAME_EXTRA_REGEX[0]).first()
if not username_extra_regex:
return username
if re.compile(username_extra_regex.val).match(username):
return username
raise serializers.ValidationError(gettext("Username Invalid"))

def validate_phone_number(self, phone_number: str) -> str:
if SyncRunner().run(self.check_phone_number_exists(phone_number)):
if USER_MODEL.objects.filter(phone_number=phone_number).exists():
raise serializers.ValidationError(gettext("Phone Number Already Exists"))
return phone_number

@database_sync_to_async
def check_phone_number_exists(self, phone_number: str):
return USER_MODEL.objects.filter(phone_number=phone_number).exists()


class VerifyCodeRequestSerializer(Serializer):
"""
Expand Down
2 changes: 1 addition & 1 deletion apps/account/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ async def sign_up(self, request, *args, **kwargs):

# validate request
request_serializer = UserRegistrySerializer(data=request.data, context={"user_ip": get_ip(request)})
request_serializer.is_valid(raise_exception=True)
await database_sync_to_async(request_serializer.is_valid)(raise_exception=True)
request_data = request_serializer.validated_data

# save
Expand Down
1 change: 1 addition & 0 deletions apps/home/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ class BuildInKeys:
USER_AGREEMENT: CONFIG_DEFAULT_T = "user_agreement", ""
PRIVACY_AGREEMENT: CONFIG_DEFAULT_T = "privacy_agreement", ""
BACKGROUND_IMAGE: CONFIG_DEFAULT_T = "background_image", ""
USERNAME_EXTRA_REGEX: CONFIG_DEFAULT_T = "username_extra_regex", ""
5 changes: 4 additions & 1 deletion locale/zh_Hans/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-12-18 15:18+0800\n"
"POT-Creation-Date: 2024-12-27 13:19+0800\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -161,6 +161,9 @@ msgstr "电话归属国家"
msgid "Phone Verify Code"
msgstr "电话验证码"

msgid "Username Invalid"
msgstr "用户名不符合校验规则"

msgid "Phone Number Already Exists"
msgstr "电话已被使用"

Expand Down

0 comments on commit ddeb2ba

Please sign in to comment.