예제 #1
0
파일: views.py 프로젝트: a740122/django-bfm
class DirectoryActions(View):

    @method_decorator(login_required)
    @method_decorator(staff_member_required)
    def dispatch(self, request):
        self.args = {'directory': None, 'action': None}
        for arg in self.args:
            if arg not in request.GET:
                return HttpResponseBadRequest()
            self.args[arg] = request.GET[arg]
        # Rename and New needs `new` variable.
        self.args['new'] = request.GET.get('new', False)

        # Initialize storage.
        self.storage = BFMStorage(self.args['directory'])
        self.root = BFMStorage('')

        # Initialize signals
        self.signals = {'pre': signals.pre_directory_action,
                        'post': signals.post_directory_action}
        self.sigfile = {'original': self.storage.path(''),
                        'new': None}
        self.sigargs = {'sender': request,
                        'action': self.args['action'],
                        'affected_files': self.sigfile}

        # Send signals and run action, then send post signal.
        a = {'new': self.new, 'rename': self.rename, 'delete': self.delete}
        if self.args['action'] in a:
            self.signals['pre'].send(**self.sigargs)
            response = a[self.args['action']]()
            if not response.status_code >= 400:
                self.signals['post'].send(**self.sigargs)
            return response
        else:
            return HttpResponseBadRequest()

    def new(self):
        if self.args['new'] is False:
            return HttpResponseBadRequest()
        new_dir = self.storage.new_directory(self.args['new'], strict=True)
        self.sigfile['new'] = new_dir
        return HttpResponse()

    def rename(self):
        if self.args['new'] is False:
            return HttpResponseBadRequest()
        path = os.path.join(self.args['directory'], '..', self.args['new'])
        new_dir = self.root.move_directory(self.args['directory'],
                                            os.path.normpath(path), strict=True)
        self.sigfile['new'] = new_dir
        return HttpResponse()

    def delete(self):
        self.root.remove_directory(self.args['directory'])
        return HttpResponse()
예제 #2
0
파일: views.py 프로젝트: a740122/django-bfm
class FileActions(View):

    @method_decorator(login_required)
    @method_decorator(staff_member_required)
    def dispatch(self, request):
        if request.method != 'POST':
            return HttpResponseBadRequest()

        # Check for all required arguments and move them to another dict.
        self.args = {'directory': None, 'action': None, 'files[]': None}
        for arg in self.args:
            if arg not in request.POST:
                return HttpResponseBadRequest()
            if arg[-2:] == '[]':
                self.args[arg] = request.POST.getlist(arg)
            else:
                self.args[arg] = request.POST[arg]
        self.args['files'] = self.args['files[]']
        # Rename needs `new` variable.
        # self.args['new'] = request.GET.get('new', False)

        # Initialize storage.
        self.storage = BFMStorage(self.args['directory'])

        # Initialize signals
        self.signals = {'pre': signals.pre_file_action,
                        'post': signals.post_file_action}
        affected_files = (self.storage.path(f) for f in self.args['files'])
        self.sigfiles = {'affected': affected_files,
                        'resulting': None}
        self.sigargs = {'sender': request,
                        'action': self.args['action'],
                        'files': self.sigfiles}

        # Send pre-action signals, run action and then send post-action signal.
        actions = {'delete': self.delete}
        if not self.args['action'] in actions:
            return HttpResponseBadRequest()

        self.signals['pre'].send(**self.sigargs)
        response = actions[self.args['action']]()
        if not response.status_code >= 400:
            self.signals['post'].send(**self.sigargs)
        return response

    def delete(self):
        for f in self.args['files']:
            self.storage.delete(f)
        return HttpResponse()
예제 #3
0
파일: views.py 프로젝트: a740122/django-bfm
def file_upload(request):
    directory = request.GET.get('directory', False)
    if request.method != 'POST':
        return HttpResponseNotAllowed(['POST'])
    elif directory == False:
        return HttpResponseBadRequest()

    form = UploadFileForm(request.POST, request.FILES)
    storage = BFMStorage(directory)

    if form.is_valid():
        signals.pre_uploaded_file_save.send(sender=request)
        f = storage.save(request.FILES['file'].name, request.FILES['file'])
        signals.uploaded_file_saved.send(sender=request,
                                        file_path=storage.path(f))
        return HttpResponse(storage.file_metadata(f, json=True))

    else:
        return HttpResponseBadRequest('Form is invalid!')