Commit ee8eb45e authored by jocelyn's avatar jocelyn
Browse files

Forbid adding a loan when there is already one running

parent 5323c072
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
# -*- 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'