Anonymous avatar Anonymous committed 3514775 Draft

patching trac with a new method ITicketFieldProvider.get_raw_fields - the change is required by bhrelations plugin

git-svn-id: https://svn.apache.org/repos/asf/bloodhound/trunk@1469232 13f79535-47bb-0310-9956-ffa450edef68

Comments (0)

Files changed (2)

trac/trac/ticket/api.py

         specified as optional.
         """
 
+    def get_raw_fields():
+        """Returns a list of fields, each represents ticket field
+        dictionary. For example:
+            * name: field name
+            * type: field type
+            * label: the label to display, preferably wrapped with N_()
+            * format: field format
+            * other appropriate field properties
+        """
+
 class TicketSystem(Component):
     implements(IPermissionRequestor, IWikiSyntaxProvider, IResourceManager,
                ITicketFieldProvider)
             field['custom'] = True
             fields.append(field)
 
+        #TODO: this is Bloodhound specific patch to the Trac. Contact Trac
+        # community about possibility to apply the change to the Trac codebase
+        self._add_raw_fields_from_field_providers(fields)
+
         return fields
 
     reserved_field_names = ['report', 'order', 'desc', 'group', 'groupdesc',
                             'col', 'row', 'format', 'max', 'page', 'verbose',
                             'comment', 'or']
 
+    def _add_raw_fields_from_field_providers(self, fields):
+        for field_provider in self.ticket_field_providers:
+            if hasattr(field_provider, 'get_raw_fields'):
+                raw_fields = field_provider.get_raw_fields()
+                if raw_fields:
+                    for raw_field in raw_fields:
+                        self._add_raw_field(
+                            raw_field, fields)
+
+    def _add_raw_field(self, raw_field, fields):
+        if raw_field["name"] in [f['name'] for f in fields]:
+            self.log.warning(
+                'Duplicate field name "%s" (ignoring)', raw_field["name"])
+        else:
+            fields.append(raw_field)
+
+
     def get_custom_fields(self):
         return copy.deepcopy(self.custom_fields)
 

trac/trac/ticket/tests/api.py

 from trac.perm import PermissionCache, PermissionSystem
-from trac.ticket.api import TicketSystem
+from trac.ticket.api import TicketSystem, ITicketFieldProvider
 from trac.ticket.model import Ticket
 from trac.test import EnvironmentStub, Mock
+from trac.core import implements, Component
 
 import unittest
 
+class TestFieldProvider(Component):
+    implements(ITicketFieldProvider)
+
+    def __init__(self):
+        self.raw_fields = []
+
+    def get_select_fields(self):
+        return []
+
+    def get_radio_fields(self):
+        return []
+
+    def get_raw_fields(self):
+        return self.raw_fields
+
 
 class TicketSystemTestCase(unittest.TestCase):
 
         self.assertEqual(['leave'], self._get_actions({'status': 'reopened'}))
         self.assertEqual(['leave'], self._get_actions({'status': 'closed'}))
 
+    def test_can_add_raw_fields_from_field_providers(self):
+        testFieldProvider = self.env[TestFieldProvider]
+        self.assertIsNotNone(testFieldProvider)
+        testFieldProvider.raw_fields = [
+            {
+                'name': "test_name",
+                'type': 'some_type',
+                'label': "some_label",
+            },
+        ]
+        fields = TicketSystem(self.env).get_ticket_fields()
+        row_added_fields = [
+            field for field in fields if field["name"] == "test_name"]
+        self.assertEqual(1, len(row_added_fields))
+
+    def test_does_not_add_duplicated_raw_fields_from_field_providers(self):
+        testFieldProvider = self.env[TestFieldProvider]
+        self.assertIsNotNone(testFieldProvider)
+        testFieldProvider.raw_fields = [
+            {
+                'name': "test_name",
+                'type': 'some_type1',
+                'label': "some_label1",
+            },
+            {
+                'name': "test_name",
+                'type': 'some_type2',
+                'label': "some_label2",
+            },
+        ]
+        fields = TicketSystem(self.env).get_ticket_fields()
+        row_added_fields = [
+            field for field in fields if field["name"] == "test_name"]
+        self.assertEqual(1, len(row_added_fields))
+
 
 def suite():
     return unittest.makeSuite(TicketSystemTestCase, 'test')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.