mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-08-04 20:55:19 +02:00
Enhance adventure management: add error handling for category fetch, implement unique email constraint in user model, and update adventure save logic to ensure category assignment
This commit is contained in:
parent
86d213bb8b
commit
736ede2417
15 changed files with 216 additions and 60 deletions
|
@ -0,0 +1,27 @@
|
|||
# Generated by Django 5.0.8 on 2024-11-18 14:51
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
def check_duplicate_email(apps, schema_editor):
|
||||
# sets an email to null if there are duplicates
|
||||
CustomUser = apps.get_model('users', 'CustomUser')
|
||||
duplicates = CustomUser.objects.values('email').annotate(email_count=models.Count('email')).filter(email_count__gt=1)
|
||||
for duplicate in duplicates:
|
||||
CustomUser.objects.filter(email=duplicate['email']).update(email=None)
|
||||
print(f"Duplicate email: {duplicate['email']}")
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('users', '0002_customuser_public_profile'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(check_duplicate_email),
|
||||
migrations.AlterField(
|
||||
model_name='customuser',
|
||||
name='email',
|
||||
field=models.EmailField(max_length=254, unique=True),
|
||||
),
|
||||
]
|
|
@ -4,6 +4,7 @@ from django.db import models
|
|||
from django_resized import ResizedImageField
|
||||
|
||||
class CustomUser(AbstractUser):
|
||||
email = models.EmailField(unique=True) # Override the email field with unique constraint
|
||||
profile_pic = ResizedImageField(force_format="WEBP", quality=75, null=True, blank=True, upload_to='profile-pics/')
|
||||
uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
|
||||
public_profile = models.BooleanField(default=False)
|
||||
|
|
|
@ -63,6 +63,11 @@ class RegisterSerializer(serializers.Serializer):
|
|||
def validate(self, data):
|
||||
if data['password1'] != data['password2']:
|
||||
raise serializers.ValidationError(_("The two password fields didn't match."))
|
||||
|
||||
# check if a user with the same email already exists
|
||||
if User.objects.filter(email=data['email']).exists():
|
||||
raise serializers.ValidationError("This email is already in use.")
|
||||
|
||||
return data
|
||||
|
||||
def custom_signup(self, request, user):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue