Skip to content

Commit

Permalink
Merge branch 'master' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
gi0baro committed May 23, 2022
2 parents 2027454 + 5cd6220 commit 43f5752
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 4 deletions.
2 changes: 1 addition & 1 deletion emmett/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "2.4.9"
__version__ = "2.4.10"
10 changes: 9 additions & 1 deletion emmett/orm/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from pydal.representers import TReprMethodWrapper, for_type as _representer_for_type

from .engines import adapters
from .helpers import GeoFieldWrapper, typed_row_reference
from .helpers import GeoFieldWrapper, PasswordFieldWrapper, typed_row_reference
from .objects import Expression, Field, Row, IterRows


Expand Down Expand Up @@ -100,6 +100,10 @@ def patch_dialect(dialect):


def patch_parser(dialect, parser):
parser.registered['password'] = ParserMethodWrapper(
parser,
_parser_for_type('password')(_parser_password).f
)
parser.registered['reference'] = ParserMethodWrapper(
parser,
_parser_for_type('reference')(_parser_reference).f,
Expand Down Expand Up @@ -377,6 +381,10 @@ def _parser_geo(parser, value):
return GeoFieldWrapper(value)


def _parser_password(parser, value):
return PasswordFieldWrapper(value)


def _representer_reference(representer, value, referenced):
rtname, _, rfname = referenced.partition('.')
rtable = representer.adapter.db[rtname]
Expand Down
4 changes: 4 additions & 0 deletions emmett/orm/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ def __json__(self):
}


class PasswordFieldWrapper(str):
_emt_field_hashed_contents_ = True


class Reference(object):
def __init__(self, *args, **params):
self.reference = [arg for arg in args]
Expand Down
2 changes: 2 additions & 0 deletions emmett/validators/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ def __call__(self, value):
length = 0
if self._between(length):
return value, None
elif getattr(value, '_emt_field_hashed_contents_', False):
return value, None
elif isinstance(value, FieldStorage):
if value.file:
value.file.seek(0, SEEK_END)
Expand Down
2 changes: 2 additions & 0 deletions emmett/validators/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ def __init__(
self.salt = salt

def __call__(self, value):
if getattr(value, '_emt_field_hashed_contents_', False):
return value, None
crypt = LazyCrypt(self, value)
if isinstance(value, LazyCrypt) and value == crypt:
return value, None
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "Emmett"
version = "2.4.9"
version = "2.4.10"
description = "The web framework for inventors"
authors = ["Giovanni Barillari <[email protected]>"]
license = "BSD-3-Clause"
Expand Down
25 changes: 24 additions & 1 deletion tests/test_orm_row.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ def _row_force_destroy(self, row):
self.super_rowmethod("destroy")(row)


class Crypted(Model):
foo = Field.string()
bar = Field.password()


@pytest.fixture(scope='module')
def _db():
app = App(__name__)
Expand All @@ -75,7 +80,7 @@ def _db():
auto_connect=True
)
)
db.define_models(One, Two, Three, Override)
db.define_models(One, Two, Three, Override, Crypted)
return db

@pytest.fixture(scope='function')
Expand Down Expand Up @@ -353,6 +358,24 @@ def test_relation_wrappers(db):
assert isinstance(r.one, RowReferenceMixin)


def test_password_wrapper(db):
db.Crypted.validate_and_insert(foo="foobar", bar="foobar")

row = Crypted.first()
crypted_value = str(row.bar)

row.save()
assert str(row.bar) == crypted_value

row.foo = "test"
row.save()
assert str(row.bar) == crypted_value

row.bar = "testtest"
row.save()
assert str(row.bar) != crypted_value


def test_pickle(db):
r1 = One.new(foo="test")
r1.save()
Expand Down

0 comments on commit 43f5752

Please sign in to comment.