Exemple #1
0
    def add_image_view(self, request):
        """
        Uploadify doesn't properly pass the csrf_token.
        """
        instance_form = self.get_add_image_form()
        form = instance_form(request.POST,
                             request.FILES,
                             prefix="gallery_image")

        new_instance = None
        if form.is_valid():
            # Don't commit because we have other things to do below.
            new_instance = form.save(commit=False)
            gi = GalleryItem()

            # Set the gallery, order, and save
            gallery = Gallery.objects.get(pk=request.POST["gallery_id"])
            gi.gallery = gallery

            gallery_items = gallery.items.order_by("-order").values_list(
                "order", flat=True)
            if gallery_items:
                if gallery_items[0]:
                    gi.order = gallery_items[0] + 1
                else:  #odd edge case with no order
                    gi.order = 1
            else:
                gi.order = 1

            new_instance.save()
            gi.object = new_instance
            gi.save()

            #render the new display to the page.
            template = select_template(self.item_add_image_template)
            context = RequestContext(request)
            context.update({"insert": self, "form": form, "item": gi})
            response = HttpResponse(template.render(context))
            response.status_code = 201
            return response

        # return the errors
        response = HttpResponse(form.errors)
        response.status_code = 400
        return response
def add_to_gallery(request):
    """
    This view accepts 5 values (gallery, position, app_label, model, and pk) which are POSTed.
    Checks the values and adds the item at the specified position.
    """
    ret = {}
    ret['error'] = []
    
    # check fields
    fields = ['gallery','position','app_label','model','pk']
    for field in fields:
        if field not in request.POST.keys():
            ret['error'].append('Missing Field ' + field)
    if len(ret['error']):
       resp = simplejson.dumps(ret)
       return HttpResponse(resp)
       
    ## check permissions
    
    ## check that gallery and item exist
    gallery = Gallery.objects.get(pk=request.POST.get('gallery'))
    content_type = ContentType.objects.get(app_label = request.POST.get('app_label'), model=request.POST.get('model'))
    item = content_type.get_object_for_this_type(pk=request.POST.get('pk'))
    
    # add
    gallery_item = GalleryItem()
    gallery_item.content_type = content_type
    gallery_item.object_id = item.pk
    gallery_item.gallery = gallery
    gallery_item.order = request.POST.get('position')
    # save
    gallery_item.save()
    
    ret['item'] = 'success'
    # return result
    resp = simplejson.dumps(ret)
    return HttpResponse(resp)