Commit 12691509 authored by apeshkov's avatar apeshkov

[BACK][VLN][master] add clients model

parent ab5e47b8
from clients.models import Client
from django.contrib import admin
class ClientAdmin(admin.ModelAdmin):
search_fields = ["name"]
admin.site.register(Client, ClientAdmin)
from django.apps import AppConfig
class ClientsConfig(AppConfig):
name = "clients"
verbose_name = "Clients"
from django_filters.rest_framework import FilterSet
class ClientFilter(FilterSet):
pass
# Generated by Django 3.1.7 on 2021-03-19 18:27
import uuid
import django.contrib.postgres.fields
import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
("tenants", "0001_initial"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name="Client",
fields=[
("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
("updated_at", models.DateTimeField(auto_now=True, null=True, verbose_name="Updated at")),
("created_at", models.DateTimeField(auto_now_add=True, null=True, verbose_name="Created at")),
("name", models.CharField(max_length=254, verbose_name="Name")),
("company", models.CharField(blank=True, max_length=254, null=True, verbose_name="Company")),
("position", models.CharField(blank=True, max_length=254, null=True, verbose_name="Position")),
("site", models.CharField(blank=True, max_length=254, null=True, verbose_name="Site")),
(
"email",
django.contrib.postgres.fields.ArrayField(
base_field=models.EmailField(max_length=254, verbose_name="Email"),
blank=True,
null=True,
size=None,
),
),
("phone_primary", models.CharField(max_length=16, verbose_name="Primary phone")),
(
"phone_extra",
django.contrib.postgres.fields.ArrayField(
base_field=models.CharField(max_length=16, verbose_name="Phone"),
blank=True,
null=True,
size=None,
),
),
(
"created_by",
models.ForeignKey(
editable=False,
null=True,
on_delete=django.db.models.deletion.PROTECT,
related_name="clients_created_client_set",
to=settings.AUTH_USER_MODEL,
verbose_name="Created by",
),
),
(
"tenant",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, related_name="clients", to="tenants.tenant"
),
),
(
"updated_by",
models.ForeignKey(
editable=False,
null=True,
on_delete=django.db.models.deletion.PROTECT,
related_name="clients_updated_client_set",
to=settings.AUTH_USER_MODEL,
verbose_name="Updated by",
),
),
],
options={
"abstract": False,
},
),
]
from django.contrib.postgres.fields import ArrayField
from django.db import models
from main.core.models import BaseAuditTrack, UUIDIDMixin
class Client(UUIDIDMixin, BaseAuditTrack):
name = models.CharField("Name", max_length=254)
company = models.CharField("Company", max_length=254, null=True, blank=True)
position = models.CharField("Position", max_length=254, null=True, blank=True)
site = models.CharField("Site", max_length=254, null=True, blank=True)
email = ArrayField(models.EmailField("Email"), null=True, blank=True)
phone_primary = models.CharField("Primary phone", max_length=16)
phone_extra = ArrayField(models.CharField("Phone", max_length=16), null=True, blank=True)
tenant = models.ForeignKey("tenants.Tenant", related_name="clients", on_delete=models.CASCADE)
def __str__(self):
return f"{self.name}"
from clients.models import Client
from main.core.serializers import NestedRouterForeignKeyDeserializerMixin
from rest_framework import serializers
class ClientSerializer(serializers.ModelSerializer):
class Meta:
model = Client
fields = "__all__"
class TenantClientSerializer(NestedRouterForeignKeyDeserializerMixin, ClientSerializer):
router_lookups = ("tenant",)
from clients.filters import ClientFilter
from clients.models import Client
from clients.serializers import ClientSerializer, TenantClientSerializer
from main.core.permissions import VLNModelPermissions
from main.core.views import VLNModelViewSet
class ClientViewSet(VLNModelViewSet):
permission_classes = (VLNModelPermissions,)
serializer_class = ClientSerializer
filterset_class = ClientFilter
queryset = Client.objects.all()
ordering = ["-id"]
class TenantClientViewSet(VLNModelViewSet):
serializer_class = TenantClientSerializer
pagination_class = None
import uuid
from django.conf import settings
from django.db import models
from main.core.fields import VLNFieldTracker
......@@ -50,3 +51,100 @@ class UUIDIDMixin(models.Model):
class Meta:
abstract = True
class UpdatedAtMixin(models.Model):
"""
Abstract model which adds update time track.
"""
updated_at = models.DateTimeField("Updated at", null=True, editable=False, auto_now=True)
class Meta:
abstract = True
class CreatedAtMixin(models.Model):
"""
Abstract model which adds creation time track.
"""
created_at = models.DateTimeField("Created at", null=True, editable=False, auto_now_add=True)
class Meta:
abstract = True
class CreatedByMixin(models.Model):
"""
Abstract model which adds a track of who created an instance.
"""
created_by = models.ForeignKey(
settings.AUTH_USER_MODEL,
verbose_name="Created by",
null=True,
on_delete=models.PROTECT,
editable=False,
related_name="%(app_label)s_created_%(class)s_set",
)
class Meta:
abstract = True
class UpdatedByMixin(models.Model):
"""
Abstract model which adds a track of who updated an instance.
"""
updated_by = models.ForeignKey(
settings.AUTH_USER_MODEL,
verbose_name="Updated by",
null=True,
on_delete=models.PROTECT,
editable=False,
related_name="%(app_label)s_updated_%(class)s_set",
)
class Meta:
abstract = True
def save(self, *args, **kwargs):
if isinstance(self, CreatedByMixin) and not self.created_by:
self.created_by = self.updated_by
super().save(*args, **kwargs)
class BaseAuditTrack(CreatedAtMixin, UpdatedAtMixin, CreatedByMixin, UpdatedByMixin):
"""
Abstract model which adds complete audit track.
"""
class Meta:
abstract = True
class ArchivableMixin(models.Model):
"""
Abstract model adding support for 'archive' action (soft delete)
"""
is_archived = models.BooleanField(verbose_name="Is archived", default=False)
class Meta:
abstract = True
def archive(self, *args, **kwargs):
self.is_archived = True
self.save()
def unarchive(self, *args, **kwargs):
self.is_archived = False
self.save()
def delete(self, *args, **kwargs):
self.archive(*args, **kwargs)
def delete_permanent(self, *args, **kwargs):
super().delete(*args, **kwargs)
......@@ -58,8 +58,9 @@ INSTALLED_APPS = [
"django_cron",
"corsheaders",
"mptt",
"tenants",
"task_templates",
"tenants.apps.TenantsConfig",
"task_templates.apps.TaskTemplateConfig",
"clients.apps.ClientsConfig",
"users.apps.UsersConfig", # users should be last in this list, or permission update may fail
]
......
......@@ -13,6 +13,7 @@ Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from clients.views import TenantClientViewSet
from django.contrib import admin
from django.contrib.auth import views as auth_views
from django.urls import include, path
......@@ -30,6 +31,7 @@ router.register("tenants", TenantViewSet, basename="tenants")
tenants_router = VLNNestedRouter(router, "tenants", lookup="tenant")
tenants_router.register("users", TenantUsersViewSet, basename="users")
tenants_router.register("task-templates", TenantTaskTemplateViewSet, basename="task-templates")
tenants_router.register("clients", TenantClientViewSet, basename="clients")
api_routes = [
path("", include(router.urls)),
......
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