Exemplo n.º 1
0
def uploadfile(request):
    """
        Initialize Ajax File Uploader
    """
    from ajaxuploader.views import AjaxFileUploader
    from pages.ajaxupload import CustomStorageUploadBackend
    from django.http import HttpResponseBadRequest

    directory = request.GET["directory"]
    directory_allowed = {"background": 0, "photo": 0, "pdf": 0, "logo": 0}
    try:
        directory_allowed[directory]
    except:
        return HttpResponseBadRequest()

    uploaderresponse = AjaxFileUploader(backend=CustomStorageUploadBackend, uploaddir="uploads/" + directory + "/")
    return uploaderresponse._ajax_upload(request)
Exemplo n.º 2
0
class UploadFileView(IssueSignalsMixin, UpdateView):
    """
    Override the uploader and cater to the multiple file associations
    @TODO turn this into a service
    """
    model = EightyThreeB
    uploader = None

    def __init__(self, *args, **kwargs):
        super(UploadFileView, self).__init__(*args, **kwargs)
        self.uploader = AjaxFileUploader()

    def post(self, request, *args, **kwargs):
        self.object = self.get_object()
        # Upload the File using the ajax_uploader object
        response = self.uploader._ajax_upload(request, *args, **kwargs)
        if response.status_code in [200]:
            # success
            data = json.loads(response.content)
            base_path, filename = os.path.split(data.get('path'))
            # create and upload to s3
            with open(os.path.join(settings.MEDIA_ROOT, 'uploads', filename)) as file_path:
                uploaded_file = File(file_path)
                attachment = self.object.attachment_set.create(eightythreeb=self.object)
                attachment.attachment = uploaded_file
                attachment.save()

            os.remove(uploaded_file.name)

            data['path'] = attachment.attachment.url
            response.content = json.dumps(data)

            # send signal
            self.issue_signals(request=request, instance=self.object, name='copy_uploaded')

        return response