Ejemplo n.º 1
0
def new_bookmark(request):
    if request.method == 'POST':
        bookmark = Bookmark()
        form = NewBookmarkForm(request.POST, instance=bookmark)
        if form.is_valid():
            bookmark.owner = request.user
            bookmark.save()
            for tag in form.cleaned_data['tags']:
                bookmark.tags.add(tag)
            bookmark.save()
            url = reverse("user-list", args=[request.user.username])
            return HttpResponseRedirect(url)
    else:
        form = NewBookmarkForm()
    template_name = "bookmarks/new_bookmark.html"
    context = {'form': form}
    return(template_name, context)
Ejemplo n.º 2
0
def create_bookmark(bookmark: Bookmark, tag_string: str, current_user: User):
    # If URL is already bookmarked, then update it
    existing_bookmark: Bookmark = Bookmark.objects.filter(
        owner=current_user, url=bookmark.url).first()

    if existing_bookmark is not None:
        _merge_bookmark_data(bookmark, existing_bookmark)
        return update_bookmark(existing_bookmark, tag_string, current_user)

    # Update website info
    _update_website_metadata(bookmark)
    # Set currently logged in user as owner
    bookmark.owner = current_user
    # Set dates
    bookmark.date_added = timezone.now()
    bookmark.date_modified = timezone.now()
    bookmark.save()
    # Update tag list
    _update_bookmark_tags(bookmark, tag_string, current_user)
    bookmark.save()
    return bookmark
Ejemplo n.º 3
0
def delicious_import(request):
    if request.method == 'POST':
        form = DeliciousImportForm(request.POST, request.FILES)
        if form.is_valid():
            try:
                soup = BeautifulStoneSoup(request.FILES['delicious_html'],
                    convertEntities="html", smartQuotesTo="html")
            except TypeError:
                raise forms.ValidationError("Not a delicious HTML file")
            rows = soup.findAll(['dt','dd'])
            for row in rows:
                if row.name == 'dd':
                    continue
                else:
                    bookmark = Bookmark()
                    link = row.first()
                    bookmark.title = link.text
                    bookmark.url = link['href']
                    bookmark.owner = request.user
                    pub_date = datetime.utcfromtimestamp(int(link['add_date']))
                    bookmark.pub_date = pub_date
                    bookmark.private = link['private'] == u"1"
                    if row.find('dd'):
                        bookmark.description = row.find('dd').text
                    bookmark.save()
                    if link.has_key('tags'):
                        tags = link['tags'].split(',')
                        for tag in tags:
                            if len(tag) > 0:
                                bookmark.tags.add(tag)
                        bookmark.save()
            url = reverse("user-list", args=[request.user.username])
            return HttpResponseRedirect(url)
    else:
        form = DeliciousImportForm()
    template_name = "bookmarks/delicious_import.html"
    context = {'form': form}
    return(template_name, context)