-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
`User` is a simple way to define an actual user as a dictionary which is storable in a database
- Loading branch information
Showing
5 changed files
with
97 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
### Documentation | ||
|
||
- Add a features list | ||
- Add a features list ([e0155add9888f3efc6b2113701f512265c54b4ac](https://github.com/hearot/pyrubrum/commit/e0155add9888f3efc6b2113701f512265c54b4ac)) | ||
|
||
### Fixes | ||
|
||
- Create `_static/examples` directory ([13c734b3fef563821e39993100c06f32e49f1aef](https://github.com/hearot/pyrubrum/commit/13c734b3fef563821e39993100c06f32e49f1aef)) | ||
- Do not raise `UnboundLocalError` anymore ([ce46b4f4a094cede204f01275fa9973b4a90e0cc](https://github.com/hearot/pyrubrum/commit/ce46b4f4a094cede204f01275fa9973b4a90e0cc)) | ||
- Keep backwards compatibility for `sphinx` ([f6a5e9c3163c619abe5dc199545879bd4d0ddccf](https://github.com/hearot/pyrubrum/commit/f6a5e9c3163c619abe5dc199545879bd4d0ddccf)) | ||
- Set `master_doc` to `index` ([0042b2e78e1e9f4b299fb81eb8eb167e4b8287d4](https://github.com/hearot/pyrubrum/commit/0042b2e78e1e9f4b299fb81eb8eb167e4b8287d4)) | ||
|
||
### New features | ||
|
||
- Add `User` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Pyrubrum - An intuitive framework for creating Telegram bots | ||
# Copyright (C) 2020 Hearot <https://github.com/hearot> | ||
# | ||
# This file is part of Pyrubrum. | ||
# | ||
# Pyrubrum is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Lesser General Public License as published | ||
# by the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# Pyrubrum is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Lesser General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Lesser General Public License | ||
# along with Pyrubrum. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
from .user import User # noqa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Pyrubrum - An intuitive framework for creating Telegram bots | ||
# Copyright (C) 2020 Hearot <https://github.com/hearot> | ||
# | ||
# This file is part of Pyrubrum. | ||
# | ||
# Pyrubrum is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published | ||
# by the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# Pyrubrum is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with Pyrubrum. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
from typing import Any | ||
from typing import Dict | ||
|
||
from pyrogram import Client | ||
|
||
from pyrubrum.database.base_database import BaseDatabase | ||
from pyrubrum.database.errors.not_found_error import NotFoundError | ||
|
||
try: | ||
import orjson as json # noqa | ||
except (ImportError, ModuleNotFoundError): | ||
import json | ||
|
||
|
||
class User(dict): | ||
def delete(self, database: BaseDatabase): | ||
database.delete(self["user_id"]) | ||
|
||
@classmethod | ||
def get(cls, database: BaseDatabase, user_id: int) -> "User": | ||
return cls.parse(database.get("user_" + str(user_id))) | ||
|
||
@classmethod | ||
def get_or_create(cls, database: BaseDatabase, user_id: int) -> "User": | ||
try: | ||
return cls.parse(database.get("user_" + str(user_id))) | ||
except NotFoundError: | ||
user = cls({"user_id": user_id}) | ||
user.save(database) | ||
return user | ||
|
||
@classmethod | ||
def parse(cls, data: str) -> "User": | ||
return cls(json.loads(data)) | ||
|
||
@classmethod | ||
def preliminary( | ||
cls, | ||
handler: "ParameterizedBaseHandler", # noqa | ||
client: Client, | ||
context: Any, | ||
parameters: Dict[str, Any] = None, | ||
): | ||
context.current_user = cls.get_or_create( | ||
handler.database, context.from_user.id | ||
) | ||
|
||
def save(self, database: BaseDatabase): | ||
database.set("user_" + str(self["user_id"]), json.dumps(self)) |