Beispiel #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)
Beispiel #2
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)
Beispiel #3
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())
Beispiel #4
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())
Beispiel #5
0
def admin_content_delete():
    return_value = success('The content has been deleted.')
    payload = get_payload(request)
    content = Content.get(payload.get('id'))
    if content:
        content.delete()
        safe_commit()
    else:
        return_value = failure('Content not found.')

    return jsonify(return_value)
Beispiel #6
0
def create_content():
    return_value = success('The content was created.')
    payload = get_payload(request)

    editing = False
    if payload.get('id'):
        content = Content.get(payload.get('id'))
        editing = True
        return_value = success('The content was updated.')
    else:
        content = Content()

    content.type = payload.get('type').lower()
    content.template = '{}.html'.format(content.type)
    content.title = payload.get('title')
    content.body = payload.get('body') or ''
    content.theme = payload.get('theme')
    content.preview = payload.get('preview') or ''
    content.user_id = payload.get('user_id')
    tags = [t.strip() for t in payload.get('tags', '').split(',') if t.strip()]
    for tag in tags:
        count = Tag.filter(Tag.name == tag).count()
        if not count:
            new_tag = Tag(name=tag)
            new_tag.insert()

    content.tags = ",".join(tags)
    content.parser = payload.get('parser', 'markdown')

    published = json.loads(payload.get('published', 'false'))
    content.published = published

    menu_item = json.loads(payload.get('menu_item', 'false'))
    content.menu_item = menu_item

    if not editing:
        content.slug = make_slug(content.title)
    else:
        published_on = payload.get('published_on')
        if published_on:
            content.published_on = parser.parse(published_on)

    valid = content.validate()
    if valid['success'] or editing:
        print(content.to_dict())
        content.insert()
        safe_commit()
        return_value['id'] = content.id
        # with app.context():
        #    cache.clear()
    else:
        return_value = valid

    return jsonify(return_value)
Beispiel #7
0
def create_content():
    return_value = success('The content was created.')
    payload = get_payload(request)

    editing = False
    if payload.get('id'):
        content = Content.get(payload.get('id'))
        editing = True
        return_value = success('The content was updated.')
    else:
        content = Content()

    content.type = payload.get('type').lower()
    content.title = payload.get('title')
    content.body = payload.get('body')
    content.user_id = payload.get('user_id')
    tags = [t.strip() for t in payload.get('tags', '').split(',') if t.strip()]
    for tag in tags:
        count = Tag.filter(Tag.name == tag).count()
        if not count:
            new_tag = Tag(name=tag)
            new_tag.insert()

    content.tags = ",".join(tags)
    content.parser = payload.get('parser', 'markdown')
    content.published = bool(payload.get('published', False))

    if not editing:
        content.slug = make_slug(content.title)
    else:
        created_on = payload.get('created_on')
        if created_on:
            content.created_on = parser.parse(created_on)

    valid = content.validate()
    if valid['success'] or editing:
        content.insert()
        safe_commit()
        return_value['id'] = content.id
    else:
        return_value = valid

    return jsonify(return_value)
Beispiel #8
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)
Beispiel #9
0
def admin_posts_edit(content_id):
    content = Content.get(content_id)
    return render('admin_content.html', user=g.user, content_type=content.type, action="Edit", content=content)
Beispiel #10
0
    def test_content_create(self):
        api_key = self.s.sign(self.api_key.name)

        '''
        CREATE
        '''
        post_data = {
            'title': 'This is a test page',
            'body': 'Blah blah blah',
            'type': 'post',
            'user_id': self.user.id
        }
        # Try to create the content with no API key
        rv = self.app.post('/content_create', data=post_data, follow_redirects=True)
        data = json.loads(rv.data)
        self.assertFalse(data['success'])

        # Create the content. This should work fine.
        post_data['api_key'] = api_key
        rv = self.app.post('/content_create', data=post_data, follow_redirects=True)
        data = json.loads(rv.data)
        self.assertTrue(data['success'])
        self.assertTrue(data['id'])
        self.assertIsNotNone(data['messages'])
        self.assertEquals(data['messages'][0], 'The content was created.')
        content_id = data['id']

        # Make sure that we can grab the content from the DB.
        content = Content.get(content_id)
        self.assertIsNotNone(content)
        self.assertEquals(content.title, post_data['title'])

        # Try to create the same content again. This should fail.
        rv = self.app.post('/content_create', data=post_data, follow_redirects=True)
        data = json.loads(rv.data)
        self.assertFalse(data['success'])
        self.assertIsNotNone(data['messages'])
        self.assertEquals(data['messages'][0], 'That post or page exists already.')

        # Clean up!
        content.delete()
        safe_commit()

        # Create the content. This should work fine.
        post_data['api_key'] = api_key
        post_data['type'] = 'page'
        rv = self.app.post('/content_create', data=post_data, follow_redirects=True)
        data = json.loads(rv.data)
        self.assertTrue(data['success'])
        self.assertTrue(data['id'])
        self.assertIsNotNone(data['messages'])
        self.assertEquals(data['messages'][0], 'The content was created.')
        content_id = data['id']

        # Make sure that we can grab the content from the DB.
        content = Content.get(content_id)
        self.assertIsNotNone(content)
        self.assertEquals(content.title, post_data['title'])

        # Try to create the same content again. This should fail.
        rv = self.app.post('/content_create', data=post_data, follow_redirects=True)
        data = json.loads(rv.data)
        self.assertFalse(data['success'])
        self.assertIsNotNone(data['messages'])
        self.assertEquals(data['messages'][0], 'That post or page exists already.')

        # Clean up!
        content.delete()
        safe_commit()
Beispiel #11
0
    def test_content_retrieve(self):
        user_id = self.user.id

        '''
        RETRIEVE
        '''

        # Create some content using the model directly...
        content = Content(title="Test Content", published=True, type="post", body="blah blah blah", user_id=self.user.id)
        content.insert()

        content1 = content.to_dict()

        content2 = Content(title="Test Content 2", published=True, type="post", body="blah blah blah", user_id=self.user.id)
        content2.insert()

        content2 = content2.to_dict()

        content3 = Content(title="Test Content 3", published=True, type="post", body="blah blah blah", user_id=self.user.id)
        content3.insert()

        content3 = content3.to_dict()

        content4 = Content(title="Test Content 4", published=True, type="post", body="blah blah blah", user_id=self.user.id)
        content4.insert()

        content4 = content4.to_dict()

        safe_commit()

        post_data = {
            'id': content.id
        }
        # retrieve the content. This should work fine.
        rv = self.app.post('/content_retrieve', data=post_data, follow_redirects=True)
        data = json.loads(rv.data)
        self.assertTrue(data['success'])
        self.assertTrue(data['contents'][0])
        self.assertIsNotNone(data['messages'])

        content = Content.get(data['contents'][0]['id'])
        self.assertEquals(content.title, data['contents'][0]['title'])
        self.assertEquals(content.body, data['contents'][0]['body'])
        self.assertEquals(user_id, data['contents'][0]['user_id'])

        post_data = {
            'content_type': 'post',
            'page_size': 3
        }
        # retrieve the content. This should work fine.
        rv = self.app.post('/content_retrieve', data=post_data, follow_redirects=True)
        data = json.loads(rv.data)
        self.assertTrue(data['success'])

        # There should be three posts.
        self.assertEquals(data['contents'][0]['title'], content4['title'])
        self.assertEquals(data['contents'][1]['title'], content3['title'])
        self.assertEquals(data['contents'][2]['title'], content2['title'])

        # And only three posts returned
        self.assertTrue(len(data['contents']) == 3)

        # Posts should be in the right order
        self.assertTrue(data['contents'][1]['published_on'] < data['contents'][0]['published_on'])

        self.assertIsNotNone(data['messages'])

        post_data = {
            'content_type': 'post',
            'current_page': 2,
            'page_size': 3
        }
        # retrieve the content. This should work fine.
        rv = self.app.post('/content_retrieve', data=post_data, follow_redirects=True)
        data = json.loads(rv.data)
        self.assertTrue(data['success'])

        # There should be one post.
        self.assertEquals(data['contents'][0]['title'], content1['title'])

        # And only one post returned
        self.assertTrue(len(data['contents']) == 1)
Beispiel #12
0
    def test_content_retrieve(self):
        user_id = self.user.id

        '''
        RETRIEVE
        '''

        # Create some content using the model directly...
        content = Content(title="Test Content", published=True, type="post", body="blah blah blah", user_id=self.user.id)
        content.insert()

        content1 = content.to_dict()

        content2 = Content(title="Test Content 2", published=True, type="post", body="blah blah blah", user_id=self.user.id)
        content2.insert()

        content2 = content2.to_dict()

        content3 = Content(title="Test Content 3", published=True, type="post", body="blah blah blah", user_id=self.user.id)
        content3.insert()

        content3 = content3.to_dict()

        content4 = Content(title="Test Content 4", published=True, type="post", body="blah blah blah", user_id=self.user.id)
        content4.insert()

        content4 = content4.to_dict()

        safe_commit()

        post_data = {
            'id': content.id
        }
        # retrieve the content. This should work fine.
        rv = self.app.post('/content_retrieve', data=post_data, follow_redirects=True)
        data = json.loads(rv.data)
        self.assertTrue(data['success'])
        self.assertTrue(data['contents'][0])
        self.assertIsNotNone(data['messages'])

        content = Content.get(data['contents'][0]['id'])
        self.assertEquals(content.title, data['contents'][0]['title'])
        self.assertEquals(content.body, data['contents'][0]['body'])
        self.assertEquals(user_id, data['contents'][0]['user_id'])

        post_data = {
            'content_type': 'post',
            'page_size': 3
        }
        # retrieve the content. This should work fine.
        rv = self.app.post('/content_retrieve', data=post_data, follow_redirects=True)
        data = json.loads(rv.data)
        self.assertTrue(data['success'])

        # There should be three posts.
        self.assertEquals(data['contents'][0]['title'], content4['title'])
        self.assertEquals(data['contents'][1]['title'], content3['title'])
        self.assertEquals(data['contents'][2]['title'], content2['title'])

        # And only three posts returned
        self.assertTrue(len(data['contents']) == 3)

        # Posts should be in the right order
        self.assertTrue(data['contents'][1]['published_on'] < data['contents'][0]['published_on'])

        self.assertIsNotNone(data['messages'])

        post_data = {
            'content_type': 'post',
            'current_page': 2,
            'page_size': 3
        }
        # retrieve the content. This should work fine.
        rv = self.app.post('/content_retrieve', data=post_data, follow_redirects=True)
        data = json.loads(rv.data)
        self.assertTrue(data['success'])

        # There should be one post.
        self.assertEquals(data['contents'][0]['title'], content1['title'])

        # And only one post returned
        self.assertTrue(len(data['contents']) == 1)
Beispiel #13
0
    def test_content_create(self):
        api_key = self.s.sign(self.api_key.name)

        '''
        CREATE
        '''
        post_data = {
            'title': 'This is a test page',
            'body': 'Blah blah blah',
            'type': 'post',
            'published': 1,
            'user_id': self.user.id
        }
        # Try to create the content with no API key
        rv = self.app.post('/content_create', data=post_data, follow_redirects=True)
        data = json.loads(rv.data)
        self.assertFalse(data['success'])

        # Create the content. This should work fine.
        post_data['api_key'] = api_key
        rv = self.app.post('/content_create', data=post_data, follow_redirects=True)
        data = json.loads(rv.data)
        self.assertTrue(data['success'])
        self.assertTrue(data['id'])
        self.assertIsNotNone(data['messages'])
        self.assertEquals(data['messages'][0], 'The content was created.')
        content_id = data['id']

        # Make sure that we can grab the content from the DB.
        content = Content.get(content_id)
        self.assertIsNotNone(content)
        self.assertEquals(content.title, post_data['title'])

        # Try to create the same content again. This should fail.
        rv = self.app.post('/content_create', data=post_data, follow_redirects=True)
        data = json.loads(rv.data)
        self.assertFalse(data['success'])
        self.assertIsNotNone(data['messages'])
        self.assertEquals(data['messages'][0], 'That post or page exists already.')

        # Clean up!
        content.delete()
        safe_commit()

        # Create the content. This should work fine.
        post_data['api_key'] = api_key
        post_data['type'] = 'page'
        rv = self.app.post('/content_create', data=post_data, follow_redirects=True)
        data = json.loads(rv.data)
        self.assertTrue(data['success'])
        self.assertTrue(data['id'])
        self.assertIsNotNone(data['messages'])
        self.assertEquals(data['messages'][0], 'The content was created.')
        content_id = data['id']

        # Make sure that we can grab the content from the DB.
        content = Content.get(content_id)
        self.assertIsNotNone(content)
        self.assertEquals(content.title, post_data['title'])

        # Try to create the same content again. This should fail.
        rv = self.app.post('/content_create', data=post_data, follow_redirects=True)
        data = json.loads(rv.data)
        self.assertFalse(data['success'])
        self.assertIsNotNone(data['messages'])
        self.assertEquals(data['messages'][0], 'That post or page exists already.')

        # Clean up!
        content.delete()
        safe_commit()
Beispiel #14
0
def admin_posts_edit(content_id):
    content = Content.get(content_id)
    return render_admin('content.html', user=g.user, content_type=content.type,
                        action="Edit", content=content)
Beispiel #15
0
def admin_pages_edit(content_id):
    content = Content.get(content_id)
    themes = [t.identifier for t in get_themes_list() if t.identifier != 'admin']
    return render_admin('content.html', user=g.user, content_type=content.type,
                        action="Edit", content=content, themes=themes)