Exemplo n.º 1
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)
Exemplo n.º 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)
Exemplo n.º 3
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))
Exemplo n.º 4
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.º 5
0
 def _api_serialize(self, request=None):
     return dict(
         user=self.user.username,
         time=unicode(self.time),
         action=self.action,
         object_type = ContentType.objects.get_for_model(self.content_object).model,
         object=api_serialize(self.content_object, request=None))
 def _api_serialize(self, request=None):
     return dict(
         user=self.user.username, 
         time=unicode(self.time),
         action=self.action,
         object_type = ContentType.objects.get_for_model(self.content_object).model,
         object=api_serialize(self.content_object, request=None))
Exemplo n.º 7
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)
Exemplo n.º 8
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)
Exemplo n.º 9
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)
Exemplo n.º 10
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.º 11
0
    def _api_serialize(self, request):
        d = Syncable._api_serialize(self, request)

        author = api_serialize(self.author)
        d['author'] = author

        if request:
            d['is_author'] = self.is_author(request.user)
            d['allowed_edit'] = self.allowed_edit(request.user)

        return d
Exemplo n.º 12
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)
Exemplo n.º 13
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)
Exemplo n.º 14
0
    def _api_serialize(self, request):
        d = Syncable._api_serialize(self, request)

        author = api_serialize(self.author)
        d['author'] = author

        if request:
            d['is_author'] = self.is_author(request.user)
            d['allowed_edit'] = self.allowed_edit(request.user)

        return d
Exemplo n.º 15
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))
Exemplo n.º 16
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))
Exemplo n.º 17
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())
    def _api_serialize(self, request):
        d = {}

        author = api_serialize(self.author)
        d['author'] = author
        d['created'] = datetostr(self.created)
        d['modified'] = datetostr(self.modified)

        if request:
            d['is_author'] = self.is_author(request.user)
            d['allowed_edit'] = self.allowed_edit(request.user)

        return d
Exemplo n.º 19
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))
    def _api_serialize(self, request):
        d = {}

        author = api_serialize(self.author)
        d['author'] = author
        d['created'] = datetostr(self.created)
        d['modified'] = datetostr(self.modified)

        if request:
            d['is_author'] = self.is_author(request.user)
            d['allowed_edit'] = self.allowed_edit(request.user)

        return d
Exemplo n.º 21
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)
Exemplo n.º 22
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)
Exemplo n.º 23
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))
Exemplo n.º 24
0
def traveler_js(request):
    boundary_obj = Boundary.objects.get_default_boundary()
    boundary = 'null';

    mp_list = []
    # The default one should go first
    for mp in MapPlace.objects.order_by('-default'):
        mp_list.append(api_serialize(mp))
        
    map_place_json = json.dumps(mp_list)

    if boundary_obj:
        boundary = boundary_obj.bounds.geojson

    return render_to_response('traveler.django.js', locals(), 
        context_instance = RequestContext(request), mimetype='text/javascript')
Exemplo n.º 25
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())
Exemplo n.º 26
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())
Exemplo n.º 27
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.º 28
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.º 29
0
 def get_me(request):
     return APIResponseOK(content=api_serialize(request.user))
    def _api_serialize(self, request):
        d = Authorable._api_serialize(self, request)
        d['author'] = api_serialize(self.author)
        d['content'] = self.body

        return d
Exemplo n.º 31
0
    def _api_serialize(self, request):
        d = Authorable._api_serialize(self, request)
        d["author"] = api_serialize(self.author)
        d["content"] = self.body

        return d
Exemplo n.º 32
0
    def _api_serialize(self, request):
        d = Authorable._api_serialize(self, request)
        d['author'] = api_serialize(self.author)
        d['content'] = self.body

        return d
Exemplo n.º 33
0
    def post(request):
        postcard = postcard_from_post(request)
        postcard.save()
        # models.UserActivity.objects.create_activity(request.user, cast, 'created')

        return APIResponseCreated(content=api_serialize(postcard, request), location=postcard.get_api_uri())
Exemplo n.º 34
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)
Exemplo n.º 35
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))
Exemplo n.º 36
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
Exemplo n.º 37
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)