Commit 94c85d8a authored by apeshkov's avatar apeshkov

[BACK][VLN][master] add tenant model

parent c9ef067b
import uuid
from django.db import models
from main.core.fields import VLNFieldTracker from main.core.fields import VLNFieldTracker
...@@ -40,3 +43,10 @@ class TrackerMixin: ...@@ -40,3 +43,10 @@ class TrackerMixin:
self.save(**kwargs) self.save(**kwargs)
else: else:
self.save(**kwargs) self.save(**kwargs)
class UUIDIDMixin(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
class Meta:
abstract = True
...@@ -58,6 +58,7 @@ INSTALLED_APPS = [ ...@@ -58,6 +58,7 @@ INSTALLED_APPS = [
"django_cron", "django_cron",
"corsheaders", "corsheaders",
"mptt", "mptt",
"tenants.apps.TenantsConfig",
"users.apps.UsersConfig", # users should be last in this list, or permission update may fail "users.apps.UsersConfig", # users should be last in this list, or permission update may fail
] ]
......
...@@ -15,20 +15,23 @@ Including another URLconf ...@@ -15,20 +15,23 @@ Including another URLconf
""" """
from django.contrib import admin from django.contrib import admin
from django.contrib.auth import views as auth_views from django.contrib.auth import views as auth_views
from django.urls import path, include from django.urls import include, path
from main.auth.views import TokenObtainPairViewEx, TokenRefreshViewEx, TokenVerifyViewEx
from main.auth.views import TokenObtainPairViewEx, TokenVerifyViewEx, TokenRefreshViewEx
from main.core.routers import VLNNestedRouter, VLNRouter from main.core.routers import VLNNestedRouter, VLNRouter
from tenants.views import TenantViewSet
from users.views import UserViewSet from users.views import TenantUsersViewSet, UserViewSet
router = VLNRouter() router = VLNRouter()
router.register("users", UserViewSet, basename="users") router.register("users", UserViewSet, basename="users")
router.register("tenants", TenantViewSet, basename="tenants")
# tenant sub-views
tenants_router = VLNNestedRouter(router, "tenants", lookup="tenant")
tenants_router.register("users", TenantUsersViewSet, basename="users")
api_routes = [ api_routes = [
path("", include(router.urls)), path("", include(router.urls)),
path("", include(tenants_router.urls)),
path("token/auth", TokenObtainPairViewEx.as_view()), path("token/auth", TokenObtainPairViewEx.as_view()),
path("token/refresh", TokenRefreshViewEx.as_view()), path("token/refresh", TokenRefreshViewEx.as_view()),
path("token/verify", TokenVerifyViewEx.as_view()), path("token/verify", TokenVerifyViewEx.as_view()),
......
from django.contrib import admin
from tenants.models import Tenant
class TenantAdmin(admin.ModelAdmin):
search_fields = ["name"]
admin.site.register(Tenant, TenantAdmin)
from django.apps import AppConfig
class TenantsConfig(AppConfig):
name = "tenants"
verbose_name = "Tenants"
from django.db.models import TextChoices
class TenantState(TextChoices):
ACTIVE = ("ACTIVE", "Active")
BLOCKED = ("BLOCKED", "Blocked")
FROZEN = ("FROZEN", "Frozen")
from django_filters.rest_framework import FilterSet
class TenantFilter(FilterSet):
pass
# Generated by Django 3.1.7 on 2021-03-19 15:10
import uuid
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = []
operations = [
migrations.CreateModel(
name="Tenant",
fields=[
("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
("name", models.CharField(max_length=254, verbose_name="Name")),
("description", models.CharField(max_length=254, verbose_name="Description")),
("country", models.CharField(max_length=254, verbose_name="Country")),
(
"state",
models.CharField(
choices=[("ACTIVE", "Active"), ("BLOCKED", "Blocked"), ("FROZEN", "Frozen")],
default="ACTIVE",
max_length=32,
verbose_name="State",
),
),
],
options={
"abstract": False,
},
),
]
from django.db import models
from main.core.models import UUIDIDMixin
from tenants.constants import TenantState
class Tenant(UUIDIDMixin):
name = models.CharField("Name", max_length=254)
description = models.CharField("Description", max_length=254)
country = models.CharField("Country", max_length=254)
state = models.CharField("State", max_length=32, choices=TenantState.choices, default=TenantState.ACTIVE)
def __str__(self):
return f"{self.name}"
from rest_framework import serializers
from tenants.models import Tenant
class TenantSerializer(serializers.ModelSerializer):
class Meta:
model = Tenant
fields = "__all__"
from main.core.permissions import VLNModelPermissions
from main.core.views import VLNModelViewSet
from tenants.filters import TenantFilter
from tenants.models import Tenant
from tenants.serializers import TenantSerializer
class TenantViewSet(VLNModelViewSet):
permission_classes = (VLNModelPermissions,)
serializer_class = TenantSerializer
filterset_class = TenantFilter
queryset = Tenant.objects.all()
ordering = ["-id"]
...@@ -3,7 +3,6 @@ from django.contrib import admin ...@@ -3,7 +3,6 @@ from django.contrib import admin
from django.contrib.admin import ModelAdmin, widgets from django.contrib.admin import ModelAdmin, widgets
from django.contrib.admin.sites import NotRegistered from django.contrib.admin.sites import NotRegistered
from django.contrib.auth.models import Group from django.contrib.auth.models import Group
from users.models import User from users.models import User
...@@ -13,14 +12,11 @@ class UserAdmin(ModelAdmin): ...@@ -13,14 +12,11 @@ class UserAdmin(ModelAdmin):
"last_name", "last_name",
"first_name", "first_name",
) )
list_display = ( list_display = ("username", "last_name", "first_name", "tenant")
"username",
"last_name",
"first_name",
)
list_filter = ("is_active",) list_filter = ("is_active",)
readonly_fields = ("username", "is_superuser") readonly_fields = ("username", "is_superuser")
autocomplete_fields = ["tenant"]
fieldsets = ( fieldsets = (
( (
None, None,
...@@ -30,9 +26,8 @@ class UserAdmin(ModelAdmin): ...@@ -30,9 +26,8 @@ class UserAdmin(ModelAdmin):
"last_name", "last_name",
"first_name", "first_name",
"email", "email",
"groups", "tenant",
"is_superuser", "is_superuser",
"object_guid",
) )
}, },
), ),
...@@ -42,22 +37,6 @@ class UserAdmin(ModelAdmin): ...@@ -42,22 +37,6 @@ class UserAdmin(ModelAdmin):
return False return False
class PairAdmin(ModelAdmin):
search_fields = (
"user__username",
"user__last_name",
"user__first_name",
"master__username",
"master__last_name",
"master__first_name",
)
list_display = (
"user",
"master",
)
autocomplete_fields = ["user", "master"]
class GroupForm(forms.ModelForm): class GroupForm(forms.ModelForm):
name = forms.CharField(disabled=True) name = forms.CharField(disabled=True)
users = forms.ModelMultipleChoiceField( users = forms.ModelMultipleChoiceField(
......
from django_filters.rest_framework import FilterSet from django_filters.rest_framework import FilterSet
from django_filters.rest_framework.filters import BaseInFilter, BooleanFilter from django_filters.rest_framework.filters import BaseInFilter
from django_filters import NumberFilter
class UserFilter(FilterSet): class UserFilter(FilterSet):
groups = BaseInFilter(field_name="groups__name", lookup_expr="in", distinct=True) groups = BaseInFilter(field_name="groups__name", lookup_expr="in", distinct=True)
masters_of = NumberFilter(field_name="employee_pairs__user_id")
is_active = BooleanFilter(field_name="is_active")
# Generated by Django 3.1.7 on 2021-03-19 15:10
import django.db.models.deletion
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("tenants", "0001_initial"),
("users", "0001_initial"),
]
operations = [
migrations.AddField(
model_name="user",
name="tenant",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="users",
to="tenants.tenant",
),
),
]
import uuid
from django.contrib.auth.models import AbstractUser from django.contrib.auth.models import AbstractUser
from django.db import models from django.db import models
from main.core.models import UUIDIDMixin
from users.constants import UserGroup, UserPermission from users.constants import UserGroup, UserPermission
class User(AbstractUser): class User(AbstractUser, UUIDIDMixin):
Group = UserGroup Group = UserGroup
Permission = UserPermission Permission = UserPermission
tenant = models.ForeignKey("tenants.Tenant", related_name="users", null=True, blank=True, on_delete=models.CASCADE)
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
class Meta: class Meta:
verbose_name = "User" verbose_name = "User"
......
from rest_framework import serializers, fields from main.core.serializers import (
ForeignKeyDeserializerMixin,
NestedRouterForeignKeyDeserializerMixin,
)
from rest_framework import fields, serializers
from rest_framework.exceptions import ValidationError from rest_framework.exceptions import ValidationError
from rest_framework.fields import ReadOnlyField from rest_framework.fields import ReadOnlyField
from main.core.serializers import ForeignKeyDeserializerMixin
from users.models import User from users.models import User
...@@ -46,5 +48,5 @@ class UserSerializer(serializers.ModelSerializer): ...@@ -46,5 +48,5 @@ class UserSerializer(serializers.ModelSerializer):
return user.get_all_permissions() return user.get_all_permissions()
class UserShortSerializerWithFKDeserializer(ForeignKeyDeserializerMixin, UserShortSerializer): class TenantUsersSerializer(NestedRouterForeignKeyDeserializerMixin, UserSerializer):
pass router_lookups = ("tenant",)
from main.core.permissions import VLNModelPermissions
from main.core.views import VLNModelViewSet
from rest_framework.decorators import action from rest_framework.decorators import action
from rest_framework.permissions import IsAuthenticated from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response from rest_framework.response import Response
from main.core.permissions import VLNModelPermissions
from main.core.views import VLNModelViewSet
from users.filters import UserFilter from users.filters import UserFilter
from users.models import User from users.models import User
from users.serializers import UserSerializer, UserShortSerializer from users.serializers import TenantUsersSerializer, UserSerializer, UserShortSerializer
class UserViewSet(VLNModelViewSet): class UserViewSet(VLNModelViewSet):
...@@ -26,3 +24,8 @@ class UserViewSet(VLNModelViewSet): ...@@ -26,3 +24,8 @@ class UserViewSet(VLNModelViewSet):
def me(self, request): def me(self, request):
serializer = UserSerializer(self.request.user) serializer = UserSerializer(self.request.user)
return Response(serializer.data) return Response(serializer.data)
class TenantUsersViewSet(VLNModelViewSet):
serializer_class = TenantUsersSerializer
pagination_class = None
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment