Esempio n. 1
0
 def test_keep_name_unique(self, app, example_hierarchy):
     with app.app_context():
         node3 = Node.get_by_id(3)
         node7 = Node.get_by_id(7)
         Node.create(name='I am unique within my tree', parent_node=node3)
         Node.create(name='I am unique within my tree', parent_node=node7)
         with pytest.raises(ValueError):
             node3.move(node7)
Esempio n. 2
0
 def test_move_subtree(self, app, example_hierarchy):
     subtree = example_hierarchy.pop(1)
     subtree['parent_id'] = 2
     example_hierarchy[0]['children'].append(subtree)
     with app.app_context():
         Node.get_by_id(5).move(Node.get_by_id(2))
         hierarchy = Node.get_hierarchy()
     
     assert hierarchy == example_hierarchy
Esempio n. 3
0
 def test_move_rigth(self, app, example_hierarchy):
     item = example_hierarchy[0]['children'].pop(0)
     item['parent_id'] = 7
     example_hierarchy[2]['children'].append(item)
     with app.app_context():
         Node.get_by_id(3).move(Node.get_by_id(7))
         hierarchy = Node.get_hierarchy()
     
     assert hierarchy == example_hierarchy
Esempio n. 4
0
    def test_create_node(self, app):
        expected_root_node = Node(id=1, name='root', parent_id=None, lft=0, rgt=3)
        expected_new_node = Node(id=2, name='new node', parent_id=1, lft=1, rgt=2, tree_id=2)
        with app.app_context():
            new_node = Node.create(expected_new_node.name, Node.get_by_id(expected_new_node.parent_id))
            assert new_node == Node.get_by_id(expected_new_node.id)
            root_node = Node.get_by_id(expected_root_node.id)

        assert root_node == expected_root_node and new_node == expected_new_node
Esempio n. 5
0
 def post(self):
     user = User.logged_in_user() 
     if user:
         payload = request.json or {}
         comment = payload.get('comment')
         post_key    = payload.get('key')
         author = Node.get_by_id(user.id)
         post = Node.get_by_id(post_key)
         comment_node = Service.create_comment(author, comment, post)
         return {'status': 'success', 'node': comment_node.__dict__, 'message': 'Successfully posted the comment'}
     else:
         return {'status': 'error', 'message': 'Please login first.'}
Esempio n. 6
0
    def get(self, url_prefix, key):

        node = Node.get_by_id(key)
        if not node:
            abort(404)
        _type = node.type
        templates = configuration['templates']
        template = None
        for k, v in templates.iteritems():
            if _type in v['page-types']:
                template = k
                break
        if template is None:
            raise Exception("No template found for _types: %s" % str(_types))
         
        url = template + '/detail.html'

        if hasattr(g, 'user') and g.user is not None and (g.user and hasattr(g.user, 'id') and (hasattr(node, 'created_by') and g.user.id == node.created_by) or g.user.is_admin()):
            show_edit = True
        else:
            show_edit = False
        
        comments = list(Node.find({'type': 'Comment', 'belongs_to': str(node._id)}))
        print '*** All Comments', comments
        return render_template(url, node=node, allcomments=comments, url_prefix=url_prefix, show_edit=show_edit)
Esempio n. 7
0
 def post(self):
     
     user = User.logged_in_user() 
     try:
         if user:
             payload = request.json or {}
             post_key    = payload.get('key')
             title = payload.get('title')
             text = payload.get('text')
             published = payload.get('published')
             image = payload.get('image')
             post = Node.get_by_id(post_key)
             post.title = title
             post.text = text 
             post.published = published
             post.save()
             if image:
                 try:
                     if image:
                         image = image[image.index(',') + 1:]
                         with open(os.getcwd() + '/app' + post.images[0],"wb") as f:
                             f.write(image.decode('base64'))
                 except Exception, e:
                     print str(e)
                     return {'status': 'error', 'message': 'Update the data but image could not be saved'}
             return {'status': 'success', 'node': post.__dict__, 'message': 'Successfully updated the post'}
         else:
Esempio n. 8
0
def item_by_id(item_id):
    # disallow direct access to the root node
    if item_id == Node.get_root_id():
        abort(404)

    node = Node.get_by_id(item_id)
    if node is None:
        abort(404)

    if request.method == 'GET':
        return jsonify(node.to_item())
    
    if request.method == 'DELETE':
        node.delete()
        return '', 204

    if request.method == 'POST':
        data = request.get_json()

        node = Node.get_by_id(item_id)
        if node is None:
            return api_message(404, 'Item not found')
    
        if 'name' in data and data['name'] != node.name:
            try:
                node = node.rename(data['name'])
            except ValueError as e:
                return api_message(400, str(e))
        
        if 'parent_id' in data:
            parent_id = data['parent_id']
            if parent_id is None:
                parent_id = Node.get_root_id()

            if parent_id != node.parent_id:
                parent_node = Node.get_by_id(parent_id)
                if parent_node is None:
                    return api_message(404, 'Parent item not found')

                try:
                    node = node.move(parent_node)
                except ValueError as e:
                    return api_message(400, str(e))
        return jsonify(node.to_item())
Esempio n. 9
0
def subtree(root_item_id):
    # disallow direct access to the root node
    if root_item_id == Node.get_root_id():
        abort(404)

    node = Node.get_by_id(root_item_id)
    if node is None:
        abort(404)
        
    return jsonify(node.get_subtree())
Esempio n. 10
0
 def post(self):
     
     user = User.logged_in_user() 
     try:
         if user:
             payload = request.json or {}
             post_key    = payload.get('key')
             name = payload.get('name')
             username= payload.get('username')
             email= payload.get('email')
             phone= payload.get('phone')
             address= payload.get('address')
             facebook= payload.get('facebook')
             linkedin = payload.get('linkedin')
             _type = payload.get('type')
             details = payload.get('details')
             image = payload.get('image')
             profile = Node.get_by_id(post_key)
             profile.name = name
             profile.username = username
             profile.email = email
             profile.phone = phone
             profile.address = address
             profile.facebook = facebook
             profile.linkedin = linkedin
             profile.type = _type
             profile.details = details
             profile.save()
             if image:
                 try:
                     if image:
                         image = image[image.index(',') + 1:]
                         with open(os.getcwd() + '/app' + post.images[0],"wb") as f:
                             f.write(image.decode('base64'))
                 except Exception, e:
                     print str(e)
                     return {'status': 'error', 'message': 'Update the data but image could not be saved'}
             return {'status': 'success', 'node': profile.__dict__, 'message': 'Successfully updated the post'}
         else:
Esempio n. 11
0
def item():
    data = request.get_json()
    if 'name' not in data:
        return api_message(400, '"name" required')

    if 'parent_id' not in data:
        return api_message(400, '"parent_id" required')

    parent_id = data['parent_id']
    if parent_id is None:
        parent_id = Node.get_root_id()

    parent_node = Node.get_by_id(parent_id)
    if parent_node is None:
        return api_message(400, 'Invalid "parent_id"')

    try:
        node = Node.create(name=data['name'], parent_node=parent_node)
    except ValueError as e:
        return api_message(400, str(e))
    
    return '', 201, {'Location': url_for('.item_by_id', item_id=node.id)}
Esempio n. 12
0
 def test_move_under_itself(self, app, example_hierarchy):
     with app.app_context():
         with pytest.raises(ValueError):
             Node.get_by_id(3).move(Node.get_by_id(3))
Esempio n. 13
0
def test_delete_node(app, example_hierarchy):
    del example_hierarchy[1]
    with app.app_context():
        Node.get_by_id(5).delete()
        hierarchy = Node.get_hierarchy()
    assert hierarchy == example_hierarchy
Esempio n. 14
0
def hierarchy():
    return jsonify(Node.get_by_id(Node.get_root_id()).get_subtree()['children'])
Esempio n. 15
0
def edit_profile(key):
    if not hasattr(g, 'user') or g.user is None:
        return redirect(url_for('index'))
    node = Node.get_by_id(key)
    back = request.referrer
    return render_template('user/editor.html', profile=node, back=back)