Esempio n. 1
0
 def POST(self, path):
     i = web.input(_method='post')
     i = web.storage(helpers.unflatten(i))
     i.key = path
     
     _ = web.storage((k, i.pop(k)) for k in i.keys() if k.startswith('_'))
     action = self.get_action(_)
     comment = _.get('_comment', None)
     
     for k, v in i.items():
         i[k] = self.trim(v)
         
     p = web.ctx.site.get(path) or web.ctx.site.new(path, {})
     p.update(i)
     
     if action == 'preview':
         p['comment_'] = comment
         return render.editpage(p, preview=True)
     elif action == 'save':
         try:
             p._save(comment)
             path = web.input(_method='GET', redirect=None).redirect or web.changequery(query={})
             raise web.seeother(path)
         except (ClientException, db.ValidationException), e:            
             add_flash_message('error', str(e))
             p['comment_'] = comment                
             return render.editpage(p)
Esempio n. 2
0
 def POST(self, path):
     i = web.input(_method='post')
     i = web.storage(helpers.unflatten(i))
     i.key = path
     
     _ = web.storage((k, i.pop(k)) for k in i.keys() if k.startswith('_'))
     action = self.get_action(_)
     comment = _.get('_comment', None)
     
     for k, v in i.items():
         i[k] = self.trim(v)
         
     p = web.ctx.site.get(path) or web.ctx.site.new(path, {})
     p.update(i)
     
     if action == 'preview':
         p['comment_'] = comment
         return render.editpage(p, preview=True)
     elif action == 'save':
         try:
             p._save(comment)
             path = web.input(_method='GET', redirect=None).redirect or web.changequery(query={})
             raise web.seeother(path)
         except (ClientException, db.ValidationException), e:            
             add_flash_message('error', str(e))
             p['comment_'] = comment                
             return render.editpage(p)
Esempio n. 3
0
    def GET(self, path):
        i = web.input(v=None, t=None)
        
        if not web.ctx.site.can_write(path):
            return render.permission_denied(path, "Permission denied to edit " + path + ".")

        if i.v is not None and safeint(i.v, None) is None:
            raise web.seeother(web.changequery(v=None))
		        
        p = db.get_version(path, i.v) or db.new_version(path, types.guess_type(path))
        
        if i.t:
            type = db.get_type(i.t)
            if type is None:
                add_flash_message('error', 'Unknown type: ' + i.t)
            else:
                p.type = type 

        return render.editpage(p)
Esempio n. 4
0
    def GET(self, path):
        i = web.input(v=None, t=None)

        if not web.ctx.site.can_write(path):
            return render.permission_denied(web.ctx.fullpath, "Permission denied to edit " + path + ".")

        if i.v is not None and safeint(i.v, None) is None:
            raise web.seeother(web.changequery(v=None))

        p = db.get_version(path, i.v) or db.new_version(path, types.guess_type(path))

        if i.t:
            type = db.get_type(i.t)
            if type is None:
                add_flash_message('error', 'Unknown type: ' + i.t)
            else:
                p.type = type

        return render.editpage(p)
Esempio n. 5
0
class edit (delegate.mode):
    def GET(self, path):
        i = web.input(v=None, t=None)
        
        if not web.ctx.site.can_write(path):
            return render.permission_denied(web.ctx.fullpath, "Permission denied to edit " + path + ".")

        if i.v is not None and safeint(i.v, None) is None:
            raise web.seeother(web.changequery(v=None))
		        
        p = db.get_version(path, i.v) or db.new_version(path, types.guess_type(path))
        
        if i.t:
            type = db.get_type(i.t)
            if type is None:
                add_flash_message('error', 'Unknown type: ' + i.t)
            else:
                p.type = type 

        return render.editpage(p)


    def trim(self, d):
        """Trims empty value from d. 
        
        >>> trim = edit().trim

        >>> trim("hello ")
        'hello'
        >>> trim(['hello ', '', ' foo'])
        ['hello', 'foo']
        >>> trim({'x': '', 'y': 'foo'})
        {'y': 'foo'}
        >>> trim({'x': '', 'unique': 'foo'})
        >>> trim([{'x': '', 'y': 'foo'}, {'x': ''}])
        [{'y': 'foo'}]
        """
        if d is None:
            return d
        elif isinstance(d, list):
            d = [self.trim(x) for x in d]
            d = [x for x in d if x]
            return d
        elif isinstance(d, dict):
            for k, v in d.items():
                d[k] = self.trim(v)
                if d[k] is None or d[k] == '' or d[k] == []:
                    del d[k]

            # hack to stop saving empty properties
            if d.keys() == [] or d.keys() == ['unique']:
                return None
            else:
                return d
        else:
            return d.strip()
        
    def POST(self, path):
        i = web.input(_method='post')
        i = web.storage(helpers.unflatten(i))
        i.key = path
        
        _ = web.storage((k, i.pop(k)) for k in i.keys() if k.startswith('_'))
        action = self.get_action(_)
        comment = _.get('_comment', None)
        
        for k, v in i.items():
            i[k] = self.trim(v)
            
        p = web.ctx.site.get(path) or web.ctx.site.new(path, {})
        p.update(i)
        
        if action == 'preview':
            p['comment_'] = comment
            return render.editpage(p, preview=True)
        elif action == 'save':
            try:
                p._save(comment)
                path = web.input(_method='GET', redirect=None).redirect or web.changequery(query={})
                raise web.seeother(path)
            except (ClientException, db.ValidationException), e:            
                add_flash_message('error', str(e))
                p['comment_'] = comment                
                return render.editpage(p)
        elif action == 'delete':
            q = dict(key=i['key'], type=dict(key='/type/delete'))
            
            try:
                web.ctx.site.save(q, comment)
            except (ClientException, db.ValidationException), e:            
                add_flash_message('error', str(e))
                p['comment_'] = comment                
                return render.editpage(p)