def login(self): """ :rtype: LoginResult """ with DBSession() as db: cred = db.query(self.credentials_class).first() if not cred: return LoginResult.CredentialsNotSpecified username = cred.username password = cred.password if not username or not password: return LoginResult.CredentialsNotSpecified try: self.tracker.login(username, password) with DBSession() as db: cred = db.query(self.credentials_class).first() cred.c_uid = self.tracker.c_uid cred.c_pass = self.tracker.c_pass cred.c_usess = self.tracker.c_usess return LoginResult.Ok except LostFilmTVLoginFailedException as e: if e.code == 6: return LoginResult.IncorrentLoginPassword return LoginResult.Unknown except Exception: # TODO: Log unexpected excepton return LoginResult.Unknown
def test_execute_reset_status_and_download(self, download, torrent_mock): def download_func(request, **kwargs): self.assertEqual(12, kwargs['timeout']) response = Response() response._content = br"d9:" response.status_code = 200 return response, request[1] engine_tracker, _, _, engine_downloads = self.create_engine_tracker() last_update = datetime.now(pytz.utc) engine_downloads.add_torrent.return_value = last_update download.side_effect = download_func torrent = torrent_mock.return_value torrent.info_hash = 'HASH1' with DBSession() as db: topic1 = self.ExecuteMockTopic(display_name='Russian / English', url='http://mocktracker2.com/1', additional_attribute='English', hash='OLDHASH', status=Status.Error) db.add(topic1) db.commit() topic1_id = topic1.id db.expunge_all() plugin = self.MockTrackerPlugin() plugin.init(TrackerSettings(12, None)) plugin.execute(plugin.get_topics(None), engine_tracker) with DBSession() as db: topic = db.query(self.ExecuteMockTopic).filter(self.ExecuteMockTopic.id == topic1_id).first() self.assertEqual(topic.last_update, last_update) self.assertEqual(topic.status, Status.Ok) self.assertEqual(topic.hash, 'HASH1')
def get_settings(self): with DBSession() as db: db_settings = db.query(self.settings_class).first() if db_settings is None: return None db.expunge_all() return db_settings
def get_execute_log_details(self, execute_id, after=None): with DBSession() as db: filters = [ExecuteLog.execute_id == execute_id] if after is not None: filters.append(ExecuteLog.id > after) log_entries = db.query(ExecuteLog).filter(*filters).all() return [row2dict(e) for e in log_entries]
def get_log_entries(self, skip, take): with DBSession() as db: downloaded_sub_query = db.query(ExecuteLog.execute_id, func.count(ExecuteLog.id).label('count'))\ .group_by(ExecuteLog.execute_id, ExecuteLog.level)\ .having(ExecuteLog.level == 'downloaded')\ .subquery() failed_sub_query = db.query(ExecuteLog.execute_id, func.count(ExecuteLog.id).label('count'))\ .group_by(ExecuteLog.execute_id, ExecuteLog.level)\ .having(ExecuteLog.level == 'failed')\ .subquery() result_query = db.query(Execute, downloaded_sub_query.c.count, failed_sub_query.c.count)\ .outerjoin(failed_sub_query, Execute.id == failed_sub_query.c.execute_id)\ .outerjoin(downloaded_sub_query, Execute.id == downloaded_sub_query.c.execute_id)\ .order_by(Execute.finish_time.desc())\ .offset(skip)\ .limit(take) result = [] for execute, downloads, fails in result_query.all(): execute_result = row2dict(execute) execute_result['downloaded'] = downloads or 0 execute_result['failed'] = fails or 0 execute_result['is_running'] = execute.id == self._execute_id result.append(execute_result) execute_count = db.query(func.count(Execute.id)).scalar() return result, execute_count
def test_get_topics_by_id(self): plugin = MockTrackerPlugin() plugin.topic_private_fields = plugin.topic_private_fields + ['additional_attribute'] plugin.topic_class = self.MockTopic all_topics = [] for i in range(1, 10): with DBSession() as db: topic_fields = { 'url': 'http://base.mocktracker.org/torrent/{0}'.format(i), 'display_name': 'Original Name / Translated Name / Info {0}'.format(i), 'additional_attribute': 'Text {0}'.format(i), 'type': 'base.mocktracker.com', } new_topic = self.MockTopic(**topic_fields) db.add(new_topic) db.commit() topic_fields['id'] = new_topic.id all_topics.append(topic_fields) # get all topics topics = plugin.get_topics(None) self.assertEqual(len(topics), len(all_topics)) # get first half of topics half_topics = all_topics[:int(len(all_topics) / 2)] topics = plugin.get_topics([t['id'] for t in half_topics]) self.assertEqual(len(topics), len(half_topics)) # get second half of topics half_topics = all_topics[int(len(all_topics) / 2):] topics = plugin.get_topics([t['id'] for t in half_topics]) self.assertEqual(len(topics), len(half_topics))
def set_topic_paused(self, id, paused): with DBSession() as db: topic = db.query(Topic).filter(Topic.id == id).first() if topic is None: raise KeyError('Topic {} not found'.format(id)) topic.paused = paused return True
def test_remove_topic_2(self): with self.assertRaises(KeyError): self.trackers_manager.remove_topic(self.tracker1_id1 + 1) with DBSession() as db: topic = db.query(Topic).filter( Topic.id == self.tracker1_id1).first() self.assertIsNotNone(topic)
def _log_entry(self, message, level): with DBSession() as db: execute_log = ExecuteLog(execute_id=self._execute_id, time=datetime.now(pytz.utc), message=message, level=level) db.add(execute_log)
def get_watching_topics(self): watching_topics = [] with DBSession() as db: dbtopics = db.query(Topic).all() for dbtopic in dbtopics: try: tracker = self.get_tracker(dbtopic.type) except KeyError: # TODO: Log warning of not existing topic # Need to think, should we return not existing plugin # as just default topic, and show it disabled on UI to # let user ability for delete such topics continue topic = row2dict( dbtopic, None, ['id', 'url', 'display_name', 'last_update', 'paused']) topic['info'] = tracker.get_topic_info(dbtopic) topic['tracker'] = dbtopic.type topic['status'] = dbtopic.status.__str__() if dbtopic.type == 'lostfilm.tv': topic['external_id'] = db.query( tracker.topic_class).filter( tracker.topic_class.id == dbtopic.id).first().cat watching_topics.append(topic) return watching_topics
def get_status_topics_ids(self, statuses): with DBSession() as db: ids = [ res.id for res in db.query(Topic.id).filter( Topic.status.in_(statuses)) ] return ids
def _set_settings(name, value): with DBSession() as db: setting = db.query(Settings).filter(Settings.name == name).first() if not setting: setting = Settings(name=name) db.add(setting) setting.value = str(value)
def _get_settings(name, default=None): with DBSession() as db: setting = db.query(Settings).filter(Settings.name == name).first() if not setting: return default # this is right convert from string to bool return setting.value or default
def update_settings(self, settings): settings = settings if isinstance(settings, dict) else settings.__dict__ settings = { k: v for (k, v) in six.iteritems(settings) if k in self.settings_fields } remove = all([ v is None or v == "" or v == 0 or v == False for v in six.itervalues(settings) ]) with DBSession() as db: dbsettings = db.query(self.settings_class).first() if dbsettings is None and not remove: dbsettings = self.settings_class() db.add(dbsettings) if dbsettings is not None: if remove: db.delete(dbsettings) else: settings[ 'is_enabled'] = dbsettings.is_enabled if dbsettings.is_enabled is not None else True dict2row(dbsettings, settings) return True
def update_topic(self, id, params): with DBSession() as db: topic = db.query(self.topic_class).filter(Topic.id == id).first() if topic is None: return False self._set_topic_params(None, None, topic, params) return True
def remove_topic(self, id): with DBSession() as db: topic = db.query(Topic).filter(Topic.id == id).first() if topic is None: raise KeyError('Topic {} not found'.format(id)) db.delete(topic) return True
def setUp(self): super(TrackersManagerDbPartTest, self).setUp() with DBSession() as db: topic = Tracker1Topic(display_name=self.DISPLAY_NAME1, url=self.URL1, type=TRACKER1_PLUGIN_NAME, some_addition_field=1) db.add(topic) db.commit() self.tracker1_id1 = topic.id self.tracker1 = Tracker1() self.tracker2 = Tracker2() tracker_settings = TrackerSettings(10, None) settings_manager = Mock() settings_manager.tracker_settings = tracker_settings # noinspection PyTypeChecker self.trackers_manager = TrackersManager( settings_manager, { TRACKER1_PLUGIN_NAME: self.tracker1, TRACKER2_PLUGIN_NAME: self.tracker2, })
def reset_topic_status(self, id): with DBSession() as db: topic = db.query(Topic).filter(Topic.id == id).first() if topic is None: raise KeyError('Topic {} not found'.format(id)) topic.status = Status.Ok return True
def set_settings(self, settings): with DBSession() as db: cred = db.query(DownloaderSettings).first() if not cred: cred = DownloaderSettings() db.add(cred) cred.path = settings['path']
def get_credentials(self): with DBSession() as db: dbcredentials = db.query(self.credentials_class).first() if dbcredentials is None: return None return row2dict(dbcredentials, None, self.credentials_public_fields)
def test_save_topic(self, last_update, status): plugin = MockTrackerPlugin() plugin.topic_private_fields = plugin.topic_private_fields + ['additional_attribute'] plugin.topic_class = self.MockTopic original_url = 'http://base.mocktracker.org/torrent/1' topic_last_update = datetime(2016, 3, 14, 18, 58, 12, tzinfo=pytz.utc) fields = { 'url': original_url, 'display_name': 'Original Name / Translated Name / Info', 'additional_attribute': 'Text', 'type': 'base.mocktracker.com', 'status': Status.Ok, 'last_update': topic_last_update } with DBSession() as db: topic = self.MockTopic(**fields) db.add(topic) db.commit() fields['id'] = topic.id topic = plugin.get_topics(None)[0] plugin.save_topic(topic, last_update, status) topic = plugin.get_topics(None)[0] self.assertEqual(last_update or topic_last_update, topic.last_update) self.assertEqual(status, topic.status)
def test_get_topic(self): plugin = MockTrackerPlugin() plugin.topic_class = self.MockTopic fields = { 'url': u'http://base.mocktracker.org/torrent/1', 'display_name': u'Original Name / Translated Name / Info', 'additional_attribute': u'Text', 'type': 'base.mocktracker.com', } with DBSession() as db: topic = self.MockTopic(**fields) db.add(topic) db.commit() fields['id'] = topic.id result = plugin.get_topic(fields['id']) expected = { 'id': fields['id'], 'display_name': fields['display_name'], 'url': fields['url'], 'last_update': None, 'info': None, 'status': Status.Ok, 'download_dir': None } self.assertEqual(expected, result) # noinspection PyTypeChecker self.assertIsNone(plugin.get_topic(fields['id'] + 1))
def _update_execute_settings(self): with DBSession() as db: settings_execute = db.query(ExecuteSettings).first() if not settings_execute: settings_execute = ExecuteSettings() db.add(settings_execute) settings_execute.interval = self._interval settings_execute.last_execute = self._last_execute
def get_topic(self, id): with DBSession() as db: topic = db.query(self.topic_class).filter(Topic.id == id).first() if topic is None: return None data = row2dict(topic, None, self.topic_public_fields) data['info'] = self.get_topic_info(topic) return data
def update_credentials(self, credentials): with DBSession() as db: dbcredentials = db.query(self.credentials_class).first() if dbcredentials is None: dbcredentials = self.credentials_class() db.add(dbcredentials) dict2row(dbcredentials, credentials, self.credentials_private_fields)
def _get_execute_settings(self): with DBSession() as db: settings_execute = db.query(ExecuteSettings).first() if not settings_execute: settings_execute = ExecuteSettings( interval=self.DEFAULT_INTERVAL, last_execute=None) else: db.expunge(settings_execute) return settings_execute
def set_settings(self, settings): with DBSession() as db: cred = db.query(QBittorrentCredentials).first() if not cred: cred = QBittorrentCredentials() db.add(cred) cred.host = settings['host'] cred.port = settings.get('port', None) cred.username = settings.get('username', None) cred.password = settings.get('password', None)
def get_settings(self): with DBSession() as db: cred = db.query(QBittorrentCredentials).first() if not cred: return None return { 'host': cred.host, 'port': cred.port, 'username': cred.username }
def log_entry(self, message, level): if self._execute_id is None: raise Exception('Execute is not started') with DBSession() as db: execute_log = ExecuteLog(execute_id=self._execute_id, time=datetime.now(pytz.utc), message=message, level=level) db.add(execute_log)
def _get_client(self): with DBSession() as db: cred = db.query(DelugeCredentials).first() if not cred: return False if not cred.port: cred.port = self.DEFAULT_PORT return DelugeRPCClient(cred.host, cred.port, cred.username, cred.password)