Beispiel #1
0
def handle_add_to_reading_list(request):
    user = request.user
    if not user.is_authenticated:
        return JsonResponse(data={'error': 'Invalid request.'}, status=403)
    link = request.POST['link']
    update_add_to_reading_list_status(user, link, 0)

    # handle validation error
    try:
        add_to_reading_list(user, link)
    except ValidationError:
        return JsonResponse(data={'error': 'Invalid URL.'}, status=400)
    except json.decoder.JSONDecodeError:
        return JsonResponse(data={'error': 'Invalid URL.'}, status=400)
    except Exception:
        traceback.print_exc()
        update_add_to_reading_list_status(user, link, 100)
        return JsonResponse(data={
            'error':
            'Error while adding to reading list. Try again or contact support.'
        },
                            status=400)

    update_add_to_reading_list_status(user, link, 100)
    return JsonResponse(get_reading_list(user), safe=False)
Beispiel #2
0
def update_deliver(request):
    user = request.user

    if not user.is_authenticated:
        return JsonResponse(data={'error': 'Invalid request.'}, status=403)

    link = request.POST['permalink']
    to_deliver = request.POST.get('to_deliver') == 'true'
    # Get Article to get Reading list item

    try:
        article = Article.objects.get(permalink=link)
    except Article.DoesNotExist:
        raise NotFound(detail='Article not found', code=404)

    if to_deliver:
        # if overflow over page limit, then force to_deliver to False
        to_deliver = decide_to_deliver(user, link, article.page_count)

    try:
        reading_list_item = ReadingListItem.objects.get(article=article,
                                                        reader=user)
        reading_list_item.to_deliver = to_deliver
        reading_list_item.save()
        return JsonResponse(get_reading_list(user), safe=False)
    except ReadingListItem.DoesNotExist:
        raise NotFound(detail='ReadingListItem with link: %s not found.' %
                       link,
                       code=404)
Beispiel #3
0
def remove_from_reading_list(request):
    user = request.user
    if not user.is_authenticated:
        return JsonResponse(data={'error': 'Invalid request.'}, status=403)

    link = request.POST['link']
    try:
        article = Article.objects.get(permalink=link)
    except Article.DoesNotExist:
        raise NotFound(detail='Article not found', code=404)
    try:
        reading_list_item = ReadingListItem.objects.get(article=article,
                                                        reader=user)
        reading_list_item.delete()
    except ReadingListItem.DoesNotExist:
        raise NotFound(detail='ReadingListItem with link: %s not found.' %
                       link,
                       code=404)

    # Remove from instapaper sync
    try:
        credentials = InstapaperCredentials.objects.get(owner=user)
        polled_bookmarks = credentials.polled_bookmarks
        polled_bookmarks.pop(link, None)
        credentials.save()
    except InstapaperCredentials.DoesNotExist:
        # nothing to remove
        pass
    return JsonResponse(get_reading_list(user), safe=False)
Beispiel #4
0
def update_reading_list(user):
    from reading_list.utils import get_reading_list
    channel_layer = get_channel_layer()
    channel_id = get_channel_id(user.email)
    reading_list = get_reading_list(user)

    async_to_sync(channel_layer.group_send)(channel_id, {
        'type': 'reading_list',
        'reading_list': reading_list,
    })
Beispiel #5
0
def archive_item(request):
    user = request.user
    if not user.is_authenticated:
        return JsonResponse(data={'error': 'Invalid request.'}, status=403)
    link = request.POST['link']
    try:
        article = Article.objects.get(permalink=link)
    except Article.DoesNotExist:
        raise NotFound(detail='Article not found', code=404)
    try:
        reading_list_item = ReadingListItem.objects.get(article=article,
                                                        reader=user)
        reading_list_item.archived = True
        reading_list_item.save()
        return JsonResponse(get_reading_list(user), safe=False)
    except ReadingListItem.DoesNotExist:
        raise NotFound(detail='ReadingListItem with link: %s not found.' %
                       link,
                       code=404)
Beispiel #6
0
def get_reading(request):
    user = request.user
    if not user.is_authenticated:
        return JsonResponse(data={'error': 'Invalid request.'}, status=403)
    return JsonResponse(get_reading_list(user), safe=False)