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
......@@ -40,3 +43,10 @@ class TrackerMixin:
self.save(**kwargs)
else:
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 = [
"django_cron",
"corsheaders",
"mptt",
"tenants.apps.TenantsConfig",
"users.apps.UsersConfig", # users should be last in this list, or permission update may fail
]
......
......@@ -15,20 +15,23 @@ Including another URLconf
"""
from django.contrib import admin
from django.contrib.auth import views as auth_views
from django.urls import path, include
from main.auth.views import TokenObtainPairViewEx, TokenVerifyViewEx, TokenRefreshViewEx
from django.urls import include, path
from main.auth.views import TokenObtainPairViewEx, TokenRefreshViewEx, TokenVerifyViewEx
from main.core.routers import VLNNestedRouter, VLNRouter
from users.views import UserViewSet
from tenants.views import TenantViewSet
from users.views import TenantUsersViewSet, UserViewSet
router = VLNRouter()
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 = [
path("", include(router.urls)),
path("", include(tenants_router.urls)),
path("token/auth", TokenObtainPairViewEx.as_view()),
path("token/refresh", TokenRefreshViewEx.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
from django.contrib.admin import ModelAdmin, widgets
from django.contrib.admin.sites import NotRegistered
from django.contrib.auth.models import Group
from users.models import User
......@@ -13,14 +12,11 @@ class UserAdmin(ModelAdmin):
"last_name",
"first_name",
)
list_display = (
"username",
"last_name",
"first_name",
)
list_display = ("username", "last_name", "first_name", "tenant")
list_filter = ("is_active",)
readonly_fields = ("username", "is_superuser")
autocomplete_fields = ["tenant"]
fieldsets = (
(
None,
......@@ -30,9 +26,8 @@ class UserAdmin(ModelAdmin):
"last_name",
"first_name",
"email",
"groups",
"tenant",
"is_superuser",
"object_guid",
)
},
),
......@@ -42,22 +37,6 @@ class UserAdmin(ModelAdmin):
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):
name = forms.CharField(disabled=True)
users = forms.ModelMultipleChoiceField(
......
from django_filters.rest_framework import FilterSet
from django_filters.rest_framework.filters import BaseInFilter, BooleanFilter
from django_filters import NumberFilter
from django_filters.rest_framework.filters import BaseInFilter
class UserFilter(FilterSet):
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.db import models
from main.core.models import UUIDIDMixin
from users.constants import UserGroup, UserPermission
class User(AbstractUser):
class User(AbstractUser, UUIDIDMixin):
Group = UserGroup
Permission = UserPermission
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
tenant = models.ForeignKey("tenants.Tenant", related_name="users", null=True, blank=True, on_delete=models.CASCADE)
class Meta:
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.fields import ReadOnlyField
from main.core.serializers import ForeignKeyDeserializerMixin
from users.models import User
......@@ -46,5 +48,5 @@ class UserSerializer(serializers.ModelSerializer):
return user.get_all_permissions()
class UserShortSerializerWithFKDeserializer(ForeignKeyDeserializerMixin, UserShortSerializer):
pass
class TenantUsersSerializer(NestedRouterForeignKeyDeserializerMixin, UserSerializer):
router_lookups = ("tenant",)
from main.core.permissions import VLNModelPermissions
from main.core.views import VLNModelViewSet
from rest_framework.decorators import action
from rest_framework.permissions import IsAuthenticated
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.models import User
from users.serializers import UserSerializer, UserShortSerializer
from users.serializers import TenantUsersSerializer, UserSerializer, UserShortSerializer
class UserViewSet(VLNModelViewSet):
......@@ -26,3 +24,8 @@ class UserViewSet(VLNModelViewSet):
def me(self, request):
serializer = UserSerializer(self.request.user)
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