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 do_bulk_edit(tags): # Redirect filters immediately if request.args.get('tag_select'): return redirect( url_for('do_bulk_edit', tags=request.args.get('tag_select'))) available_tags = Tag.get_by_user(current_user.userid) if request.method == 'POST': linkids = request.form.getlist('linkids') edit_links = Link.get_by_id(linkids) valid = _links_are_valid(edit_links) tag_text = request.form.get('tag_text') if valid and tag_text and request.form.get('tag_submit'): for link in edit_links: link.add_tag(tag_text) link.save() flash('Tagged %d links as "%s"' % (len(edit_links), tag_text), 'success') elif valid and tag_text and request.form.get('tag_remove'): for link in edit_links: link.remove_tag(tag_text) link.save() flash( 'Removed tag "%s" from %d links' % (tag_text, len(edit_links)), 'success') if valid and request.form.get('set_privacy') and request.form.get( 'privacy_submit'): _set_link_privacy(edit_links, request.form.get('set_privacy')) if valid and request.form.get('delete_selected_submit'): for link in edit_links: link.delete() flash('Deleted %s links' % len(edit_links), 'success') if tags == 'untagged': title = 'Manage Untagged Links' links = Link.get_untagged(current_user.userid) elif tags: taglist = tags.split(',') title = 'Manage Links in ' + tags links = Link.get_by_tag(taglist[0], current_user.userid) else: title = "Manage Links" links = Link.get_by_user(current_user.userid) return render_template("link_edit_bulk.html", pageoptions=get_default_data(), url_tags=tags, links=links, section_title=title, tags=available_tags)
def show_link(linkid): link = Link.get_by_id(linkid) if link is None: abort(404) if link.is_owner(current_user): related = Link.get_recent_by_tag(link.get_taglist(), current_user.userid) else: related = Link.get_recent_public_by_tag(link.get_taglist(), link.userid) return render_template('link.html', pageoptions=get_default_data(), link=link, user=current_user, related=related)
def show_user_index(username): if username is None: usr = current_user else: usr = User.get_by_username(username) if not usr: abort(404) if usr is current_user: links = Link.get_by_user(current_user.userid) else: links = Link.get_public_by_user(usr.userid) return render_template('link_index.html', pageoptions=get_default_data(), links=links, user=current_user)
def show_user_tag(name): links = Link.get_by_tag(name, current_user.userid) title = 'My Tagged Links - "%s"' % name return render_template('link_index.html', pageoptions=get_default_data(), user=current_user, links=links, section_title=title, page_title=title)
def api_get_url_data(): if not request.form.get('url'): return json_error('Missing url parameter') try: link = Link.create_from_url(request.form.get('url')) return json_success({'url': link.url, 'name': link.name, 'description': link.description}) except Exception as ex: return json_error('Error fetching link - ' + str(ex))
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 show_tag(name): links = Link.get_public_by_tag(name) title = 'Links for Tag - "%s"' % name return render_template('link_index.html', pageoptions=get_default_data(), user=None, curr_user=current_user, links=links, section_title=title, page_title=title)
def show_user(username): usr = User.get_by_username(username) links = Link.get_public_by_most_recent(usr.userid, 30) tags = Tag.get_public_by_user(usr.userid) return render_template('public_dashboard.html', pageoptions=get_default_data(), tags=tags, links=links, user=usr, curr_user=current_user)
def show_all_user_tagged(name, username): user = User.get_by_username(username) links = Link.get_public_by_tag(name, user.userid) title = 'Links for tag "%s" by %s' % (name, user.username) return render_template('link_index.html', pageoptions=get_default_data(), user=user, curr_user=current_user, links=links, section_title=title, page_title=title)
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 show_delete_link(linkid): link = Link.get_by_id(linkid) if not link.is_owner(current_user): flash('You do not have permission to delete this link.', 'error') elif request.form.get('confirm'): link.delete() flash('Deleted link', 'success') return redirect(url_for('show_index')) elif request.form.get('cancel'): return redirect(request.form.get('referer')) return render_template("link_delete.html", pageoptions=get_default_data(), link=link)
def api_delete_link(): if not request.form.get('linkid'): return json_error('No linkid specified') link = Link.get_by_id(request.form.get('linkid')) error = validate_link(link) if error: return error try: link.delete() return json_success() except Exception as ex: return json_error('Error deleting link - ' + str(ex))
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")
def do_add_from_url(url): fetch_url = url or request.form.get('fetch_url') if fetch_url: try: link = Link.create_from_url(fetch_url) redir_url = url_for('do_add_link', name=link.name, url=link.url, description=link.description, redirect_to_target=1) return redirect(redir_url) except Exception as ex: flash('Error getting link - ' + str(ex), 'error') return render_template('link_add_url.html', pageoptions=get_default_data(), url=fetch_url or '')
def api_add_tag(): if not request.form.get('linkid') or not request.form.get('tags'): return json_error('Missing linkid or tags parameters') link = Link.get_by_id(request.form.get('linkid')) tags = request.form.get('tags').split(',') error = validate_link(link) if error: return error try: for tag in tags: link.add_tag(tag.strip()) link.save() return json_success() except Exception as ex: return json_error('Error adding tags - ' + str(ex))
def do_edit_link(linkid): errors = [] link = Link.get_by_id(linkid) if not link: abort(404) options = { 'button_label': 'Save', 'post_view': url_for('do_edit_link', linkid=linkid) } if request.method == 'POST': link.name = request.form.get('name') link.description = request.form.get('description') link.shortname = request.form.get('shortname') link.url = request.form.get('url') link.is_public = request.form.get('is_public') == '1' if request.form.get('tags').strip() == '': taglist = [] else: taglist = request.form.get('tags').split(',') link.set_tags(taglist) if not (request.form.get('name') and request.form.get('url')): errors.append("Name and URL are required") if errors: link.save() if request.form.get('referer'): return redirect(request.form.get('referer')) tags = Tag.get_by_user(current_user.userid) return render_template("link_add.html", pageoptions=get_default_data(), link=link, options=options, tags=tags, 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)
def test_get_by_id_nonexistent(self): result = Link.get_by_id(123) self.assertEqual(result, None)
def test_get_by_id_exists(self): result = Link.get_by_id(1) self.assertTrue(result.__class__ is Link)
def test_get_by_id_array_all_exist(self): result = Link.get_by_id([1, 2]) self.assertEqual(len(result), 2)
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_get_by_id_array_none_exist(self): result = Link.get_by_id([234, 123]) self.assertEqual(len(result), 0)
def test_get_public_by_user_nonexistent(self): result = Link.get_public_by_user(123) self.assertEqual(len(result), 0)
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_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_public_by_user_exists(self): result = Link.get_public_by_user(1) self.assertEqual(len(result), 2)
def test_get_untagged(self): result = Link.get_untagged() self.assertEqual(len(result), 2)
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_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)