Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recursive applying of string format to reference fields #326

Open
RekGRpth opened this issue Feb 2, 2016 · 8 comments
Open

Recursive applying of string format to reference fields #326

RekGRpth opened this issue Feb 2, 2016 · 8 comments
Labels

Comments

@RekGRpth
Copy link

RekGRpth commented Feb 2, 2016

diff --git a/gluon/validators.py b/gluon/validators.py
index 6e471b7..96f6342 100644
--- a/gluon/validators.py
+++ b/gluon/validators.py
@@ -24,6 +24,9 @@ from cStringIO import StringIO
 from gluon.utils import simple_hash, web2py_uuid, DIGEST_ALG_BY_SIZE
 from pydal.objects import FieldVirtual, FieldMethod

+from pydal.helpers.methods import _fieldformat
+
+
 regex_isint = re.compile('^[+-]?\d+$')

 JSONErrors = (NameError, TypeError, ValueError, AttributeError,
@@ -556,6 +559,14 @@ class IS_IN_DB(Validator):
         if self._and:
             self._and.record_id = id

+    def labelize(self, r):
+        if isinstance(self.label, str):
+            for n, v in r.items():
+                if hasattr(v, '_table'):
+                    r[n] = _fieldformat(v._table, v)
+            return self.label % r
+        return self.label(r)
+
     def build_set(self):
         table = self.dbset.db[self.ktable]
         if self.fields == 'all':
@@ -580,10 +591,7 @@ class IS_IN_DB(Validator):
             dd = dict(orderby=orderby, cache=self.cache, cacheable=True)
             records = self.dbset(table).select(table.ALL, **dd)
         self.theset = [str(r[self.kfield]) for r in records]
-        if isinstance(self.label, str):
-            self.labels = [self.label % r for r in records]
-        else:
-            self.labels = [self.label(r) for r in records]
+        self.labels = [self.labelize(r) for r in records]

     def options(self, zero=True):
         self.build_set()
diff --git a/pydal/helpers/methods.py b/pydal/helpers/methods.py
index 7c1d184..1111009 100644
--- a/pydal/helpers/methods.py
+++ b/pydal/helpers/methods.py
@@ -250,6 +250,9 @@ def _fieldformat(r, id):
     if not row:
         return str(id)
     elif hasattr(r, '_format') and isinstance(r._format, str):
+        for n, v in row.items():
+            if hasattr(v, '_table'):
+                row[n] = _fieldformat(v._table, v)
         return r._format % row
     elif hasattr(r, '_format') and callable(r._format):
         return r._format(row)
@RekGRpth RekGRpth changed the title Recursive applying of string filter to reference fields Recursive applying of string format to reference fields Feb 2, 2016
@niphlod
Copy link
Member

niphlod commented Feb 2, 2016

you should open bugs that show the actual bug and then do a PR with - possibly - tests to highlight what these fixes are for. Frankly it's absolutely not understandable what is the bug you're trying to fix.

@RekGRpth
Copy link
Author

RekGRpth commented Feb 2, 2016

it is not a bug, it is an add new feature

@niphlod
Copy link
Member

niphlod commented Feb 2, 2016

what feature then ?

@RekGRpth
Copy link
Author

RekGRpth commented Feb 2, 2016

let table "opf" has format '%(name)s' and table "company" has reference to "opf" and format '%(opf)s "%(name)s"'
by default, company row represented as "1 something"
where "1" is id of "opf" row and "something" - is name of "company" row
with my code it represented as "test something"
where "test" is name of "opf" row

@niphlod
Copy link
Member

niphlod commented Feb 2, 2016

still not getting it. Are you changing a default behaviour ?

@RekGRpth
Copy link
Author

RekGRpth commented Feb 2, 2016

improved, not changed

@niphlod
Copy link
Member

niphlod commented Feb 2, 2016

so it's not a new feature.
it's an enhancement of an existing feature.
But you're changing the default representation of records ?

@gi0baro
Copy link
Member

gi0baro commented Feb 2, 2016

@RekGRpth this is also a mixed proposal for both web2py and pydal. I would be happy to see 2 PRs one referring each other.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants