def get(self, _id=None): if _id: device = DeviceModel.find_by_id(_id) if device: return BaseResponse.ok_response('Successful.', device.json()) return BaseResponse.bad_request_response('Device does not exists.', {}) else: devices = list(map(lambda x: x.json(), DeviceModel.find_all())) return BaseResponse.ok_response('Successful.', devices)
def post(cls): json_data = request.get_json() try: device_data = DeviceSchema().load(json_data) print(device_data) except ValidationError as err: return err.messages, 401 try: device_model = DeviceModel(**device_data) device_model.insert_device() except: return {"Message": "DEVICE_INSERTION_ERROR"}, 401 return {"Message": "DEVICE_REGISTRATION_SUCCESSFUL"}, 201
def put(cls, reqId): json_data = request.get_json() request_data = RequestModel.find_by_id(reqId) if request_data.reqStatus == "pending": device_data = DeviceModel.find_by_id(request_data.deviceId) user_data = UserModel.find_by_id(request_data.userId) req_audit_obj = {"reqId": reqId, "handleBy": json_data["admin_id"]} req_audit_model = RequestAuditModel(**req_audit_obj) admin_data = UserModel.find_by_id(json_data['admin_id']) if request_data and admin_data.role == "admin": request_data.reqStatus = "declined" device_data.status = "available" device_data.assignTo = "0" device_data.releaseDate = None request_data.insert_request() device_data.insert_device() req_audit_model.insert_request_audit() # declined request return {"Message": "Request Declined Successfully"}, 201 return {"Message": "Request Not Found"}, 401 return {"Message": "Request Already Resolve"}, 403
def post(cls): json_data = request.get_json() user = UserModel.find_by_id(json_data["userId"]) device = DeviceModel.find_by_id(json_data["deviceId"]) print(json_data["releaseDate"]) if device.status == "available" or device.status == "created": if user and device: if device.isActivated: if device.status == "created" or device.status == "available": try: req_obj = { "deviceId": json_data["deviceId"], "userId": json_data["userId"] } request_data = request_schema.load(req_obj) except ValidationError as err: return err.messages, 401 try: request_model = RequestModel(**request_data) device.status = "blocked" device.releaseDate = json_data["releaseDate"] request_model.insert_request() device.insert_device() except: return {"Message": "REQUEST INSERT ERROR"}, 401 return {"Message": "REQUEST SUCCESSFULLY ADDED"}, 201 return {"Message": "DEVICE IS NOT AVAILABLE"}, 401 return {"Message": "DEVICE IS NOT ACTIVATED"}, 401 return {"Message": "SOMETHING GETTING WRONG"}, 401 return {"Messgae": "Device is not available to request"}, 401
def post(cls, userId): json_data = request.get_json() user_data = UserModel.find_by_id(userId) admin_data = UserModel.find_by_id(json_data['admin_id']) user_devices = DeviceModel.find_my_devices(userId) type(user_devices) print(len(user_devices)) if user_data: if len(user_devices) == 0: if admin_data: if admin_data.role == "admin": if user_data.isActivated: user_data.isActivated = False user_data.insert_user() return {"Message": "User is DeActivated"}, 201 return {"Message": "User is Already DeActivated"}, 401 return { "Message": "You dont have an access to deactivate other user" }, 401 return {"Message": "Admin Not Found"}, 401 return { "Message": "User has some devices so you cannot deactivate him" }, 401 return {"Message": "User Not Found"}, 401
def put(cls, deviceId, userId): json_data = request.get_json() device_data = DeviceModel.find_by_id(deviceId) if device_data.status == "blocked" or device_data.status == "allocated": return {"Message": "Device is not available"}, 401 user_data = UserModel.find_by_id(userId) req_obj = {"deviceId": deviceId, "userId": userId} request_model = RequestModel(**req_obj) device_obj = { "deviceId": deviceId, "userId": userId, "allocateBy": json_data["admin_id"] } device_audit_model = DeviceAuditModel(**device_obj) admin_data = UserModel.find_by_id(json_data['admin_id']) if device_data and user_data and admin_data.role == "admin": # if device_data.status == "created" or device_data.status == "available" : if user_data.isActivated: if device_data.isActivated: # device_data.isAvailable = False # if user_data.role == "admin": request_model.reqStatus = "approved" device_data.status = "allocated" device_data.assignTo = user_data.email device_data.releaseDate = json_data["releaseDate"] try: device_data.insert_device() request_model.insert_request() # return {"Message": "DEVICE ASSIGNED"}, 201 except: return {"Message": "INTERNAL SERVER ERROR"}, 401 req_model = RequestModel.get_my_last_request( deviceId, userId) req_audit_obj = { "reqId": req_model.reqId, "handleBy": json_data["admin_id"] } req_audit_model = RequestAuditModel(**req_audit_obj) # try: req_audit_model.insert_request_audit() device_audit_model.insert_device_audit() return {"Message": "DEVICE ASSIGNED"}, 201 # except: # return {"Message": "INTERNAL SERVER ERROR"}, 403 # return {"Message": "DEVICE ALREADY ASSIGNED TO USER"}, 403 return {"Message": "DEVICE IS NOT ACTIVATED TO ASSIGN"}, 400 return {"Message": "USER IS NOT ACTIVATED TO ASSIGN"}, 400 return {"MESSAGE": "INVALID REQUEST"}, 400
def put(cls, deviceId): json_data = request.get_json() device_data = DeviceModel.find_by_id(deviceId) if device_data: device_data.osVersion = json_data['osVersion'] device_data.rom = json_data['rom'] device_data.insert_device() return {"Message": "Device Updated Successfully"}, 201 return {"Message": "Internal server error"}, 401
def put(self, _id=None): try: data = User.parser.parse_args() user = UserModel.find_by_id(_id) if user: hash_password = UserModel.hash_password(data['password']) user.roleId = data['roleId'] if (data['roleId'] is not None) else user.roleId user.email = data['email'] if (data['email'] is not None) else user.email user.password = hash_password if ( data['password'] is not None) else user.password user.fullName = data['fullName'] if ( data['fullName'] is not None) else user.fullName user.lastName = data['lastName'] if ( data['lastName'] is not None) else user.lastName user.phoneNumber = data['phoneNumber'] if ( data['phoneNumber'] is not None) else user.phoneNumber user.profilePicture = data['profilePicture'] if (data['profilePicture'] is not None) \ else user.profilePicture user.lastIPConnection = data['lastIPConnection'] if (data['lastIPConnection'] is not None) \ else user.lastIPConnection user.updatedOn = datetime.now().strftime('%Y-%m-%d %H:%M:%S') user.save_to_db() if user.lastIPConnection and DeviceModel.find_by_ip( user.lastIPConnection) is None: device = DeviceModel(user_id=user.id, ip=user.lastIPConnection, created_at=datetime.now().strftime( '%Y-%m-%d %H:%M:%S')) device.save_to_db() return BaseResponse.ok_response('User updated successfully.', user.json(is_long=True)) else: return BaseResponse.not_acceptable_response( 'User does not exists.', {}) except Exception as e: return BaseResponse.server_error_response(str(e))
def put(cls, deviceId): device_data = DeviceModel.find_by_id(deviceId) if device_data: device_data.isActivated = True try: device_data.insert_device() return {"Message": "DEVICE ACTIVATED"}, 201 except: return {"Message": "INTERNAL SERVER ERROR"}, 401 return {"MESSAGE": "INVALID REQUEST"}, 400
def put(cls, deviceId): device_data = DeviceModel.find_by_id(deviceId) if device_data: if device_data.assignTo != "0": return {"Message": "DEVICE IS ASSIGNED TO SOMEONE"}, 401 device_data.isActivated = False try: device_data.insert_device() return {"Message": "DEVICE DE ACTIVATED"}, 201 except: return {"Message": "INTERNAL SERVER ERROR"}, 401 return {"MESSAGE": "INVALID REQUEST"}, 401
def post(): try: data = User.parser.parse_args() if UserModel.find_by_email(data['email']): return BaseResponse.bad_request_response( 'This email already exists.', {}) elif UserModel.find_by_phone(data['phoneNumber']): return BaseResponse.bad_request_response( 'This phone number already exists.', {}) hash_password = UserModel.hash_password(data['password']) user = UserModel(role_id=data['roleId'], email=data['email'], password=hash_password, full_name=data['fullName'], last_name=data['lastName'], phone_number=data['phoneNumber'], profile_picture=data['profilePicture'], last_ip_connection=data['lastIPConnection'], created_at=None, updated_on=None, status=None) user.save_to_db() if user.lastIPConnection and DeviceModel.find_by_ip( user.lastIPConnection) is None: device = DeviceModel( user_id=user.id, ip=user.lastIPConnection, created_at=datetime.now().strftime('%Y-%m-%d %H:%M:%S')) device.save_to_db() return BaseResponse.created_response('User created successfully.', user.json(is_long=True)) except Exception as e: return BaseResponse.server_error_response(str(e))
def post(): try: data = Login.parser.parse_args() if data['email']: user = UserModel.find_by_email(data['email']) else: user = UserModel.find_by_phone(data['phoneNumber']) pass if data['lastIPConnection'] and DeviceModel.find_by_ip(data['lastIPConnection']) is None \ and user is not None: user.lastIPConnection = data['lastIPConnection'] if (data['lastIPConnection'] is not None) \ else user.lastIPConnection user.save_to_db() device = DeviceModel( user_id=user.id, ip=data['lastIPConnection'], created_at=datetime.now().strftime('%Y-%m-%d %H:%M:%S')) device.save_to_db() if user and user.check_password(data['password']): access_token = create_access_token( identity=user.json(is_long=False), fresh=True) refresh_token = create_refresh_token(identity=user.json( is_long=False)) return BaseResponse.ok_response( 'Login successfully.', { 'accessToken': access_token, 'refreshToken': refresh_token, 'user': user.json(is_long=False) }) return BaseResponse.bad_request_response('Incorrect credentials.', {}) except Exception as e: return BaseResponse.server_error_response(str(e))
def put(cls, deviceId): json_data = request.get_json() device_data = DeviceModel.find_by_id(deviceId) user_data = UserModel.find_by_email(device_data.assignTo) device_audit = DeviceAuditModel.find_my_device(deviceId, user_data.id) if device_data and device_audit: # device_data.isAvailable = True device_data.status = "available" device_data.assignTo = "0" device_data.releaseDate = None device_audit.deallocateBy = json_data['admin_id'] device_audit.deallocateDate = str( date.today().strftime("%d/%m/%Y")) try: device_data.insert_device() device_audit.insert_device_audit() return {"Message": "DEVICE DE ALLOCATED"}, 201 except: return {"Message": "INTERNAL SERVER ERROR"}, 401 return {"MESSAGE": "INVALID REQUEST"}, 400
def put(cls, reqId): json_data = request.get_json() request_data = RequestModel.find_by_id(reqId) device_data = DeviceModel.find_by_id(request_data.deviceId) if request_data.reqStatus == "pending": if device_data.status == "blocked": user_data = UserModel.find_by_id(request_data.userId) req_audit_obj = { "reqId": reqId, "handleBy": json_data["admin_id"] } req_audit_model = RequestAuditModel(**req_audit_obj) device_obj = { "deviceId": request_data.deviceId, "userId": request_data.userId, "allocateBy": json_data["admin_id"] } device_audit_model = DeviceAuditModel(**device_obj) admin_data = UserModel.find_by_id(json_data['admin_id']) if request_data and admin_data.role == "admin": request_data.reqStatus = "approved" device_data.status = "allocated" device_data.assignTo = user_data.email request_data.insert_request() device_data.insert_device() req_audit_model.insert_request_audit() device_audit_model.insert_device_audit() return {"Message": "Request Approved"}, 201 return {"Message": "Request Not Found"}, 401 return {"Message": "Device is not requested"}, 401 return {"Message": "Request Already Resolve"}, 403
def get(cls, deviceId): device_data = DeviceModel.find_by_id(deviceId) return DeviceSchema().dump(device_data), 201
def get(cls, id: int): my_devices = DeviceModel.find_my_devices(id) return DeviceSchema(many=True).dump(my_devices), 201
def get(cls): assigned_devices = DeviceModel.find_assigned() return DeviceSchema(many=True).dump(assigned_devices), 201
def get(cls): device_data = DeviceModel.find_available() return DeviceSchema(many=True).dump(device_data), 201
def get(cls): activated_devices = DeviceModel.activated_devices() return DeviceSchema(many=True).dump(activated_devices), 201