I have an app for managing test cases, which are organised into various projects. I'm trying to set permissions on a per project basis, ie every user has different permissions for each project. Here's what I've come up with so far:
class TestProjectMember(models.Model):
"""Per project permissions - a role can be set for each user for each project"""
member_name = models.ForeignKey(User, on_delete=models.SET_NULL)
project = models.ForeignKey(TestProject, on_delete=models.CASCADE)
member_role = models.CharField(choices=Choices.roles)
class TestCase(models.Model):
"""Test cases"""
tc_title = models.CharField(max_length=500, unique=True)
tc_project = models.ForeignKey(TestProject, on_delete=models.CASCADE)
class TestProject(models.Model):
"""Projects"""
project_name = models.CharField(max_length=200)
project_desc = models.CharField(max_length=500)
class TestCaseEditHeader(View):
def get(self, request, pk):
case = get_object_or_404(TestCase, id=pk)
if self.get_perm(case.tc_project, request.user, 'TA'):
form = TestCaseHeaderForm(instance=case)
context = {'case': case, 'form': form}
return render(request, 'test_case/tc_header_edit.html', context)
else:
return redirect('case_list')
def get_perm(self, curr_project, curr_user, perm):
model_perm = TestProjectMember.objects.filter(member_name=curr_user,
project=curr_project).values_list('member_role', flat=True)
if perm in model_perm:
return True
return False
It works, but it's a bit clunky. I'd have to call the get_perm() method from every get() or post() method from each view. A better solution would probably be a mixin. What has me stumped is how to pass the required role to the mixin for each view. For each view there is a required role that the user has to have to be able to use the view for the project the test case belongs to. How do I tell the mixin, which particular role is required for which view?
You can just set it as a class attribute.
Note, your query is pretty inefficient; you should just request the perm you want directly.
class TestPermMixin:
def get_perm(self, curr_project, curr_user):
return TestProjectMember.objects.filter(
member_name=curr_user, project=curr_project, member_role=self.perm
).exists()
And then set the attribute in the concrete class:
class TestCaseEditHeader(View):
perm = 'TA'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.