Skip to content

Commit

Permalink
Creating many-to-many relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
yahyaaly151989 committed Oct 3, 2023
1 parent 78c8f5a commit 2de359d
Show file tree
Hide file tree
Showing 68 changed files with 1,044 additions and 0 deletions.
Binary file modified Module04/bookmarks/account/__pycache__/urls.cpython-311.pyc
Binary file not shown.
Binary file modified Module04/bookmarks/account/__pycache__/views.cpython-311.pyc
Binary file not shown.
Binary file modified Module04/bookmarks/db.sqlite3
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
7 changes: 7 additions & 0 deletions Module05/bookmarks/account/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.contrib import admin
from .models import Profile

@admin.register(Profile)
class ProfileAdmin(admin.ModelAdmin):
list_display = ['user', 'date_of_birth', 'photo']
raw_id_fields = ['user']
6 changes: 6 additions & 0 deletions Module05/bookmarks/account/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class AccountConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'account'
17 changes: 17 additions & 0 deletions Module05/bookmarks/account/authentication.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from django.contrib.auth.models import User

class EmailAuthBackend():
def authenticate(self, request, username=None, password=None):
try:
user = User.objects.get(email=username)
if user.check_password(password):
return user
return None
except (User.DoesNotExist, User.MultipleObjectsReturned):
return None

def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
48 changes: 48 additions & 0 deletions Module05/bookmarks/account/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from django import forms
from django.contrib.auth.models import User
from .models import Profile


class LoginForm(forms.Form):
username = forms.CharField()
password = forms.CharField(widget=forms.PasswordInput)


class UserRegistrationForm(forms.ModelForm):
password = forms.CharField(label= 'password', widget=forms.PasswordInput)
password2 = forms.CharField(label= 'Repeat password', widget=forms.PasswordInput)

class Meta:
model = User
fields = ['username', 'first_name', 'email']

def clean_password2(self):
cd = self.cleaned_data

if cd['password'] != cd['password2']:
raise forms.ValidationError("Passwords don't match.")
return cd['password2']

def clean_email(self):
data = self.cleaned_data['email']
if User.objects.filter(email=data).exists():
raise forms.ValidationError('Email already exists.')
return data


class UserEditForm(forms.ModelForm):
class Meta:
model = User
fields = ['first_name', 'last_name', 'email']

def clean_email(self):
data = self.cleaned_data['email']
qs = User.objects.exclude(id=self.instance.id).filter(email=data)
if qs.exists():
raise forms.ValidationError('Email already exists.')
return data

class ProfileEditForm(forms.ModelForm):
class Meta:
model = Profile
fields = ['date_of_birth', 'photo']
26 changes: 26 additions & 0 deletions Module05/bookmarks/account/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 4.2.4 on 2023-09-22 05:52

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name='Profile',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('date_of_birth', models.DateField(blank=True, null=True)),
('photo', models.ImageField(blank=True, upload_to='users/%Y/%m/%d/')),
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]
18 changes: 18 additions & 0 deletions Module05/bookmarks/account/migrations/0002_alter_profile_photo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.4 on 2023-09-22 14:33

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('account', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='profile',
name='photo',
field=models.ImageField(blank=True, upload_to='users/%Y/%m/%d'),
),
]
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
11 changes: 11 additions & 0 deletions Module05/bookmarks/account/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.db import models
from django.conf import settings

class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)

date_of_birth = models.DateField(blank=True, null=True)
photo = models.ImageField(upload_to='users/%Y/%m/%d', blank=True)

def __str__(self):
return f'Profile of {self.user.username}'
Loading

0 comments on commit 2de359d

Please sign in to comment.