def get_or_create(self, pk, *args, **kwargs): try: return self.get(pk), False except ObjectDoesNotExist: user = User(pk=pk, store=self, *args, **kwargs) self.save(user) return user, True
def get(self, pk): ds = self.db.collection('users').document(str(pk)).get() if not ds.exists: raise ObjectDoesNotExist("Object does not exist") data = ds.to_dict() data['store'] = self return User.from_dict(data)
def get(self, pk): try: with open(self.filename, 'r') as f: data = json.load(f) except FileNotFoundError: raise ObjectDoesNotExist() data['store'] = self return User.from_dict(data)
def save(self, instance: User): with open(self.filename, 'w') as f: json.dump(instance.to_dict(), f, indent=2)
_LOGGER.info('New apartment amount: %s', len(new_apartments)) for notifier in self.notifiers: _LOGGER.info('Notifying with %s', notifier.__class__.__name__) notifier.notify(new_apartments) _LOGGER.info("Saving results") self.user.last_check_datetime = current_check_datetime self.user.save() _LOGGER.info('Finished...') if __name__ == '__main__': from apartment_notifier.stores import JsonFileStore, FireStore, ObjectDoesNotExist from telegram_bot_mini.bot_api import TelegramBotApi logging.config.dictConfig(settings.LOGGING) # store = JsonFileStore(settings.store) store = FireStore() pk = 0 try: user = store.get(pk) except ObjectDoesNotExist: print('Object does not exist. Create new:') chat_id = int(input('chat_id: ')) onlinerby_url = input('onlinerby_url: ').strip() user = User(pk, chat_id, onlinerby_url=onlinerby_url, store=store) user.save() print(user) tg_bot_api = TelegramBotApi(settings.telegram_api_key) runner = Runner(settings, user, tg_bot_api) runner.run()
def all(self): for document_snapshot in self.db.collection('users').stream(): data = document_snapshot.to_dict() data['store'] = self yield User.from_dict(data)
def save(self, instance: User): data = instance.to_dict() self.db.collection('users').document(str(instance.pk)).set(data)