Пример #1
0
def new(url, user_id, description=None, tags=None, title=None, added=None):
    new_bookmark = Bookmark()
    new_bookmark.main_url = url[:2000]
    if title is not None:
        new_bookmark.title = title[:1024]
    if description is not None:
        new_bookmark.description = description[:256]
    new_bookmark.user = user_id
    if added is None:
        new_bookmark.added_on = datetime.utcnow()
    else:
        try:
            datetime.datetime.utcfromtimestamp(added)
            new_bookmark.added_on = added  # UNIX timestamp in seconds since epoch, only
        except ValueError:
            new_bookmark.added_on = datetime.utcnow()
    new_bookmark.deleted = False
    if tags is not None:
        tags = tags.split(',')
        new_bookmark.tags = tags
        for tag in tags:
            # If tag is present, increment counter by one, or create if not present
            get_tag = Tag.query.filter_by(text=tag,
                                          user=user_id).first()
            if not get_tag:
                new_tag = Tag(text=tag, user=user_id)
                new_tag.count = 1
                db.session.add(new_tag)
            else:
                get_tag.count += 1
    db.session.add(new_bookmark)
    db.session.commit()
    # Send off for archiving
    archive.do_archives(new_bookmark)
    return new_bookmark