Ejemplo n.º 1
0
def retrieve_content():
    return_value = success('The content was retrieved.')
    return_value['contents'] = []

    payload = get_payload(request)

    content_id = payload.get('id')
    if content_id:
        content = Content.get(content_id)
        if content:
            return_value['contents'] = [content.to_dict(camel_case=True)]
        else:
            return_value['success'] = False
            return_value['messages'] = ['No content found with that ID.']
    else:
        # No ID passed... we should return more than one result.
        current_page = payload.get('current_page', 1)
        page_size = payload.get('page_size', 5)
        content_type = payload.get('content_type', 'post')
        published = payload.get('published', True)
        contents = Content.filter(Content.type == content_type)\
                          .filter(Content.published == published)\
                          .order_by(Content.published_on.desc())

        contents, maxpages = paginate(contents, current_page, page_size)
        if contents:
            return_value['contents'] = results_to_dict(
                contents, camel_case=True)

    return jsonify(return_value)
Ejemplo n.º 2
0
def render_page(content_id):
    content = Content.get(content_id)
    if not content:
        content = Content.filter(Content.slug == content_id).first()

    return render(content.template, user=import_user(),
                  content=content, menu_items=get_menu_items())
Ejemplo n.º 3
0
def retrieve_content():
    return_value = success('The content was retrieved.')
    return_value['contents'] = []

    payload = get_payload(request)

    content_id = payload.get('id')
    if content_id:
        content = Content.get(content_id)
        if content:
            return_value['contents'] = [content.to_dict(camel_case=True)]
        else:
            return_value['success'] = False
            return_value['messages'] = ['No content found with that ID.']
    else:
        # No ID passed... we should return more than one result.
        current_page = payload.get('current_page', 1)
        page_size = payload.get('page_size', 5)
        content_type = payload.get('content_type', 'post')
        published = payload.get('published', True)
        contents = Content.filter(Content.type == content_type)\
                          .filter(Content.published == published)\
                          .order_by(Content.published_on.desc())

        contents, maxpages = paginate(contents, current_page, page_size)
        if contents:
            return_value['contents'] = results_to_dict(contents, camel_case=True)

    return jsonify(return_value)
Ejemplo n.º 4
0
def get_menu_items():
    items = []
    contents = Content.filter(Content.menu_item == True).filter(
        Content.published == True).all()
    for content in contents:
        items.append({'title': content.title, 'slug': content.slug})

    return items
Ejemplo n.º 5
0
def search_page():
    payload = get_payload(request)
    search = payload.get('search')
    contents = Content.filter(or_(Content.body.ilike('%{}%'.format(search)),
                                  Content.tags.ilike('%{}%'.format(search)),
                                  Content.title.ilike('%{}%'.format(search))))\
        .filter(Content.published == True).all()

    return render('search.html', user=import_user(), contents=contents,
                  menu_items=get_menu_items())
Ejemplo n.º 6
0
def render_post(content_id):
    tag_chunks = []
    tags = json.loads(get_tags_in_use())
    if tags:
        if len(tags) > 16:
            tags = tags[:16]
        tag_chunks = chunks(tags, 4)
    content = Content.get(content_id)
    if not content:
        content = Content.filter(Content.slug == content_id).first()
    return render(content.template, user=import_user(), content=content, tag_chunks=tag_chunks, menu_items=get_menu_items())
Ejemplo n.º 7
0
def get_tags_in_use():
    all_tags = {}
    contents = Content.filter(Content.published == True).all()
    for content in contents:
        tags = [t.strip() for t in content.tags.split(',') if t.strip()]
        for tag in tags:
            tag_count = all_tags.get(tag, 0)
            if tag_count:
                all_tags[tag] += 1
            else:
                all_tags[tag] = 1

    all_tags = sorted(all_tags, key=all_tags.get, reverse=True)

    return json.dumps(all_tags)
Ejemplo n.º 8
0
def main():
    """@todo: Docstring for main.
    :returns: @todo

    """
    # Clear the frozen directory
    for file in os.listdir(FROZEN_DIR):
        if file != '.empty':
            path = os.path.abspath(os.path.join(FROZEN_DIR, file))
            if os.path.isdir(path):
                shutil.rmtree(path)
            else:
                os.remove(path)

    # Copy themes
    theme_dirs = get_theme_dirs()
    for theme_dir in theme_dirs:
        dest = os.path.join(FROZEN_DIR, theme_dir[0])
        shutil.copytree(theme_dir[1], dest)

    # Copy Uploads
    shutil.copytree(os.path.abspath('uploads/'), os.path.abspath(os.path.join(FROZEN_DIR, 'uploads/')))

    # Render all the templates
    with app.app_context():
        theme = get_current_theme()
        app.jinja_env.globals['theme_static'] = theme_static
        contents = Content.filter(Content.published == True).all()
        for content in contents:
            template = content.template
            f_template = 'freeze-{}'.format(content.template)
            freeze_template = os.path.join('impression/themes/',
                                           theme.identifier,
                                           'templates/',
                                           f_template)
            if (os.path.isfile(os.path.abspath(freeze_template))):
                template = f_template

            html = render(template, content=content, menu_items=get_menu_items(), freeze=True)
            with open('frozen/{}.html'.format(content.slug), 'w') as f:
                f.write(html)
Ejemplo n.º 9
0
def blog_index(page=1, tag=None):
    tag_chunks = []
    tags = json.loads(get_tags_in_use())
    if tags:
        if len(tags) > 16:
            tags = tags[:16]
        tag_chunks = chunks(tags, 4)

    limit = get_setting("posts-per-page", 4)

    posts = Content.filter(Content.published == True)\
                   .filter(Content.type == "post")\
                   .order_by(Content.published_on.desc())

    if tag:
        posts = posts.filter(Content.tags.contains(tag))

    posts, max_pages = paginate(posts, page, limit)
    return render('index.html', user=import_user(), posts=posts, current_page=page,
                  max_pages=max_pages, tag_chunks=tag_chunks, tag=tag,
                  menu_items=get_menu_items())
Ejemplo n.º 10
0
def render_page(content_id):
    content = Content.get(content_id)
    if not content:
        content = Content.filter(Content.slug == content_id).first()
    return render('page.html', user=g.user, content=content)
Ejemplo n.º 11
0
def admin_posts_list():
    contents = Content.filter(Content.type == 'post').all()
    return render('admin_content_list.html', contents=contents, content_type="Posts")
Ejemplo n.º 12
0
def make_slug(title, delimiter='-'):
    slug = delimiter.join([w for w in re.sub('[^\w ]', '', title.replace('-', ' ')).lower().split(' ') if w])
    count = Content.filter(Content.slug == slug).count()
    slug = slug if count == 0 else "{0}{1}{2}".format(slug, delimiter, count)
    return slug
Ejemplo n.º 13
0
def admin_posts_list():
    contents = Content.filter(Content.type == 'post').order_by(
        Content.published_on.desc()).all()
    return render_admin('content_list.html', contents=contents, content_type="Posts")