Browse Source

Added a verbose_name for AlternativeData

Marcos Dumay de Medeiros 8 years ago
parent
commit
acf5386ba7
2 changed files with 18 additions and 5 deletions
  1. 17 4
      src/rapid/rapidfields.py
  2. 1 1
      testproject/testproject/models.py

+ 17 - 4
src/rapid/rapidfields.py

@@ -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

+ 1 - 1
testproject/testproject/models.py

@@ -28,4 +28,4 @@ class Test1(models.Model):
     some_time = models.TimeField()
     alt_data_type = rapidfields.AlternativeDataTables((AltData1, AltData2))
     alt_data_id = models.PositiveIntegerField()
-    alt_data = rapidfields.AlternativeData('alt_data_type', 'alt_data_id')
+    alt_data = rapidfields.AlternativeData('alt_data_type', 'alt_data_id', verbose_name='test alt data')