def convert(self): anchors = self.soup.find_all('a') links = {} duplicates = [] errors = [] folders = {} for anchor in anchors: data = {'userid': self.user.userid} data['url'] = anchor.attrs['href'] # Handle nameless links - bookmarklets? if anchor.contents: data['name'] = data['url'][:250] else: data['name'] = anchor.contents[0] desc_node = self.get_description(anchor) if desc_node: data['description'] = desc_node.contents[0] try: link = Link(data) if links.get(link.url): duplicates.append(link) else: links[link.url] = link appdb.session.add(link) folder = self.get_folder(anchor) if folder: fld = folders.get(folder.contents[0]) if not fld: fld = Folder() fld.userid = self.user.userid fld.name = folder.contents[0] folders[folder.contents[0]] = fld appdb.session.add(fld) fld.links.append(link) if self.tag_folders: link.set_tags([folder.contents[0]]) appdb.session.commit() except Exception: errors.append(link) appdb.session.rollback() return {'links': links, 'folders': folders, 'duplicates': duplicates, 'errors': errors}
def do_add_link(): req = request.form if request.method == 'POST' else request.args form = forms.AddLink(req, referer=get_referer()) data = form.data data['userid'] = current_user.userid options = { 'button_label': 'Add Link', 'post_view': req.get('post_view') if req.get('post_view') else url_for('do_add_link'), } link = Link(data) if request.method == 'POST' or request.args.get('submit') == '1 ': if link.already_exists(): flash('This link already exists. Try editing it instead.', 'error') return redirect(url_for('do_edit_link', linkid=link.linkid)) taglist = form.tags.data.split(',') if form.tags.data else [] link.set_tags(taglist) if form.validate(): link.save() redir_target = link.url if form.redirect_to_target else get_referer( ) return redirect(redir_target) flash('Invalid link submission', 'error') return str(form.errors) tags = Tag.get_by_user(current_user.userid) return render_template("link_add.html", pageoptions=get_default_data(), form=form, link=link, tags=tags, options=options)
class TestLinks(unittest.TestCase): def setUp(self): tests.db.init_test_db() self.link = Link() self.link.userid = 1 self.link.name = 'Test link' self.link.url = 'http://www.example.com/' self.link.tags = [Tag('test1'), Tag('test2')] def tearDown(self): tests.db.delete_test_db() def make_user(self, userid): usr = User() usr.userid = userid return usr def test_is_owner_same_user(self): u = self.make_user(1) self.assertTrue(self.link.is_owner(u)) def test_is_owner_different_user(self): u = self.make_user(2) self.assertFalse(self.link.is_owner(u)) def test_is_owner_empty_user(self): u = None self.assertFalse(self.link.is_owner(u)) def test_get_taglist_has_tags(self): self.assertEqual(len(self.link.tags), 2) self.assertTrue('test1' in self.link.get_taglist()) self.assertTrue('test2' in self.link.get_taglist()) def test_get_taglist_no_tags(self): self.link.tags = [] self.assertEqual(self.link.get_taglist(), []) def test_set_tags_and_get(self): self.link.set_tags(['test3', 'test4']) self.assertEqual(len(self.link.tags), 2) self.assertTrue('test3' in self.link.get_taglist()) self.assertTrue('test4' in self.link.get_taglist()) def test_add_tag_and_get(self): self.link.add_tag('test5') self.assertTrue('test5' in self.link.get_taglist()) def test_add_tag_duplicate(self): taglist = self.link.get_taglist() self.link.add_tag('test1') self.assertTrue(taglist == self.link.get_taglist()) def test_get_by_id_exists(self): result = Link.get_by_id(1) self.assertTrue(result.__class__ is Link) def test_get_by_id_nonexistent(self): result = Link.get_by_id(123) self.assertEqual(result, None) def test_get_by_id_array_all_exist(self): result = Link.get_by_id([1, 2]) self.assertEqual(len(result), 2) def test_get_by_id_array_one_exists(self): result = Link.get_by_id([1, 123]) self.assertEqual(len(result), 1) def test_get_by_id_array_none_exist(self): result = Link.get_by_id([234, 123]) self.assertEqual(len(result), 0) def test_get_by_user_exists(self): result = Link.get_by_user(1) self.assertEqual(len(result), 3) def test_get_by_user_nonexistent(self): result = Link.get_by_user(123) self.assertEqual(len(result), 0) def test_get_public_by_user_exists(self): result = Link.get_public_by_user(1) self.assertEqual(len(result), 2) def test_get_public_by_user_nonexistent(self): result = Link.get_public_by_user(123) self.assertEqual(len(result), 0) def test_get_by_shortname_exists(self): result = Link.get_by_shortname('foo') self.assertEqual(result.__class__, Link) def test_get_by_shortname_nonexistent(self): result = Link.get_by_shortname('this does not exist') self.assertEqual(result, None) def test_get_by_tag_exists_nouser(self): result = Link.get_by_tag('test1') self.assertEqual(len(result), 4) def test_get_by_tag_exists_hasuser(self): result = Link.get_by_tag('test1', 1) self.assertEqual(len(result), 3) def test_get_by_tag_nonexistent_nouser(self): result = Link.get_by_tag('this does not exist') self.assertEqual(len(result), 0) def test_get_by_tag_nonexistent_hasuser(self): result = Link.get_by_tag('this does not exist', 1) self.assertEqual(len(result), 0) def test_get_public_by_tag_exists_nouser(self): result = Link.get_public_by_tag('test1') self.assertEqual(len(result), 3) def test_get_public_by_tag_exists_hasuser(self): result = Link.get_public_by_tag('test1', 1) self.assertEqual(len(result), 2) def test_get_public_by_tag_nonexistent_nouser(self): result = Link.get_public_by_tag('this does not exist') self.assertEqual(len(result), 0) def test_get_public_by_tag_nonexistent_hasuser(self): result = Link.get_public_by_tag('this does not exist', 1) self.assertEqual(len(result), 0) def test_get_by_most_hits(self): result = Link.get_by_most_hits() self.assertEqual(len(result), 3) self.assertEqual(result[0].linkid, 1) def test_get_by_most_hits_with_owner(self): result = Link.get_by_most_hits(1) self.assertEqual(len(result), 2) self.assertEqual(result[0].linkid, 1) self.assertEqual(result[1].linkid, 2) def test_get_by_most_hits_with_limit(self): result = Link.get_by_most_hits(1, 1) self.assertEqual(len(result), 1) self.assertEqual(result[0].linkid, 1) def test_get_by_most_recent_hit(self): result = Link.get_by_most_hits() self.assertEqual(len(result), 3) self.assertEqual(result[0].linkid, 1) def test_get_by_most_recent_hit_with_owner(self): result = Link.get_by_most_hits(1) self.assertEqual(len(result), 2) self.assertEqual(result[0].linkid, 1) self.assertEqual(result[1].linkid, 2) def test_get_by_most_recent_hit_with_limit(self): result = Link.get_by_most_hits(1, 1) self.assertEqual(len(result), 1) self.assertEqual(result[0].linkid, 1) def test_get_by_most_recent(self): result = Link.get_by_most_recent() self.assertEqual(len(result), 9) self.assertEqual(result[0].linkid, 4) def test_get_by_most_recent_with_owner(self): result = Link.get_by_most_recent(1) self.assertEqual(len(result), 3) self.assertEqual(result[0].linkid, 3) def test_get_by_most_recent_with_limit(self): result = Link.get_by_most_recent(1, 2) self.assertEqual(len(result), 2) self.assertEqual(result[0].linkid, 3) def test_get_untagged(self): result = Link.get_untagged() self.assertEqual(len(result), 2) def test_get_untagged_with_owner(self): result = Link.get_untagged(3) self.assertEqual(len(result), 1) self.assertEqual(result[0].linkid, 6) def test_get_public_untagged(self): result = Link.get_public_untagged() for r in result: print(str(r.linkid) + ' ' + str(r.is_public)) self.assertEqual(len(result), 1) self.assertEqual(result[0].linkid, 7) def test_get_public_untagged_with_owner(self): result = Link.get_public_untagged(4) self.assertEqual(len(result), 1) self.assertEqual(result[0].linkid, 7) result = Link.get_public_untagged(3) self.assertEqual(len(result), 0) def test_create_from_webpage(self): url = 'http://www.example.com' html = """ <html> <head> <title>This is a test</title> </head> <body>Test</body> </html> """ link = Link.create_from_webpage(url, html) self.assertEqual(link.url, url) self.assertEqual(link.name, 'This is a test') def test_create_from_webpage_with_description(self): url = 'http://www.example.com' html = """ <html> <head> <title>This is a test</title> <meta name="description" content="Describe this page" /> </head> <body>Test</body> </html> """ link = Link.create_from_webpage(url, html) self.assertEqual(link.url, url) self.assertEqual(link.name, 'This is a test') self.assertEqual(link.description, "Describe this page")