def test_bookmark_resolved_title(self): bookmark = Bookmark(title='Custom title', website_title='Website title', url='https://example.com') self.assertEqual(bookmark.resolved_title, 'Custom title') bookmark = Bookmark(title='', website_title='Website title', url='https://example.com') self.assertEqual(bookmark.resolved_title, 'Website title') bookmark = Bookmark(title='', website_title='', url='https://example.com') self.assertEqual(bookmark.resolved_title, 'https://example.com')
def save(self): try: bookmark = Bookmark(user=self.instance.user, sound=self.instance.sound) if not self.cleaned_data['category']: if self.cleaned_data['new_category_name'] != "": category = BookmarkCategory( user=self.instance.user, name=self.cleaned_data['new_category_name']) category.save() bookmark.category = category self.cleaned_data['category'] = category else: bookmark.category = self.cleaned_data['category'] if self.cleaned_data['name'] != "": bookmark.name = self.cleaned_data['name'] bookmark.save() return True except: return False
def get_form_for_sound(request, sound_id): sound = Sound.objects.get(id=sound_id) form = BookmarkForm(instance=Bookmark(name=sound.original_filename), prefix=sound.id) form.fields['category'].queryset = BookmarkCategory.objects.filter( user=request.user) categories_already_containing_sound = BookmarkCategory.objects.filter( user=request.user, bookmarks__sound=sound).distinct() add_bookmark_url = '/'.join( request.build_absolute_uri(reverse( 'add-bookmark', args=[sound_id])).split('/')[:-2]) + '/' tvars = { 'bookmarks': Bookmark.objects.filter(user=request.user, sound=sound).count() != 0, 'sound_id': sound.id, 'form': form, 'categories_aready_containing_sound': categories_already_containing_sound, 'add_bookmark_url': add_bookmark_url } template = 'bookmarks/bookmark_form.html' return render(request, template, tvars)
def setup_bookmark( self, is_archived: bool = False, tags=None, user: User = None, url: str = '', title: str = '', description: str = '', website_title: str = '', website_description: str = '', ): if tags is None: tags = [] if user is None: user = self.get_or_create_test_user() if not url: unique_id = get_random_string(length=32) url = 'https://example.com/' + unique_id bookmark = Bookmark(url=url, title=title, description=description, website_title=website_title, website_description=website_description, date_added=timezone.now(), date_modified=timezone.now(), owner=user, is_archived=is_archived) bookmark.save() for tag in tags: bookmark.tags.add(tag) bookmark.save() return bookmark
def add_bookmark(request): incorrect_url = False added = False in_db = False if request.POST: url = request.POST["url"] if not url.startswith("http://www."): if not url.startswith("www."): url = "http://www." + url else: url = "http://" + url if correct_url(url): username = request.user.username if Bookmark.objects.filter(username=username, url=url).count(): in_db = True else: metadata = parse_meta(url) bookmark = Bookmark(username=username, url=url, title=metadata["title"], favicon=metadata["favicon"], description=metadata["description"]) bookmark.save() added = True else: incorrect_url = not correct_url(url) context = {"incorrect_url": incorrect_url, "added": added, "in_db": in_db} return render(request, "bookmarks/add_bookmark.html", context)
def create_initial_content(self): LegFile.objects.create(key=1, id='a', title="first", intro_date=date(2011, 12, 13), type="Bill") LegFile.objects.create(key=2, id='b', title="second", intro_date=date(2011, 12, 13), type="Bill") LegFile.objects.create(key=3, id='c', title="third", intro_date=date(2011, 12, 13), type="Bill") legfile = LegFile.objects.get(key=1) self.subscriber.bookmarks.add(Bookmark(content=legfile))
def test_can_update_bookmark(self): bookmark = Bookmark( user=self.user, name='Bookmark 1', url='https://www.google.com', notes='This is the first note.', ) bookmark.save() form_data = { 'name': 'Bookmark 8', 'url': 'https://www.google123.com', 'notes': 'This is the first note.', } self.client.force_login(self.user) update_url = "/bookmarks/edit/%s" % bookmark.id response = self.client.post(update_url, data=form_data) self.assertEquals(response.status_code, 302) self.assertRedirects(response, '/bookmarks/') bookmark.refresh_from_db() self.assertEquals(form_data['name'], bookmark.name) self.assertEquals(form_data['url'], bookmark.url) self.assertEquals(form_data['notes'], bookmark.notes)
def create(self, validated_data): bookmark = Bookmark() bookmark.url = validated_data['url'] bookmark.title = validated_data['title'] bookmark.description = validated_data['description'] tag_string = build_tag_string(validated_data['tag_names'], ' ') return create_bookmark(bookmark, tag_string, self.context['user'])
def add_bookmark(): form = BookmarkForm(link='http://') if form.validate_on_submit() and request.method == 'POST': # Get form data link = form.link.data follow_redirects = form.follow_redirects.data # T/F for following link redirects # Test that link works, or return error to user try: r = requests.get(link, headers={'user-agent': USER_AGENT}, allow_redirects=follow_redirects, timeout=TIMEOUT) r.raise_for_status() # Exceptions for 400 or 500 errors except requests.exceptions.HTTPError as e: flash('Please check your link. It leads to this error: {}.'.format( e), category='danger') # Missing schema except requests.exceptions.MissingSchema as e: flash('No schema. Did you mean http://{}?'.format(link), category='danger') # Timeout errors except requests.exceptions.Timeout: flash('There was a timeout error. Please check URL.', category='danger') # Connection errors except requests.exceptions.ConnectionError: flash('Could not connect to your link. Please check URL.', category='danger') # Too many redirects except requests.exceptions.TooManyRedirects: flash('Exceeded max number of redirects. Please check URL.', category='danger') # All other requests-related exceptions except requests.exceptions.RequestException as e: flash('Please check your link. It leads to this error: {}.'.format( e), category='danger') # No errors when requesting link, so add to database else: # Generate a possible id b_id = hex_gen() # If it exists, keep regenerating while (Bookmark.query.filter(Bookmark.id == b_id).one_or_none() is not None): b_id = hex_gen() url = r.url # Get final url (from redirects) url_root = request.url_root b = Bookmark(b_id, link=url, user_id=flask_login.current_user.id) db_session.add(b) db_session.commit() msg_uf = ('Successfully added {0}. Your short link is ' + '<a target="_blank" href="{1}">{1}</a>.') msg = msg_uf.format(url, url_root + b_id) flash(Markup(msg), category='success') return redirect(url_for('add_bookmark'), 303) return render_template('add_bookmark.html', form=form)
def save(request): form = BookmarksSaveForm(request.POST) if not form.is_valid(): return HttpResponseBadRequest(json.dumps({'error': form.errors})) content = form.cleaned_data['content'] id = md5(content.encode('utf-8')).hexdigest() book = Bookmark(id=id, content=content) book.save() response = HttpResponse(json.dumps({'id': id})) response['Content-Type'] = "application/json" return response
def handle_data(self, data): if self.in_a: bookmark = Bookmark() bookmark.url = self.attrs.get('href') bookmark.title = data bookmark.bookmarked_at = unixtime2datetime( self.attrs.get('add_date')) try: bookmark.save() except IntegrityError: pass
def setUp(self): self.client = Client() self.user = User.objects.create_user(username='******', email='*****@*****.**', password='******') self.bookmark = Bookmark( user=self.user, name='Bookmark 1', url='https://www.google.com', notes='This is the first note.', ) self.bookmark.save()
def _handle_import(contents, use_tags, owner): """ Handles the import of a bookmarks file Loops through all links in the file and adds them as bookmarks. It then tags them if the file contains tags and "use_tags" is true. """ lines = contents.decode("utf-8").split("\n") title = re.compile(r"<a.*?>(.+?)</a>", re.I) url = re.compile(r"""<a.*href=['"](.+?)['"]""", re.I) tags = re.compile(r"""<a.*?tags=["'](.+?)["']""", re.I) addTime = re.compile(r"""<a.*?add_date=["'](\d+?)["']""", re.I) for l in lines: if "<a" in l.lower() and "</a>" in l.lower(): bookmark = {} bookmark["title"] = title.search(l) if not bookmark["title"]: continue bookmark["title"] = _unescape(bookmark["title"].group(1)) bookmark["url"] = url.search(l) if not bookmark["url"]: continue bookmark["url"] = _unescape(bookmark["url"].group(1)) bookmark["tags"] = []; if use_tags: result = tags.search(l) if result: bookmark["tags"] = map(_unescape, result.group(1).split(",")) bookmark["added"] = addTime.search(l) if bookmark["added"]: bookmark["added"] = bookmark["added"].group(1) if not Bookmark.objects.filter(owner=owner, url=bookmark["url"]).exists(): bm = Bookmark(owner=owner, url=bookmark["url"], title=bookmark["title"]) bm.save() if bookmark["added"]: bm.added = datetime.datetime.fromtimestamp(int(bookmark["added"])) for t in bookmark["tags"]: bm.tag(t) bm.save() bm.autotag_rules()
def test_archive_bookmark(self): bookmark = Bookmark(url='https://example.com', date_added=timezone.now(), date_modified=timezone.now(), owner=self.user) bookmark.save() self.assertFalse(bookmark.is_archived) archive_bookmark(bookmark) updated_bookmark = Bookmark.objects.get(id=bookmark.id) self.assertTrue(updated_bookmark.is_archived)
def handle(self, *args, **options): ''' The layout of the export puts links into <dt> tags and any related comments into a <dd> tag immediately following. If a link has no comments then the <dd> tag is not present. Ignore all other tags ''' user = get_user_model().objects.get(pk=1) # Load the export html into a beautiful soup object soup = BeautifulSoup(open('delicious.html')) print 'Parsing export with %s rows' % len(soup.dl.contents) for i in range(len(soup.dl.contents)): if soup.dl.contents[i].name == 'dt': print('{row}: Creating new bookmark for {url}'.format( row=i, url=soup.dl.contents[i].a['href'])) bookmark = Bookmark( user=user, url=soup.dl.contents[i].a['href'], title=soup.dl.contents[i].a.string, saved=timezone.make_aware( timezone.datetime.fromtimestamp( float(soup.dl.contents[i].a['add_date'])), timezone.get_current_timezone())) print(' {row}: Bookmark title: {title}'.format( row=i, title=soup.dl.contents[i].a.text.encode('utf-8'))) try: # Check for any comments for this bookmark if soup.dl.contents[(i + 1)].name == 'dd': print(' {row}: Got comments: {comments}'.format( row=i, comments=soup.dl.contents[(i + 1)].string)) bookmark.comment = soup.dl.contents[(i + 1)].string except IndexError: # We've reached the end of the export file pass bookmark.save() # Add the tags for tag in soup.dl.contents[i].a['tags'].split(','): print(' {row}: Found tag {tag}'.format(row=i, tag=tag)) if tag != '': try: the_tag = Tag.objects.get(name=tag.strip(' ')) except Tag.DoesNotExist as e: the_tag = Tag(name=tag.strip(' ')) the_tag.save() bookmark.tags.add(the_tag)
def _run_bookmark_model_url_validity_checks(self, cases): for case in cases: url, expectation = case bookmark = Bookmark(url=url, date_added=datetime.datetime.now(), date_modified=datetime.datetime.now(), owner=self.user) try: bookmark.full_clean() self.assertTrue(expectation, 'Did not expect validation error') except ValidationError as e: self.assertFalse(expectation, 'Expected validation error') self.assertTrue('url' in e.message_dict, 'Expected URL validation to fail')
def add(): form = BookmarkForm() if form.validate_on_submit(): url = form.url.data description = form.description.data tags = form.tags.data bm = Bookmark(user=current_user, url=url, description=description, tags=tags) db.session.add(bm) db.session.commit() flash("Stored '{}'".format(description)) return redirect(url_for('index')) return render_template('bookmark_form.html', form=form, title="Add a bookmark")
def setup_bookmark(self, is_archived: bool = False, tags: [Tag] = [], user: User = None): if user is None: user = self.get_or_create_test_user() unique_id = get_random_string(length=32) bookmark = Bookmark(url='https://example.com/' + unique_id, date_added=timezone.now(), date_modified=timezone.now(), owner=user, is_archived=is_archived) bookmark.save() for tag in tags: bookmark.tags.add(tag) bookmark.save() return bookmark
def bookmark_new(): form = BookmarkForm(request.form) if form.validate_on_submit(): try: bookmark = Bookmark(form.url.data, form.title.data, description=form.description.data, tags=form.tags.data, public=form.public.data) db.session.add(bookmark) db.session.commit() except IntegrityError: flash(u'Boomark for "%s" exists already.' % form.url.data, 'error') return render_template('bookmark-form.html', form=form) flash(u'Done!') return redirect('%d' % bookmark.id) flash_errors(form) return render_template('bookmark-form.html', form=form)
def add(request): """ Adds a new bookmark Expects the url to be provided by the url POST value. The URL is automatically validated, if it isn't a valid URL, then http:// is prepended to it. If that fails, then the bookmark isn't added and an error is sent. The title for the bookmark is automatically downloaded based on the URL. If it succeeds, it returns the JSON representation of thebookmark it has added. If it fails it returns a JSON object with only an "error" property. """ if "url" not in request.POST: raise SuspiciousOperation url = request.POST["url"] val = URLValidator() if request.user.settings.url_settings == Settings.URL_SETTINGS_VALIDATE: try: val(url) except ValidationError: try: val("http://"+url) url = "http://"+url except ValidationError: return HttpResponse('{"error":"Invalid URL"}', content_type="application/json", status=422) bm = Bookmark(owner=request.user, url=url) bm.title = url bm.save() if bm.valid_url: bm.download_title() bm.save() if "tag" in request.POST: bm.tag(request.POST["tag"]) bm.autotag_rules() return HttpResponse(bm.to_json(), content_type="application/json")
def handle(self, **options): with open(options['path'], 'r') as file: data = json.load(file) self.log_message( u"Found {} bookmarks".format(len(data)) ) for bookmark in data: self.log_message(u"{}".format(bookmark['title'])) b = Bookmark( title=bookmark['title'], description=bookmark['description'], date_added=bookmark['date_added'], private=bookmark['private'], url=bookmark['url'] ) b.save() for tag in bookmark['tags']: b.tags.add(tag)
def add_bookmark(request, sound_id): sound = get_object_or_404(Sound, id=sound_id) if request.POST: form = BookmarkForm(request.POST, instance=Bookmark(user=request.user, sound=sound)) form.fields['category'].queryset = BookmarkCategory.objects.filter(user=request.user) if form.is_valid(): form.save() if request.is_ajax(): return HttpResponse() else: msg = "Added new bookmark for sound \"" + sound.original_filename + "\"." messages.add_message(request, messages.WARNING, msg) next = request.GET.get("next", "") if next: return HttpResponseRedirect(next) else: return HttpResponseRedirect(reverse("sound", args=[sound.user.username, sound.id]))
def _post_bookmarks(request): try: url = request.POST['url'] title = request.POST['title'] form = BookmarkForm({'url': url, 'title': title}) if form.is_valid(): bookmark = Bookmark() bookmark.url = url bookmark.title = title bookmark.save() return JsonResponse({}, status=201) else: data = { 'errors': [{ 'title': '入力エラー', 'data': json.loads(form.errors.as_json()) }] } return JsonResponse(data, status=400) except: data = {'errors': [{'title': 'ブックマークを登録できませんでした'}]} return JsonResponse(data, status=500)
def add_bookmark(url, description, tags): db.session.add(Bookmark(url=url, description=description, user=admin, tags=tags))
def _get_or_create_bookmark(url: str, user: User): try: return Bookmark.objects.get(url=url, owner=user) except Bookmark.DoesNotExist: return Bookmark()