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 Aug 20, 2023
2 parents c9b5f7c + cf1e6a6 commit 2322563
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ We would be very glad if you contributed to the project in one or all of these w

## License

Emmmett is released under the BSD License.
Emmett is released under the BSD License.

However, due to original license limitations, some components are included
in Emmett under their original licenses. Please check the LICENSE file for
Expand Down
33 changes: 32 additions & 1 deletion docs/auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,38 @@ event.user

### Customizing auth models

*section in development*
*Changed in version 2.5*

By extending the models provided by the `Auth` module, you can easily customise the fields depending on your needs.

Once you defined your own models, you can provide them to the `Auth` class in the same way we saw for the user:

```python
from emmett.orm import Field
from emmett.tools.auth import Auth
from emmett.tools.auth.models import AuthUser, AuthGroup, AuthMembership, AuthPermission

class User(AuthUser):
pass

class Group(AuthGroup):
extra_field = Field.string()

class Membership(AuthMembership):
pass

class Permission(AuthPermission):
pass

auth = Auth(
app,
db,
user_model=User,
group_model=Group,
membership_model=Membership,
permission_model=Permission
)
```

Users management
---------------
Expand Down
2 changes: 1 addition & 1 deletion emmett/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "2.5.3"
__version__ = "2.5.4"
28 changes: 23 additions & 5 deletions emmett/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,15 +482,33 @@ def widget_float(attr, field, value, _class="float", _id=None):

@staticmethod
def widget_date(attr, field, value, _class="date", _id=None):
return FormStyle.widget_string(attr, field, value, _class, _id)
return tag.input(
_type="date",
_name=field.name,
_value=value if value is not None else "",
_class=_class,
_id=_id or field.name
)

@staticmethod
def widget_time(attr, field, value, _class="time", _id=None):
return FormStyle.widget_string(attr, field, value, _class, _id)
return tag.input(
_type="time",
_name=field.name,
_value=value if value is not None else "",
_class=_class,
_id=_id or field.name
)

@staticmethod
def widget_datetime(attr, field, value, _class="datetime", _id=None):
return FormStyle.widget_string(attr, field, value, _class, _id)
return tag.input(
_type="datetime-local",
_name=field.name,
_value=value if value is not None else "",
_class=_class,
_id=_id or field.name
)

@staticmethod
def widget_password(attr, field, value, _class="password", _id=None):
Expand Down Expand Up @@ -548,9 +566,9 @@ def selected(k):
)

@staticmethod
def widget_list(field, value):
def widget_list(field, value, _id=None):
options, _ = FormStyle._field_options(field)
return FormStyle.widget_multiple(None, field, value, options)
return FormStyle.widget_multiple(None, field, value, options, _id=_id)

@staticmethod
def widget_upload(attr, field, value, _class="upload", _id=None):
Expand Down
13 changes: 9 additions & 4 deletions emmett/orm/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -860,18 +860,21 @@ def _row_record_query_pks(self, row):

def __define_query_helpers(self):
if not self.primary_keys:
self._query_id = self.table.id != None
self._query_id = self.table.id != None # noqa
self._query_row = self._row_record_query_id
self._order_by_id_asc = self.table.id
self._order_by_id_desc = ~self.table.id
elif len(self.primary_keys) == 1:
self._query_id = self.table[self.primary_keys[0]] != None
self._query_id = self.table[self.primary_keys[0]] != None # noqa
self._query_row = self._row_record_query_pk
self._order_by_id_asc = self.table[self.primary_keys[0]]
self._order_by_id_desc = ~self.table[self.primary_keys[0]]
else:
self._query_id = reduce(
operator.and_, [self.table[key] != None for key in self.primary_keys]
operator.and_, [
self.table[key] != None # noqa
for key in self.primary_keys
]
)
self._query_row = self._row_record_query_pks
self._order_by_id_asc = reduce(
Expand Down Expand Up @@ -1151,7 +1154,9 @@ def __init__(self, db, relation_data):
self.field = relation_data.name

def __set__(self, obj, val):
if val is not None and not isinstance(val, RowReferenceMixin):
if not val:
val = None
elif val and not isinstance(val, RowReferenceMixin):
if isinstance(val, StructuredRow):
val = typed_row_reference_from_record(val, val._model)
else:
Expand Down
14 changes: 12 additions & 2 deletions emmett/tools/auth/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,20 @@


class Auth:
def __init__(self, app, db, user_model=None):
def __init__(
self,
app,
db,
user_model=None,
group_model=None,
membership_model=None,
permission_model=None
):
self.ext = app.use_extension(AuthExtension)
self.ext.bind_auth(self)
self.ext.use_database(db, user_model)
self.ext.use_database(
db, user_model, group_model, membership_model, permission_model
)
self.ext.init_forms()
self.pipe = AuthPipe(self)

Expand Down
31 changes: 25 additions & 6 deletions emmett/tools/auth/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,32 @@ def _inject_pipe(self):
if self.config.inject_pipe:
self.app.pipeline.append(self.auth.pipe)

def use_database(self, db, user_model=None):
def __set_model_for_key(self, key, model):
if not model:
return
_model_bases = {
'user': AuthModel,
'group': AuthGroup,
'membership': AuthMembership,
'permission': AuthPermission
}
if not issubclass(model, _model_bases[key]):
raise RuntimeError(f'{model.__name__} is an invalid {key} auth model')
self.config.models[key] = model

def use_database(
self,
db,
user_model=None,
group_model=None,
membership_model=None,
permission_model=None
):
self.db = db
if user_model:
if not issubclass(user_model, AuthModel):
raise RuntimeError(
'%s is an invalid user model' % user_model.__name__)
self.config.models['user'] = user_model
self.__set_model_for_key("user", user_model)
self.__set_model_for_key("group", group_model)
self.__set_model_for_key("membership", membership_model)
self.__set_model_for_key("permission", permission_model)
self.define_models()

def __set_models_labels(self):
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.5.3"
version = "2.5.4"
description = "The web framework for inventors"
authors = ["Giovanni Barillari <[email protected]>"]
license = "BSD-3-Clause"
Expand Down

0 comments on commit 2322563

Please sign in to comment.