Example #1
0
    def post_media_content(request, cast_id, media_id):
        get_object(models.Cast, cast_id)
        media = get_object(models.Media, media_id)

        if not media.author == request.user:
            raise exceptions.APIForbidden

        content_type = get_param(request.META, 'CONTENT_TYPE')
        mime_type = content_type.split(';')[0]

        # Form upload
        if mime_type == 'multipart/form-data':
            # multipart/form-data; boundary=----pluploadboundaryp15s131vjl18tdnlk1sj11min19p42

            file = get_param(request.FILES, 'file')

            if not file:
                #TODO: perhaps if there is no file, and there is an error, delete the original?
                raise exceptions.APIBadRequest('Error uploading file!')

            media.content.file.save(file.name, file, save=True)

        else:
            media.content.create_file_from_data(request.body, mime_type)

        # media is the generic holder, media.content is the specific
        # content model (ImageMedia, VideoMedia etc.).

        media.content.content_state = models.Media.STATE_COMPLETE
        media.content.save()

        return APIResponseOK(content=api_serialize(media, request))
Example #2
0
    def get(request, postcard_id=None, coll_id=None, format='.json'):

        # single postcard
        if postcard_id:
            postcard = get_object(models.Postcard, id=postcard_id)

            if not postcard.allowed_access(request.user):
                raise exceptions.APIForbidden
                
            postcard_dict = api_serialize(postcard, request)
            return APIResponseOK(content=postcard_dict, total=1)

        # multiple postcards
        else:
            base_query = models.Postcard.get_privacy_q(request) & \
               Q(content_state=4) 
            q = qstranslate.QueryTranslator(models.Postcard, ruleset, base_query)
            query = request.GET.copy()
            objs = total = pg = None
            try:
                objs = q.filter(query)
                objs, total, pg = paginate(objs, request.GET)
            except qstranslate.InvalidParameterException, e:
                raise exceptions.APIBadRequest(e.message)

            postcard_arr = []
            for p in objs:
                postcard_arr.append(api_serialize(p, request))

            return APIResponseOK(content=postcard_arr, total=total, pg=pg)
Example #3
0
    def post_comments_flag(request, cast_id, comment_id):
        cast = get_object(models.Cast, id=cast_id)

        comment = comment_api.check_comment(cast, comment_id)
        comment.flag(request.user)

        return APIResponseOK(content='success')
Example #4
0
def check_cast_media(media_id, cast_id):
    cast = get_object(models.Cast, id=cast_id)
    try:
        cast.media_set.get(id=media_id)
    except models.Media.DoesNotExist:
        raise exceptions.APIBadRequest('Media object is not part of cast')
    return cast
Example #5
0
    def post_comments_flag(request, cast_id, comment_id):
        cast = get_object(models.Cast, id=cast_id)

        comment = comment_api.check_comment(cast, comment_id)
        comment.flag(request.user)

        return APIResponseOK(content='success')
Example #6
0
    def get(request, user_id=None):

        # Single user
        if user_id:
            u = get_object(LocastUser, id=user_id)
            content = api_serialize(u)
            return APIResponseOK(content=content, total=1)
        
        # Multiple users
        else:
            q = QueryTranslator(LocastUser, ruleset)
            query = request.GET.copy()

            objs = total = pg = None
            try:
                objs = q.filter(query)
                objs, total, pg = paginate(objs, request.GET)
            except InvalidParameterException, e:
                raise APIBadRequest(e.message)

            user_arr = []
            for m in objs:
                user_arr.append(api_serialize(m, request))

            return APIResponseOK(content=user_arr, total=total, pg=pg)
Example #7
0
    def get_comments(request, cast_id, comment_id=None):
        cast = get_object(models.Cast, id=cast_id)

        if not cast.allowed_access(request.user):
            raise exceptions.APIForbidden

        return comment_api.get_comments(request, cast, comment_id)
Example #8
0
def check_collection(cast_id, coll_id):
    coll = get_object(models.Collection, id=coll_id)
    try:
        coll.related_casts.get(id=cast_id)
    except models.Cast.DoesNotExist:
        raise exceptions.APIBadRequest('Cast is not part of collection')
    return coll
Example #9
0
    def get_comments(request, cast_id, comment_id=None):
        cast = get_object(models.Cast, id=cast_id)

        if not cast.allowed_access(request.user):
            raise exceptions.APIForbidden

        return comment_api.get_comments(request, cast, comment_id)
Example #10
0
def check_collection(cast_id, coll_id):
    coll = get_object(models.Collection, id=coll_id)
    try:
        coll.related_casts.get(id=cast_id)
    except models.Cast.DoesNotExist:
        raise exceptions.APIBadRequest('Cast is not part of collection')
    return coll
Example #11
0
def check_cast_media(media_id, cast_id):
    cast = get_object(models.Cast, id=cast_id)
    try:
        cast.media_set.get(id=media_id)
    except models.Media.DoesNotExist:
        raise exceptions.APIBadRequest('Media object is not part of cast')
    return cast
Example #12
0
def check_postcard_photo(postcard_id, photo_id):
    postcard = get_object(models.Postcard, id=postcard_id)
    try:
        postcard.postcardcontent_set.get(id=photo_id)
    except models.PostcardContent.DoesNotExist:
        raise exceptions.APIBadRequest('Photo is not part of this postcard')

    return postcard
Example #13
0
    def get_media_content(request, cast_id, media_id):
        media = get_object(models.Media, media_id)
        cast = check_cast_media(media_id, cast_id)

        if not cast.allowed_access(request.user):
            raise exceptions.APIForbidden
            
        media_dict = api_serialize(media, request)
        return APIResponseOK(content=media_dict, total=1)
Example #14
0
    def get_media_content(request, cast_id, media_id):
        media = get_object(models.Media, media_id)
        cast = check_cast_media(media_id, cast_id)

        if not cast.allowed_access(request.user):
            raise exceptions.APIForbidden
            
        media_dict = api_serialize(media, request)
        return APIResponseOK(content=media_dict, total=1)
Example #15
0
    def get_photo(request, postcard_id, photo_id = None):
        if photo_id:
            photo = get_object(models.Photo, photo_id)
            check_postcard_photo(postcard_id, photo_id)

            return APIResponseOK(content=api_serialize(photo.contentmodel))

        else:
            postcard = get_object(models.Postcard, postcard_id)

            if not postcard.allowed_access(request.user):
                raise exceptions.APIForbidden

            photo_dicts = []
            for p in postcard.postcardcontent_set.all():
                photo_dicts.append(api_serialize(p, request))

        return APIResponseOK(content=photo_dicts)
Example #16
0
    def post_favorite(request, coll_id):
        coll = get_object(Collection, id=coll_id)
        favorite = get_param(request.POST, 'favorite')

        if favorite:
            coll.favorite(request.user)
        else:
            coll.unfavorite(request.user)

        return APIResponseOK(content='success')
Example #17
0
    def delete_media_content(request, cast_id, media_id):
        media = get_object(models.Media, media_id)
        cast = check_cast_media(media_id, cast_id)

        if not cast.allowed_edit(request.user):
            raise exceptions.APIForbidden

        media.delete()

        return APIResponseOK(content='success')
Example #18
0
    def post_favorite(request, coll_id):
        coll = get_object(Collection, id=coll_id)
        favorite = get_param(request.POST, 'favorite')

        if favorite:
            coll.favorite(request.user)
        else:
            coll.unfavorite(request.user)

        return APIResponseOK(content='success')
Example #19
0
    def delete_media_content(request, cast_id, media_id):
        media = get_object(models.Media, media_id)
        cast = check_cast_media(media_id, cast_id)

        if not cast.allowed_edit(request.user):
            raise exceptions.APIForbidden

        media.delete()

        return APIResponseOK(content='success')
Example #20
0
    def delete(request, postcard_id):

        postcard = get_object(models.Postcard, postcard_id)

        if not postcard.is_author(request.user):
            raise exceptions.APIForbidden

        postcard.delete()

        return APIResponseOK(content='success')
Example #21
0
    def delete(request, cast_id, coll_id=None):

        if coll_id: check_collection(cast_id, coll_id)
        cast = get_object(models.Cast, cast_id)

        if not cast.allowed_edit(request.user):
            raise exceptions.APIForbidden

        cast.delete()

        return APIResponseOK(content='success')
Example #22
0
    def get_media(request, cast_id):
        cast = get_object(models.Cast, cast_id)

        if not cast.allowed_access(request.user):
            raise exceptions.APIForbidden

        media_dicts = []
        for m in cast.media_set.all():
            media_dicts.append(api_serialize(m, request))

        return APIResponseOK(content=media_dicts)
Example #23
0
    def get_media(request, cast_id):
        cast = get_object(models.Cast, cast_id)

        if not cast.allowed_access(request.user):
            raise exceptions.APIForbidden

        media_dicts = []
        for m in cast.media_set.all():
            media_dicts.append(api_serialize(m, request))

        return APIResponseOK(content=media_dicts)
Example #24
0
    def delete(request, cast_id, coll_id=None):

        if coll_id: check_collection(cast_id, coll_id)
        cast = get_object(models.Cast, cast_id)

        if not cast.allowed_edit(request.user):
            raise exceptions.APIForbidden

        cast.delete()

        return APIResponseOK(content='success')
Example #25
0
    def get_geofeature(request, cast_id):
        cast = get_object(models.Cast, id=cast_id)

        if not cast.allowed_access(request.user):
            raise exceptions.APIForbidden

        if cast.location:
            geojson = geojson_serialize(cast, cast.location, request)
            return APIResponseOK(content=geojson)
        else:
            raise exceptions.APINotFound('No location')
Example #26
0
    def get_geofeature(request, cast_id):
        cast = get_object(models.Cast, id=cast_id)

        if not cast.allowed_access(request.user):
            raise exceptions.APIForbidden

        if cast.location:
            geojson = geojson_serialize(cast, cast.location, request)
            return APIResponseOK(content=geojson)
        else:
            raise exceptions.APINotFound('No location')
Example #27
0
    def put(request, cast_id, coll_id=None):

        if coll_id: check_collection(cast_id, coll_id)
        cast = get_object(models.Cast, cast_id)

        if not cast.allowed_edit(request.user):
            raise exceptions.APIForbidden

        cast = cast_from_post(request, cast)
        cast.save()

        return APIResponseOK(content=api_serialize(cast, request))
Example #28
0
    def put(request, cast_id, coll_id=None):

        if coll_id: check_collection(cast_id, coll_id)
        cast = get_object(models.Cast, cast_id)

        if not cast.allowed_edit(request.user):
            raise exceptions.APIForbidden

        cast = cast_from_post(request, cast)
        cast.save()

        return APIResponseOK(content=api_serialize(cast, request))
Example #29
0
    def post_photo(request, postcard_id, photo_id = None):

        postcard = get_object(models.Postcard, id = postcard_id)
        if not postcard.allowed_edit(request.user):
            raise exceptions.APIForbidden

        # If there is a photo_id, posting raw photo data to a photo
        if photo_id:
            photo = get_object(models.Photo, id = photo_id)
            check_postcard_photo(postcard_id, photo_id)

            if not photo.is_author(request.user):
                raise exceptions.APIForbidden

            content_type = get_param(request.META, 'CONTENT_TYPE')
            mime_type = content_type.split(';')[0]

            if not mime_type:
                raise exceptions.APIBadRequest('Invalid file type!')

            try:
                photo.create_file_from_data(request.raw_post_data, mime_type)
            except LocastContent.InvalidMimeType:
                raise exceptions.APIBadRequest('Invalid file type!')

            try:
                import Image
                i = Image.open(photo.file.path)
                i.load()
            except IOError:
                raise exceptions.APIBadRequest('Corrupted image!')

            return APIResponseOK(content=api_serialize(photo.contentmodel))

        # If there is not, posting a new photo object
        else:
            photo = photo_from_post(request, postcard_id)
            photo.save()

            return APIResponseCreated(content=api_serialize(photo.contentmodel, request), location=photo.get_api_uri())
Example #30
0
    def put(request, postcard_id = None):
        if not postcard_id:
            pass

        postcard = get_object(models.Postcard, postcard_id)

        if not postcard.is_author(request.user):
            raise exceptions.APIForbidden

        postcard = postcard_from_post(request, postcard)
        postcard.save()

        return APIResponseOK(content=api_serialize(postcard, request))
Example #31
0
    def put_photo(request, postcard_id, photo_id = None):
        if not photo_id:
            return HttpResponseNotAllowed(['GET', 'POST', 'HEAD'])

        photo = get_object(models.Photo, photo_id)
        check_postcard_photo(postcard_id, photo_id)

        if not photo.allowed_edit(request.user):
            raise exceptions.APIForbidden

        photo = photo_from_post(request, postcard_id, photo)
        photo.save()

        return APIResponseOK(content=api_serialize(photo, request))
Example #32
0
    def delete_photo(request, postcard_id, photo_id = None):
        # currently can't delete all photos at once
        if not photo_id:
            return HttpResponseNotAllowed(['GET', 'POST', 'HEAD'])

        photo = get_object(models.Photo, id = photo_id)
        postcard = check_postcard_photo(postcard_id, photo_id)

        if (not photo.is_author(request.user)) and (not postcard.is_author(request.user)):
            raise exceptions.APIForbidden

        photo.delete()

        return APIResponseOK(content='success')
Example #33
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())
Example #34
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())
Example #35
0
    def post(request, cast_id=None, coll_id=None):

        if cast_id:
            raise exceptions.APIBadRequest('Attempting to post with a cast id specified')

        coll = None
        if coll_id:
            coll = get_object(models.Collection, coll_id)

        cast = cast_from_post(request)

        if coll:
            coll.related_casts.add(cast)

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

        return APIResponseCreated(content=api_serialize(cast, request), location=cast.get_api_uri())
Example #36
0
    def post(request, cast_id=None, coll_id=None):

        if cast_id:
            raise exceptions.APIBadRequest('Attempting to post with a cast id specified')

        coll = None
        if coll_id:
            coll = get_object(models.Collection, coll_id)

        cast = cast_from_post(request)

        if coll:
            coll.related_casts.add(cast)

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

        return APIResponseCreated(content=api_serialize(cast, request), location=cast.get_api_uri())
Example #37
0
    def get(request, coll_id = None):

        if coll_id:
            collection = get_object(Collection, id=coll_id)
            collection_dict = api_serialize(collection, request)
            
            return APIResponseOK(content=collection_dict, total=1)

        else:
            query = request.GET.copy()
            q = QueryTranslator(Collection, ruleset)
            try:
                objs = q.filter(query)
            except InvalidParameterException, e:
                raise APIBadRequest(e)

            objs, total, pg = paginate(objs, query)

            collection_arr = []
            for i in objs:
                collection_arr.append(api_serialize(i, request))

            return APIResponseOK(content=collection_arr, total=total, pg=pg)
Example #38
0
    def get(request, coll_id=None):

        if coll_id:
            collection = get_object(Collection, id=coll_id)
            collection_dict = api_serialize(collection, request)

            return APIResponseOK(content=collection_dict, total=1)

        else:
            query = request.GET.copy()
            q = QueryTranslator(Collection, ruleset)
            try:
                objs = q.filter(query)
            except InvalidParameterException, e:
                raise APIBadRequest(e)

            objs, total, pg = paginate(objs, query)

            collection_arr = []
            for i in objs:
                collection_arr.append(api_serialize(i, request))

            return APIResponseOK(content=collection_arr, total=total, pg=pg)
Example #39
0
    def post_comments(request, cast_id):
        cast = get_object(models.Cast, id=cast_id)
        models.UserActivity.objects.create_activity(request.user, cast, 'commented')

        return comment_api.post_comment(request, cast)
Example #40
0
def update_facebook_likes(request):
    p_id = int(request.POST.get('id', None))
    postcard = get_object(models.Postcard, id=p_id)
    postcard.update_facebook_likes()

    return APIResponseOK(content=api_serialize(postcard))
Example #41
0
    def post_comments(request, cast_id):
        cast = get_object(models.Cast, id=cast_id)
        models.UserActivity.objects.create_activity(request.user, cast, 'commented')

        return comment_api.post_comment(request, cast)
Example #42
0
    def post_flag(request, cast_id):
        cast = get_object(models.Cast, id=cast_id)
        cast.flag(request.user)

        return APIResponseOK(content='success')
Example #43
0
    def get(request, cast_id=None, coll_id=None, format='.json'):

        # single cast
        if cast_id:
            if coll_id: check_collection(cast_id, coll_id)
            cast = get_object(models.Cast, id=cast_id)

            if not cast.allowed_access(request.user):
                raise exceptions.APIForbidden
                
            if format == '.json':
                cast_dict = api_serialize(cast, request)
                return APIResponseOK(content=cast_dict, total=1)
                
            if format == '.html':
                is_flagged = cast.is_flagged_by(request.user)
                is_favorited = cast.is_favorited_by(request.user)
                allowed_edit = cast.allowed_edit(request.user)
                    
                content = render_to_string('ajax/cast_frontpage.django.html', locals(), context_instance = RequestContext(request))
                resp = HttpResponse(content=content)
                return resp
            
            else:
                raise exceptions.APIBadRequest('Invalid response format')

        # multiple casts
        else:
            base_query = models.Cast.get_privacy_q(request)

            if coll_id:
                get_object(models.Collection, id=coll_id)
                base_query = base_query & Q(collection=coll_id)

            q = qstranslate.QueryTranslator(models.Cast, ruleset, base_query)
            query = request.GET.copy()

            # Need to do some magic to order by popularity, so remove from
            # the query that will be sent to qstranslate
            popularity_order = False
            if 'orderby' in query and query['orderby'] == 'popularity':
                popularity_order = True
                del query['orderby']

            objs = total = pg = None
            try:
                objs = q.filter(query)

                # popularity magic!
                if popularity_order:
                    objs = objs.annotate(popularity=Count('favorited_by')).order_by('-popularity')

                objs, total, pg = paginate(objs, request.GET)
            except qstranslate.InvalidParameterException, e:
                raise exceptions.APIBadRequest(e.message)

            cast_arr = []
            for c in objs:
                cast_arr.append(api_serialize(c, request))

            return APIResponseOK(content=cast_arr, total=total, pg=pg)
Example #44
0
    def post_flag(request, cast_id):
        cast = get_object(models.Cast, id=cast_id)
        cast.flag(request.user)

        return APIResponseOK(content='success')
Example #45
0
    def get(request, cast_id=None, coll_id=None, format='.json'):

        # single cast
        if cast_id:
            if coll_id: check_collection(cast_id, coll_id)
            cast = get_object(models.Cast, id=cast_id)

            if not cast.allowed_access(request.user):
                raise exceptions.APIForbidden
                
            if format == '.json':
                cast_dict = api_serialize(cast, request)
                return APIResponseOK(content=cast_dict, total=1)
                
            if format == '.html':
                is_flagged = cast.is_flagged_by(request.user)
                is_favorited = cast.is_favorited_by(request.user)
                allowed_edit = cast.allowed_edit(request.user)
                    
                content = render_to_string('ajax/cast_frontpage.django.html', locals(), context_instance = RequestContext(request))
                resp = HttpResponse(content=content)
                return resp
            
            else:
                raise exceptions.APIBadRequest('Invalid response format')

        # multiple casts
        else:
            base_query = models.Cast.get_privacy_q(request)

            if coll_id:
                get_object(models.Collection, id=coll_id)
                base_query = base_query & Q(collection=coll_id)

            q = qstranslate.QueryTranslator(models.Cast, ruleset, base_query)
            query = request.GET.copy()

            # Need to do some magic to order by popularity, so remove from
            # the query that will be sent to qstranslate
            popularity_order = False
            if 'orderby' in query and query['orderby'] == 'popularity':
                popularity_order = True
                del query['orderby']

            objs = total = pg = None
            try:
                objs = q.filter(query)

                # popularity magic!
                if popularity_order:
                    objs = objs.annotate(popularity=Count('favorited_by')).order_by('-popularity')

                objs, total, pg = paginate(objs, request.GET)
            except qstranslate.InvalidParameterException, e:
                raise exceptions.APIBadRequest(e.message)

            cast_arr = []
            for c in objs:
                cast_arr.append(api_serialize(c, request))

            return APIResponseOK(content=cast_arr, total=total, pg=pg)
Example #46
0
 def post_favorite(request, cast_id):
     cast = get_object(models.Cast, id=cast_id)
     return favorite_api.post_favorite(request, cast)