Example #1
0
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
Example #2
0
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))
Example #3
0
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)
        try:
            # Pinboard format
            description = bookmark["extended"]
            time = datetime.datetime.strptime(bookmark["time"],
                                              "%Y-%m-%dT%H:%M:%SZ")
        except Exception:
            description = bookmark["description"]
            time = datetime.datetime.strptime(bookmark["time"],
                                              "%Y-%m-%dT%H:%M:%S")
        bookmark_attr = {
            "href": bookmark["href"],
            "description": description,
            "title": bookmark["title"],
            "shared": [bookmark["shared"] == "yes" and True or False][0],
            "to_read": [bookmark["toread"] == "yes" and True or False][0],
            "time": time,
            "tags": tags,
        }
        new_bookmark = bookmark_contr.create(**bookmark_attr)
        nb_bookmarks += 1
    return nb_bookmarks
Example #4
0
File: data.py Project: JARR/JARR
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
Example #5
0
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))
Example #6
0
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))
Example #7
0
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))
Example #8
0
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))