|
@@ -1,6 +1,10 @@
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
+"""Various helpers designed to help configuration backends regarding
|
|
|
+repetitive tasks."""
|
|
|
+
|
|
|
from django.core.exceptions import ValidationError
|
|
|
+from django.db.models import Q
|
|
|
|
|
|
|
|
|
def ValidateBackendType(object):
|
|
@@ -17,9 +21,19 @@ def ValidateBackendType(object):
|
|
|
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))
|
|
|
+
|
|
|
+
|
|
|
+def filter_subscriptions(backend_name, instance):
|
|
|
+ """Helper function for configuration backends, allowing to filter
|
|
|
+ subscriptions that have the right """
|
|
|
+ return Q(offer__backend=backend_name) & (
|
|
|
+ # Select "unassociated" subscriptions, plus our own
|
|
|
+ # subscription (in case we are editing the object).
|
|
|
+ Q((backend_name, None)) | Q((backend_name, instance.pk)))
|