def append_downloaded_articles(url, articles): """ Appends downloaded articles to database Args: url (string): url to rss articles (list): list of articles objects """ dbh = DatabaseHandler() username = CredentialsHandler.lastUsername res = dbh.get_entry(username) for i, entry in enumerate(res['urls']): if entry['actual_url'] == url: for nart in articles: addThisUrl = True for eart in entry['articles']: if eart['title'] == nart.title: addThisUrl = False break if addThisUrl: nentry = { "title": nart.title, "link": nart.link, "desc": nart.content, "pub_date": nart.pubDate, "pub_date_parsed": nart.pub_date_parsed, "seen": False, } entry['articles'].append(nentry) dbh.add_entry(username, res)
def add_url_to_group(url, group): """ Add url to selected group Args: url (string): selected url group (string): name of selected group Returns: string: Returns index of added url from url list """ dbh = DatabaseHandler() username = CredentialsHandler.lastUsername res = dbh.get_entry(username) for i, entry in enumerate(res['urls']): if entry['actual_url'] == url: if group in res['groups']: if i not in res['groups'][group]: res['groups'][group].append(i) else: return -1 else: res['groups'][group] = [i] dbh.add_entry(username, res) return i
def get_most_popular_urls(): """ Return list of most popular urls In user database that method creates group of most popular urls, before creating it checks if one exists, if there is that group method deletes it and creates it with fetched list from database Returns: tuple: tuple contains list of mostpopular urls and indexes of those urls in database """ dbh = DatabaseHandler() groups = GroupHandler() user = dbh.get_entry(CredentialsHandler.lastUsername) if URLHandler.popular_name in user["groups"]: groups.remove_group(URLHandler.popular_name) groups.add_group(URLHandler.popular_name) mostpopular = dbh.filter_list() indexes = [] for stat in mostpopular: add_to_user_urls = True url = stat[0] idx = 0 for idx, user_url in enumerate(user["urls"]): if user_url["actual_url"] == url: add_to_user_urls = False break if add_to_user_urls: idx = URLHandler.add_url(url) indexes.append(idx) URLHandler.add_url_to_group(url, URLHandler.popular_name) return mostpopular, indexes
def test_hashes_nonempty_bytes(self): db = DatabaseHandler(db_path="pytests/", dbIsTemp=True) credh = CredentialsHandler('testusername', b'nonemptytestpassword') credh.encrypt_credentials() self.assertEqual(credh.password, '3217aa106d566c22fb0f2e44af0e28d024d4fa98') DatabaseHandler.destroy_database()
def test_hashes_empty_bytes(self): db = DatabaseHandler(db_path="pytests/", dbIsTemp=True) credh = CredentialsHandler('testusername', b'') credh.encrypt_credentials() self.assertEqual(credh.password, 'da39a3ee5e6b4b0d3255bfef95601890afd80709') DatabaseHandler.destroy_database()
def refresh_feed(self, item): """Refreshes content of FeedView Args: item (QItem): Item of selected group from groupview """ dbh = DatabaseHandler() self.entry = dbh.get_entry(CredentialsHandler.lastUsername) self.set_group(item, True)
def does_user_exist(self): dbh = DatabaseHandler() result = dbh.get_entry(self.username) if result == None: return False else: return True
def test_credentials_validation(self): db = DatabaseHandler(db_path="pytests/", dbIsTemp=True) credh = CredentialsHandler('testusername', b'nonemptytestpassword') credh.encrypt_credentials() # Until we have no database connected here this will always pass, if it fails # rewrite the test in a way it's taking into consideration the database self.assertEqual(credh.are_cred_valid(), False) DatabaseHandler.destroy_database()
def add_url(url): """ Adds url to database Args: url (string): url to rss.xml Returns: int: index of newly added url in database, returns -1 if url was already in use and -2 if url wasn't in all directory """ dbh = DatabaseHandler() username = CredentialsHandler.lastUsername res = dbh.get_entry(username) ret_index = -1 url_index = -1 for i, entry in enumerate(res['urls']): if entry['actual_url'] == url: url_index = i break if url_index == -1: new_entry = { 'actual_url': url, 'rss_title': None, 'rss_link': None, 'rss_desc': None, 'articles': [], } res['urls'].append(new_entry) ret_index = len(res['urls'])-1 dbh.add_entry(username, res) elif url_index in res['groups']['All']: return ret_index elif url_index not in res['groups']['All']: ret_index = -2 stats = dbh.get_entry("__all_urls_statistics__") if stats == None: stats = [] stats.append([url, 1]) dbh.add_entry("__all_urls_statistics__", stats) return ret_index url_exists = False for i, stat in enumerate(stats): if url in stat: url_exists = True stats[i][1] += 1 break if not url_exists: stats.append([url, 1]) dbh.add_entry("__all_urls_statistics__", stats) return ret_index
def refresh_groups(self, download=False): """ Refreshes groups after adding/removing group or url Args: download (bool, optional): Option to fetch newset article while refreshing content. Defaults to False. """ dbh = DatabaseHandler() self.entry = dbh.get_entry(CredentialsHandler.lastUsername) self.get_user_groups(update=True) if download: self.group_view.menu_refresh_callback( self.group_view.selectedItems()[0])
def remove_url(url): """ Removes url from database Args: url (string): removes url from database """ dbh = DatabaseHandler() username = CredentialsHandler.lastUsername res = dbh.get_entry(username) for i, entry in enumerate(res['urls']): if entry['actual_url'] == url: for group in res['groups']: if i in res['groups'][group] and group != 'Most Popular URLs': res['groups'][group].remove(i) dbh.add_entry(username, res) stats = dbh.get_entry("__all_urls_statistics__") if stats == None: return url_exists = False for i, stat in enumerate(stats): if url in stat: url_exists = True stats[i][1] -= 1 break if url_exists and stats[i][1] == 0: stats.pop(i) dbh.add_entry("__all_urls_statistics__", stats) return
def test_basic_operations(self): db = DatabaseHandler(db_path="pytests/", dbIsTemp=True) credh = CredentialsHandler('basic_username', 'basic_password') credh.encrypt_credentials() self.assertEqual(credh.does_user_exist(), False) self.assertEqual(credh.are_cred_valid(), False) credh.create_user() self.assertEqual(credh.does_user_exist(), True) self.assertEqual(credh.are_cred_valid(), True) DatabaseHandler.destroy_database()
def create_user(self): """ """ dbh = DatabaseHandler() value = { 'password': self.password, 'urls': [], 'groups': { 'All': [], }, } result = dbh.add_entry(self.username, value) return result
def add_url_to_group_callback(self): """ Callback method for the button "Add url to group" It creates a window that allows the user to mark the groups that and urls that are supposed to be added to them """ w = QWidget() f = QHBoxLayout(w) db = DatabaseHandler() entries = db.get_entry(CredentialsHandler.lastUsername) ldata = [url for url in entries['groups']] ldata = self.exclude_groups(ldata) ls = ListerView('Groups', 'Groups', ldata, self.parent) rdata = [] for index in entries['groups']['All']: rdata.append(entries['urls'][index]['actual_url']) rs = ListerView('Urls', 'Urls', rdata, self.parent) rs.layout().setContentsMargins(0, 0, 0, 0) ls.layout().setContentsMargins(0, 0, 0, 0) f.addWidget(ls) f.addWidget(rs) q = QDialog(self.parent) q.setWindowTitle('Add URL to Group') mf = QVBoxLayout(q) mf.addWidget(w) buttonBox = QDialogButtonBox( QDialogButtonBox.Ok | QDialogButtonBox.Cancel, Qt.Horizontal, self.parent) mf.addWidget(buttonBox) buttonBox.accepted.connect(q.accept) buttonBox.rejected.connect(q.reject) if q.exec_(): groups = ls.get_results() urls = rs.get_results() for group in groups: for url in urls: index = URLHandler.add_url_to_group(url, group) if index > -1: self.mainView.group_view.add_url(url, group, index)
def test_basic_operations(self): db = DatabaseHandler(db_path="pytests/", dbIsTemp=True) db.add_entry('testkey', 'testvalue') res = db.get_entry('testkey') self.assertEqual(res, 'testvalue') res = db.delete_entry('testkey') self.assertEqual(res, True) res = db.get_entry('nonvalidtestkey') self.assertEqual(res, None) DatabaseHandler.destroy_database()
def are_cred_valid(self): """ Checks if provided credentials are valid Returns: bool: True or False """ dbh = DatabaseHandler() # Password has to encrypted by this point result = dbh.get_entry(self.username) if result == None: return False else: if result['password'] == self.password: return True else: return False
def remove_url_from_group(url, group): """ Removes url from selected group in database Args: url ([type]): [description] group ([type]): [description] """ dbh = DatabaseHandler() username = CredentialsHandler.lastUsername res = dbh.get_entry(username) for i, entry in enumerate(res['urls']): if entry['actual_url'] == url: if group in res['groups'] and i in res['groups'][group]: res['groups'][group].remove(i) dbh.add_entry(username, res) return
def remove_group(group): """ Remove group from the database Args: group (string): name of the group Returns: bool: True if group has been removed succesfully """ dbh = DatabaseHandler() username = CredentialsHandler.lastUsername res = dbh.get_entry(username) for i, entry in enumerate(res['groups']): if group in entry: res['groups'].pop(group) dbh.add_entry(username, res) return True return False
def add_group(group): """ Add group to the database if one doesn't exists Args: group (string): name of the group Returns: bool: True if group has been added succesfully """ dbh = DatabaseHandler() username = CredentialsHandler.lastUsername res = dbh.get_entry(username) if any([entry for entry in res['groups'] if group in entry]): return False res['groups'][group] = [] dbh.add_entry(username, res) return True
def set_article_seen(url, seen): """ After reading article this method is used to inform database about viwing it Args: url (string): url to article seen (bool): true if article has been seen, otherwise false """ dbh = DatabaseHandler() username = CredentialsHandler.lastUsername res = dbh.get_entry(username) for i, entry in enumerate(res['urls']): for j, article in enumerate(entry['articles']): if article['link'] == url: res['urls'][i]['articles'][j]['seen'] = seen break dbh.add_entry(username, res)
def remove_group_callback(self): """ Callback method for the button "Remove group" It creates a ListerView that allows the user to mark the groups that are supposed to be remove from the database """ prompt = 'List of Groups' title = 'Choose group to remove' db = DatabaseHandler() entries = db.get_entry(CredentialsHandler.lastUsername) data = [url for url in entries['groups']] data = self.exclude_groups(data) ls = ListerView(prompt, title, data, self.parent) ls.enable_button_box() if ls.exec_(): reslist = ls.get_results() for res in reslist: GroupHandler.remove_group(res) self.mainView.group_view.remove_group(res)
def menu_refresh_callback(self, clicked_item): """ Callback called when refresh is clicked in context menu it is fetching newest data from select rss range Args: clicked_item (QItem): Group or URL item in groupview """ if clicked_item.rss_type == 'url': url = clicked_item.text(0) self.refresh_url_data(url) self.parent().parent().refresh_feed(clicked_item) elif clicked_item.rss_type == 'group': groupName = clicked_item.text(0) dbh = DatabaseHandler() res = dbh.get_entry(CredentialsHandler.lastUsername) for idx in res['groups'][groupName]: self.refresh_url_data(res['urls'][idx]['actual_url']) self.parent().parent().refresh_feed(clicked_item)
def test_pickle_storing(self): db = DatabaseHandler(db_path="pytests/", dbIsTemp=True) l = [str(i) for i in range(100)] db.add_entry('testkey', l) res = db.get_entry('testkey') self.assertNotEqual(res, None) self.assertEqual(res, l) DatabaseHandler.destroy_database()
def remove_url_callback(self): """ Callback method for the button "Remove URL" It creates a ListerView that allows the user to mark the URLs that are supposed to be remove from the database """ prompt = 'List of URLs' title = 'Choose URL to remove' db = DatabaseHandler() data = [] entries = db.get_entry(CredentialsHandler.lastUsername) for index in entries['groups']['All']: data.append(entries['urls'][index]['actual_url']) ls = ListerView(prompt, title, data, self.parent) ls.enable_button_box() if ls.exec_(): reslist = ls.get_results() for res in reslist: URLHandler.remove_url(res) self.mainView.group_view.remove_url(res, "All")