From bd48fd072b6a5e44059eabb3a2a83e8ec62e5b0a Mon Sep 17 00:00:00 2001 From: Benjamin Bertrand <benjamin.bertrand@esss.se> Date: Thu, 21 Dec 2017 15:17:54 +0100 Subject: [PATCH] Add API to view/create comments on item --- app/api/inventory.py | 17 +++++++++++++++++ app/api/utils.py | 3 ++- app/models.py | 12 ++++++++++++ tests/functional/test_api.py | 2 +- 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/app/api/inventory.py b/app/api/inventory.py index 2fe7fc1..245ef2f 100644 --- a/app/api/inventory.py +++ b/app/api/inventory.py @@ -113,6 +113,23 @@ def patch_item(id_): return jsonify(item.to_dict()) +@bp.route('/items/<id_>/comments') +@jwt_required +def get_item_comments(id_): + item = get_item_by_id_or_ics_id(id_) + return jsonify([comment.to_dict() for comment in item.comments]) + + +@bp.route('/items/<id_>/comments', methods=['POST']) +@jwt_required +@jwt_groups_accepted('admin', 'create') +def create_item_comment(id_): + item = get_item_by_id_or_ics_id(id_) + return create_generic_model(models.ItemComment, + mandatory_fields=('text',), + item_id=item.id) + + @bp.route('/actions') @jwt_required def get_actions(): diff --git a/app/api/utils.py b/app/api/utils.py index 89f9480..64471fd 100644 --- a/app/api/utils.py +++ b/app/api/utils.py @@ -39,11 +39,12 @@ def get_generic_model(model, args, order_by=None): return jsonify(data) -def create_generic_model(model, mandatory_fields=('name',)): +def create_generic_model(model, mandatory_fields=('name',), **kwargs): data = request.get_json() if data is None: raise utils.CSEntryError('Body should be a JSON object') current_app.logger.debug(f'Received: {data}') + data.update(kwargs) for mandatory_field in mandatory_fields: if mandatory_field not in data: raise utils.CSEntryError(f"Missing mandatory field '{mandatory_field}'", status_code=422) diff --git a/app/models.py b/app/models.py index 1a9c29f..eaac006 100644 --- a/app/models.py +++ b/app/models.py @@ -333,6 +333,7 @@ class Item(CreatedMixin, db.Model): 'children': [str(child) for child in self.children], 'macs': [str(mac) for mac in self.macs], 'history': self.history(), + 'comments': [str(comment) for comment in self.comments], }) return d @@ -360,6 +361,17 @@ class ItemComment(CreatedMixin, db.Model): text = db.Column(db.Text, nullable=False) item_id = db.Column(db.Integer, db.ForeignKey('item.id'), nullable=False) + def __str__(self): + return self.text + + def to_dict(self): + d = super().to_dict() + d.update({ + 'text': self.text, + 'item': str(self.item), + }) + return d + class Network(CreatedMixin, db.Model): vlan_name = db.Column(CIText, nullable=False, unique=True) diff --git a/tests/functional/test_api.py b/tests/functional/test_api.py index 781302c..2991814 100644 --- a/tests/functional/test_api.py +++ b/tests/functional/test_api.py @@ -207,7 +207,7 @@ def test_create_item(client, user_token): assert response.status_code == 201 assert {'id', 'ics_id', 'serial_number', 'manufacturer', 'model', 'location', 'status', 'parent', 'children', 'macs', 'history', - 'updated_at', 'created_at', 'user'} == set(response.json.keys()) + 'updated_at', 'created_at', 'user', 'comments'} == set(response.json.keys()) assert response.json['serial_number'] == '123456' # Check that serial_number doesn't have to be unique -- GitLab