Пример #1
0
def create_resource(request):
    """Create a new file or folder """
    post = json.loads(request.body.decode())
    path = post.get('path')
    if not path:
        return HttpResponseBadRequest(missing_parameter('path'))
    
    path = join_fb_root(path)
    name = os.path.basename(path)
    
    try:
        if any(c in name for c in settings.FILEBROWSER_DISALLOWED_CHAR):
            msg = "Can't create '%s': name should not contain any of %s."
            return HttpResponseBadRequest(msg % (name, str(settings.FILEBROWSER_DISALLOWED_CHAR)))
        
        if os.path.exists(path):
            return HttpResponseBadRequest("Can't create '%s': this name is already used." % name)
        if post.get('type', '') == 'file':
            with open(path, "w") as f:
                print(post.get('content', ''), file=f)
            
            return JsonResponse({'path': rm_fb_root(path)})
        
        else:
            os.mkdir(path)
            return JsonResponse({'path': rm_fb_root(path)})
    except Exception as e:  # pragma: no cover
        msg = "Impossible to create '{0}' : {1}".format(name, htmlprint.code(
            str(type(e)) + ' - ' + str(e)))
        messages.error(request, msg)
        if settings.DEBUG:
            msg += ("DEBUG set to True: " + htmlprint.html_exc())
        return HttpResponseNotFound(msg)
Пример #2
0
def rename_resource(request):
    """Rename a  file or folder """
    post = json.loads(request.body.decode())
    
    path = post.get('path')
    target = post.get('target')
    if not path:
        return HttpResponseBadRequest(missing_parameter('path'))
    if not target:
        return HttpResponseBadRequest(missing_parameter('target'))
    
    path = join_fb_root(path)
    name = os.path.basename(path)
    new_path = os.path.join(os.path.dirname(path), target)
    
    try:
        if any(c in target for c in settings.FILEBROWSER_DISALLOWED_CHAR):
            msg = "Can't rename '{0}' to '{1}': name should not contain any of {2}." \
                .format(name, target, settings.FILEBROWSER_DISALLOWED_CHAR)
            return HttpResponseBadRequest(msg)
        if os.path.exists(new_path):
            msg = "Can't rename '{0}' to '{1}': this name is already used.".format(name, target)
            return HttpResponseBadRequest(msg)
        
        os.rename(path, new_path)
        return JsonResponse(
            {'path': rm_fb_root(new_path), 'status': 200})
    except Exception as e:  # pragma: no cover
        msg = "Impossible to rename '{0}' to '{1}': {2}".format(name, target, htmlprint.code(
            str(type(e)) + ' - ' + str(e)))
        if settings.DEBUG:
            msg += ("DEBUG set to True: " + htmlprint.html_exc())
        return HttpResponseNotFound(msg)
Пример #3
0
 def add_extends(self, d, k, v, o):
     
     def merge(target, source):
         """
         Copy every key and value of source in target
         if key is not present in target or override
         if key is in self.OVERRIDE_ON_EXTENDS
         """
         for key, value in source.items():
             if key not in target:
                 target[key] = value
             elif type(target[key]) is dict:
                 if key in self.OVERRIDE_ON_EXTENDS:
                     target[key].update(value)
                 else:
                     merge(target[key], value)
             elif type(target[key]) is list:
                 target[key] += value
         return target
     
     
     if o != '=':
         self.error(
             type=SYNTAX_ERROR,
             message='"%s" should be used with "=" operator' % k
         )
     else:
         try:
             path = rm_fb_root(self.abspath(v))
             parser = find_parser()
             ast = parser.parse(path, directory=self.directory, extends=True)
             
             merge(self.ast, ast)
             
             self.add_depend(path)
             self.ast['__extends'].append({
                 'file':    self.path,
                 'extends': v,
                 'lineno':  self.lineno
             })
         except Exception:
             self.error(UNRESOLVED_REFERENCE, v)
Пример #4
0
 def test_rm_fb_root(self):
     path1 = os.path.join(settings.FILEBROWSER_ROOT, "dir/file.txt")
     path2 = "/path/to/file.txt"
     self.assertEqual(utils.rm_fb_root(path1), "dir/file.txt")
     self.assertEqual(utils.rm_fb_root(path2), path2)
Пример #5
0
 def add_depend(self, path):
     self.ast['__dependencies'].append({
         'file':     self.path,
         'dependTo': rm_fb_root(path),
         'lineno':   self.lineno
     })