-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from b2b-marketplace/feature/company_registration
регистрация компании
- Loading branch information
Showing
25 changed files
with
2,110 additions
and
982 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from rest_framework.routers import DynamicRoute, SimpleRouter | ||
|
||
|
||
class DynamicRouter(SimpleRouter): | ||
"""Роутер для динамических маршрутов.""" | ||
|
||
routes = [ | ||
# Dynamically generated list routes. Generated using | ||
# @action(detail=False) decorator on methods of the viewset. | ||
DynamicRoute( | ||
url=r"^{prefix}/{url_path}{trailing_slash}$", | ||
name="{basename}-{url_name}", | ||
detail=False, | ||
initkwargs={}, | ||
), | ||
# Dynamically generated detail routes. Generated using | ||
# @action(detail=True) decorator on methods of the viewset. | ||
DynamicRoute( | ||
url=r"^{prefix}/{lookup}/{url_path}{trailing_slash}$", | ||
name="{basename}-{url_name}", | ||
detail=True, | ||
initkwargs={}, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Generated by Django 4.1 on 2023-08-14 16:43 | ||
|
||
from django.db import migrations | ||
|
||
import apps.users.models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("users", "0001_initial"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterModelManagers( | ||
name="customuser", | ||
managers=[ | ||
("objects", apps.users.models.CustomUserManager()), | ||
], | ||
), | ||
] |
42 changes: 42 additions & 0 deletions
42
apps/users/migrations/0003_alter_company_company_account_alter_company_ogrn.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Generated by Django 4.1 on 2023-08-16 04:56 | ||
|
||
from django.db import migrations, models | ||
|
||
import apps.users.models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("users", "0002_alter_customuser_managers"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="company", | ||
name="company_account", | ||
field=models.CharField( | ||
max_length=20, | ||
null=True, | ||
unique=True, | ||
validators=[ | ||
apps.users.models.validate_account, | ||
apps.users.models.validate_digits_only, | ||
], | ||
verbose_name="Account", | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name="company", | ||
name="ogrn", | ||
field=models.CharField( | ||
max_length=13, | ||
null=True, | ||
unique=True, | ||
validators=[ | ||
apps.users.models.validate_ogrn, | ||
apps.users.models.validate_digits_only, | ||
], | ||
verbose_name="PSRN", | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Generated by Django 4.1 on 2023-08-16 05:11 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("users", "0002_alter_address_options_alter_address_address_and_more"), | ||
("users", "0003_alter_company_company_account_alter_company_ogrn"), | ||
] | ||
|
||
operations = [] |
44 changes: 44 additions & 0 deletions
44
apps/users/migrations/0005_alter_company_company_account_alter_company_ogrn.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Generated by Django 4.1 on 2023-08-16 05:17 | ||
|
||
from django.db import migrations, models | ||
|
||
import apps.users.models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("users", "0004_merge_20230816_0511"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="company", | ||
name="company_account", | ||
field=models.CharField( | ||
blank=True, | ||
max_length=20, | ||
null=True, | ||
unique=True, | ||
validators=[ | ||
apps.users.models.validate_account, | ||
apps.users.models.validate_digits_only, | ||
], | ||
verbose_name="Account", | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name="company", | ||
name="ogrn", | ||
field=models.CharField( | ||
blank=True, | ||
max_length=13, | ||
null=True, | ||
unique=True, | ||
validators=[ | ||
apps.users.models.validate_ogrn, | ||
apps.users.models.validate_digits_only, | ||
], | ||
verbose_name="PSRN", | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from rest_framework import serializers | ||
|
||
from apps.users.models import Address | ||
|
||
|
||
class AddressSerializer(serializers.ModelSerializer): | ||
"""Сериализатор для получения данных оо адресе.""" | ||
|
||
class Meta: | ||
model = Address | ||
fields = ("id", "address") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from django.contrib.auth import get_user_model | ||
from django.db import transaction | ||
from djoser.conf import settings | ||
from rest_framework import serializers | ||
|
||
from apps.users.models import Address, PhoneNumber | ||
|
||
User = get_user_model() | ||
|
||
|
||
class BaseSerializer(serializers.ModelSerializer): | ||
"""Абстрактный сериализатор для создания нового пользователя.""" | ||
|
||
password = serializers.CharField(style={"input_type": "password"}, write_only=True) | ||
|
||
def perform_create(self, validated_data, **fields): | ||
"""Создает пользователя.""" | ||
with transaction.atomic(): | ||
user = User.objects.create_user(**validated_data, **fields) | ||
if settings.SEND_ACTIVATION_EMAIL: | ||
user.is_active = False | ||
user.save(update_fields=["is_active"]) | ||
return user | ||
|
||
def _address_phone_attach(self, obj): | ||
"""Создает объекты адреса и номера телефона.""" | ||
address = obj.pop("address", None) | ||
phone_number = obj.pop("phone_number", None) | ||
|
||
address_obj = Address.objects.create(**address) | ||
phone_number_obj = PhoneNumber.objects.create(**phone_number) | ||
|
||
return {"address": address_obj, "phone_number": phone_number_obj} | ||
|
||
def _extract_relations(self, validated_data): | ||
"""Извлекает один уровень вложенных отношений.""" | ||
reverse_relations = {} | ||
for field_name, field in self.fields.items(): | ||
if isinstance(field, serializers.ModelSerializer | serializers.ListSerializer): | ||
if field.source not in validated_data: | ||
continue | ||
|
||
reverse_relations[field_name] = ( | ||
self.fields[field_name].Meta.model, | ||
validated_data.pop(field_name), | ||
) | ||
return reverse_relations | ||
|
||
def update_or_create(self, validated_data): | ||
"""Создает объекты компании или физ. лица.""" | ||
reverse_relations = {} | ||
relations = self._extract_relations(validated_data) | ||
|
||
for field_name, (model, data) in relations.items(): | ||
with transaction.atomic(): | ||
address_phone = self._address_phone_attach(data) | ||
reverse_relations[field_name] = model.objects.create(**data, **address_phone) | ||
return reverse_relations |
Oops, something went wrong.