Ejemplo n.º 1
0
    def Post(self):
        text = self.request.params['geebaby_text']
            
        #log.debug(repr(text))
        #log.debug(str(text))
        
        uri = "http://%s%s?%s" % (os.environ["SERVER_NAME"], os.environ["PATH_INFO"], os.environ["QUERY_STRING"])
        tags, entities, actions = uri2tea(uri)
        
        soup = soupmagic.BeautifulSoup(text.replace(u"\xa0", " "))
        objects = soup.findAll(attrs={'class': lambda x: x and 'geebaby' in x.split()})
        
        if len(objects) == 1 and not objects[0].get("id"):
            # new object
            gb_list = [Geebaby()]
        else:
            # existing objects
            gb_list = db.get(obj.get("id") for obj in objects[:1]) 
        
        for i, gb in enumerate(gb_list):
            if not gb:
                gb = Geebaby()

            obj = objects[i]
            #log.debug("obj:%s gb:%s" % (obj.get("id"), gb.key()))
            
            attributes = {}
            if actions:
                attributes['text__type'] = actions[0]
            for x in obj.findAll(attrs={'class': lambda x: x and x.split()[0] == 'x'}):
                value = x.renderContents(encoding=None)
                parts = x["class"].split()
                key = parts[1].replace('-x', '')
                escaped = 'escaped' in parts[2:] 
                if escaped:
                    from HTMLParser import HTMLParser
                    value = soupmagic.BeautifulSoup(value)
                    #value = soupmagic.replace_tags(value, "br", "\n")
                    value = soupmagic.flatten(value)
                    value = HTMLParser().unescape(value)
                if x.name in ('ol', 'ul'):
                    value = filter(None, [soupmagic.flatten(t).strip() for t in x('li')])
                    
                
                if key not in attributes:
                    attributes[key] = value
                else:
                    attributes[key] += value
            
            if not attributes.get('name'):
                attributes['name'] = entities and entities[0]
            if not attributes.get('name'):
                from geebaby.utils.slugify import slugify_name
                attributes['name'] = slugify_name(attributes.get('title'))
            if not attributes.get('name'):
                from md5 import md5
                attributes['name'] = md5().hexdigest()
                        
            for key, value in attributes.items():
                if value != getattr(gb, key, None):
                    try:
                        setattr(gb, key, value)
                    except:
                        log.exception(key)
                        log.debug(repr(value))
                        raise
                    gb._changed = True
                
            if not gb.is_saved() or getattr(gb, '_changed', None):
                gb.put()
            else:
                log.debug("Not modified: %s" % gb)