Skip to content
Snippets Groups Projects
Commit bd48fd07 authored by Benjamin Bertrand's avatar Benjamin Bertrand
Browse files

Add API to view/create comments on item

parent 5e8b9d5c
No related branches found
No related tags found
No related merge requests found
......@@ -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():
......
......@@ -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)
......
......@@ -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)
......
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment