first base of project-changed apps: Herd-livestock-tag-log-elasticsearch-
This commit is contained in:
0
apps/authorization/api/__init__.py
Normal file
0
apps/authorization/api/__init__.py
Normal file
0
apps/authorization/api/v1/__init__.py
Normal file
0
apps/authorization/api/v1/__init__.py
Normal file
36
apps/authorization/api/v1/api.py
Normal file
36
apps/authorization/api/v1/api.py
Normal file
@@ -0,0 +1,36 @@
|
||||
from rest_framework_simplejwt.authentication import JWTAuthentication
|
||||
from rest_framework.permissions import AllowAny, IsAuthenticated
|
||||
from apps.authorization.api.v1.serializers import (
|
||||
RoleSerializer,
|
||||
PermissionSerializer,
|
||||
UserRelationSerializer
|
||||
)
|
||||
from rest_framework.response import Response
|
||||
from apps.authorization.models import (
|
||||
Role,
|
||||
Permissions,
|
||||
UserRelations
|
||||
)
|
||||
from rest_framework import viewsets
|
||||
|
||||
|
||||
class RoleViewSet(viewsets.ModelViewSet):
|
||||
""" Crud Operations For User Roles """
|
||||
|
||||
queryset = Role.objects.all()
|
||||
serializer_class = RoleSerializer
|
||||
|
||||
|
||||
class PermissionViewSet(viewsets.ModelViewSet):
|
||||
""" Crud Operations for Permissions """
|
||||
|
||||
queryset = Permissions.objects.all()
|
||||
serializer_class = PermissionSerializer
|
||||
|
||||
|
||||
class UserRelationViewSet(viewsets.ModelViewSet):
|
||||
""" Crud Operations for User Relations """
|
||||
|
||||
queryset = UserRelations.objects.all()
|
||||
serializer_class = UserRelationSerializer
|
||||
|
||||
81
apps/authorization/api/v1/serializers.py
Normal file
81
apps/authorization/api/v1/serializers.py
Normal file
@@ -0,0 +1,81 @@
|
||||
from rest_framework import serializers
|
||||
from apps.authorization.models import (
|
||||
Role,
|
||||
Permissions,
|
||||
UserRelations
|
||||
)
|
||||
from apps.authentication.api.v1.serializers import serializer as auth_serializer
|
||||
from apps.authentication.models import Organization
|
||||
|
||||
|
||||
class PermissionSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Permissions
|
||||
fields = [
|
||||
'id',
|
||||
'name',
|
||||
'description'
|
||||
]
|
||||
|
||||
|
||||
class RoleSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Role
|
||||
fields = [
|
||||
'id',
|
||||
'role_name',
|
||||
'description',
|
||||
'type',
|
||||
'permissions'
|
||||
]
|
||||
extra_kwargs = {
|
||||
'permissions': {'required': False} # permissions not required for some roles
|
||||
}
|
||||
|
||||
def to_representation(self, instance):
|
||||
"""
|
||||
using @to_representation for many_to_many permissions in response
|
||||
"""
|
||||
representation = super().to_representation(instance)
|
||||
representation['type'] = auth_serializer.OrganizationTypeSerializer(instance.type).data
|
||||
representation['permissions'] = PermissionSerializer(instance.permissions, many=True).data
|
||||
return representation
|
||||
|
||||
|
||||
class UserRelationSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = UserRelations
|
||||
fields = [
|
||||
'id',
|
||||
'user',
|
||||
'organization',
|
||||
'role',
|
||||
'permissions',
|
||||
]
|
||||
|
||||
def to_representation(self, instance):
|
||||
representation = super().to_representation(instance)
|
||||
if isinstance(instance, UserRelations):
|
||||
if instance.user:
|
||||
representation['user'] = auth_serializer.UserSerializer(instance.user).data
|
||||
if instance.organization:
|
||||
representation['organization'] = auth_serializer.OrganizationSerializer(instance.organization).data
|
||||
if instance.role:
|
||||
representation['role'] = RoleSerializer(instance.role).data
|
||||
if instance.permissions:
|
||||
representation['permissions'] = PermissionSerializer(instance.permissions, many=True).data
|
||||
|
||||
return representation
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
""" update user relation object """
|
||||
if validated_data.get('role'):
|
||||
instance.role = Role.objects.get(id=validated_data.get('role', instance.role))
|
||||
if validated_data.get('organization'):
|
||||
instance.organization = Organization.objects.get(id=validated_data.get(
|
||||
'organization', instance.organization
|
||||
))
|
||||
instance.save()
|
||||
instance.permissions.clear()
|
||||
instance.permissions.add(*(validated_data.get('permissions', instance.permissions)))
|
||||
return instance
|
||||
18
apps/authorization/api/v1/urls.py
Normal file
18
apps/authorization/api/v1/urls.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from rest_framework.routers import DefaultRouter
|
||||
from django.urls import path, include
|
||||
from .api import (
|
||||
RoleViewSet,
|
||||
PermissionViewSet,
|
||||
UserRelationViewSet
|
||||
)
|
||||
|
||||
router = DefaultRouter() # set router
|
||||
|
||||
# register route to router
|
||||
router.register(r'role', RoleViewSet, basename='role')
|
||||
router.register(r'permission', PermissionViewSet, basename='permission')
|
||||
router.register(r'user-relations', UserRelationViewSet, basename='organization-role')
|
||||
|
||||
urlpatterns = [
|
||||
path('', include(router.urls))
|
||||
]
|
||||
0
apps/authorization/fixtures/.gitkeep
Normal file
0
apps/authorization/fixtures/.gitkeep
Normal file
0
apps/authorization/management/__init__.py
Normal file
0
apps/authorization/management/__init__.py
Normal file
0
apps/authorization/management/commands/__init__.py
Normal file
0
apps/authorization/management/commands/__init__.py
Normal file
1
apps/authorization/management/commands/command.py
Normal file
1
apps/authorization/management/commands/command.py
Normal file
@@ -0,0 +1 @@
|
||||
# Your custom management commands go here.
|
||||
18
apps/authorization/migrations/0004_alter_role_permissions.py
Normal file
18
apps/authorization/migrations/0004_alter_role_permissions.py
Normal file
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 4.2.20 on 2025-05-07 05:29
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('authorization', '0003_remove_organizationrole_otp_status'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='role',
|
||||
name='permissions',
|
||||
field=models.ManyToManyField(null=True, to='authorization.permissions'),
|
||||
),
|
||||
]
|
||||
18
apps/authorization/migrations/0005_alter_role_permissions.py
Normal file
18
apps/authorization/migrations/0005_alter_role_permissions.py
Normal file
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 4.2.20 on 2025-05-07 05:30
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('authorization', '0004_alter_role_permissions'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='role',
|
||||
name='permissions',
|
||||
field=models.ManyToManyField(to='authorization.permissions'),
|
||||
),
|
||||
]
|
||||
18
apps/authorization/migrations/0006_alter_role_role_name.py
Normal file
18
apps/authorization/migrations/0006_alter_role_role_name.py
Normal file
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 4.2.20 on 2025-05-07 08:03
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('authorization', '0005_alter_role_permissions'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='role',
|
||||
name='role_name',
|
||||
field=models.CharField(max_length=50, unique=True),
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 4.2.20 on 2025-05-07 09:14
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('authorization', '0006_alter_role_role_name'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='organizationrole',
|
||||
name='permissions',
|
||||
field=models.ManyToManyField(to='authorization.permissions'),
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,16 @@
|
||||
# Generated by Django 4.2.20 on 2025-05-10 06:26
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('authorization', '0007_organizationrole_permissions'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.DeleteModel(
|
||||
name='OrganizationRole',
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,40 @@
|
||||
# Generated by Django 4.2.20 on 2025-05-10 08:51
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
('authentication', '0008_remove_organization_type_organizationtype'),
|
||||
('authorization', '0008_delete_organizationrole'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='role',
|
||||
name='type',
|
||||
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='organization_role_type', to='authentication.organizationtype'),
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='UserRelations',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('create_date', models.DateTimeField(auto_now_add=True)),
|
||||
('modify_date', models.DateTimeField(auto_now=True)),
|
||||
('trash', models.BooleanField(default=False)),
|
||||
('created_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='%(class)s_createdby', to=settings.AUTH_USER_MODEL)),
|
||||
('modified_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='%(class)s_modifiedby', to=settings.AUTH_USER_MODEL)),
|
||||
('organization', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='user_organization', to='authentication.organization')),
|
||||
('permissions', models.ManyToManyField(to='authorization.permissions')),
|
||||
('role', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='user_role', to='authorization.role')),
|
||||
('user', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='user_relation', to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
options={
|
||||
'abstract': False,
|
||||
},
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,37 @@
|
||||
# Generated by Django 4.2.20 on 2025-05-17 06:01
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('authorization', '0009_role_type_userrelations'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveField(
|
||||
model_name='permissions',
|
||||
name='created_by',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='permissions',
|
||||
name='modified_by',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='role',
|
||||
name='created_by',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='role',
|
||||
name='modified_by',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='userrelations',
|
||||
name='created_by',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='userrelations',
|
||||
name='modified_by',
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,43 @@
|
||||
# Generated by Django 4.2.20 on 2025-05-17 06:24
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('authorization', '0010_remove_permissions_created_by_and_more'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='permissions',
|
||||
name='created_by',
|
||||
field=models.CharField(max_length=50, null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='permissions',
|
||||
name='modified_by',
|
||||
field=models.CharField(max_length=50, null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='role',
|
||||
name='created_by',
|
||||
field=models.CharField(max_length=50, null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='role',
|
||||
name='modified_by',
|
||||
field=models.CharField(max_length=50, null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='userrelations',
|
||||
name='created_by',
|
||||
field=models.CharField(max_length=50, null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='userrelations',
|
||||
name='modified_by',
|
||||
field=models.CharField(max_length=50, null=True),
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,37 @@
|
||||
# Generated by Django 4.2.20 on 2025-05-17 06:27
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('authorization', '0011_permissions_created_by_permissions_modified_by_and_more'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveField(
|
||||
model_name='permissions',
|
||||
name='created_by',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='permissions',
|
||||
name='modified_by',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='role',
|
||||
name='created_by',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='role',
|
||||
name='modified_by',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='userrelations',
|
||||
name='created_by',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='userrelations',
|
||||
name='modified_by',
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,46 @@
|
||||
# Generated by Django 4.2.20 on 2025-05-17 06:29
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
('authorization', '0012_remove_permissions_created_by_and_more'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='permissions',
|
||||
name='created_by',
|
||||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='%(class)s_createddby', to=settings.AUTH_USER_MODEL),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='permissions',
|
||||
name='modified_by',
|
||||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='%(class)s_modifiedby', to=settings.AUTH_USER_MODEL),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='role',
|
||||
name='created_by',
|
||||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='%(class)s_createddby', to=settings.AUTH_USER_MODEL),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='role',
|
||||
name='modified_by',
|
||||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='%(class)s_modifiedby', to=settings.AUTH_USER_MODEL),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='userrelations',
|
||||
name='created_by',
|
||||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='%(class)s_createddby', to=settings.AUTH_USER_MODEL),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='userrelations',
|
||||
name='modified_by',
|
||||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='%(class)s_modifiedby', to=settings.AUTH_USER_MODEL),
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,43 @@
|
||||
# Generated by Django 4.2.20 on 2025-05-17 06:35
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('authorization', '0013_permissions_created_by_permissions_modified_by_and_more'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='permissions',
|
||||
name='creator_info',
|
||||
field=models.CharField(max_length=100, null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='permissions',
|
||||
name='modifier_info',
|
||||
field=models.CharField(max_length=100, null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='role',
|
||||
name='creator_info',
|
||||
field=models.CharField(max_length=100, null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='role',
|
||||
name='modifier_info',
|
||||
field=models.CharField(max_length=100, null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='userrelations',
|
||||
name='creator_info',
|
||||
field=models.CharField(max_length=100, null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='userrelations',
|
||||
name='modifier_info',
|
||||
field=models.CharField(max_length=100, null=True),
|
||||
),
|
||||
]
|
||||
@@ -17,8 +17,14 @@ class Permissions(BaseModel):
|
||||
|
||||
|
||||
class Role(BaseModel):
|
||||
role_name = models.CharField(max_length=50)
|
||||
role_name = models.CharField(max_length=50, unique=True)
|
||||
description = models.TextField(max_length=500)
|
||||
type = models.ForeignKey(
|
||||
auth_models.OrganizationType,
|
||||
on_delete=models.CASCADE,
|
||||
related_name="organization_role_type",
|
||||
null=True
|
||||
)
|
||||
permissions = models.ManyToManyField(Permissions)
|
||||
|
||||
def __str__(self):
|
||||
@@ -28,26 +34,28 @@ class Role(BaseModel):
|
||||
super(Role, self).save(*args, **kwargs)
|
||||
|
||||
|
||||
class OrganizationRole(BaseModel):
|
||||
class UserRelations(BaseModel):
|
||||
user = models.ForeignKey(
|
||||
auth_models.User,
|
||||
on_delete=models.CASCADE,
|
||||
related_name='organization_user',
|
||||
related_name='user_relation',
|
||||
null=True
|
||||
)
|
||||
organization = models.ForeignKey(
|
||||
auth_models.Organization,
|
||||
on_delete=models.CASCADE,
|
||||
related_name='organization'
|
||||
related_name='user_organization'
|
||||
)
|
||||
role = models.ForeignKey(
|
||||
Role,
|
||||
on_delete=models.CASCADE,
|
||||
related_name='organization_role'
|
||||
related_name='user_role',
|
||||
null=True
|
||||
)
|
||||
permissions = models.ManyToManyField(Permissions)
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.organization.name}-{self.user.username}'
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
super(OrganizationRole, self).save(*args, **kwargs)
|
||||
super(UserRelations, self).save(*args, **kwargs)
|
||||
|
||||
0
apps/authorization/tests/test_common_services.py
Normal file
0
apps/authorization/tests/test_common_services.py
Normal file
5
apps/authorization/urls.py
Normal file
5
apps/authorization/urls.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from django.urls import path, include
|
||||
|
||||
urlpatterns = [
|
||||
path('api/v1/', include('apps.authorization.api.v1.urls'))
|
||||
]
|
||||
@@ -1,3 +0,0 @@
|
||||
from django.shortcuts import render
|
||||
|
||||
# Create your views here.
|
||||
Reference in New Issue
Block a user