Skip to content

Commit

Permalink
Add a new permission to hierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
Heibert committed Sep 10, 2024
1 parent d73b657 commit c81b4a3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
6 changes: 4 additions & 2 deletions INSIGHTSAPI/hierarchy/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.contrib import admin
from users.models import User
from django.db.models import Q
from .models import Area, JobPosition


Expand All @@ -26,9 +27,10 @@ class AreaAdmin(admin.ModelAdmin):
def formfield_for_foreignkey(self, db_field, request, **kwargs):
"""Customize the queryset for the manager field."""
if db_field.name == "manager":
# Customizing the queryset to show only users with a rank >= 4
# Customizing the queryset to show only users with a rank >= 4 or have the manage_area permission
kwargs["queryset"] = User.objects.filter(
job_position__rank__gte=4
Q(job_position__rank__gte=4)
| Q(user_permissions__codename="manage_area")
).order_by("first_name")
return super().formfield_for_foreignkey(db_field, request, **kwargs)

Expand Down
17 changes: 17 additions & 0 deletions INSIGHTSAPI/hierarchy/migrations/0007_alter_area_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 5.0.7 on 2024-09-10 15:52

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('hierarchy', '0006_area_parent'),
]

operations = [
migrations.AlterModelOptions(
name='area',
options={'permissions': [('manage_area', 'Can manage the area')]},
),
]
5 changes: 5 additions & 0 deletions INSIGHTSAPI/hierarchy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ class Area(models.Model):
"users.User", on_delete=models.SET_NULL, null=True, related_name="managed_areas"
)

class Meta:
permissions = [
("manage_area", "Can manage the area"),
]

def __str__(self) -> str:
"""String representation of the model."""
return self.name
Expand Down

0 comments on commit c81b4a3

Please sign in to comment.