Skip to content

Commit

Permalink
Edit user profile
Browse files Browse the repository at this point in the history
  • Loading branch information
yahyaaly151989 committed Sep 22, 2023
1 parent 15e3bbf commit c2f7847
Show file tree
Hide file tree
Showing 21 changed files with 120 additions and 12 deletions.
Binary file modified Module04/bookmarks/account/__pycache__/admin.cpython-311.pyc
Binary file not shown.
Binary file modified Module04/bookmarks/account/__pycache__/forms.cpython-311.pyc
Binary file not shown.
Binary file modified Module04/bookmarks/account/__pycache__/models.cpython-311.pyc
Binary file not shown.
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.
6 changes: 5 additions & 1 deletion Module04/bookmarks/account/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from django.contrib import admin
from .models import Profile

# Register your models here.
@admin.register(Profile)
class ProfileAdmin(admin.ModelAdmin):
list_display = ['user', 'date_of_birth', 'photo']
raw_id_fields = ['user']
13 changes: 12 additions & 1 deletion Module04/bookmarks/account/forms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django import forms
from django.contrib.auth.models import User
from .models import Profile


class LoginForm(forms.Form):
Expand All @@ -20,4 +21,14 @@ def clean_password2(self):

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

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

class ProfileEditForm(forms.ModelForm):
class Meta:
model = Profile
fields = ['date_of_birth', 'photo']
26 changes: 26 additions & 0 deletions Module04/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 Module04/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'),
),
]
Binary file not shown.
Binary file not shown.
10 changes: 9 additions & 1 deletion Module04/bookmarks/account/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
from django.db import models
from django.conf import settings

# Create your models here.
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}'
6 changes: 5 additions & 1 deletion Module04/bookmarks/account/templates/account/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@

{% block content %}
<h1>Dashboard</h1>
<p>Welcome to your dashboard.</p>
<p>
Welcome to your dashboard.
You can <a href="{% url 'edit' %}">edit your profile</a>
or you can <a href="{% url 'password_change' %}">change your password</a>
</p>
{% endblock %}
14 changes: 14 additions & 0 deletions Module04/bookmarks/account/templates/account/edit.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% extends "base.html" %}

{% block title %}Edit your account{% endblock %}

{% block content %}
<h1>Edit your account</h1>
<p>You can edit your account using the following form:</p>
<form method="post", enctype="multipart/form-data">
{{ user_form.as_p }}
{{ profile_form.as_p }}
{% csrf_token %}
<p><input type="submit" value="Save changes"></p>
</form>
{% endblock %}
7 changes: 2 additions & 5 deletions Module04/bookmarks/account/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,5 @@
path('', include('django.contrib.auth.urls')),
path('dashboard/', views.dashboard, name='dashboard'),
path('register/', views.register, name='register'),



]
8
path('edit/', views.edit, name='edit'),
]
21 changes: 19 additions & 2 deletions Module04/bookmarks/account/views.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from django.shortcuts import render
# from django.http import HttpResponse
from .forms import LoginForm, UserRegistrationForm
from .forms import LoginForm, UserRegistrationForm, UserEditForm, ProfileEditForm
# from django.contrib.auth import authenticate, login
from django.contrib.auth.decorators import login_required
from .models import Profile

# def user_login(request):
# if request.method == 'POST':
Expand Down Expand Up @@ -33,7 +34,23 @@ def register(request):
new_user = user_form.save(commit=False)
new_user.name = new_user.set_password(user_form.cleaned_data['password'])
new_user.save()
Profile.objects.create(user=new_user)
return render(request, 'account/register_done.html', {'new_user': new_user})
else:
user_form = UserRegistrationForm()
return render(request, 'account/register.html', {'user_form': user_form})
return render(request, 'account/register.html', {'user_form': user_form})

@login_required
def edit(request):
if request.method == 'POST':
user_form = UserEditForm(instance=request.user, data=request.POST)
profile_form = ProfileEditForm(instance=request.user.profile, data=request.POST, files=request.FILES)
if user_form.is_valid() and profile_form.is_valid():
user_form.save()
profile_form.save()
else:
user_form = UserEditForm(instance=request.user)
profile_form = ProfileEditForm(instance=request.user.profile)

return render(request, 'account/edit.html', {'user_form': user_form, 'profile_form': profile_form})

Binary file modified Module04/bookmarks/bookmarks/__pycache__/settings.cpython-311.pyc
Binary file not shown.
Binary file modified Module04/bookmarks/bookmarks/__pycache__/urls.cpython-311.pyc
Binary file not shown.
5 changes: 4 additions & 1 deletion Module04/bookmarks/bookmarks/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,7 @@
LOGOUT_URL = 'logout'


EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

MEDIA_URL = 'media/'
MEDIA_ROOT = BASE_DIR / 'media'
6 changes: 6 additions & 0 deletions Module04/bookmarks/bookmarks/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
path('admin/', admin.site.urls),
path('account/', include('account.urls')),
]

if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Binary file modified Module04/bookmarks/db.sqlite3
Binary file not shown.

0 comments on commit c2f7847

Please sign in to comment.