114 lines
3.9 KiB
Python
114 lines
3.9 KiB
Python
from datetime import date
|
|
|
|
from django.contrib.contenttypes.models import ContentType
|
|
from django.shortcuts import redirect, render
|
|
from django.urls import reverse
|
|
|
|
from apps.accounts.forms import SellerStep1Form, SellerStep2Form, SellerStep3Form
|
|
from apps.accounts.models import Role, User
|
|
from apps.catalog.models import Brand, VerificationStatus
|
|
from apps.moderation.models import AuditLog, ModerationRequest
|
|
|
|
SESSION_KEY = 'seller_registration'
|
|
|
|
|
|
def _get_session_data(request):
|
|
return request.session.get(SESSION_KEY, {})
|
|
|
|
|
|
def seller_register(request):
|
|
"""Многошаговая регистрация продавца (FR-01)."""
|
|
|
|
step = int(request.GET.get('step', 1))
|
|
data = _get_session_data(request)
|
|
|
|
if step == 1:
|
|
form = SellerStep1Form(request.POST or None)
|
|
if request.method == 'POST' and form.is_valid():
|
|
cleaned = form.cleaned_data
|
|
# date нельзя сериализовать в сессию (JSON) — храним ISO-строку.
|
|
cleaned['birth_date'] = cleaned['birth_date'].isoformat()
|
|
data['step1'] = cleaned
|
|
request.session[SESSION_KEY] = data
|
|
return redirect(reverse('accounts:seller_register') + '?step=2')
|
|
return render(request, 'accounts/seller_register.html', {
|
|
'form': form, 'step': 1, 'total_steps': 3,
|
|
})
|
|
|
|
if step == 2:
|
|
if 'step1' not in data:
|
|
return redirect(reverse('accounts:seller_register'))
|
|
form = SellerStep2Form(request.POST or None)
|
|
if request.method == 'POST' and form.is_valid():
|
|
data['step2'] = form.cleaned_data
|
|
request.session[SESSION_KEY] = data
|
|
return redirect(reverse('accounts:seller_register') + '?step=3')
|
|
return render(request, 'accounts/seller_register.html', {
|
|
'form': form, 'step': 2, 'total_steps': 3,
|
|
})
|
|
|
|
if step == 3:
|
|
if 'step1' not in data or 'step2' not in data:
|
|
return redirect(reverse('accounts:seller_register'))
|
|
form = SellerStep3Form(request.POST or None, request.FILES or None)
|
|
if request.method == 'POST' and form.is_valid():
|
|
_create_seller(request, data, form.cleaned_data)
|
|
request.session.pop(SESSION_KEY, None)
|
|
return redirect(reverse('accounts:seller_register_done'))
|
|
return render(request, 'accounts/seller_register.html', {
|
|
'form': form, 'step': 3, 'total_steps': 3,
|
|
})
|
|
|
|
return redirect(reverse('accounts:seller_register'))
|
|
|
|
|
|
def _create_seller(request, data, step3):
|
|
s1 = data['step1']
|
|
s2 = data['step2']
|
|
|
|
user = User.objects.create_user(
|
|
username=s1['username'],
|
|
email=s1.get('email', ''),
|
|
password=s1['password1'],
|
|
role=Role.SELLER,
|
|
full_name=s1['full_name'],
|
|
birth_date=date.fromisoformat(s1['birth_date']),
|
|
region=s1['region'],
|
|
pd_consent=step3['pd_consent'],
|
|
)
|
|
|
|
socials = [
|
|
line.strip()
|
|
for line in s2.get('socials', '').splitlines()
|
|
if line.strip()
|
|
]
|
|
|
|
brand = Brand.objects.create(
|
|
user=user,
|
|
name=s2['brand_name'],
|
|
legal_status=s2['legal_status'],
|
|
inn=s2.get('inn', ''),
|
|
city=s2.get('city', ''),
|
|
region=s1['region'],
|
|
socials=socials,
|
|
story=s2.get('story', ''),
|
|
production_photo=step3.get('production_photo'),
|
|
statement_doc=step3.get('statement_doc'),
|
|
verification_status=VerificationStatus.PENDING,
|
|
)
|
|
|
|
ModerationRequest.objects.create(
|
|
content_type=ContentType.objects.get_for_model(Brand),
|
|
object_id=brand.pk,
|
|
decision='pending',
|
|
)
|
|
AuditLog.objects.create(
|
|
user=user,
|
|
action='seller_registered',
|
|
object_repr=f'Бренд «{brand.name}»',
|
|
)
|
|
return user, brand
|
|
|
|
|
|
def seller_register_done(request):
|
|
return render(request, 'accounts/seller_register_done.html') |