def delete_action(): try: ids = get_delete_ids() actions = Action.query.filter(Action.id.in_(ids)).many() for action in actions: if action.rules.count() != 0: raise ReferencedError() db.session.delete(action) db.session.commit() except IntegrityError: raise ReferencedError() return '', 204
def delete_data_points(): delete_ids = get_delete_ids() data_points = DataPoint.query \ .filter(DataPoint.id.in_(delete_ids)) \ .many(allow_none=False, expect_result=len(delete_ids)) try: for data_point in data_points: if data_point.dataStreams.count() > 0: raise ReferencedError(field='dataStream') db.session.delete(data_point) db.session.commit() except IntegrityError: raise ReferencedError() return '', 204
def delete_group(): delete_ids = get_delete_ids() query_results = Group.query.filter(Group.id.in_(delete_ids)).many() try: for group in query_results: device_count = db.session.query(db.func.count(GroupDevice.c.deviceIntID)) \ .filter(GroupDevice.c.groupID == group.groupID).scalar() if device_count > 0: raise ReferencedError(field='device') db.session.delete(group) db.session.commit() except IntegrityError: raise ReferencedError() return '', 204
def delete_cert(): delete_ids = get_delete_ids() device_count = db.session \ .query(func.count(CertDevice.c.deviceIntID)) \ .filter(CertDevice.c.certIntID.in_(delete_ids)).scalar() if device_count > 0: raise ReferencedError(field='devices') query_results = Cert.query \ .filter(Cert.id.in_(delete_ids)) \ .many(allow_none=False, expect_result=len(delete_ids)) try: for cert in query_results: db.session.delete(cert) db.session.commit() except IntegrityError: raise ReferencedError() return '', 204
def delete_codec(): delete_ids = get_delete_ids() query_results = Codec.query.filter(Codec.id.in_(delete_ids)).many() try: for codec in query_results: db.session.delete(codec) db.session.commit() except IntegrityError: raise ReferencedError() return '', 204
def delete_current_alerts(): delete_ids = get_delete_ids() query_results = CurrentAlert.query.filter( CurrentAlert.id.in_(delete_ids)).many() try: for alert in query_results: db.session.delete(alert) db.session.commit() except IntegrityError: raise ReferencedError() return '', 204
def delete_product(): delete_ids = get_delete_ids() query_results = Product.query \ .filter(Product.id.in_(delete_ids)) \ .many(allow_none=False, expect_result=len(delete_ids)) # check device is included in the delete product device_count = db.session.query(func.count(Device.id)) \ .join(Product, Device.productID == Product.productID) \ .filter(Product.id.in_(delete_ids)) \ .scalar() if device_count: raise ReferencedError(field='device') try: for product in query_results: db.session.delete(product) db.session.commit() except IntegrityError: raise ReferencedError() return '', 204
def delete_data_streams(): delete_ids = get_delete_ids() data_streams = DataStream.query \ .filter(DataStream.id.in_(delete_ids)) \ .many(allow_none=False, expect_result=len(delete_ids)) try: for data_stream in data_streams: db.session.delete(data_stream) db.session.commit() except IntegrityError: raise ReferencedError() return '', 204
def delete_application(): app_ids = get_delete_ids() applications = Application.query \ .filter(Application.id.in_(app_ids)) \ .many(allow_none=False, expect_result=len(app_ids)) try: for app in applications: app.delete() db.session.commit() except IntegrityError: raise ReferencedError() return '', 204
def delete_cert_devices(cert_id): cert = Cert.query.filter(Cert.id == cert_id).first_or_404() devices_id = get_delete_ids() devices = Device.query.filter(Device.id.in_(devices_id)) \ .many(allow_none=False, expect_result=len(devices_id)) try: for device in devices: cert.devices.remove(device) db.session.commit() except IntegrityError: raise ReferencedError() return '', 204
def delete_device(): delete_ids = get_delete_ids() # check endDevice under the delete endDevice parent_device = db.session.query(db.func.count(EndDevice.id)) \ .filter(EndDevice.parentDevice.in_(delete_ids)) \ .scalar() if parent_device > 0: raise ReferencedError(field='parentDevice') # check endDevice under the delete gateway gateway_device = db.session.query(db.func.count(EndDevice.id)) \ .filter(EndDevice.gateway.in_(delete_ids)) \ .scalar() if gateway_device > 0: raise ReferencedError(field='endDevice') query_results = Device.query.filter(Device.id.in_(delete_ids)).many() try: for device in query_results: db.session.delete(device) db.session.commit() except IntegrityError: raise ReferencedError() return '', 204
def delete_rule(): try: ids = get_delete_ids() rules = Rule.query \ .filter(Rule.id.in_(ids)) \ .many() for rule in rules: db.session.delete(rule) url = f"{current_app.config.get('STREAM_RULE_URL')}/{rule.id}" stream_rule_http('delete', url=url) db.session.commit() except IntegrityError: raise ReferencedError() return '', 204
def delete_invitations(): try: ids = [int(i) for i in request.args.get('ids').split(',')] except ValueError: raise ParameterInvalid(field='ids') try: Invitation.query \ .filter(Invitation.id.in_(ids)) \ .filter(Invitation.tenantID == g.tenant_uid) \ .delete(synchronize_session='fetch') db.session.commit() except IntegrityError: raise ReferencedError() return '', 204
def delete_timer_publish(): delete_ids = get_delete_ids() query_results = TimerPublish.query \ .join(User, User.id == TimerPublish.userIntID) \ .filter(User.tenantID == g.tenant_uid, TimerPublish.id.in_(delete_ids)) \ .all() if len(delete_ids) != len(query_results): raise ParameterInvalid(field='ids') try: for query_result in query_results: db.session.delete(query_result) db.session.commit() except IntegrityError: raise ReferencedError() return '', 204
def delete_role(): try: ids = [int(i) for i in request.args.get('ids').split(',')] except ValueError: raise ParameterInvalid(field='ids') try: query = Role.query \ .filter(Role.id.in_(ids)) \ .filter(~Role.id.in_([1, 2, 3])) \ .filter(Role.id != g.role_id) \ .filter(Role.tenantID == g.tenant_uid) check_request(query).delete(synchronize_session='fetch') db.session.commit() except IntegrityError: raise ReferencedError() return '', 204
def delete_user(): user_ids = get_delete_ids() try: if g.role_id == 1: User.query \ .filter(User.id.in_(user_ids)) \ .filter(User.roleIntID != 1) \ .delete(synchronize_session='fetch') else: User.query \ .filter(User.id.in_(user_ids)) \ .filter(~User.roleIntID.in_([1, 2, 3])) \ .filter(User.id != g.user_id) \ .filter(User.tenantID == g.tenant_uid) \ .delete(synchronize_session='fetch') db.session.commit() except IntegrityError: raise ReferencedError() return '', 204
def delete_messages(): try: ids = [int(i) for i in request.args.get('ids').split(',')] except ValueError: raise ParameterInvalid(field='ids') try: messages = Message.query \ .filter(Message.id.in_(ids), or_(Message.tenantID == g.tenant_uid, Message.tenantID.is_(None))) \ .all() for message in messages: if message.tenantID is None: raise APIException() db.session.delete(message) db.session.commit() except IntegrityError: raise ReferencedError() return '', 204