|
@@ -1,3 +1,5 @@
|
|
|
+# coding=utf-8
|
|
|
+
|
|
|
from django.contrib.contenttypes.fields import GenericForeignKey
|
|
|
from django.contrib.contenttypes.models import ContentType
|
|
|
from rapid.rapidforms import RapidAlternativesField
|
|
@@ -20,30 +22,41 @@ class AlternativeData(GenericForeignKey):
|
|
|
"""
|
|
|
is_alternatives = True
|
|
|
|
|
|
+ def __init__(self, *args, **kwargs):
|
|
|
+ verbose_name = kwargs.get('verbose_name')
|
|
|
+ if 'verbose_name' in kwargs:
|
|
|
+ del(kwargs['verbose_name'])
|
|
|
+ super(AlternativeData, self).__init__(*args, **kwargs)
|
|
|
+ if verbose_name:
|
|
|
+ self.verbose_name = verbose_name
|
|
|
+ elif self.name:
|
|
|
+ self.verbose_name = self.name.replace('_', ' ')
|
|
|
+
|
|
|
def formfield(self, **kwargs):
|
|
|
kwargs['form_class'] = RapidAlternativesField
|
|
|
+ # noinspection PyUnresolvedReferences
|
|
|
return super(GenericForeignKey, self).formfield(**kwargs)
|
|
|
|
|
|
|
|
|
class AlternativeDataTables(models.ForeignKey):
|
|
|
"""
|
|
|
- Use an AlternativeDataTables model field on AlternativeData represetntations, instead of
|
|
|
+ Use an AlternativeDataTables model field on AlternativeData representations, instead of
|
|
|
a ForeignKey with ContentTypes.
|
|
|
"""
|
|
|
is_alternatives = True
|
|
|
|
|
|
- def __init__(self, alternatives=[], **kwargs):
|
|
|
+ def __init__(self, alternatives=(), **kwargs):
|
|
|
"""
|
|
|
Receives the same named parameters of a ForeignKey, but instead of a target table,
|
|
|
receives a list of the tables that may keep the alternative data.
|
|
|
:param alternatives: List of the tables that hold the alternative data.
|
|
|
:param kwargs: Arguments to the ForeignKey, without a relationship target.
|
|
|
"""
|
|
|
- pks=[]
|
|
|
+ pks = []
|
|
|
try:
|
|
|
pks = [ContentType.objects.get_for_model(a).pk for a in alternatives]
|
|
|
except RuntimeError:
|
|
|
- #Querying ContentType within a content type is an issue at migrations
|
|
|
+ # Querying ContentType within a content type is an issue at migrations
|
|
|
pass
|
|
|
kwargs['limit_choices_to'] = {'pk__in': pks}
|
|
|
kwargs['to'] = ContentType
|