当前位置: 首页>>代码示例>>Python>>正文


Python Page.title_slug方法代码示例

本文整理汇总了Python中models.Page.title_slug方法的典型用法代码示例。如果您正苦于以下问题:Python Page.title_slug方法的具体用法?Python Page.title_slug怎么用?Python Page.title_slug使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在models.Page的用法示例。


在下文中一共展示了Page.title_slug方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: edit_page

# 需要导入模块: from models import Page [as 别名]
# 或者: from models.Page import title_slug [as 别名]
def edit_page(request, slug):
	try:
		page = Page.objects.get(title_slug=slug)
	except Page.DoesNotExist:
		page = Page()
		page.title = slug
		page.title_slug = slug
	
	if update_post_with_request(page, request, update_user=True, force_slug=False):
		return HttpResponseRedirect(page.get_absolute_url())
	else:
		return render_req_to_resp(request, 'post_edit.html', {'post': page})
开发者ID:biappi,项目名称:radiocicletta,代码行数:14,代码来源:views.py

示例2: save

# 需要导入模块: from models import Page [as 别名]
# 或者: from models.Page import title_slug [as 别名]
def save(user_slug, wiki_slug, page_slug):
    """ Saves page changes in the database.
    -] patch - array of changed content blocks; 'undefined' if unchanged
    <- slugified page title
    """ 
    user = User.get_by(name_slug=user_slug)
    if user != g.user:
        return False
    wiki = user.wiki_by_slug(wiki_slug)
    try:
        page = wiki.page_by_slug(page_slug)
    except NoResultFound:
        old_last_page = Page.get_by(wiki=wiki, next_page_id=-1)
        page = Page(wiki=wiki, title="", title_slug="", 
            content="<h1>Untitled</h1>", next_page_id=-1)
        models.session.commit()
        old_last_page.next_page_id = page.id
    patch = request.form.getlist('patch')
    block = re.compile('<(?:h1|h2|h3|p)>.*?</(?:h1|h2|h3|p)>')
    content_blocks = block.findall(page.content)
    # If the page's title has been changed in the content...
    new_title = page.title
    if len(patch) and patch[0] != 'undefined':
        new_title = patch[0][4:-5]
        newline = re.compile('\n')
        new_title = re.sub(newline, "", new_title)
    if page.title != new_title:
        # ..if a page exists with this title, append " (alternative)" to it.
        try:
            same_title_page = Page.get_by(title=new_title)
        except NoResultFound:
            pass
        if same_title_page:
            stp = same_title_page
            header_end = 4 + len(stp.title)
            stp.title += " (alternative)"
            stp.title_slug = slugify(stp.title)
            stp.content = stp.content[:header_end] + " (alternative)" + \
                stp.content[header_end:]
        # ..if the page with changed title was the main page, make a new one.
        if page.title == wiki.title:
            try:
                if new_title:
                    second_page = page
                else:
                    second_page = Page.get_by(id=page.next_page_id)
                next_page_id = second_page.id
            except NoResultFound:
                next_page_id = -1
            new_main_page = Page(wiki=wiki, title=wiki.title, 
                title_slug=slugify(wiki.title), 
                content="<h1>%s</h1><p></p>" % wiki.title, 
                next_page_id=next_page_id)
            models.session.commit()
            wiki.first_page_id = new_main_page.id
        # ..change the page's title in the database.
        page.title = new_title
        page.title_slug = slugify(new_title)
    # Replace content with patched content.
    for i, block in enumerate(patch):
        if block != 'undefined':
            if i < len(content_blocks):
                content_blocks[i] = block
            else:
                content_blocks.append(block)
    # Sanitize unsafe angle brackets.
    content = ''.join(content_blocks)
    unsafe_lt = re.compile(r"<(?!/?(h1|h2|h3|p|b|i|u)>)")
    content = re.sub(unsafe_lt, '&lt;', content)
    # The content is reversed to get around regex lookbehind limitation.
    unsafe_gt = re.compile(r">(?!(1h|2h|3h|p|b|i|u)/?<)")
    content = re.sub(unsafe_gt, ';tg&', content[::-1])
    page.content = content[::-1]
    # If the title is blank, delete the page.
    deleted = False
    blank_title = re.compile('<h1>(<br>)*</h1>')
    if blank_title.match(page.content):
        try:
            previous_page = Page.get_by(wiki=wiki, next_page_id=page.id)
            previous_page.next_page_id = page.next_page_id
        except NoResultFound:
            pass
        page.delete()
        deleted = True
    # Update the wiki's update date.
    wiki.update_date = datetime.now()
    # Commit to database!
    models.session.commit()
    if not deleted:
        return slugify(page.title)
    else:
        return "untitled!"
开发者ID:aostrega,项目名称:piki,代码行数:94,代码来源:piki.py


注:本文中的models.Page.title_slug方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。