Beispiel #1
0
def template_set_default(template_id):
    '''
    UI for setting a given template as the default for an archive type
    '''

    user = auth.is_logged_in(request)
    tpl = Template.load(template_id)
    blog = Blog.load(tpl.blog.id)
    permission = auth.is_blog_designer(user, blog)

    auth.check_template_lock(blog)

    tags = template_tags(template=tpl, user=user)

    from core.utils import Status
    import settings

    if request.forms.getunicode('confirm') == user.logout_nonce:

        # check for form submission
        # setting should be done by way of class object
        # theme? blog? template?
        # blog.set_default_archive_template(template,{archive_type.index...})

        status = Status(
            type='success',
            close=False,
            message=
            'Template <b>{}</b> was successfully refreshed from theme <b>{}</b>.'
            .format(tpl.for_display, tpl.theme.for_display),
            action='Return to template',
            url='{}/template/{}/edit'.format(settings.BASE_URL, tpl.id))
        tags.status = status
    else:
        pass

    from core.models import archive_defaults

    return template('edit/template-set-default',
                    icons=icons,
                    search_context=(search_contexts['blog'], tags.blog),
                    menu=generate_menu('blog_edit_template', tags.template),
                    sidebar=sidebar.render_sidebar(
                        panel_set='edit_template',
                        publishing_mode=publishing_mode,
                        types=template_type,
                        **tags.__dict__),
                    archive_defaults=archive_defaults,
                    **tags.__dict__)
Beispiel #2
0
def template_delete(template_id):
    '''
    UI for deleting a template
    '''
    user = auth.is_logged_in(request)
    tpl = Template.load(template_id)
    blog = Blog.load(tpl.blog)
    permission = auth.is_blog_designer(user, blog)

    from core.utils import Status
    import settings

    tags = template_tags(template_id=tpl.id, user=user)

    if request.forms.getunicode('confirm') == user.logout_nonce:

        # _template.delete(tpl)
        tpl.delete_instance()

        status = Status(type='success',
                        close=False,
                        message='Template {} was successfully deleted.'.format(
                            tpl.for_log),
                        action='Return to template list',
                        url='{}/blog/{}/templates'.format(
                            settings.BASE_URL, blog.id))

    else:

        status = Status(
            type='warning',
            close=False,
            message=
            'You are attempting to delete template <b>{}</b> from blog <b>{}</b>.'
            .format(tpl.for_display, blog.for_display),
            no={
                'url': '{}/template/{}/edit'.format(settings.BASE_URL, tpl.id),
                'label': 'No, I don\'t want to delete this template'
            },
            yes={
                'id': 'delete',
                'name': 'confirm',
                'label': 'Yes, I want to delete this template',
                'value': user.logout_nonce
            })

    tags.status = status

    return report(tags, 'blog_delete_template', tpl)
Beispiel #3
0
def blog_select_themes(blog_id):
    user = auth.is_logged_in(request)
    blog = Blog.load(blog_id)
    permission = auth.is_blog_designer(user, blog)
    errormsg = auth.check_template_lock(blog, True)

    class ThemeListing(Theme):

        @property
        def blog(self):
            return blog

        class Meta:
            db_table = 'theme'

    return listing(request, blog, ThemeListing.select().order_by(ThemeListing.title),
                   'themes', 'blog_manage_themes',
                   user=user,
                   errormsg=errormsg,
                   tags_data={'blog':blog}
                   )
Beispiel #4
0
def template_edit(template_id):
    '''
    UI for editing a blog template
    '''

    user = auth.is_logged_in(request)
    edit_template = Template.load(template_id)
    blog = Blog.load(edit_template.blog.id)
    permission = auth.is_blog_designer(user, blog)

    auth.check_template_lock(blog)

    utils.disable_protection()

    tags = template_tags(template_id=template_id, user=user)

    # find out if the template object returns a list of all the mappings, or just the first one
    # it's edit_template.mappings

    tags.mappings = template_mapping_index[edit_template.template_type]

    return template_edit_output(tags)
Beispiel #5
0
def blog_select_themes(blog_id):
    user = auth.is_logged_in(request)
    blog = Blog.load(blog_id)
    permission = auth.is_blog_designer(user, blog)
    errormsg = auth.check_template_lock(blog, True)

    class ThemeListing(Theme):
        @property
        def blog(self):
            return blog

        class Meta:
            db_table = 'theme'

    return listing(request,
                   blog,
                   ThemeListing.select().order_by(ThemeListing.title),
                   'themes',
                   'blog_manage_themes',
                   user=user,
                   errormsg=errormsg,
                   tags_data={'blog': blog})
Beispiel #6
0
def new_template(blog_id, tpl_type):
    with db.atomic() as txn:
        user = auth.is_logged_in(request)
        blog = Blog.load(blog_id)
        permission = auth.is_blog_designer(user, blog)

        auth.check_template_lock(blog)

        mappings_index = template_mapping_index.get(tpl_type, None)
        if mappings_index is None:
            raise Exception('Mapping type not found')

        template = Template(
            blog=blog,
            theme=blog.theme,
            template_type=tpl_type,
            publishing_mode=publishing_mode.do_not_publish,
            body='',
        )
        template.save(user)
        template.title = 'Untitled Template #{}'.format(template.id)
        template.save(user)

        if tpl_type != template_type.media:

            new_template_mapping = TemplateMapping(
                template=template,
                is_default=True,
                path_string="'" + utils.create_basename(template.title, blog) +
                "'")

            new_template_mapping.save()
            from core.cms import fileinfo
            fileinfo.build_mapping_xrefs((new_template_mapping, ))

    from settings import BASE_URL
    redirect(BASE_URL + '/template/{}/edit'.format(template.id))
Beispiel #7
0
def template_edit_save(template_id):
    '''
    UI for saving a blog template
    '''
    user = auth.is_logged_in(request)
    tpl = Template.load(template_id)
    blog = Blog.load(tpl.blog)
    permission = auth.is_blog_designer(user, blog)

    auth.check_template_lock(blog)

    from core.utils import Status
    from core.error import TemplateSaveException, PageNotChanged

    status = None

    save_mode = int(request.forms.getunicode('save', default="0"))

    if save_mode in (1, 2, 3):
        try:
            message = template_save(request, user, tpl, blog)
        except TemplateSaveException as e:
            status = Status(type='danger',
                            no_sure=True,
                            message="Error saving template <b>{}</b>:".format(
                                tpl.for_display),
                            message_list=(e, ))
        except PageNotChanged as e:
            status = Status(type='success',
                            message="Template <b>{}</b> was unchanged.".format(
                                tpl.for_display))

        except Exception as e:
            raise e
            status = Status(
                type='warning',
                no_sure=True,
                message="Problem saving template <b>{}</b>: <br>".format(
                    tpl.for_display),
                message_list=(e, ))

        else:
            tpl.delete_preview()
            status = Status(
                type='success',
                message="Template <b>{}</b> saved successfully. {}".format(
                    tpl.for_display,
                    message)  # TODO: move messages into message lister
            )

    tags = template_tags(template_id=template_id, user=user)

    tags.mappings = template_mapping_index[tpl.template_type]

    tags.status = status

    from core.models import (template_type as template_types)

    return template('edit/template_ajax',
                    sidebar=sidebar.render_sidebar(
                        panel_set='edit_template',
                        publishing_mode=publishing_mode,
                        types=template_types,
                        **tags.__dict__),
                    **tags.__dict__)
Beispiel #8
0
def template_refresh(template_id):
    '''
    UI for reloading a template from the original version stored in the
    template's theme (assuming such an original template exists)
    '''
    user = auth.is_logged_in(request)
    tpl = Template.load(template_id)
    blog = Blog.load(tpl.blog)
    permission = auth.is_blog_designer(user, blog)

    from core.utils import Status
    import settings

    tags = template_tags(template_id=tpl.id, user=user)

    if request.forms.getunicode('confirm') == user.logout_nonce:

        import os, json
        template_path = (os.path.join(tpl.theme.path, tpl.template_ref))

        with open(template_path, 'r') as f:
            tp_json = json.loads(f.read())
            # TODO: We will eventually merge in all the other refresh functions
            # and convert this to a generic function called from
            # mgmt.theme_apply_to_blog as well
            with open(template_path[:-5] + '.tpl', 'r') as b:
                tpl.body = b.read()
            tpl.save(user)

        status = Status(
            type='success',
            close=False,
            message=
            'Template <b>{}</b> was successfully refreshed from theme <b>{}</b>.'
            .format(tpl.for_display, tpl.theme.for_display),
            action='Return to template',
            url='{}/template/{}/edit'.format(settings.BASE_URL, tpl.id))

    else:

        status = Status(type='warning',
                        close=False,
                        message='''
You are attempting to refresh template <b>{}</b> for blog <b>{}</b> from its underlying theme <b>{}</b>.</p>
<p>This will <b>overwrite</b> the current version of the template and replace it with the original version
from the theme.
'''.format(tpl.for_display, blog.for_display, tpl.theme.for_display),
                        no={
                            'url':
                            '{}/template/{}/edit'.format(
                                settings.BASE_URL, tpl.id),
                            'label':
                            'No, I don\'t want to replace this template'
                        },
                        yes={
                            'id': 'delete',
                            'name': 'confirm',
                            'label': 'Yes, I want to replace this template',
                            'value': user.logout_nonce
                        })

    tags.status = status

    return report(tags, 'blog_delete_template', tpl)
Beispiel #9
0
def blog_templates(blog_id):
    '''
    List all templates in a given blog
    '''
    user = auth.is_logged_in(request)
    blog = Blog.load(blog_id)
    permission = auth.is_blog_designer(user, blog)

    reason = auth.check_template_lock(blog, True)

    tags = template_tags(blog_id=blog.id, user=user)

    tags.status = reason

    from core.libs.peewee import JOIN_LEFT_OUTER

    template_list = Template.select(Template, TemplateMapping).join(
        TemplateMapping, JOIN_LEFT_OUTER).where(
            (Template.blog == blog)).order_by(Template.title)

    index_templates = template_list.select(
        Template,
        TemplateMapping).where(Template.template_type == template_type.index)

    page_templates = template_list.select(
        Template,
        TemplateMapping).where(Template.template_type == template_type.page)

    archive_templates = template_list.select(
        Template,
        TemplateMapping).where(Template.template_type == template_type.archive)

    template_includes = template_list.select(
        Template,
        TemplateMapping).where(Template.template_type == template_type.include)

    media_templates = template_list.select(
        Template,
        TemplateMapping).where(Template.template_type == template_type.media)

    code_templates = template_list.select(
        Template,
        TemplateMapping).where(Template.template_type == template_type.code)

    system_templates = template_list.select(
        Template,
        TemplateMapping).where(Template.template_type == template_type.system)

    from core.models import archive_defaults

    tags.list_items = (
        {
            'title': 'Index Templates',
            'type': template_type.index,
            'data': index_templates,
            'defaults': archive_defaults[template_type.index]
        },
        {
            'title': 'Page Templates',
            'type': template_type.page,
            'data': page_templates,
            'defaults': archive_defaults[template_type.page]
        },
        {
            'title': 'Archive Templates',
            'type': template_type.archive,
            'data': archive_templates,
            'defaults': archive_defaults[template_type.archive]
        },
        {
            'title': 'Includes',
            'type': template_type.include,
            'data': template_includes
        },
        {
            'title': 'Media Templates',
            'type': template_type.media,
            'data': media_templates
        },
        {
            'title': 'Code',
            'type': template_type.code,
            'data': code_templates
        },
        {
            'title': 'System Templates',
            'type': template_type.system,
            'data': system_templates
        },
    )

    return template('ui/ui_blog_templates',
                    icons=icons,
                    section_title="Templates",
                    publishing_mode=publishing_mode,
                    search_context=(search_contexts['blog_templates'], blog),
                    menu=generate_menu('blog_manage_templates', blog),
                    templates_with_defaults=('Index', 'Page', 'Archive'),
                    **tags.__dict__)
Beispiel #10
0
def blog_templates(blog_id):
    '''
    List all templates in a given blog
    '''
    user = auth.is_logged_in(request)
    blog = Blog.load(blog_id)
    permission = auth.is_blog_designer(user, blog)

    reason = auth.check_template_lock(blog, True)

    tags = template_tags(blog_id=blog.id,
        user=user)

    tags.status = reason

    from core.libs.peewee import JOIN_LEFT_OUTER

    template_list = Template.select(Template, TemplateMapping).join(
        TemplateMapping, JOIN_LEFT_OUTER).where(
        (Template.blog == blog)
        ).order_by(Template.title)

    index_templates = template_list.select(Template, TemplateMapping).where(
        Template.template_type == template_type.index)

    page_templates = template_list.select(Template, TemplateMapping).where(
        Template.template_type == template_type.page)

    archive_templates = template_list.select(Template, TemplateMapping).where(
        Template.template_type == template_type.archive)

    template_includes = template_list.select(Template, TemplateMapping).where(
        Template.template_type == template_type.include)

    media_templates = template_list.select(Template, TemplateMapping).where(
        Template.template_type == template_type.media)

    code_templates = template_list.select(Template, TemplateMapping).where(
        Template.template_type == template_type.code)

    system_templates = template_list.select(Template, TemplateMapping).where(
        Template.template_type == template_type.system)

    from core.models import archive_defaults

    tags.list_items = (
        {'title':'Index Templates',
        'type': template_type.index,
        'data':index_templates,
        'defaults': archive_defaults[template_type.index]},
        {'title':'Page Templates',
        'type': template_type.page,
        'data':page_templates,
        'defaults':archive_defaults[template_type.page]},
        {'title':'Archive Templates',
        'type': template_type.archive,
        'data':archive_templates,
        'defaults': archive_defaults[template_type.archive]},
        {'title':'Includes',
        'type': template_type.include,
        'data':template_includes},
        {'title':'Media Templates',
        'type': template_type.media,
        'data':media_templates},
        {'title':'Code',
        'type': template_type.code,
        'data':code_templates},
        {'title':'System Templates',
        'type': template_type.system,
        'data':system_templates},
        )

    return template('ui/ui_blog_templates',
        icons=icons,
        section_title="Templates",
        publishing_mode=publishing_mode,
        search_context=(search_contexts['blog_templates'], blog),
        menu=generate_menu('blog_manage_templates', blog),
        templates_with_defaults=('Index', 'Page', 'Archive'),
        ** tags.__dict__)