Exemple #1
0
def me_picture_view(request):
    """
    Save the URL of a picture.
    """
    pic_url = request.POST.get('pic_url', '')
    bg_url = request.POST.get('bg_url', '')
    if not pic_url and not bg_url:
        return HttpResponse('Please add the URL for a picture.')

    # TODO: check for valid URL schema.

    # If imgur pic, set "medium" size for normal pics, "large" for backgrounds.
    try:
        if pic_url:
            pic_url = set_imgur_url(pic_url, size='m')
            assert_pic_accessible(pic_url)
        elif bg_url:
            assert_pic_accessible(set_imgur_url(bg_url, size='l'))
    except PictureInaccessibleError as e:
        return HttpResponse(e)

    if pic_url:
        if pic_url in [x['url'] for x in request.user.profile.pics]:
            return HttpResponse('That picture already exists in your profile.')
        if len(request.user.profile.pics) >= settings.USER_MAX_PICS_COUNT:
            messages.info(request, 'oldest picture was deleted to make room '
                          'for the picture you added.')
        else:
            # messages.info(request, 'picture added.')
            pass

        # prepend the new pic to make it the new "profile pic"
        request.user.profile.pics = \
            [{'url': pic_url}] + request.user.profile.pics

    if bg_url:
        request.user.profile.background_pic = bg_url
        messages.info(request, 'background picture updated.')

    request.user.profile.save()

    if request.is_ajax():
        return HttpResponse()  # HTTP 200

    return redirect(reverse('me_page') + '#id_pics')
Exemple #2
0
def me_picture_view(request):
    """
    Save the URL of a picture.
    """
    allowed_content_types = ['image/jpeg', 'image/gif', 'image/webp',
                             'image/png']
    pic_url = request.POST.get('pic_url', '')
    if not pic_url:
        return HttpResponse('Please add the URL for a picture.')

    # TODO: check for valid URL schema.

    # If imgur.com picture, set to "medium" size.
    pic_url = set_imgur_url(pic_url, size='m')
    # Check for HTTP 200 response on that URL, load time,
    # file size, file type, etc.
    try:
        r = requests.head(pic_url, timeout=5)  # ? sec timeout
    except:
        return HttpResponse(
            'The image {} is loading too slowly.'.format(pic_url))

    if r.status_code == 302 and 'imgur.com' in pic_url:
        return HttpResponse('The image at "{}" can not be accessed, it was '
                            '<b>probably deleted on Imgur</b>.'.
                            format(pic_url))
    if r.status_code != 200:
        return HttpResponse('The image "{}"" can not be accessed, it returned '
                            'HTTP status code "{}".'.
                            format(pic_url, r.status_code))
    if r.headers.get('content-type', None) not in allowed_content_types:
        return HttpResponse('Not recognized as an image file. Please only '
                            'use jpg, gif, png, or webp images. Recognized '
                            'mime type was "{}".'.
                            format(r.headers.get('content-type', '')))
    if force_int(r.headers.get('content-length')) > (1024 * 512):
        x = int(int(r.headers.get('content-length')) / 1024)
        return HttpResponse('The image file size ({} kiB) is too large. '
                            'Please use a smaller size (max. 500 kiB).'.
                            format(x))
    if pic_url in [x['url'] for x in request.user.profile.pics]:
        return HttpResponse('That picture already exists in your profile.')

    if len(request.user.profile.pics) >= settings.USER_MAX_PICS_COUNT:
        messages.info(request, 'oldest picture was deleted to make room for '
                               'the picture you added.')

    # prepend the new pic to make it the new "profile pic"
    request.user.profile.pics = [{'url': pic_url}] + request.user.profile.pics
    request.user.profile.save()

    return redirect(reverse('me_page') + '#id_pics')
Exemple #3
0
    def test_set_imgur_url(self):
        orig = ('https://i.imgur.com/kMoI9Vn.jpg',
                'https://i.imgur.com/f7VXJQF',
                'https://imgur.com/S1dZBPm',
                'https://imgur.com/gallery/HFoOCeg',
                'http://redddate.com/static/nopic.jpg')

        mres = ('https://i.imgur.com/kMoI9Vnm.jpg',
                'https://i.imgur.com/f7VXJQFm.jpg',
                'https://i.imgur.com/S1dZBPmm.jpg',
                'https://i.imgur.com/HFoOCegm.jpg',
                'http://redddate.com/static/nopic.jpg')

        tres = ('https://i.imgur.com/kMoI9Vnt.jpg',
                'https://i.imgur.com/f7VXJQFt.jpg',
                'https://i.imgur.com/S1dZBPmt.jpg',
                'https://i.imgur.com/HFoOCegt.jpg',
                'http://redddate.com/static/nopic.jpg')

        for i in range(len(orig)):
            res = toolbox.set_imgur_url(orig[i], 'm')
            self.assertEqual(res, mres[i])
            res = toolbox.set_imgur_url(orig[i], 't')
            self.assertEqual(res, tres[i])
Exemple #4
0
def me_picture_view(request):
    """
    Save the URL of a picture.
    """
    allowed_content_types = ['image/jpeg', 'image/gif', 'image/webp',
                             'image/png']
    pic_url = request.POST.get('pic_url', '')
    if not pic_url:
        return HttpResponse('Please add the URL for a picture.')

    # TODO: check for valid URL schema.

    # If imgur.com picture, set to "medium" size.
    pic_url = set_imgur_url(pic_url, size='m')
    # Check for HTTP 200 response on that URL, load time,
    # file size, file type, etc.
    try:
        r = requests.head(pic_url, timeout=5)  # ? sec timeout
    except:
        return HttpResponse(
            'The image {} is loading too slowly.'.format(pic_url))

    if r.status_code != 200:
        return HttpResponse('The image "{}"" can not be accessed, it returned '
                            'HTTP status code "{}".'.
                            format(pic_url, r.status_code))
    if r.headers.get('content-type', None) not in allowed_content_types:
        return HttpResponse('Not recognized as an image file. Please only '
                            'use jpg, gif, png, or webp images. Recognized '
                            'mime type was "{}".'.
                            format(r.headers.get('content-type', '')))
    if force_int(r.headers.get('content-length')) > (1024 * 512):
        x = int(int(r.headers.get('content-length')) / 1024)
        return HttpResponse('The image file size ({} kiB) is too large. '
                            'Please use a smaller size (max. 500 kiB).'.
                            format(x))
    if pic_url in [x['url'] for x in request.user.profile.pics]:
        return HttpResponse('That picture already exists in your profile.')

    if len(request.user.profile.pics) >= settings.USER_MAX_PICS_COUNT:
        messages.info(request, 'oldest picture was deleted to make room for '
                               'the picture you added.')

    # prepend the new pic to make it the new "profile pic"
    request.user.profile.pics = [{'url': pic_url}] + request.user.profile.pics
    request.user.profile.save()

    return redirect(reverse('me_page') + '#id_pics')
Exemple #5
0
 def display_background_pic(self):
     """
     Return the URL of the background_pic with the correct size Byte in
     case of Imgur pictures.
     """
     return set_imgur_url(self.background_pic, size="l")