You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
2.0KB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2019 Giacomo Ferretti
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import json
  16. import logging
  17. import os
  18. __logger__ = logging.getLogger(__name__)
  19. __users_file__ = 'users.json'
  20. class Users:
  21. def __init__(self):
  22. self.__users__ = {}
  23. # Load saved users
  24. if os.path.isfile(__users_file__) and os.stat(__users_file__).st_size != 0:
  25. __logger__.info('Loading "{}"...'.format(__users_file__))
  26. with open(__users_file__) as f:
  27. self.__users__ = json.loads(f.read())
  28. __logger__.info('Loaded {} users.'.format(len(self.__users__)))
  29. else:
  30. __logger__.warning('"{}" not found or empty.'.format(__users_file__))
  31. def __save_users(self):
  32. with open(__users_file__, 'w') as f:
  33. f.write(json.dumps(self.__users__, indent=2))
  34. def get_users(self):
  35. return self.__users__
  36. def add_user(self, user):
  37. user_dict = {
  38. 'firstName': user.get('first_name', ''),
  39. 'lastName': user.get('last_name', ''),
  40. 'username': user.get('username', ''),
  41. 'languageCode': user.get('language_code', ''),
  42. 'active': True,
  43. 'offersGenerated': 0
  44. }
  45. self.__users__[str(user['id'])] = user_dict
  46. self.__save_users()
  47. def remove_user(self, id_):
  48. self.__users__.pop(str(id_))
  49. self.__save_users()
  50. def to_dict(self):
  51. return self.__users__