Exemplo n.º 1
0
def photo_from_post(request, postcard_id, photo = None):
    data = {}
    if photo:
        data = api_serialize(photo)

    data.update(get_json(request.raw_post_data))

    data['postcard'] = postcard_id

    location = None
    if 'location' in data:
        location = data['location']

    if not photo:
        data['author'] = request.user.id
    else:
        data['author'] = photo.author.id

    # Modified and created cannot be set
    if 'modified' in data: del data['modified']
    if 'created' in data: del data['created']

    try:
        photo = form_validate(forms.PhotoAPIForm, data, instance = photo)
    # If the error is a UUID conflict, return the uri to the client
    except exceptions.APIBadRequest, e:
        error = simplejson.loads(e.message)
        if 'uuid' in error:
            # uuid errors almost always indicate that there's a conflict. find it
            conflicting = models.Photo.objects.get(uuid = data['uuid'])
            if conflicting:
                error['uri'] = conflicting.get_api_uri()
            raise exceptions.APIConflict(simplejson.dumps(error))
        else:
            raise e
Exemplo n.º 2
0
def cast_from_post(request, cast = None):

    data = {}

    # this allows PUT operations to only send the data they
    # are interested in updating
    if cast:
        data = api_serialize(cast)

    data.update(get_json(request.body))

    if not cast:
        data['author'] = request.user.id
    else:
        data['author'] = cast.author.id

    # Tags won't validate correctly, so store them to set later
    tags = None
    if 'tags' in data:
        tags = data['tags']
        del data['tags']

    # Location needs to be set manually
    location = None
    if 'location' in data:
        location = data['location']

        # check to make sure its within
        boundary = models.Boundary.objects.get_default_boundary()
        if boundary and (not Point(location[0], location[1]).within(boundary.bounds)):
            raise exceptions.APIBadRequest('{"location":"outside default boundary"}')

        del data['location']

    # Modified and created cannot be set
    if 'modified' in data: del data['modified']
    if 'created' in data: del data['created']

    # Maps privacy names to values
    if 'privacy' in data: 
        data['privacy'] = models.Cast.get_privacy_value(data['privacy'])

    cast = form_validate(forms.CastAPIForm, data, instance = cast)

    if not tags == None:
        # Clear all tags
        if tags == "":
            cast.tags.all().delete()
        cast.set_tags(tags)

    if location:
        cast.set_location(location[0], location[1])

    cast.save()

    return cast
Exemplo n.º 3
0
    def post_media(request, cast_id):
        data = get_json(request.body)
        cast = get_object(models.Cast, cast_id)
        
        if not cast.allowed_edit(request.user):
            raise exceptions.APIForbidden

        if 'content_type' in data:
            content_type = data['content_type'] 
            del data['content_type']

        else:
            raise exceptions.APIBadRequest('missing "content_type"')

        data['language'] = translation.get_language()
        data['author'] = request.user.id

        form_model = None
        if content_type == 'videomedia':
            form_model = forms.VideoMediaForm
            
        elif content_type == 'imagemedia':
            form_model = forms.ImageMediaForm

        elif content_type == 'linkedmedia':
            form_model = forms.LinkedMediaForm

        media = form_validate(form_model, data)
        cast.media_set.add(media)

        models.UserActivity.objects.create_activity(request.user, media, 'created')

        # this is needed as for some odd reason, it doesn't return an object with author, modified, etc.
        # TODO investigate why this is needed, perhaps it can be fixed by using media.content
        media = get_object(models.Media, media.id)

        return APIResponseCreated(content=api_serialize(media, request), location=media.get_api_uri())
Exemplo n.º 4
0
    def post_media(request, cast_id):
        data = get_json(request.body)
        cast = get_object(models.Cast, cast_id)
        
        if not cast.allowed_edit(request.user):
            raise exceptions.APIForbidden

        if 'content_type' in data:
            content_type = data['content_type'] 
            del data['content_type']

        else:
            raise exceptions.APIBadRequest('missing "content_type"')

        data['language'] = translation.get_language()
        data['author'] = request.user.id

        form_model = None
        if content_type == 'videomedia':
            form_model = forms.VideoMediaForm
            
        elif content_type == 'imagemedia':
            form_model = forms.ImageMediaForm

        elif content_type == 'linkedmedia':
            form_model = forms.LinkedMediaForm

        media = form_validate(form_model, data)
        cast.media_set.add(media)

        models.UserActivity.objects.create_activity(request.user, media, 'created')

        # this is needed as for some odd reason, it doesn't return an object with author, modified, etc.
        # TODO investigate why this is needed, perhaps it can be fixed by using media.content
        media = get_object(models.Media, media.id)

        return APIResponseCreated(content=api_serialize(media, request), location=media.get_api_uri())
Exemplo n.º 5
0
def cast_from_post(request, cast = None):

    data = {}

    # this allows PUT operations to only send the data they
    # are interested in updating
    if cast:
        data = api_serialize(cast)

    data.update(get_json(request.body))

    if not cast:
        data['author'] = request.user.id
    else:
        data['author'] = cast.author.id

    # Tags won't validate correctly, so store them to set later
    tags = None
    if 'tags' in data:
        tags = data['tags']
        del data['tags']

    # Location needs to be set manually
    location = None
    if 'location' in data:
        location = data['location']

        # check to make sure its within
        boundary = models.Boundary.objects.get_default_boundary()
        if boundary and (not Point(location[0], location[1]).within(boundary.bounds)):
            raise exceptions.APIBadRequest('{"location":"outside default boundary"}')

        del data['location']

    # Modified and created cannot be set
    if 'modified' in data: del data['modified']
    if 'created' in data: del data['created']

    if 'favorite' in data:
        if data['favorite'] == True:
            cast.favorite(request.user);
        else:
            cast.unfavorite(request.user);

        del data['favorite']

    # Maps privacy names to values
    if 'privacy' in data: 
        data['privacy'] = models.Cast.get_privacy_value(data['privacy'])

    cast = form_validate(forms.CastAPIForm, data, instance = cast)

    if not tags == None:
        # Clear all tags
        if tags == "":
            cast.tags.all().delete()
        cast.set_tags(tags)

    if location:
        cast.set_location(location[0], location[1])

    cast.save()

    return cast