Skip to content

Commit

Permalink
enable reference on non-integer value
Browse files Browse the repository at this point in the history
  • Loading branch information
James Burke committed Jan 19, 2017
1 parent a73a097 commit 40f0825
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
41 changes: 41 additions & 0 deletions pydal/helpers/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,47 @@ def __setitem__(self, key, value):
self.__allocate()
self._record[key] = value

class ReferenceUUID(str):
def __allocate(self):
if not self._record:
self._record = self._table[str(self)]
if not self._record:
raise RuntimeError(
"Using a recursive select but encountered a broken " +
"reference: %s %d" % (self._table, str(self))
)

def __getattr__(self, key):
if key == 'id':
return str(self)
if key in self._table:
self.__allocate()
if self._record:
# to deal with case self.update_record()
return self._record.get(key, None)
else:
return None

def get(self, key, default=None):
return self.__getattr__(key, default)

def __setattr__(self, key, value):
if key.startswith('_'):
str.__setattr__(self, key, value)
return
self.__allocate()
self._record[key] = value

def __getitem__(self, key):
if key == 'id':
return str(self)
self.__allocate()
return self._record.get(key, None)

def __setitem__(self, key, value):
self.__allocate()
self._record[key] = value


def Reference_unpickler(data):
return marshal.loads(data)
Expand Down
7 changes: 5 additions & 2 deletions pydal/parsers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from decimal import Decimal
from .._compat import PY2, integer_types, basestring, to_bytes, to_native
from ..adapters.base import SQLAdapter
from ..helpers.classes import Reference
from ..helpers.classes import Reference, ReferenceUUID
from ..helpers.methods import bar_decode_string, bar_decode_integer
from . import Parser, parsers, for_type, before_parse

Expand Down Expand Up @@ -43,7 +43,10 @@ def reference_extras(self, field_type):
@for_type('reference')
def _reference(self, value, referee):
if '.' not in referee:
value = Reference(value)
try:
value = Reference(value)
except ValueError:
value = ReferenceUUID(value)
value._table, value._record = self.adapter.db[referee], None
return value

Expand Down

0 comments on commit 40f0825

Please sign in to comment.