Skip to content

Commit

Permalink
feat: add User
Browse files Browse the repository at this point in the history
`User` is a simple way to define an actual user as a dictionary which is storable in a database
  • Loading branch information
hearot committed Jun 22, 2020
1 parent e0155ad commit 17c92f7
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 2 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

### Documentation

- Add a features list
- Add a features list ([e0155add9888f3efc6b2113701f512265c54b4ac](https://github.com/hearot/pyrubrum/commit/e0155add9888f3efc6b2113701f512265c54b4ac))

### Fixes

Expand All @@ -13,6 +13,10 @@
- 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`

## v0.1a2 - 2020-06-19

### Fixes
Expand Down
6 changes: 5 additions & 1 deletion FEATURES.md
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`
1 change: 1 addition & 0 deletions pyrubrum/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from .handlers import ParameterizedHandler # noqa
from .handlers import pass_handler # noqa
from .handlers import pass_parameterized_handler # noqa
from .objects import User # noqa
from .keyboard import Button # noqa
from .keyboard import Element # noqa
from .keyboard import Keyboard # noqa
Expand Down
19 changes: 19 additions & 0 deletions pyrubrum/objects/__init__.py
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
67 changes: 67 additions & 0 deletions pyrubrum/objects/user.py
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))

0 comments on commit 17c92f7

Please sign in to comment.