def put(self, *args, **kwargs): """ put请求,通过工厂id和需要更新的数据 :param args: name: 需要更新的工厂名称 :param kwargs: /factory_id :return: 成功返回200和更新的工厂id,未找到工厂返回status_24 参数错误返回status_22 """ # api日志记录 SpiderApi.request(kwargs['c_user_id'], self.request.remote_ip, self.request.method, self.request.uri) factory_id = kwargs['parameter'] request_data = kwargs['request_data'] name = request_data.get('name') if factory_id and name: factory_obj = FactoryDB() result = factory_obj.update(factory_id, name) # 同步缓存写入 factory_obj.close() if result: SpiderApi.response(200) self.write_json(result) else: SpiderApi.response(errors.status_24) raise HTTPError(**errors.status_24) else: SpiderApi.response(errors.status_22) raise HTTPError(**errors.status_22)
def get(self, *args, **kwargs): """ get请求,通过工厂id获取工厂的信息json :param args: :param kwargs: /factory_id :return: 成功返回200和设备信息的json,未找到设备信息返回status_24 参数错误返回status_22 """ # api日志记录 SpiderApi.request(kwargs['c_user_id'], self.request.remote_ip, self.request.method, self.request.uri) factory_id = kwargs['parameter'] if factory_id: factory_obj = FactoryDB() result = factory_obj.find(factory_id) factory_obj.close() if result: SpiderApi.response(200) self.write_json(result) else: SpiderApi.response(errors.status_24) raise HTTPError(**errors.status_24) else: SpiderApi.response(errors.status_22) raise HTTPError(**errors.status_22)
def post(self, *args, **kwargs): """ post请求,获取所有工厂的列表 :param args: page: 第几页 per_page: 每页显示几条数据 :param kwargs: :return: 成功返回工厂列表和201,未找到工厂返回status_24 参数错误返回status_22 """ # api日志记录 SpiderApi.request(kwargs['c_user_id'], self.request.remote_ip, self.request.method, self.request.uri) request_data = kwargs['request_data'] page = request_data.get('page') per_page = request_data.get('per_page') if page and per_page: limit = int(per_page) offset = (int(page) - 1) * limit factory_obj = FactoryDB() result = factory_obj.findall(offset, limit) factory_obj.close() if result: SpiderApi.response(201) self.write_json(result, 201) else: SpiderApi.response(errors.status_24) raise HTTPError(**errors.status_24) else: SpiderApi.response(errors.status_22) raise HTTPError(**errors.status_22)
def delete(self, *args, **kwargs): """ delete请求,通过工厂ID删除工厂,删除前需要做判断该工厂的车间数量是否为空 :param args: :param kwargs: /factory_id :return: 成功返回204,未找到数据返回status_24 参数错误返回status_22,还存在子类返回status_30 """ # api日志记录 SpiderApi.request(kwargs['c_user_id'], self.request.remote_ip, self.request.method, self.request.uri) factory_id = kwargs['parameter'] workshop_obj = WorkShopDB(factory_id=factory_id) workshop_count = workshop_obj.get_counts_by_field( 'factory_id', factory_id) workshop_obj.close() if not workshop_count: if id: factory_obj = FactoryDB() result = factory_obj.remove(factory_id) # 同步缓存写入 factory_obj.close() if result: SpiderApi.response(204) self.write_json(None, 204) else: SpiderApi.response(errors.status_24) raise HTTPError(**errors.status_24) else: SpiderApi.response(errors.status_22) raise HTTPError(**errors.status_22) else: SpiderApi.response(errors.status_30) raise HTTPError(**errors.status_30)
def post(self, *args, **kwargs): """ post请求,通过用户名和密码获取token,rf_token以及用户的角色 token的有效期为两小时,rf_token的有效期为30小时 :param args: username: 用户名 password: 密码 :param kwargs: :return: 成功返回token,rf_token,role的json以及201,密码验证错误返回status_23 没有该用户返回status_26,缺少参数返回status_22 """ # 请求数据检查 content_type = self.request.headers.get('Content-Type') if content_type != 'application/json': raise HTTPError(**errors.status_34) try: request_data = json.loads(self.request.body) except: raise HTTPError(**errors.status_35) username = request_data.get("username") password = request_data.get("password") # api日志记录 SpiderApi.request(username, self.request.remote_ip, self.request.method, self.request.uri) if username and password: user = UserDB().find(username=username) if user: encrypt_password = Encryption.generate_password( password, parser.auth_salt) if encrypt_password == user["password"]: token = genTokenSeq(user["_id"], user["user_role"], 7200) rf_token = genRfTokenSeq(user["_id"], user["user_role"], 108000) SpiderApi.response(201) # 获取工厂名称 factory_obj = FactoryDB() factory_name = factory_obj.find( id=user["factory_id"])['name'] self.write_json( { 'token': token, 'rf_token': rf_token, 'role': user["user_role"], 'factory_id': user["factory_id"], "factory_name": factory_name, "username": user['username'], "user_id": user["_id"] }, 201) else: SpiderApi.response(errors.status_23) raise HTTPError(**errors.status_23) else: SpiderApi.response(errors.status_26) raise HTTPError(**errors.status_26) else: SpiderApi.response(errors.status_22) raise HTTPError(**errors.status_22)
def get(self, *args, **kwargs): init_factory_id = parser.factoryid result = FactoryDB().find(id=init_factory_id) if result: self.write_json('True') else: self.write_json('False')
def post(self, *args, **kwargs): """ post请求,通过工厂名字插入工厂数据, 单机版只能允许新建一个工厂 :param args: name: 工厂名称 :param kwargs: :return: 成功返回工厂id和201,未成功插入返回status_24 参数错误返回status_22,已经有一个工厂返回status_31 """ # api日志记录 SpiderApi.request(kwargs['c_user_id'], self.request.remote_ip, self.request.method, self.request.uri) factory_obj = FactoryDB() count = factory_obj.get_counts() request_data = kwargs['request_data'] name = request_data.get('name') if not count: if name: result = factory_obj.insert(name=name) # 同步缓存写入 factory_obj.close() if result: SpiderApi.response(201) self.write_json(result, 201) else: SpiderApi.response(errors.status_24) raise HTTPError(**errors.status_24) else: SpiderApi.response(errors.status_22) raise HTTPError(**errors.status_22) else: SpiderApi.response(errors.status_31) raise HTTPError(**errors.status_31)
def findall(self, number=None, page=None): # 返回该工厂下的所有的车间 try: result = [] workshops = self.collection.find({}, { 'create_time': 0 }).skip(number).limit(page) for workshop in workshops: factory_name = FactoryDB().find(workshop['factory_id'])['name'] workshop['factory_name'] = factory_name result.append(workshop) return result except: return None
def wrapper(self, *args, **kwargs): content_type = self.request.headers.get('Content-Type') if content_type == 'application/json': init_factory_id = parser.factoryid result = FactoryDB().find(id=init_factory_id) try: request_data = json.loads(self.request.body) except: raise HTTPError(**errors.status_35) if not result: return method(self, request_data=request_data, c_factory_id=init_factory_id, *args, **kwargs) else: raise HTTPError(**errors.status_1) else: raise HTTPError(**errors.status_34)
def findall(self, number=None, page=None): # 返回该工厂下的所有生产线 try: result = [] lines = self.collection.find({}, { "create_time": 0 }).skip(number).limit(page) for line in lines: factory_name = FactoryDB().find(line['factory_id'])['name'] workshop_name = WorkShopDB( line['factory_id']).find(id=line['workshop_id'])['name'] line['factory_name'] = factory_name line['workshop_name'] = workshop_name result.append(line) return result except: return None
def findall(self, number=None, page=None): # 返回该工厂下的所有的车间 try: result = [] equipments = self.collection.find({}, { 'create_time': 0 }).skip(number).limit(page) for equipment in equipments: factory_name = FactoryDB().find( equipment['factory_id'])['name'] workshop_name = \ WorkShopDB(equipment['factory_id']).find(id=equipment['workshop_id'])['name'] line_name = ProductLineDB(equipment['factory_id']).find( id=equipment['line_id'])['name'] equipment['factory_name'] = factory_name equipment['workshop_name'] = workshop_name equipment['line_name'] = line_name result.append(equipment) return result except: return None