def get(self): if not request.args.get('_page') or not request.args.get('_limit'): return StandardResponse(400, 1, u'Invalid Request Parameters') try: project_page = copy.deepcopy(self.project_page__) project_page["page_index"] = int(request.args.get('_page')) project_page["page_size"] = int(request.args.get('_limit')) pro_name = request.args.get('pro_name') projects = None if not pro_name or len(pro_name) == u"": project_page["total"] = ProjectsModel.query.count() projects = ProjectsModel.query.paginate( project_page["page_index"], per_page=project_page["page_size"], error_out=False) else: project_page["total"] = ProjectsModel.query.filter( ProjectsModel.pro_name.like('%' + pro_name + '%')).count() projects = ProjectsModel.query.filter( ProjectsModel.pro_name.like( '%' + pro_name + '%')).paginate( project_page["page_index"], per_page=project_page["page_size"], error_out=False) for itr in projects.items: project = copy.deepcopy(self.project__) project["name"] = itr.pro_name project["address"] = itr.pro_address project["uuid"] = itr.pro_uuid project['lng'] = float(itr.pro_lng) if itr.pro_lng else 0.0 project['lat'] = float(itr.pro_lat) if itr.pro_lat else 0.0 project_page["list"].append(project) except Exception, e: return StandardResponse(500, 1, e.message)
def __ProcessResult(self, result, param): if not result: return StandardResponse(200, 0, u'Found Nothing') projects_map = {} max_project = 0.0 for itr in result: try: if itr.project.pro_name in projects_map: projects_map[itr.project.pro_name]['amount'] += itr.amount projects_map[itr.project.pro_name]['area'] += itr.area projects_map[itr.project.pro_name]['number'] += itr.number else: statistic = { 'address': itr.project.pro_address, 'lat': float(itr.project.pro_lat) if itr.project.pro_lat else 0.0, 'lng': float(itr.project.pro_lng) if itr.project.pro_lng else 0.0, 'amount': itr.amount, 'area': itr.area, 'number': itr.number } projects_map[itr.project.pro_name] = statistic except Exception, e: return StandardResponse(500, 1, e.message)
def get(self): # search interface try: user_instance = UsersModel.find_by_id(current_identity.id) user_dump, errors = UsersSchema().dump(user_instance) if errors: return StandardResponse(404, 1, u'Resource Not Found') except Exception, e: return StandardResponse(500, 1, u'SQLAlchemy Error')
def post(self): # register interface req_data = request.get_json() user_name = req_data.get('username', None) user_pass = req_data.get('password', None) if not user_name or not user_pass: return StandardResponse(403, 1, 'Forbidden') user = UsersModel(user_name, user_pass) try: user.add(user) except Exception, e: return StandardResponse(500, 1, e.message)
def delete(self, uuid): req_data = request.get_json() if not req_data: return StandardResponse(400, 1, u"无效的请求参数") if not req_data.has_key("project"): return StandardResponse(400, 1, u"无效的请求参数") project = ProjectsModel.query.filter( ProjectsModel.pro_uuid == req_data["project"]).first() if not project: return StandardResponse(500, 1, u"无法更新目标数据") project.delete() return StandardResponse(200, 0, u'Delete Success')
def __ProcessArea(self, request): param = self.__GenerateParam(request, 'area') if not param: return StandardResponse(400, 1, u'Invalid Request Parameters') filters = self.__GenerateFilters(DatasetAreaModel, param) if len(filters) == 0: return StandardResponse(500, 1, u'SQLAlchemy Error') result = None try: result = DatasetAreaModel().query.join(ProjectsModel).filter( *filters).all() except Exception, e: return StandardResponse(500, 1, e.message)
def __ProcessNone(self, request): param = self.__GenerateParam(request, 'none') if not param: return StandardResponse(400, 1, u'解析请求参数出错') filters = self.__GenerateFilters(DatasetNoneModel, param) if not len(filters): return StandardResponse(500, 1, u'SQLAlchemy Error') result = None try: result = DatasetNoneModel().query.join(ProjectsModel).filter( *filters).all() except Exception, e: return StandardResponse(500, 1, e.message)
def get(self): """ Query specific instance """ if current_identity.roles not in ['super']: return pretty_response(403) options_instance = None try: if request.args.get('opt_key'): options_instance = OptionsModel.query.filter( OptionsModel.opt_key == request.args.get( 'opt_key')).first() else: return StandardResponse(400, 1, u'Invalid Request Parameters') except Exception, e: return StandardResponse(500, 1, u'SQLAlchemy Error')
def delete(self): req_data = request.get_json() if not req_data: return StandardResponse(400, 1, u"无效的请求参数") if not req_data.has_key("project"): return StandardResponse(400, 1, u"无效的请求参数") try: project = ProjectsModel.query.filter( ProjectsModel.pro_uuid == req_data["project"]).fetchone() if not project: return StandardResponse(500, 1, u"无法更新目标数据") project.delete() except Exception, e: return StandardResponse(500, 1, e.message)
def put(self): req_data = request.get_json() if not req_data: return StandardResponse(400, 1, u"无效的请求参数") if not req_data.has_key("project") or \ not req_data.has_key("name") or \ not req_data.has_key("address"): return StandardResponse(400, 1, u"无效的请求参数") try: project = ProjectsModel.query.filter( ProjectsModel.pro_uuid == req_data["project"]).fetchone() if not project: return StandardResponse(500, 1, u"无法更新目标数据") project.pro_name = req_data["name"] project.pro_address = req_data["address"] project.update() except Exception, e: return StandardResponse(500, 1, e.message)
def put(self, uuid): req_data = request.get_json() if not req_data: return StandardResponse(400, 1, u"无效的请求参数") if not req_data.has_key("uuid") or \ not req_data.has_key("name") or \ not req_data.has_key("address") or \ not req_data.has_key("lat") or \ not req_data.has_key("lng"): return StandardResponse(400, 1, u"无效的请求参数") project = ProjectsModel.query.filter( ProjectsModel.pro_uuid == uuid).first() if not project: return StandardResponse(500, 1, u"无法更新目标数据") project.pro_name = req_data["name"] project.pro_address = req_data["address"] project.pro_lat = req_data["lat"] project.pro_lng = req_data["lng"] project.update() return StandardResponse(200, 0, u"Update Success")
def post(self): req_data = request.get_json() result = self.__RequestInstance(req_data) if not result: return StandardResponse(400, 1, u'Invalid Request Parameters') if result['arrange'] == u'amount': return self.__ProcessAmount(result) elif result['arrange'] == u'price': return self.__ProcessPrice(result) elif result['arrange'] == u'area': return self.__ProcessArea(result) elif result['arrange'] == u'room': return self.__ProcessRoom(result) else: return self.__ProcessNone(result)
class Project(Resource): def __init__(self): self.param__ = { 'arrange': '', 'province': '', 'city': '', 'district': '', 'datetype': '', 'statdate': '', 'starttm': '', 'endtm': '', 'stattype': [], 'property': '', 'intervals': '', } self.stattype_map__ = { "sale": u"成交情况", "supply": u"上市情况", "stock": u"可售情况" } self.project__ = { 'name': '', 'lat': '', 'lng': '', 'address': '', 'count': 0.0 } self.result__ = {'max': 0.0, 'data': []} #请求参数序列化 def __RequestInstance(self, req): result = {} if not req: return None result['stattype'] = req['stattype'] if req.has_key( 'stattype') else None result['datetype'] = req['datetype'] if req.has_key( 'datetype') else None result['statdate'] = req['statdate'] if req.has_key( 'statdate') else None if req.has_key('scope'): result['province'] = None result['city'] = req['scope'][0] result['district'] = None if req['scope'][1] == '' else req[ 'scope'][1] result['property'] = req['property'] if req.has_key( 'property') else None result['arrange'] = req['arrange'] if req.has_key('arrange') else None result['intervals'] = req['intervals'] if req.has_key( 'intervals') else None return result #请求参数格式化 def __GenerateParam(self, request, data_type): param = copy.deepcopy(self.param__) param['province'] = request['province'] param['city'] = request['city'] param['district'] = request['district'] param['datetype'] = request['datetype'] if request['datetype'] in ['daterange']: param['starttm'] = request['statdate'][0] param['endtm'] = request['statdate'][1] else: if request['datetype'] == 'week': start_tm = time.strptime(request['statdate'], "%Y-%m-%d") param['starttm'] = request['statdate'] end_tm = datetime(start_tm.tm_year, start_tm.tm_mon, start_tm.tm_mday) + timedelta(days=6) param['endtm'] = end_tm.strftime("%Y-%m-%d") elif request['datetype'] == 'month': start_tm = time.strptime(request['statdate'], "%Y-%m-%d") first_day, last_day = GetMonFirstLast(start_tm.tm_year, start_tm.tm_mon) param['starttm'] = first_day.strftime("%Y-%m-%d") param['endtm'] = last_day.strftime("%Y-%m-%d") elif request['datetype'] == 'monthrange': start_tm = time.strptime(request['statdate'][0], "%Y-%m-%d") first_s, last_s = GetMonFirstLast(start_tm.tm_year, start_tm.tm_mon) param['starttm'] = first_s.strftime("%Y-%m-%d") end_tm = time.strptime(request['statdate'][1], "%Y-%m-%d") first_e, last_e = GetMonFirstLast(end_tm.tm_year, end_tm.tm_mon) param['endtm'] = last_e.strftime("%Y-%m-%d") else: date_tm = time.strptime(request['statdate'], "%Y-%m-%d") start_tm = datetime(date_tm.tm_year, 1, 1) end_tm = datetime(date_tm.tm_year, 12, 31) param['starttm'] = start_tm.strftime("%Y-%m-%d") param['endtm'] = end_tm.strftime("%Y-%m-%d") param['stattype'] = request['stattype'] param['property'] = request['property'] param['arrange'] = request['arrange'] if data_type in ['amount', 'price', 'area']: split_list = request['intervals'].split(':') if len(split_list[0]) == 0: split_list[0] = 0 elif len(split_list[1]) == 0: split_list[1] = 3000000 param['intervals'] = split_list elif data_type == 'room': param['intervals'] = request['intervals'] return param #sqlalchemy过滤器生成 def __GenerateFilters(self, table, param): filters = [] filter = [] if param['city']: filter.append(table.city == param['city']) if param['district']: filter.append(table.scope == param['district']) if param['datetype'] in ['daterange', 'week']: filter.append(table.datetype == 'day') elif param['datetype'] in ['monthrange', 'month', 'year']: filter.append(table.datetype == 'month') else: filter.append(table.datetype == 'month') if param['starttm'] and param['endtm']: filter.append(table.statdate >= param['starttm']) filter.append(table.statdate <= param['endtm']) if param['property']: filter.append(table.property == param['property']) if len(param['stattype']) != 0: stattype = self.stattype_map__[param['stattype'][0]] filter.append(table.stattype == stattype) if len(param['intervals']) != 0: if param['arrange'] in ['amount', 'price', 'area']: filter.append(table.intervals >= int(param['intervals'][0])) filter.append(table.intervals < int(param['intervals'][1])) else: filter.append(table.intervals == param['intervals']) filters.append(and_(*filter)) return filters #处理查询结果 def __ProcessResult(self, result, param): if not result: return StandardResponse(200, 0, u'Found Nothing') projects_map = {} max_project = 0.0 for itr in result: try: if itr.project.pro_name in projects_map: projects_map[itr.project.pro_name]['amount'] += itr.amount projects_map[itr.project.pro_name]['area'] += itr.area projects_map[itr.project.pro_name]['number'] += itr.number else: statistic = { 'address': itr.project.pro_address, 'lat': float(itr.project.pro_lat) if itr.project.pro_lat else 0.0, 'lng': float(itr.project.pro_lng) if itr.project.pro_lng else 0.0, 'amount': itr.amount, 'area': itr.area, 'number': itr.number } projects_map[itr.project.pro_name] = statistic except Exception, e: return StandardResponse(500, 1, e.message) del result projects = [] try: for name, stat in projects_map.items(): project = copy.deepcopy(self.project__) project['name'] = name project['address'] = stat['address'] project['lat'] = stat['lat'] project['lng'] = stat['lng'] count = 0.0 if param['stattype'][1] == u'amount': count = stat['amount'] elif param['stattype'][1] == u'price': count = int(stat['amount'] * 10000 / stat['area']) if stat['area'] != 0 else 0 elif param['stattype'][1] == u'area': count = stat['area'] elif param['stattype'][1] == u'number': count = stat['number'] if max_project < count: max_project = count project['count'] = count projects.append(project) except Exception, e: return StandardResponse(500, 1, e.message)
count = stat['amount'] elif param['stattype'][1] == u'price': count = int(stat['amount'] * 10000 / stat['area']) if stat['area'] != 0 else 0 elif param['stattype'][1] == u'area': count = stat['area'] elif param['stattype'][1] == u'number': count = stat['number'] if max_project < count: max_project = count project['count'] = count projects.append(project) except Exception, e: return StandardResponse(500, 1, e.message) if len(projects) == 0: return StandardResponse(200, 0, u'Found Nothing') result = copy.deepcopy(self.result__) result['data'] = projects result['max'] = max_project return StandardResponse(200, 0, data=result) def __ProcessAmount(self, request): param = self.__GenerateParam(request, 'amount') if not param: return StandardResponse(400, 1, u'Invalid Request Parameters') filters = self.__GenerateFilters(DatasetAmountModel, param) if len(filters) == 0: return StandardResponse(500, 1, u'SQLAlchemy Error') result = None try: result = DatasetAmountModel().query.join(ProjectsModel).filter(
def put(self, uuid): # update interface return StandardResponse(405, 1, u'Method Not Allowed')
def user_delete(self, uuid): # delete interface return StandardResponse(405, 1, u'Method Not Allowed')
return StandardResponse(400, 1, u"无效的请求参数") if not req_data.has_key("project") or \ not req_data.has_key("name") or \ not req_data.has_key("address"): return StandardResponse(400, 1, u"无效的请求参数") try: project = ProjectsModel.query.filter( ProjectsModel.pro_uuid == req_data["project"]).fetchone() if not project: return StandardResponse(500, 1, u"无法更新目标数据") project.pro_name = req_data["name"] project.pro_address = req_data["address"] project.update() except Exception, e: return StandardResponse(500, 1, e.message) return StandardResponse(200, 0, u"Delete Success") @jwt_required() def delete(self): req_data = request.get_json() if not req_data: return StandardResponse(400, 1, u"无效的请求参数") if not req_data.has_key("project"): return StandardResponse(400, 1, u"无效的请求参数") try: project = ProjectsModel.query.filter( ProjectsModel.pro_uuid == req_data["project"]).fetchone() if not project: return StandardResponse(500, 1, u"无法更新目标数据") project.delete() except Exception, e: