Ejemplo n.º 1
0
 def post(self, parent_name=None):
     self.new_form = self.NewForm(self.request)
     if self.new_form.is_valid():
         name = bleach.clean(self.new_form.cleaned_data['name'])
         models.Category(key_name=name, name=name, parent_category=models.Category.get_by_key_name(parent_name) if parent_name else None, description=self.new_form.cleaned_data['description']).put()
         return self.get(parent_name)
     else:
         return Action.Result.DEFAULT
Ejemplo n.º 2
0
 def save(self):
     self.body = bleach.linkify(bleach.clean(self.body).replace('\n', '<br>\n'), parse_email=True, target='_blank')
     
     is_insert = not self.is_saved()
     
     db.run_in_transaction_options(xg_on, super(self.__class__, self).put)
     if is_insert:
         self.sort_key = ('%s%32s' % (self.parent_comment.sort_key, base62_encode(self.key().id()))).replace(' ', '0') if self.parent_comment else '%s' % base62_encode(self.key().id())
         db.run_in_transaction_options(xg_on, super(self.__class__, self).put)
         db.run_in_transaction_options(xg_on, self.article.increase_comment_count)
         db.run_in_transaction_options(xg_on, self.author.increase_comment_count)
         Subscription.get_or_insert('%s-%s' % (self.article.key().id(), self.author.key()), article=self.article, user=self.author)
     return self
Ejemplo n.º 3
0
    def save(self):
        self.title = bleach.clean(self.title)
        self.body = bleach.clean(self.body, tags=['code', 'a', 'p', 'span', 'div', 'strong', 'b', 'em', 'i', 'strike', 'cite', 'mark', 'small', 'blockquote', 'figure', 'figcaption', 'strong', 'sub', 'sup', 'img', 'iframe', 'br', 'pre', 'hr'], attributes=['style', 'title', 'src', 'frameborder', 'width', 'height', 'alt', 'href', 'target'], styles=['width', 'height', 'font-size', 'font-family', 'text-decoration', 'color', 'background-color', 'text-align', 'padding-left'])
        self.last_updated = datetime.datetime.now()

        has_image = False
        has_video = False
        self.image = None
        self.video = None
        
        soup = BeautifulSoup(self.body)
        img = soup.find('img')
        
        if img and hasattr(img, 'src') and re.search('tiny_mce/plugins/emotions', img['src'].lower()) is None:
            self.image = img['src']
            has_image = True
        
        iframes = soup.findAll('iframe')
        for item in iframes:
            if re.match('^http(s)?://(.+\.)?(youtube.com|youtu.be)/', item['src'].lower()) is not None:
                if self.video is None:
                    self.video = item['src']
                    has_video = True
            else:
                item.decompose()
        
        is_insert = not self.is_saved()
        
        excerpt = strip_tags(self.body).strip()
        self.excerpt = '%s...' % excerpt[:253] if len(excerpt) > 253 else excerpt
        
        diff = None
        if not is_insert:
            previous = self.__class__.get(self.key())
            Tag.decrease(previous.tags)
            if previous.body != self.body:
                diff = DIFFER.make_table(previous.body, self.body)
        db.run_in_transaction_options(xg_on, super(self.__class__, self).put)
        if is_insert:
            db.run_in_transaction_options(xg_on, self.author.increase_article_count)
            for item in self.category.get_path():
                db.run_in_transaction_options(xg_on, item.increase_article_count)
        elif diff:
            db.run_in_transaction_options(xg_on, ArticleHistory(article=self, diff=diff).put)
            
        key_name = str(self.key().id())
        if has_image:
            ImageArticle.get_or_insert(key_name, article=self)
        else:
            found = ImageArticle.gql('WHERE article = :1', self).get()
            if found != None:
                found.delete()
        if has_video:
            VideoArticle.get_or_insert(key_name, article=self)
        else:
            found = VideoArticle.gql('WHERE article = :1', self).get()
            if found != None:
                found.delete()
                
        if is_insert:
            Subscription.get_or_insert('%s-%s' % (self.key().id(), self.author.key()), article=self, user=self.author)

        return self
Ejemplo n.º 4
0
 def change_nickname(self, nickname):
     db.run_in_transaction_options(xg_on, UserNicknameHistory(user=self, nickname=bleach.clean(self.nickname)).put)
     self.nickname = bleach.clean(nickname)
     db.run_in_transaction_options(xg_on, self.put)
Ejemplo n.º 5
0
 def normalize(cls, tag):
     tag = strip_entities(bleach.clean(tag))
     normalized = TAG_NORMALIZE_PATTERN.sub(u'', tag).lower()
     return [tag, normalized] if tag != normalized else [tag]