def bookmarklet(): bookmark_contr = BookmarkController(current_user.id) href = (request.args if request.method == 'GET' else request.form)\ .get('href', None) if not href: flash(gettext("Couldn't add bookmark: url missing."), "error") raise BadRequest("url is missing") title = (request.args if request.method == 'GET' else request.form)\ .get('title', None) if not title: title = href bookmark_exists = bookmark_contr.read(**{'href': href}).all() if bookmark_exists: flash(gettext("Couldn't add bookmark: bookmark already exists."), "warning") return redirect(url_for('bookmark.form', bookmark_id=bookmark_exists[0].id)) bookmark_attr = {'href': href, 'description': '', 'title': title, 'shared': True, 'to_read': True} new_bookmark = bookmark_contr.create(**bookmark_attr) flash(gettext('Bookmark successfully created.'), 'success') return redirect(url_for('bookmark.form', bookmark_id=new_bookmark.id))
def import_pinboard_json(user, json_content): """Import bookmarks from a pinboard JSON export. """ bookmark_contr = BookmarkController(user.id) tag_contr = BookmarkTagController(user.id) bookmarks = json.loads(json_content.decode("utf-8")) nb_bookmarks = 0 for bookmark in bookmarks: tags = [] for tag in bookmark['tags'].split(' '): new_tag = BookmarkTag(text=tag.strip(), user_id=user.id) tags.append(new_tag) bookmark_attr = { 'href': bookmark['href'], 'description': bookmark['extended'], 'title': bookmark['description'], 'shared': [bookmark['shared']=='yes' and True or False][0], 'to_read': [bookmark['toread']=='yes' and True or False][0], 'time': datetime.datetime.strptime(bookmark['time'], '%Y-%m-%dT%H:%M:%SZ'), 'tags': tags } new_bookmark = bookmark_contr.create(**bookmark_attr) nb_bookmarks += 1 return nb_bookmarks
def list_(per_page, status='all'): "Lists the bookmarks." head_titles = [gettext("Bookmarks")] user_id = None filters = {} tag = request.args.get('tag', None) if tag: filters['tags_proxy__contains'] = tag query = request.args.get('query', None) if query: query_regex = '%' + query + '%' filters['__or__'] = {'title__ilike': query_regex, 'description__ilike': query_regex} if current_user.is_authenticated: # query for the bookmarks of the authenticated user user_id = current_user.id if status == 'public': # load public bookmarks only filters['shared'] = True elif status == 'private': # load private bookmarks only filters['shared'] = False else: # no filter: load shared and public bookmarks pass if status == 'unread': filters['to_read'] = True else: pass else: # query for the shared bookmarks (of all users) head_titles = [gettext("Recent bookmarks")] not_created_before = datetime.datetime.today() - \ datetime.timedelta(days=900) filters['time__gt'] = not_created_before # only "recent" bookmarks filters['shared'] = True bookmarks = BookmarkController(user_id) \ .read(**filters) \ .order_by(desc('time')) #tag_contr = BookmarkTagController(user_id) #tag_contr.read().join(bookmarks).all() page, per_page, offset = get_page_args() pagination = Pagination(page=page, total=bookmarks.count(), css_framework='bootstrap3', search=False, record_name='bookmarks', per_page=per_page) return render_template('bookmarks.html', head_titles=head_titles, bookmarks=bookmarks.offset(offset).limit(per_page), pagination=pagination, tag=tag, query=query)
def export_bookmarks(user): """Export all bookmarks of a user (compatible with Pinboard). """ bookmark_contr = BookmarkController(user.id) bookmarks = bookmark_contr.read() export = [] for bookmark in bookmarks: export.append({ 'href': bookmark.href, 'description': bookmark.description, 'title': bookmark.title, 'shared': 'yes' if bookmark.shared else 'no', 'toread': 'yes' if bookmark.to_read else 'no', 'time': bookmark.time.isoformat(), 'tags': ' '.join(bookmark.tags_proxy) }) return jsonify(export)
def process_form(bookmark_id=None): "Process the creation/edition of bookmarks." form = BookmarkForm() bookmark_contr = BookmarkController(current_user.id) tag_contr = BookmarkTagController(current_user.id) if not form.validate(): return render_template('edit_bookmark.html', form=form) if form.title.data == '': title = form.href.data else: title = form.title.data bookmark_attr = {'href': form.href.data, 'description': form.description.data, 'title': title, 'shared': form.shared.data, 'to_read': form.to_read.data} if bookmark_id is not None: tags = [] for tag in form.tags.data.split(','): new_tag = tag_contr.create(text=tag.strip(), user_id=current_user.id, bookmark_id=bookmark_id) tags.append(new_tag) bookmark_attr['tags'] = tags bookmark_contr.update({'id': bookmark_id}, bookmark_attr) flash(gettext('Bookmark successfully updated.'), 'success') return redirect(url_for('bookmark.form', bookmark_id=bookmark_id)) # Create a new bookmark new_bookmark = bookmark_contr.create(**bookmark_attr) tags = [] for tag in form.tags.data.split(','): new_tag = tag_contr.create(text=tag.strip(), user_id=current_user.id, bookmark_id=new_bookmark.id) tags.append(new_tag) bookmark_attr['tags'] = tags bookmark_contr.update({'id': new_bookmark.id}, bookmark_attr) flash(gettext('Bookmark successfully created.'), 'success') return redirect(url_for('bookmark.form', bookmark_id=new_bookmark.id))
def delete_all(): "Delete all bookmarks." bookmark = BookmarkController(current_user.id).read().delete() db.session.commit() flash(gettext("Bookmarks successfully deleted."), 'success') return redirect(redirect_url())
def delete(bookmark_id=None): "Delete a bookmark." bookmark = BookmarkController(current_user.id).delete(bookmark_id) flash(gettext("Bookmark %(bookmark_name)s successfully deleted.", bookmark_name=bookmark.title), 'success') return redirect(url_for('bookmarks.list_'))