Skip to content

Commit 5ef0f1f

Browse files
koitororjoshuaocero
authored andcommitted
feature(delete device): admin user can delete device (#439)
- enable deletion of a device by admin - add migrations file for adding state column to devices table - add tests [finishes #166540045]
1 parent 1c3884b commit 5ef0f1f

File tree

5 files changed

+96
-4
lines changed

5 files changed

+96
-4
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""add state column to devices table
2+
3+
Revision ID: af8e4f84b552
4+
Revises: 1f5e47273894
5+
Create Date: 2019-06-21 12:01:57.913494
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
11+
12+
# revision identifiers, used by Alembic.
13+
revision = 'af8e4f84b552'
14+
down_revision = '1f5e47273894'
15+
branch_labels = None
16+
depends_on = None
17+
18+
19+
def upgrade():
20+
# ### commands auto generated by Alembic - please adjust! ###
21+
op.add_column('devices', sa.Column('state', sa.Enum('active', 'archived', 'deleted', name='statetype'), nullable=True))
22+
# ### end Alembic commands ###
23+
24+
25+
def downgrade():
26+
# ### commands auto generated by Alembic - please adjust! ###
27+
op.drop_column('devices', 'state')
28+
# ### end Alembic commands ###

api/devices/models.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
from sqlalchemy import (Column, String, Integer, DateTime, ForeignKey)
1+
from sqlalchemy import (Column, String, Integer, DateTime, ForeignKey, Enum)
22
from sqlalchemy.schema import Sequence
33
from sqlalchemy.orm import relationship
44

55
from helpers.database import Base
66
from utilities.validations import validate_empty_fields
7-
from utilities.utility import Utility
7+
from utilities.utility import Utility, StateType
88

99

1010
class Devices(Base, Utility):
@@ -17,6 +17,7 @@ class Devices(Base, Utility):
1717
location = Column(String, nullable=False)
1818
room_id = Column(Integer, ForeignKey('rooms.id', ondelete="CASCADE"))
1919
room = relationship('Room')
20+
state = Column(Enum(StateType), default="active")
2021

2122
def __init__(self, **kwargs):
2223
validate_empty_fields(**kwargs)

api/devices/schema.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,30 @@ def mutate(self, info, device_id, **kwargs):
8484
return UpdateDevice(device=exact_device)
8585

8686

87+
class DeleteDevice(graphene.Mutation):
88+
"""
89+
Returns device payload on deleting a device
90+
"""
91+
class Arguments:
92+
device_id = graphene.Int(required=True)
93+
state = graphene.String()
94+
95+
device = graphene.Field(Devices)
96+
97+
@Auth.user_roles('Admin')
98+
def mutate(self, info, device_id, **kwargs):
99+
query_device = Devices.get_query(info)
100+
result = query_device.filter(DevicesModel.state == "active")
101+
exact_device = result.filter(
102+
DevicesModel.id == device_id
103+
).first()
104+
if not exact_device:
105+
raise GraphQLError("Device not found")
106+
update_entity_fields(exact_device, state="archived", **kwargs)
107+
exact_device.save()
108+
return DeleteDevice(device=exact_device)
109+
110+
87111
class Query(graphene.ObjectType):
88112
"""
89113
Query to get list of all devices
@@ -167,3 +191,8 @@ class Mutation(graphene.ObjectType):
167191
\n- location: The location of the device\
168192
\n- room_id: Unique identifier of a room where the device is found\
169193
[required]")
194+
delete_device = DeleteDevice.Field(
195+
description="Deletes a given device given the arguments to delete\
196+
\n- device_id: Unique identifier of the tag\
197+
[required]"
198+
)

fixtures/devices/devices_fixtures.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@
203203
}
204204
}
205205

206-
207206
query_with_non_existant_id = '''
208207
mutation{
209208
updateDevice(
@@ -222,6 +221,28 @@
222221
}
223222
'''
224223

224+
delete_device_mutation = '''
225+
mutation{
226+
deleteDevice(
227+
deviceId:1
228+
){
229+
device{
230+
id
231+
}
232+
}
233+
}
234+
'''
235+
236+
delete_device_response = {
237+
"data": {
238+
"deleteDevice": {
239+
"device": {
240+
"id": "1"
241+
}
242+
}
243+
}
244+
}
245+
225246
create_device_query_invalid_room = '''
226247
mutation{
227248
createDevice(
@@ -240,7 +261,6 @@
240261

241262
non_existant_id_response = "DeviceId not found"
242263
devices_query = '/mrm?query='+create_devices_query
243-
devices_query_response = b'{"data":{"createDevice":{"device":{"name":"Apple tablet","location":"Kenya","deviceType":"External Display"}}}}' # noqaE501
244264

245265
search_device_by_name = '''
246266
query{
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from tests.base import BaseTestCase, CommonTestCases
2+
from fixtures.devices.devices_fixtures import (
3+
delete_device_mutation,
4+
delete_device_response
5+
)
6+
7+
8+
class TestDeleteDevices(BaseTestCase):
9+
def test_delete_device(self):
10+
CommonTestCases.admin_token_assert_equal(
11+
self,
12+
delete_device_mutation,
13+
delete_device_response
14+
)

0 commit comments

Comments
 (0)