Example #1
0
    def test_bookmark_form_should_validate_required_fields(self):
        form = BookmarkForm(data={'url': ''})

        self.assertEqual(len(form.errors), 1)
        self.assertIn('required', str(form.errors))

        form = BookmarkForm(data={'url': None})

        self.assertEqual(len(form.errors), 1)
        self.assertIn('required', str(form.errors))
Example #2
0
def edit(request, bookmark_id: int):
    bookmark = Bookmark.objects.get(pk=bookmark_id)

    if request.method == 'POST':
        form = BookmarkForm(request.POST, instance=bookmark)
        return_url = form.data['return_url']
        if form.is_valid():
            update_bookmark(form.save(commit=False), form.data['tag_string'],
                            request.user)
            return HttpResponseRedirect(return_url)
    else:
        return_url = request.GET.get('return_url')
        form = BookmarkForm(instance=bookmark)

    return_url = return_url if return_url else reverse('bookmarks:index')

    form.initial['tag_string'] = build_tag_string(bookmark.tag_names, ' ')
    form.initial['return_url'] = return_url
    all_tags = get_user_tags(request.user)

    context = {
        'form': form,
        'bookmark_id': bookmark_id,
        'all_tags': all_tags,
        'return_url': return_url
    }

    return render(request, 'bookmarks/edit.html', context)
Example #3
0
def new(request):
    initial_url = request.GET.get('url')
    initial_auto_close = 'auto_close' in request.GET

    if request.method == 'POST':
        form = BookmarkForm(request.POST)
        auto_close = form.data['auto_close']
        if form.is_valid():
            current_user = request.user
            create_bookmark(form.save(commit=False), form.data['tag_string'],
                            current_user)
            if auto_close:
                return HttpResponseRedirect(reverse('bookmarks:close'))
            else:
                return HttpResponseRedirect(reverse('bookmarks:index'))
    else:
        form = BookmarkForm()
        if initial_url:
            form.initial['url'] = initial_url
        if initial_auto_close:
            form.initial['auto_close'] = 'true'

    all_tags = get_user_tags(request.user)
    context = {
        'form': form,
        'auto_close': initial_auto_close,
        'all_tags': all_tags,
        'return_url': reverse('bookmarks:index')
    }

    return render(request, 'bookmarks/new.html', context)
Example #4
0
def update_bookmark(form: BookmarkForm, current_user: User):
    bookmark = form.save(commit=False)
    # Update website info
    _update_website_metadata(bookmark)
    # Update tag list
    _update_bookmark_tags(bookmark, form.data['tag_string'], current_user)
    # Update dates
    bookmark.date_modified = timezone.now()
    bookmark.save()
Example #5
0
    def _run_bookmark_form_url_validity_checks(self, cases):
        for case in cases:
            url, expectation = case
            form = BookmarkForm(data={'url': url})

            if expectation:
                self.assertEqual(len(form.errors), 0)
            else:
                self.assertEqual(len(form.errors), 1)
                self.assertIn('Enter a valid URL', str(form.errors))
Example #6
0
def create_bookmark(form: BookmarkForm, current_user: User):
    # If URL is already bookmarked, then update it
    existing_bookmark = Bookmark.objects.filter(owner=current_user,
                                                url=form.data['url']).first()

    if existing_bookmark is not None:
        update_form = BookmarkForm(data=form.data, instance=existing_bookmark)
        update_bookmark(update_form, current_user)
        return

    bookmark = form.save(commit=False)
    # 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, form.data['tag_string'], current_user)
    bookmark.save()
Example #7
0
def create_bookmark(form: BookmarkForm, current_user: User):
    bookmark = form.save(commit=False)
    # 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, form.data['tag_string'], current_user)
    bookmark.save()
Example #8
0
def edit(request, bookmark_id: int):
    bookmark = Bookmark.objects.get(pk=bookmark_id)
    if request.method == 'POST':
        form = BookmarkForm(request.POST, instance=bookmark)
        if form.is_valid():
            update_bookmark(form, request.user)
            return HttpResponseRedirect(reverse('bookmarks:index'))
    else:
        form = BookmarkForm(instance=bookmark)

    form.initial['tag_string'] = build_tag_string(bookmark.tag_names, ' ')

    all_tags = get_user_tags(request.user)
    context = {'form': form, 'bookmark_id': bookmark_id, 'all_tags': all_tags}

    return render(request, 'bookmarks/edit.html', context)