Ejemplo n.º 1
0
 def upload_file_view(self, request):
     self.init_parent_folder(request)
     if not self.has_add_permission(request):
         raise PermissionDenied
     if request.method == 'POST':
         form = UploadForm(request.POST, request.FILES)
         if form.is_valid():
             node = FileNode(file=form.cleaned_data['file'], node_type=FileNode.FILE)
             parent_folder = self.get_parent_folder(request)
             if not parent_folder.is_top_node():
                 node.parent = parent_folder
             self.save_model(request, node, None, False)
             # Respond with 'ok' for the client to verify that the upload was successful, since sometimes a failed
             # request would not result in a HTTP error and look like a successful upload.
             # For instance: When requesting the admin view without authentication, there is a redirect to the
             # login form, which to SWFUpload looks like a successful upload request.
             if request.is_ajax() or 'Adobe Flash' in request.META.get('HTTP_USER_AGENT', ''):
                 return HttpResponse("ok", mimetype="text/plain")
             else:
                 messages.info(request, _('Successfully uploaded file %s.') % node.name)
                 return HttpResponseRedirect(reverse('admin:media_tree_filenode_changelist'))
         else:
             if not settings.DEBUG:
                 raise ValidationError
                 return
     if not settings.DEBUG:
         raise ViewDoesNotExist
     else:
         # Form is rendered for troubleshooting SWFUpload. If this form works, the problem is not server-side.
         from django.template import Template, RequestContext
         if request.method != 'POST':
             form = UploadForm()
         return render_to_response('admin/media_tree/filenode/upload_form.html',
             {'form': form, 'node': self.get_parent_folder(request)}, context_instance=RequestContext(request))
Ejemplo n.º 2
0
    def upload_file_view(self, request):
        try:
            if not self.has_add_permission(request):
                raise PermissionDenied

            FILE_PARAM_NAME = 'qqfile'
            self.init_parent_folder(request)

            if request.method == 'POST':

                if request.is_ajax() and request.GET.get(FILE_PARAM_NAME, None):
                    from django.core.files.base import ContentFile
                    from django.core.files.uploadedfile import UploadedFile
                    content_file = ContentFile(request.raw_post_data)
                    uploaded_file = UploadedFile(content_file, request.GET.get(FILE_PARAM_NAME), None, content_file.size)
                    form = UploadForm(request.POST, {'file': uploaded_file})
                else:
                    form = UploadForm(request.POST, request.FILES)

                if form.is_valid():
                    node = FileNode(file=form.cleaned_data['file'], node_type=FileNode.FILE)
                    parent_folder = self.get_parent_folder(request)
                    if not parent_folder.is_top_node():
                        node.parent = parent_folder
                    self.save_model(request, node, None, False)
                    # Respond with 'ok' for the client to verify that the upload was successful, since sometimes a failed
                    # request would not result in a HTTP error and look like a successful upload.
                    # For instance: When requesting the admin view without authentication, there is a redirect to the
                    # login form, which to SWFUpload looks like a successful upload request.
                    if request.is_ajax():
                        return HttpResponse('{"success": true}', mimetype="application/json")
                    else:
                        messages.info(request, _('Successfully uploaded file %s.') % node.name)
                        return HttpResponseRedirect(reverse('admin:media_tree_filenode_changelist'))
                else:
                    # invalid form data
                    if request.is_ajax():
                        return HttpResponse('{"error": "%s"}' % ' '.join(
                            [item for sublist in form.errors.values() for item in sublist]), 
                            mimetype="application/json")

            # Form is rendered for troubleshooting SWFUpload. If this form works, the problem is not server-side.
            if not settings.DEBUG:
                raise ViewDoesNotExist
            if request.method == 'GET':
                form = UploadForm()
            return render_to_response('admin/media_tree/filenode/upload_form.html', {'form': form})            

        except Exception as e:
            if request.is_ajax():
                return HttpResponse('{"error": "%s"}' % ugettext('Server Error'), 
                    mimetype="application/json")
            else:
                raise
Ejemplo n.º 3
0
    def upload_file_view(self, request):
        try:
            if not self.has_add_permission(request):
                raise PermissionDenied

            self.init_parent_folder(request)

            if request.method == 'POST':
                form = UploadForm(request.POST, request.FILES)
                if form.is_valid():
                    node = FileNode(**form.cleaned_data)
                    self.save_model(request, node, None, False)

                    # Respond with success
                    if request.is_ajax():
                        return HttpResponse(json.dumps({'success': True}),
                                            content_type="application/json")
                    else:
                        messages.info(
                            request,
                            _('Successfully uploaded file %s.') % node.name)
                        return HttpResponseRedirect(
                            reverse('admin:media_tree_filenode_changelist'))
                else:
                    # invalid form data
                    if request.is_ajax():
                        return HttpResponse(json.dumps({
                            'error':
                            ' '.join([
                                item for sublist in form.errors.values()
                                for item in sublist
                            ])
                        }),
                                            content_type="application/json",
                                            status=403)

            # Form is rendered for troubleshooting XHR upload.
            # If this form works, the problem is not server-side.
            if not settings.DEBUG:
                raise ViewDoesNotExist
            if request.method == 'GET':
                form = UploadForm()
            return render_to_response(
                'admin/media_tree/filenode/upload_form.html', {'form': form},
                context_instance=RequestContext(request))

        except Exception as e:
            if not settings.DEBUG and request.is_ajax():
                return HttpResponse(json.dumps(
                    {'error': ugettext('Server Error')}),
                                    content_type="application/json")
            else:
                raise
Ejemplo n.º 4
0
    def upload_file_view(self, request):
        try:
            if not self.has_add_permission(request):
                raise PermissionDenied

            self.init_parent_folder(request)

            if request.method == 'POST':
                form = UploadForm(request.POST, request.FILES)
                if form.is_valid():
                    node = FileNode(file=form.cleaned_data['file'], node_type=FileNode.FILE)
                    parent_folder = self.get_parent_folder(request)
                    if not parent_folder.is_top_node():
                        node.parent = parent_folder
                    self.save_model(request, node, None, False)
                    # Respond with success
                    if request.is_ajax():
                        return HttpResponse('{"success": true}', mimetype="application/json")
                    else:
                        messages.info(request, _('Successfully uploaded file %s.') % node.name)
                        return HttpResponseRedirect(reverse('admin:media_tree_filenode_changelist'))
                else:
                    # invalid form data
                    if request.is_ajax():
                        return HttpResponse('{"error": "%s"}' % ' '.join(
                            [item for sublist in form.errors.values() for item in sublist]), 
                            mimetype="application/json")

            # Form is rendered for troubleshooting XHR upload. 
            # If this form works, the problem is not server-side.
            if not settings.DEBUG:
                raise ViewDoesNotExist
            if request.method == 'GET':
                form = UploadForm()
            return render_to_response('admin/media_tree/filenode/upload_form.html', {'form': form},
                context_instance=RequestContext(request))

        except Exception as e:
            if request.is_ajax():
                return HttpResponse('{"error": "%s"}' % ugettext('Server Error'), 
                    mimetype="application/json")
            else:
                raise
Ejemplo n.º 5
0
    def upload_file_view(self, request):
        try:
            if not self.has_add_permission(request):
                raise PermissionDenied

            FILE_PARAM_NAME = 'qqfile'
            self.init_parent_folder(request)

            if request.method == 'POST':

                if request.is_ajax() and request.GET.get(
                        FILE_PARAM_NAME, None):
                    from django.core.files.base import ContentFile
                    from django.core.files.uploadedfile import UploadedFile
                    content_file = ContentFile(request.raw_post_data)
                    uploaded_file = UploadedFile(
                        content_file, request.GET.get(FILE_PARAM_NAME), None,
                        content_file.size)
                    form = UploadForm(request.POST, {'file': uploaded_file})
                else:
                    form = UploadForm(request.POST, request.FILES)

                if form.is_valid():
                    node = FileNode(file=form.cleaned_data['file'],
                                    node_type=FileNode.FILE)
                    parent_folder = self.get_parent_folder(request)
                    if not parent_folder.is_top_node():
                        node.parent = parent_folder
                    self.save_model(request, node, None, False)
                    # Respond with 'ok' for the client to verify that the upload was successful, since sometimes a failed
                    # request would not result in a HTTP error and look like a successful upload.
                    # For instance: When requesting the admin view without authentication, there is a redirect to the
                    # login form, which to SWFUpload looks like a successful upload request.
                    if request.is_ajax():
                        return HttpResponse('{"success": true}',
                                            mimetype="application/json")
                    else:
                        messages.info(
                            request,
                            _('Successfully uploaded file %s.') % node.name)
                        return HttpResponseRedirect(
                            reverse('admin:media_tree_filenode_changelist'))
                else:
                    # invalid form data
                    if request.is_ajax():
                        return HttpResponse('{"error": "%s"}' % ' '.join([
                            item for sublist in form.errors.values()
                            for item in sublist
                        ]),
                                            mimetype="application/json")

            # Form is rendered for troubleshooting SWFUpload. If this form works, the problem is not server-side.
            if not settings.DEBUG:
                raise ViewDoesNotExist
            if request.method == 'GET':
                form = UploadForm()
            return render_to_response(
                'admin/media_tree/filenode/upload_form.html', {'form': form})

        except Exception as e:
            if request.is_ajax():
                return HttpResponse('{"error": "%s"}' %
                                    ugettext('Server Error'),
                                    mimetype="application/json")
            else:
                raise