Advertisement
Guest User

user.py

a guest
Aug 19th, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.81 KB | None | 0 0
  1. # MIT License
  2. #
  3. # Copyright (c) 2017 matteo bocci aka matteob99
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in all
  13. # copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22.  
  23. from datetime import datetime as dt
  24. import redis
  25. from dateutil.parser import parse
  26. import config
  27.  
  28. r = redis.StrictRedis(host=config.REDIS_HOST, port=config.REDIS_PORT,
  29.                       db=config.REDIS_DB, password=config.REDIS_PASSWORD)
  30.  
  31.  
  32. class User:
  33.     """
  34.    User base object
  35.    """
  36.  
  37.     def __init__(self, user):
  38.         """
  39.        Create an user object
  40.        :param user: Telegram's user object
  41.        """
  42.         self.id = user.id
  43.         self.rhash = 'user:' + str(user.id)
  44.  
  45.         # Redis database
  46.         if r.hget(self.rhash, 'id') != user.id:
  47.             r.hset(self.rhash, 'id', user.id)
  48.         if r.hget(self.rhash, 'username') != user.username:
  49.             r.hset(self.rhash, 'username', user.username)
  50.         if r.hget(self.rhash, 'first_name') != user.first_name:
  51.             r.hset(self.rhash, 'first_name', user.first_name)
  52.         if r.hget(self.rhash, 'last_name') != user.last_name:
  53.             r.hset(self.rhash, 'last_name', user.last_name)
  54.         r.hset(self.rhash, 'last_update', dt.now())
  55.         if not self.state():
  56.             r.hset(self.rhash, 'state', 'home')
  57.         if not self.stateold():
  58.             r.hset(self.rhash, 'stateold', 'home')
  59.         if not self.lastr():
  60.             r.hset(self.rhash, "lastr", 0)
  61.         if not self.idrichiesta():
  62.             r.hset(self.rhash, "idrichiesta", 0)
  63.         r.hset(self.rhash, "active", True)
  64.  
  65.     def state(self, new_state=None):
  66.         """
  67.        Get current user state or set a new user state
  68.        :param new_state: new state for the user
  69.        :return: state
  70.        """
  71.         if not new_state:
  72.             return r.hget(self.rhash, 'state')
  73.         self.stateold(self.state())
  74.         r.hset(self.rhash, 'state', new_state)
  75.         return True
  76.  
  77.     def stateold(self, new_stateold=None):
  78.         if not new_stateold:
  79.             return r.hget(self.rhash, 'state')
  80.         r.hset(self.rhash, 'stateold', new_stateold)
  81.         return True
  82.  
  83.     def lastr(self, time=None):
  84.         if not time:
  85.             return r.hget(self.rhash, "lastr")
  86.  
  87.         r.hset(self.rhash, "lastr", dt.now())
  88.  
  89.     def idrichiesta(self, idrichiesta=None):
  90.         if not idrichiesta:
  91.             return r.hget(self.rhash, "idrichiesta")
  92.         r.hset(self.rhash, "idrichiesta", idrichiesta)
  93.  
  94.     def setRedis(self, key, value):
  95.         """
  96.        Set a redis value
  97.        :param key: redis key
  98.        :param value: redis value
  99.        :return: value
  100.        """
  101.         return r.hset(self.rhash, key, value)
  102.  
  103.     def getRedis(self, key):
  104.         """
  105.        Get a redis value
  106.        :param key: redis key
  107.        :return: value
  108.        """
  109.         return r.hget(self.rhash, key)
  110.  
  111.     def delRedis(self, key):
  112.         """
  113.        Delete a redis value
  114.        :param key: redis key
  115.        :return: None
  116.        """
  117.         return r.hdel(self.rhash, key)
  118.  
  119.     def increaseStat(self, stat):
  120.         """
  121.        Increase a stat value
  122.        :param stat: which stat increase
  123.        :return: redis response
  124.        """
  125.         response = r.hincrby(self.rhash, stat)
  126.         return response
  127.  
  128.     def isActive(self):
  129.         return bool(r.hget(self.rhash, "active")) or False
  130.  
  131.     def first_name(self):
  132.         r.hget(self.rhash, 'first_name')
  133.  
  134.     def last_name(self):
  135.         r.hget(self.rhash, 'last_name')
  136.  
  137.     def userrequest(self, idrequest=None):
  138.         if not idrequest:
  139.             return False
  140.         self.rhash = 'user:' + str(idrequest)
  141.         returnv = [r.hget(self.rhash, 'id'),
  142.                    r.hget(self.rhash, 'username'),
  143.                    r.hget(self.rhash, 'first_name'),
  144.                    r.hget(self.rhash, 'last_name')]
  145.         return returnv
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement