Browse Source

Forbid adding a loan when there is already one running

Jocelyn Delalande 6 years ago
parent
commit
2577927d61
1 changed files with 13 additions and 0 deletions
  1. 13 0
      hardware_provisioning/models.py

+ 13 - 0
hardware_provisioning/models.py

@@ -1,6 +1,7 @@
 # -*- coding: utf-8 -*-
 
 from __future__ import unicode_literals
+from django.core.exceptions import ValidationError
 from django.db import models
 from django.db.models import Q
 from django.conf import settings
@@ -172,6 +173,18 @@ class Loan(models.Model):
     is_running.boolean = True
     is_running.short_description = 'En cours ?'
 
+    def clean(self):
+        current_loan = self.item.get_current_loan()
+        if (
+                self.is_running()
+                and
+                current_loan
+                and
+                self.item.get_current_loan() != self
+        ):
+            raise ValidationError(
+                "Il y a déjà un emprunt en cours sur cet objet")
+
     class Meta:
         verbose_name = 'prêt d’objet'
         verbose_name_plural = 'prêts d’objets'