init
This commit is contained in:
BIN
accounts/__pycache__/admin.cpython-313.pyc
Normal file
BIN
accounts/__pycache__/admin.cpython-313.pyc
Normal file
Binary file not shown.
BIN
accounts/__pycache__/forms.cpython-313.pyc
Normal file
BIN
accounts/__pycache__/forms.cpython-313.pyc
Normal file
Binary file not shown.
BIN
accounts/__pycache__/models.cpython-313.pyc
Normal file
BIN
accounts/__pycache__/models.cpython-313.pyc
Normal file
Binary file not shown.
BIN
accounts/__pycache__/urls.cpython-313.pyc
Normal file
BIN
accounts/__pycache__/urls.cpython-313.pyc
Normal file
Binary file not shown.
BIN
accounts/__pycache__/views.cpython-313.pyc
Normal file
BIN
accounts/__pycache__/views.cpython-313.pyc
Normal file
Binary file not shown.
53
accounts/forms.py
Normal file
53
accounts/forms.py
Normal file
@ -0,0 +1,53 @@
|
||||
from django import forms
|
||||
from django.contrib.auth.models import User
|
||||
from django.contrib.auth.forms import UserCreationForm
|
||||
from .models import Profile
|
||||
from instructor.models import Instructor
|
||||
|
||||
class UserRegistrationForm(UserCreationForm):
|
||||
email = forms.EmailField(required=True)
|
||||
first_name = forms.CharField(required=True)
|
||||
last_name = forms.CharField(required=True)
|
||||
phone = forms.CharField(max_length=15, required=True)
|
||||
address = forms.CharField(widget=forms.Textarea, required=True)
|
||||
birth_date = forms.DateField(required=True, widget=forms.DateInput(attrs={'type': 'date'}))
|
||||
specialization = forms.CharField(max_length=100, required=True)
|
||||
experience_years = forms.IntegerField(min_value=0, required=True)
|
||||
|
||||
class Meta:
|
||||
model = User
|
||||
fields = ('username', 'email', 'first_name', 'last_name', 'password1', 'password2')
|
||||
|
||||
def save(self, commit=True):
|
||||
user = super().save(commit=False)
|
||||
user.email = self.cleaned_data['email']
|
||||
user.first_name = self.cleaned_data['first_name']
|
||||
user.last_name = self.cleaned_data['last_name']
|
||||
|
||||
if commit:
|
||||
user.save()
|
||||
|
||||
# Создаем профиль
|
||||
profile = user.profile
|
||||
profile.user_type = 'instructor'
|
||||
profile.phone = self.cleaned_data['phone']
|
||||
profile.address = self.cleaned_data['address']
|
||||
profile.birth_date = self.cleaned_data['birth_date']
|
||||
profile.save()
|
||||
|
||||
# Создаем инструктора
|
||||
instructor = Instructor.objects.create(
|
||||
profile=profile,
|
||||
specialization=self.cleaned_data['specialization'],
|
||||
experience_years=self.cleaned_data['experience_years']
|
||||
)
|
||||
|
||||
return user
|
||||
|
||||
class ProfileUpdateForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = Profile
|
||||
fields = ['phone', 'address', 'birth_date']
|
||||
widgets = {
|
||||
'birth_date': forms.DateInput(attrs={'type': 'date'}),
|
||||
}
|
30
accounts/migrations/0001_initial.py
Normal file
30
accounts/migrations/0001_initial.py
Normal file
@ -0,0 +1,30 @@
|
||||
# Generated by Django 5.0.2 on 2025-06-10 19:01
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
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')),
|
||||
('user_type', models.CharField(choices=[('student', 'Студент'), ('instructor', 'Инструктор'), ('dispatcher', 'Диспетчер'), ('admin', 'Администратор')], max_length=20)),
|
||||
('phone', models.CharField(blank=True, max_length=15)),
|
||||
('address', models.TextField(blank=True)),
|
||||
('birth_date', models.DateField(blank=True, null=True)),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
),
|
||||
]
|
0
accounts/migrations/__init__.py
Normal file
0
accounts/migrations/__init__.py
Normal file
BIN
accounts/migrations/__pycache__/0001_initial.cpython-313.pyc
Normal file
BIN
accounts/migrations/__pycache__/0001_initial.cpython-313.pyc
Normal file
Binary file not shown.
BIN
accounts/migrations/__pycache__/__init__.cpython-313.pyc
Normal file
BIN
accounts/migrations/__pycache__/__init__.cpython-313.pyc
Normal file
Binary file not shown.
32
accounts/models.py
Normal file
32
accounts/models.py
Normal file
@ -0,0 +1,32 @@
|
||||
from django.db import models
|
||||
from django.contrib.auth.models import User
|
||||
from django.db.models.signals import post_save
|
||||
from django.dispatch import receiver
|
||||
|
||||
class Profile(models.Model):
|
||||
USER_TYPE_CHOICES = (
|
||||
('student', 'Студент'),
|
||||
('instructor', 'Инструктор'),
|
||||
('dispatcher', 'Диспетчер'),
|
||||
('admin', 'Администратор'),
|
||||
)
|
||||
|
||||
user = models.OneToOneField(User, on_delete=models.CASCADE)
|
||||
user_type = models.CharField(max_length=20, choices=USER_TYPE_CHOICES)
|
||||
phone = models.CharField(max_length=15, blank=True)
|
||||
address = models.TextField(blank=True)
|
||||
birth_date = models.DateField(null=True, blank=True)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.user.username} - {self.get_user_type_display()}"
|
||||
|
||||
@receiver(post_save, sender=User)
|
||||
def create_user_profile(sender, instance, created, **kwargs):
|
||||
if created:
|
||||
Profile.objects.create(user=instance)
|
||||
|
||||
@receiver(post_save, sender=User)
|
||||
def save_user_profile(sender, instance, **kwargs):
|
||||
instance.profile.save()
|
15
accounts/urls.py
Normal file
15
accounts/urls.py
Normal file
@ -0,0 +1,15 @@
|
||||
from django.urls import path
|
||||
from django.contrib.auth import views as auth_views
|
||||
from . import views
|
||||
|
||||
app_name = 'accounts'
|
||||
|
||||
urlpatterns = [
|
||||
path('', views.home, name='home'),
|
||||
path('register/', views.register, name='register'),
|
||||
path('register/instructor/', views.register_instructor, name='register_instructor'),
|
||||
path('profile/', views.profile, name='profile'),
|
||||
path('profile/update/', views.profile_update, name='profile_update'),
|
||||
path('login/', auth_views.LoginView.as_view(template_name='accounts/login.html'), name='login'),
|
||||
path('logout/', auth_views.LogoutView.as_view(template_name='accounts/logout.html'), name='logout'),
|
||||
]
|
46
accounts/views.py
Normal file
46
accounts/views.py
Normal file
@ -0,0 +1,46 @@
|
||||
from django.shortcuts import render, redirect
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from .models import Profile
|
||||
from .forms import UserRegistrationForm, ProfileUpdateForm
|
||||
|
||||
def home(request):
|
||||
return render(request, 'accounts/home.html')
|
||||
|
||||
def register(request):
|
||||
if request.method == 'POST':
|
||||
form = UserRegistrationForm(request.POST)
|
||||
if form.is_valid():
|
||||
user = form.save()
|
||||
messages.success(request, 'Аккаунт успешно создан!')
|
||||
return redirect('login')
|
||||
else:
|
||||
form = UserRegistrationForm()
|
||||
return render(request, 'accounts/register.html', {'form': form})
|
||||
|
||||
def register_instructor(request):
|
||||
if request.method == 'POST':
|
||||
form = UserRegistrationForm(request.POST)
|
||||
if form.is_valid():
|
||||
user = form.save()
|
||||
messages.success(request, 'Инструктор успешно зарегистрирован!')
|
||||
return redirect('login')
|
||||
else:
|
||||
form = UserRegistrationForm()
|
||||
return render(request, 'accounts/register_instructor.html', {'form': form})
|
||||
|
||||
@login_required
|
||||
def profile(request):
|
||||
return render(request, 'accounts/profile.html')
|
||||
|
||||
@login_required
|
||||
def profile_update(request):
|
||||
if request.method == 'POST':
|
||||
form = ProfileUpdateForm(request.POST, instance=request.user.profile)
|
||||
if form.is_valid():
|
||||
form.save()
|
||||
messages.success(request, 'Профиль успешно обновлен!')
|
||||
return redirect('profile')
|
||||
else:
|
||||
form = ProfileUpdateForm(instance=request.user.profile)
|
||||
return render(request, 'accounts/profile_update.html', {'form': form})
|
Reference in New Issue
Block a user