Browse Source

Add configuration subscription validation to ensure a subscription with different configuration type can't be selected by user

Fabs 10 years ago
parent
commit
5fc261d014
2 changed files with 12 additions and 34 deletions
  1. 12 1
      coin/configuration/forms.py
  2. 0 33
      coin/offers/backends.py

+ 12 - 1
coin/configuration/forms.py

@@ -1,6 +1,6 @@
 # -*- coding: utf-8 -*-
 
-from django.forms import ModelForm
+from django.forms import ModelForm, ValidationError
 from django.db.models import Q
 
 from coin.offers.models import OfferSubscription
@@ -24,3 +24,14 @@ class ConfigurationForm(ModelForm):
                 Q(offer__configuration_type=self.instance.model_name) & (
                 Q(configuration=None) | Q(configuration=self.instance.pk)))
             self.fields['offersubscription'].queryset = queryset
+
+    def clean_offersubscription(self):
+        """
+        This check if the selected administrative subscription is linked to an
+        offer which use the same configuration type than the edited configuration.
+        """
+        offersubscription = self.cleaned_data['offersubscription']
+        if offersubscription.offer.configuration_type != self.instance.model_name():
+            raise ValidationError('Administrative subscription must refer an offer having a "{}" configuration type.'.format(self.instance.model_name()))
+
+        return offersubscription

+ 0 - 33
coin/offers/backends.py

@@ -1,33 +0,0 @@
-# -*- coding: utf-8 -*-
-
-"""Various helpers designed to help configuration backends regarding
-repetitive tasks."""
-
-from django.core.exceptions import ValidationError
-
-from coin.offers.models import OfferSubscription
-
-
-class ValidateBackendType(object):
-    """Helper validator for configuration backends.
-
-    It ensures that the related subscription has the right backend
-    type. This is a bit ugly, as validators are not meant for
-    database-level sanity checks, but this is the cost of doing genericity
-    the way we do.
-
-    Note that this validator should not be needed for most cases, as the
-    "limit_choices_to" parameter of the "administrative_subscription"
-    OneToOneField should automatically limit the available choices on
-    forms.  But it does not protect us if we fiddle manually with the
-    database: better safe than sorry.
-    """
-
-    def __init__(self, backend_name):
-        self.backend = backend_name
-
-    def __call__(self, subscription):
-        if OfferSubscription.objects.get(pk=subscription).offer.backend != self.backend:
-            raise ValidationError('Administrative subscription must have a "{}" backend.'.format(self.backend))
-
-