Exemplo n.º 1
0
def edit_image(request):
    if request.method != 'POST':
        return redirect(main_map)
    context = {}
    errs = []
    context['errors'] = errs

    # Get lat/lng/caption data
    lat = request.POST.get('lat')
    lng = request.POST.get('lng')
    caption = request.POST.get('caption')
    im_id = request.POST.get('img_id')
    albums_str = request.POST.get('edit-album')
    # Parse albums string into array of ints
    try:
        albums = [int(s) for s in albums_str.split(',')]
    except:
        albums = []
    username = request.user.username

    image,errors = Image.update(im_id=im_id,
        username=username, lat=lat, lng=lng, caption=caption, albums=albums)
    context['image'] = image

    if errors:
        print(errors)
        errs.extend(errors)
    else:
        context['message'] = "Image successfully updated!"

    return render(request, 'json/upload_response.json', context, content_type="application/json")
Exemplo n.º 2
0
def upload(request):
    if request.method != 'POST':
        return redirect(main_map)
    context = {}
    errs = []
    context['errors'] = errs

    # Get either external or file pic
    if request.POST.get('external'):
        url = request.POST.get('url')
        img_temp = NamedTemporaryFile()
        try:
            img_temp.write(urlopen(url).read())
            img_temp.flush()
            pic = File(img_temp)
        except URLError as e:
            # Error reading file
            pic = None
    else:
        pic = request.FILES.get('pic')

    # Get lat/lng/caption data
    lat = request.POST.get('lat')
    lng = request.POST.get('lng')
    caption = request.POST.get('caption', '')
    user = request.user
    albums_str = request.POST.get('upload-album')
    # Parse albums string into array of ints
    try:
        albums = [int(s) for s in albums_str.split(',')]
    except:
        albums = []

    # Try to create the image
    image,errors = Image.create(username=user.username,
        image=pic, lat=lat, lng=lng, caption=caption, albums=albums)
    context['image'] = image

    if errors:
        print(errors)
        errs.extend(errors)
    else:
        context['message'] = "Image successfully uploaded!"

    return render(request, 'json/upload_response.json', context, content_type="application/json")
Exemplo n.º 3
0
def patch_image(request):
    try:
        data = json.loads(request.body.decode('utf-8')).get('data')
    except:
        return JsonResponse(bad_format_errors(["Could not parse JSON body"]), status=400)

    if not data:
        return JsonResponse(bad_format_errors(["Missing 'data' component of request"]), status=400)

    api_key = request.GET.get('api_key')
    # Does this api key exist?
    try:
        profile = Profile.objects.get(api_key=api_key)
    except:
        return JsonResponse(bad_api_key_error, status=403)

    image_id = data.get('id')

    # Try to get image with this id
    try:
        image = Image.objects.get(id=image_id)
    except:
        return JsonResponse(not_found_error("Image not found"), status=404)

    # If incorrect user, return not found
    if image.user != profile.user:
        return JsonResponse(not_found_error("Image not found"), status=404)

    # Retrieve attribute data
    attributes = data.get('attributes', {})

    lat = attributes.get('lat')
    lng = attributes.get('lng')
    caption = attributes.get('caption', None)

    # Retrieve new album data
    relationships = data.get('relationships')
    if relationships:
        album_diff = relationships.get('albums', {})
        add = album_diff.get('add', [])
        remove = album_diff.get('remove', [])

        try:
            curr_album_ids = image.album_ids()

            add_ids = [a.get('id') for a in add if a.get('id') not in curr_album_ids]
            remove_ids = [a.get('id') for a in remove]

            # Add new elements to list
            curr_album_ids.extend(add_ids)

            # Remove elements to remove
            album_ids = [x for x in curr_album_ids if x not in remove_ids]
        except:
            return JsonResponse(bad_format_errors(["Malformed 'album' relationship objects"]), status=400)
    else:
        album_ids = image.album_ids()

    image,errs = Image.update(im_id=image_id,
        username=profile.user.username, lat=lat, lng=lng, caption=caption, albums=album_ids)

    if errs:
        return JsonResponse(bad_format_errors(errs), status=400)

    # Create response object
    context = {
        'data': image.as_dict(True,True)
    }
    return JsonResponse(context)
Exemplo n.º 4
0
def post_image(request):
    try:
        data = json.loads(request.body.decode('utf-8')).get('data')
    except:
        return JsonResponse(bad_format_errors(["Could not parse JSON body"]), status=400)

    if not data:
        return JsonResponse(bad_format_errors(["Missing 'data' component of request"]), status=400)

    api_key = request.GET.get('api_key')
    # Does this api key exist?
    try:
        profile = Profile.objects.get(api_key=api_key)
    except:
        return JsonResponse(bad_api_key_error, status=403)


    attributes = data.get('attributes')
    try:
        src = attributes.get('src')
        lat = attributes.get('lat')
        lng = attributes.get('lng')
        caption = attributes.get('caption')
    except:
        return JsonResponse(bad_format_errors(["Missing 'attributes' component of request data"]), status=400)

    relationships = data.get('relationships')
    if relationships:
        album_objects = relationships.get('albums', [])

        try:
            album_ids = [a.get('id') for a in album_objects]
        except:
            return JsonResponse(bad_format_errors(["Malformed 'album' relationship objects"]), status=400)
    else:
        album_ids = []

    src_type = data.get('src_type')

    if src_type == 'url':
        img_temp = NamedTemporaryFile()
        try:
            img_temp.write(urlopen(src).read())
        except URLError as e:
            # Thrown if there's a network error
            return JsonResponse(bad_format_errors(["Error with image source: %s" % (e.reason)]), status=400)
        img_temp.flush()
        pic = File(img_temp)
    else:
        return JsonResponse(bad_format_errors(["Invalid src_type parameter"]), status=400)

    image,errs = Image.create(profile.user.username, pic, lat, lng, caption, album_ids)

    if errs:
        return JsonResponse(bad_format_errors(errs), status=400)

    # Create response object
    context = {
        'data': image.as_dict(True,True)
    }
    return JsonResponse(context, status=201)
Exemplo n.º 5
0
    def test_create_bad_lat(self):
        i,err = Image.create(username='******', image=self.file_mock, lat="foo", lng=15.0, caption='hey')

        self.assertEqual(i, None)
Exemplo n.º 6
0
    def test_create_no_lng(self):
        i,err = Image.create(username='******', image=self.file_mock, lat=10.0, lng=None, caption='hey')

        self.assertEqual(i, None)
Exemplo n.º 7
0
    def test_create_no_file(self):
        i,err = Image.create(username='******', image=None, lat=15.0, lng=10.0, caption='hey')

        self.assertEqual(i, None)
Exemplo n.º 8
0
    def test_create_username_no_exist(self):
        i,err = Image.create(username='******', image=self.file_mock, lat=15.0, lng=10.0, caption='hey')

        self.assertEqual(i, None)