fixed 2 bugs

This commit is contained in:
Cayo Puigdefabregas 2021-02-24 23:22:36 +01:00
parent 929ad8e2e9
commit aa33688382
1 changed files with 11 additions and 5 deletions

View File

@ -11,6 +11,13 @@ from ereuse_devicehub.resources.tag import Tag
class TagView(View): class TagView(View):
def one(self, id):
"""Gets the device from the named tag, /tags/namedtag."""
tag = Tag.from_an_id(id).one() # type: Tag
if not tag.device:
raise TagNotLinked(tag.id)
return redirect(location=url_for_resource(Device, tag.device.id))
@auth.Auth.requires_auth @auth.Auth.requires_auth
def post(self): def post(self):
"""Creates a tag.""" """Creates a tag."""
@ -37,7 +44,6 @@ class TagView(View):
def _create_many_regular_tags(self, num: int): def _create_many_regular_tags(self, num: int):
tags_id, _ = g.tag_provider.post('/', {}, query=[('num', num)]) tags_id, _ = g.tag_provider.post('/', {}, query=[('num', num)])
tags = [Tag(id=tag_id, provider=g.inventory.tag_provider) for tag_id in tags_id] tags = [Tag(id=tag_id, provider=g.inventory.tag_provider) for tag_id in tags_id]
import pdb; pdb.set_trace()
db.session.add_all(tags) db.session.add_all(tags)
db.session().final_flush() db.session().final_flush()
response = things_response(self.schema.dump(tags, many=True, nested=1), code=201) response = things_response(self.schema.dump(tags, many=True, nested=1), code=201)
@ -45,8 +51,6 @@ class TagView(View):
return response return response
def _post_one(self): def _post_one(self):
# todo do we use this?
# import pdb; pdb.set_trace()
t = request.get_json() t = request.get_json()
tag = Tag(**t) tag = Tag(**t)
if tag.like_etag(): if tag.like_etag():
@ -62,7 +66,6 @@ class TagDeviceView(View):
def one(self, id): def one(self, id):
"""Gets the device from the tag.""" """Gets the device from the tag."""
import pdb; pdb.set_trace()
tag = Tag.from_an_id(id).one() # type: Tag tag = Tag.from_an_id(id).one() # type: Tag
if not tag.device: if not tag.device:
raise TagNotLinked(tag.id) raise TagNotLinked(tag.id)
@ -71,9 +74,9 @@ class TagDeviceView(View):
return app.resources[Device.t].schema.jsonify(tag.device) return app.resources[Device.t].schema.jsonify(tag.device)
# noinspection PyMethodOverriding # noinspection PyMethodOverriding
@auth.Auth.requires_auth
def put(self, tag_id: str, device_id: str): def put(self, tag_id: str, device_id: str):
"""Links an existing tag with a device.""" """Links an existing tag with a device."""
import pdb; pdb.set_trace()
tag = Tag.from_an_id(tag_id).one() # type: Tag tag = Tag.from_an_id(tag_id).one() # type: Tag
if tag.device_id: if tag.device_id:
if tag.device_id == device_id: if tag.device_id == device_id:
@ -81,7 +84,10 @@ class TagDeviceView(View):
else: else:
raise LinkedToAnotherDevice(tag.device_id) raise LinkedToAnotherDevice(tag.device_id)
else: else:
# Check if this device exist for this woner
Device.query.filter_by(owner=g.user).filter_by(id=device_id).one()
tag.device_id = device_id tag.device_id = device_id
db.session().final_flush() db.session().final_flush()
db.session.commit() db.session.commit()
return Response(status=204) return Response(status=204)