Ejemplo n.º 1
0
def PictureCreateView(request):
    if request.POST:
        try:
            f = request.FILES.get('file')
            name = f.name.split('.')
            f.name = "%s.%s" % (random_string(32), name[len(name)-1])
            name = f.name
            p = Picture(user=request.user, file=f)
            p.save()

            image = "%s/%s" % (settings.MEDIA_ROOT, upload_to(p, name))
            thumb = pil.create_thumb(image)
            thumb_address = "%s/%s" % (settings.MEDIA_ROOT, thumb_upload_to(p, name))
            thumb.save(thumb_address)
            thumb_address = "%s%s/%s" % (settings.MEDIA_URL, thumb_upload_url(p.user.patient), name)
            p.thumb = thumb_address
            p.save()
            
            data = [{'id': p.id, 'name': f.name, 'url': settings.MEDIA_URL + upload_url(p.user.patient) + "/" + f.name.replace(" ", "_"), 'thumbnail_url': settings.MEDIA_URL + thumb_upload_url(p.user.patient) + "/" + f.name.replace(" ", "_"), 'delete_url': reverse('upload-delete', args=[p.id]), 'delete_type': "DELETE"}]
        except:
            raise
            data = [{'status': 'Error', 'error': 'Error', 'message': 'Failed to submit files'}]
    else:
        data = []
        response = JSONResponse(data, {}, "text/plain")
        response['Content-Disposition'] = 'inline; filename=files.json'
        return response
    response = JSONResponse(data, {}, "application/json")
    response['Content-Disposition'] = 'inline; filename=files.json'
    return response