From 43e5ff7c0e2cf8554467575fa1232fa0c5ef3287 Mon Sep 17 00:00:00 2001 From: Alex Barcelo Date: Wed, 16 Dec 2015 17:15:54 +0100 Subject: [PATCH] added delete mechanism on Queue for auto-clean of permissions --- helpdesk/models.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/helpdesk/models.py b/helpdesk/models.py index 8f496bde..1d533e52 100644 --- a/helpdesk/models.py +++ b/helpdesk/models.py @@ -10,6 +10,7 @@ models.py - Model (and hence database) definitions. This is the core of the from __future__ import unicode_literals from django.contrib.auth.models import Permission from django.contrib.contenttypes.models import ContentType +from django.core.exceptions import ObjectDoesNotExist from django.db import models from django.contrib.auth import get_user_model from django.conf import settings @@ -286,6 +287,18 @@ class Queue(models.Model): super(Queue, self).save(*args, **kwargs) + def delete(self, *args, **kwargs): + permission_name = self.permission_name + super(Queue, self).delete(*args, **kwargs) + + # once the Queue is safely deleted, remove the permission (if exists) + if permission_name: + try: + p = Permission.objects.get(codename=permission_name[9:]) + p.delete() + except ObjectDoesNotExist: + pass + class Ticket(models.Model): """