anomaly / anomaly/lab-python-server

Building Custom Comparator for SQLAlchemy for the password property

未关闭
#51 2 条评论 0 个 reaction 已指派 1 人 已被 @devraj 认领 在 GitHub 查看
主要语言
Python
星标
16
派生
4
PR 合并指标
30 天内没有已合并 PR

描述

Extending from the pattern that allows developers to simply set the `password` property and a setter hashes it on the way in. The `User` model class has substantial documentation on this. The general approach is to declare the property as:

```python
_password = Column("password",
String,
nullable=True)
```

followed by exposing the `password` property as:

```python
@property
def password(self):
return self._password
```

and finally overriding the setter to `hash` the password on the way in:
```python
@password.setter
def password(self, password):
self._password = hash_password(password)
```

The [Building Custom Comparator](https://docs.sqlalchemy.org/en/14/orm/extensions/hybrid.html#building-custom-comparators) guide outlines how we can override the comparator of `hybrid_property` in `SQLAlchemy`, if we adopt this paradigm then we can achieve comparing password as:

```python
user.password == password
```

as opposed using `check_password` method

贡献指南

这个仓库没有索引到贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。