def file_edit_post_view(self):
        length = int(self.headers['Content-Length'])
        data = self.rfile.read(length)
        post_data = parse_qs(data.decode('utf-8'))

        name = post_data['name'][0]
        query = FileResource.select().where(FileResource.name == name)
        if not query.exists():
            logger.warn("file '{}' not found".format(name))
            self.render_template("message.html", code=404, message="Файла с таким именем не существует")
            return

        file = query.get()
        if not file.can_write(self.user):
            logger.warn("user {} have no permission to write to '{}'<{}>".format(self.user.name, file.name, file.mode()))
            self.render_template("message.html", code=404, message="Недостаточно прав")
            return

        file.data = post_data['text'][0]
        file.ru = 'ru' in post_data
        file.wu = 'wu' in post_data
        file.ro = 'ro' in post_data
        file.wo = 'wo' in post_data
        file.save()
        self.send_response(302)
        self.send_header('location', '/file/update?name={}'.format(file.name))
        self.end_headers()
        logger.info("file '{}' <{}> updated by '{}'".format(file.name, file.mode(), file.owner))
 def wrap_view(self, name, *args, **kwargs):
     try:
         file = FileResource.select().where(FileResource.name == name).get()
     except DoesNotExist:
         logger.warn("requested file '{}' not found for view {}".format(name, view.__name__))
         self.render_template("message.html", message="Файл не найден", code=404)
         return
     view(self, file, *args, **kwargs)
    def file_create_post_view(self):
        length = int(self.headers['Content-Length'])
        data = self.rfile.read(length)
        post_data = parse_qs(data.decode('utf-8'))

        if FileResource.select().where(FileResource.name == post_data['name'][0]).exists():
            logger.warn("file '{}' already exists found".format(post_data['name'][0]))
            self.render_template("message.html", message="Файл с таким именем уже существует")
            return
        file = FileResource.create(name=post_data['name'][0], owner=self.user)
        file.data = post_data['text'][0]

        file.ru = 'ru' in post_data
        file.wu = 'wu' in post_data
        file.ro = 'ro' in post_data
        file.wo = 'wo' in post_data

        file.save()
        self.send_response(302)
        self.send_header('location', '/file/update?name={}'.format(file.name))
        self.end_headers()
        logger.info("file {} <{}> created by {}".format(file.name, file.mode(), file.owner))
 def file_list_view(self):
     self.render_template("list.html", list=FileResource.select())