def post(self): """Create a new corp single. """ try: data = DotDict(json_decode(self.request.body)) logging.info("[UWEB] Add single request: %s, cid: %s", data, self.current_user.cid) except Exception as e: status = ErrorCode.ILLEGAL_DATA_FORMAT logging.exception("[UWEB] Invalid data format. Exception: %s", e.args) self.write_ret(status) return try: singles = QueryHelper.get_singles_by_cid(self.current_user.cid, self.db) if len(singles) > LIMIT.REGION - 1: self.write_ret(ErrorCode.REGION_ADDITION_EXCESS) return status = ErrorCode.SUCCESS single_id = -1 # default value single_name = data.single_name single_shape = int(data.single_shape) if single_shape == UWEB.REGION_SHAPE.CIRCLE: circle = DotDict(data.circle) longitude = circle.longitude latitude = circle.latitude radius = circle.radius single_info = dict( single_name=single_name, longitude=longitude, latitude=latitude, radius=radius, shape=single_shape, cid=self.current_user.cid, ) single_id = add_single(single_info, self.db, self.redis) elif single_shape == UWEB.REGION_SHAPE.POLYGON: polygon = data.polygon points_lst = [] points = "" for p in polygon: tmp = ",".join([str(p["latitude"]), str(p["longitude"])]) points += tmp points_lst.append(tmp) points = ":".join(points_lst) single_info = dict( single_name=single_name, points=points, shape=single_shape, cid=self.current_user.cid ) single_id = add_single(single_info, self.db, self.redis) else: logging.error( "[UWEB] Add single failed, unknown single_shape: %s, uid: %s", single_shape, self.current_user.uid ) self.write_ret(status, dict_=DotDict(single_id=single_id)) except Exception as e: logging.exception("[UWEB] cid: %s create single failed. Exception: %s", self.current_user.cid, e.args) status = ErrorCode.SERVER_BUSY self.write_ret(status)
def get(self): """ Get singles by cid""" status = ErrorCode.SUCCESS try: cid = self.current_user.cid except Exception as e: status = ErrorCode.ILLEGAL_DATA_FORMAT logging.exception( "[UWEB] uid: %s get region data format illegal. Exception: %s", self.current_user.uid, e.args ) self.write_ret(status) return try: res = [] singles = QueryHelper.get_singles_by_cid(self.current_user.cid, self.db) for single in singles: if single.single_shape == UWEB.REGION_SHAPE.CIRCLE: r = DotDict( single_id=single.single_id, single_name=single.single_name, single_shape=single.single_shape, circle=DotDict(latitude=single.latitude, longitude=single.longitude, radius=single.radius), ) elif single.single_shape == UWEB.REGION_SHAPE.POLYGON: polygon = [] points = single.points point_lst = points.split(":") for point in point_lst: latlon = point.split(",") dct = {"latitude": latlon[0], "longitude": latlon[1]} polygon.append(dct) r = DotDict( single_id=single.single_id, single_name=single.single_name, single_shape=single.single_shape, polygon=polygon, ) res.append(r) self.write_ret(status, dict_=DotDict(res=res)) except Exception as e: logging.exception("[UWEB] cid: %s get singles failed. Exception: %s", cid, e.args) status = ErrorCode.SERVER_BUSY self.write_ret(status)