def test_edit_parent_bookmark_missingchild(self): ''' Test editing parent of a non-existing Bookmark. ''' from frostmark import db_base, user_data from frostmark.db import get_session from frostmark.models import Bookmark from frostmark.editor import Editor data = dirname(abspath(user_data.__file__)) self.assertNotIn(db_base.DB_NAME, listdir(data)) item = { 'id': 321, 'title': 'child', 'url': '<url>', 'icon': b'<bytes>' } session = get_session() try: session.add(Bookmark(**item)) session.commit() finally: session.close() with self.assertRaises(Exception): Editor.change_parent_bookmark(bookmark_id=2**31, parent_id=item['id']) self.assertIn(db_base.DB_NAME, listdir(data)) remove(join(data, db_base.DB_NAME))
def test_edit_parent_folder_missingparent(self): ''' Test editing parent to a non-existing Folder. ''' from frostmark import db_base, user_data from frostmark.db import get_session from frostmark.models import Folder from frostmark.editor import Editor data = dirname(abspath(user_data.__file__)) self.assertNotIn(db_base.DB_NAME, listdir(data)) item = {'id': 321, 'folder_name': 'child'} session = get_session() try: session.add(Folder(**item)) session.commit() finally: session.close() with self.assertRaises(Exception): Editor.change_parent_folder(folder_id=item['id'], parent_id=2**31) self.assertIn(db_base.DB_NAME, listdir(data)) remove(join(data, db_base.DB_NAME))
def test_edit_parent_folder(self): ''' Test editing a parent folder of a Folder. ''' from frostmark import db_base, user_data from frostmark.db import get_session from frostmark.models import Folder from frostmark.editor import Editor data = dirname(abspath(user_data.__file__)) self.assertNotIn(db_base.DB_NAME, listdir(data)) items = [{ 'id': 321, 'folder_name': 'old parent' }, { 'id': 666, 'folder_name': 'child', 'parent_folder_id': 321 }, { 'id': 123, 'folder_name': 'new parent' }] session = get_session() try: for item in items: session.add(Folder(**item)) session.commit() for item in items: self.assertEqual( session.query(Folder).filter( Folder.folder_name == item['folder_name']).first().id, item['id']) self.assertEqual( session.query(Folder).filter( Folder.id == items[1]['id']).first().parent_folder_id, items[0]['id']) Editor.change_parent_folder(folder_id=items[1]['id'], parent_id=items[2]['id']) self.assertEqual( session.query(Folder).filter( Folder.id == items[1]['id']).first().parent_folder_id, items[2]['id']) finally: session.close() self.assertIn(db_base.DB_NAME, listdir(data)) remove(join(data, db_base.DB_NAME))
def test_change_bookmark_url(self): ''' Test renaming a Bookmark item. ''' from frostmark import db_base, user_data from frostmark.db import get_session from frostmark.models import Bookmark from frostmark.editor import Editor data = dirname(abspath(user_data.__file__)) self.assertNotIn(db_base.DB_NAME, listdir(data)) item = { 'id': 321, 'title': 'child', 'url': '<url>', 'icon': b'<bytes>' } session = get_session() try: session.add(Bookmark(**item)) session.commit() self.assertEqual( session.query(Bookmark).filter( Bookmark.title == item['title']).first().id, item['id']) self.assertEqual( session.query(Bookmark).filter( Bookmark.id == item['id']).first().title, item['title']) Editor.change_bookmark_url(bookmark_id=item['id'], url=f"http://{item['url']}") self.assertEqual( session.query(Bookmark).filter( Bookmark.id == item['id']).first().url, f"http://{item['url']}") finally: session.close() self.assertIn(db_base.DB_NAME, listdir(data)) remove(join(data, db_base.DB_NAME))
def test_rename_folder(self): ''' Test renaming a Folder item. ''' from frostmark import db_base, user_data from frostmark.db import get_session from frostmark.models import Folder from frostmark.editor import Editor data = dirname(abspath(user_data.__file__)) self.assertNotIn(db_base.DB_NAME, listdir(data)) item = {'id': 321, 'folder_name': 'old parent'} session = get_session() try: session.add(Folder(**item)) session.commit() self.assertEqual( session.query(Folder).filter( Folder.folder_name == item['folder_name']).first().id, item['id']) self.assertEqual( session.query(Folder).filter( Folder.id == item['id']).first().folder_name, item['folder_name']) Editor.rename_folder(folder_id=item['id'], name=item['folder_name'][:3]) self.assertEqual( session.query(Folder).filter( Folder.id == item['id']).first().folder_name, item['folder_name'][:3]) finally: session.close() self.assertIn(db_base.DB_NAME, listdir(data)) remove(join(data, db_base.DB_NAME))
def edit_bookmark(): """ Edit a single bookmark via HTML form. """ # collect parameters bookmark_id = request.form.get('id', type=int) folder_id = request.form.get('folder', type=int) name = request.form.get('title') url = request.form.get('url') status = 302 # only edit if all the fields are ok if all([item is not None for item in (bookmark_id, folder_id, name, url)]): Editor.rename_bookmark( bookmark_id=bookmark_id, name=name ) Editor.change_bookmark_url( bookmark_id=bookmark_id, url=url ) Editor.change_parent_bookmark( bookmark_id=bookmark_id, parent_id=folder_id ) status = 301 response = Response( status=status, headers={ 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Headers': '*', 'Access-Control-Allow-Methods': '*', 'Location': '/' }, mimetype='application/json' ) return response
'arg_values'][0]))) PARSER.console_parser.add_argument( '-cpf', '--change-parent-folder', help='change parent for a folder', required=False, nargs=2, metavar=('FOLDER_ID', 'PARENT_ID'), # pylint: disable=unnecessary-lambda action=lambda *args, **kwargs: ExecuteAction( *args, **kwargs, func=lambda *args, **kwargs: Editor.change_parent_folder( folder_id=int(kwargs['arg_values'][0]), parent_id=int(kwargs['arg_values'][1])))) PARSER.console_parser.add_argument( '-cpb', '--change-parent-bookmark', help='change parent for a bookmark', required=False, nargs=2, metavar=('BOOKMARK_ID', 'PARENT_ID'), # pylint: disable=unnecessary-lambda action=lambda *args, **kwargs: ExecuteAction( *args, **kwargs, func=lambda *args, **kwargs: Editor.change_parent_bookmark(
def test_edit_parent_bookmark(self): ''' Test editing a parent folder of a bookmark. ''' from frostmark import db_base, user_data from frostmark.db import get_session from frostmark.models import Folder, Bookmark from frostmark.editor import Editor data = dirname(abspath(user_data.__file__)) self.assertNotIn(db_base.DB_NAME, listdir(data)) items = [{ 'id': 321, 'folder_name': 'old parent', 'type': Folder }, { 'id': 666, 'title': 'child', 'folder_id': 321, 'type': Bookmark, 'url': '<url>', 'icon': b'<bytes>' }, { 'id': 123, 'folder_name': 'new parent', 'type': Folder }] session = get_session() try: for item in items: item_type = item.pop('type') if item_type == Folder: session.add(Folder(**item)) elif item_type == Bookmark: session.add(Bookmark(**item)) item['type'] = item_type session.commit() for item in items: item_type = item.pop('type') if item_type == Folder: self.assertEqual( session.query(Folder).filter( Folder.folder_name == item['folder_name']).first().id, item['id']) elif item_type == Bookmark: self.assertEqual( session.query(Bookmark).filter( Bookmark.title == item['title']).first().id, item['id']) item['type'] = item_type self.assertEqual( session.query(Bookmark).filter( Bookmark.id == items[1]['id']).first().folder_id, items[0]['id']) Editor.change_parent_bookmark(bookmark_id=items[1]['id'], parent_id=items[2]['id']) self.assertEqual( session.query(Bookmark).filter( Bookmark.id == items[1]['id']).first().folder_id, items[2]['id']) finally: session.close() self.assertIn(db_base.DB_NAME, listdir(data)) remove(join(data, db_base.DB_NAME))