Each restaurant can have multiple managers.
class Restaurant(models.Model):
...
managers = models.ManyToManyField(User, related_name='restaurants_which_they_manage')
Only restaurant managers can change a restaurant listing. I'm using django-rules to enforce this. I've got a predicate that creates a nice verbose "is_restaurant_manager" reference :
@rules.predicate
def is_restaurant_manager(user, restaurant):
return user in restaurant.managers.all()
And here is the permission :
rules.add_perm('restaurants.change_restaurant', is_restaurant_manager)
Finally, here is my view :
class RestaurantChange(PermissionRequiredMixin, UpdateView):
model = Restaurant
permission_required = 'restaurants.change_restaurant'
fields = ['name', 'description', ]
I've got two tests.
Test A checks that the permission works properly :
self.assertEqual(rules.has_perm('restaurants.change_restaurant', self.user, self.restaurant), True)
This first test passes successfully.
Test B attempts to access the url with a valid user :
url = reverse('restaurants__restaurant_change', kwargs={'pk': self.restaurant.key,})
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
Test B fails, as I get a redirection. This also happens if I try to access the url via the browser. The redirection goes to the login process, as though the user didn't have permission to access the view.
What's wrong with my code ?