Skip to content

Commit

Permalink
Created User definition for database wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Javinator9889 committed Aug 21, 2020
1 parent ac3336f commit 54587c7
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 7 deletions.
2 changes: 2 additions & 0 deletions YouTubeMDBot/database/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@
from ..database.history import HistoryDB
from ..database.stats import YouTubeStatsDB
from ..database.preferences import PreferencesDB

from ..database.redis import DatabaseWrapper
16 changes: 16 additions & 0 deletions YouTubeMDBot/database/redis/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# YouTubeMDBot
# Copyright (C) 2020 - Javinator9889
#
# This program 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.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
from .wrapper import DatabaseWrapper
73 changes: 73 additions & 0 deletions YouTubeMDBot/database/redis/user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# YouTubeMDBot
# Copyright (C) 2020 - Javinator9889
#
# This program 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.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
from typing import Optional, Any
from datetime import datetime
from .. import UserDB
from . import DatabaseWrapper


class History(DatabaseWrapper):
pass


class User(DatabaseWrapper):
def __init__(self, user_id: int):
super(User, self).__init__(name=str(user_id))
self.user_id = user_id
self._user = UserDB(self.item)

@property
def name(self) -> str:
return self._get_or_query("name")

@name.setter
def name(self, value: str):
self.delete("name")
self._user.update_username(self.user_id, name=value)

@property
def tag(self) -> Optional[str]:
return self._get_or_query("tag")

@tag.setter
def tag(self, value: Optional[str]):
self.delete("tag")
self._user.update_user_tag(self.user_id, tag=value)

@property
def lang(self) -> str:
return self._get_or_query("lang")

@lang.setter
def lang(self, value: str):
self.delete("lang")
self._user.update_user_lang(self.user_id, lang=value)

@property
def first_access(self) -> datetime:
return self._get_or_query("first_access")

@property
def history(self) -> History:
pass # TODO

def _get_or_query(self, attr: str) -> Any:
value = self.get(attr)
if value:
return value
value = self._user.get_user_information(self.user_id)[attr]
self.set(attr, value)
return value
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@
from abc import ABC
from typing import Union, Optional
from threading import Lock
from . import (
PostgreSQLItem, UserDB, PreferencesDB, MetadataDB, YouTubeDB,
YouTubeStatsDB, HistoryDB, FileDB, Initializer
)
from .. import REDIS_UNIX_SOCKET
from database import PostgreSQLItem
from YouTubeMDBot import REDIS_UNIX_SOCKET

wrapper_lock = Lock()

Expand All @@ -52,10 +49,10 @@ def __init__(self):
self.__must_initialize = False


class DatabaseWrapper:
class DatabaseWrapper(ABC):
def __init__(self, name: str, **kwargs):
super().__init__()
self.__item = PostgreSQLItem()
self.item = PostgreSQLItem()
self.__wrapper = BaseWrapper()
self.redis = self.__wrapper.redis
self.name = name
Expand Down

0 comments on commit 54587c7

Please sign in to comment.