def CreateComments(self): '''Расставляем комментарии''' for _ in range(random.randint(3, 15)): random.shuffle(self.commentsList) commentText = ' '.join(self.commentsList[:random.randint(2, 4)]) comment = Comment(commentText) random.choice(self.soup.findAll(True)).insert(0, comment)
def commentClass(self, data): return TextNode(Comment(data), self.soup)
def sunset_embed(body, request=False): # Moved the import down here to avoid a circular import from sunset.models import image self_closing = [ 'sunset', ] if body and "<sunset" in body: body_raw = BeautifulSoup(body, selfClosingTags=self_closing) imglist = body_raw.findAll('sunset') for imgtag in imglist: err = 'Unknown error parsing Sunset embed tag' new_tag = '' img_pk = imgtag.get('id', False) cur_type = imgtag.get('type', 'icon') if img_pk: img_check = image.objects.filter(pk=int(img_pk)).filter( access_query(request)).select_related('cat') if img_check: cur_img = img_check.first() asset_check = cur_img.assets.filter(type=cur_type) if asset_check: cur_asset = asset_check.first() new_tag = BeautifulSoup(selfClosingTags=self_closing) new_a = Tag(new_tag, 'a') new_img = Tag(new_tag, 'img') new_a['class'] = 'sunset_embed sunset_%s' % cur_type new_a['href'] = cur_img.get_absolute_url() new_a['title'] = cur_img new_img['alt'] = cur_img new_img['title'] = cur_img new_img['src'] = cur_asset.get_url() new_tag.insert(0, new_a) new_a.insert(0, new_img) err = False else: err = 'Sunset image asset type specified in embed tag was not found' else: err = 'Sunset image specified in embed tag was not found' else: err = 'Invalid or missing image ID in Sunset embed tag' if err: imgtag.replaceWith( Comment('%s. Original was: %s' % (err, imgtag))) else: imgtag.replaceWith(new_tag) return unicode(body_raw) else: # Nothing to do. return body