def list(self, user_id={"atype": int, "adef": 0}): """获取单个用户的消息列表""" message_t = MessageModel.table() cond = message_t.user_id == user_id message_list = MessageModel.objectlist() message_list.find(cond) return self._response(PyobjectList(Error.success, message_list))
def get(self, user_id): return { 'messages': [ message.to_json() for message in MessageModel.list_messages_by_author(user_id) ] }
def create_message(self, name, tel, email, content): message = MessageModel(name=name, tel=tel, email=email, content=content) with self.session_scope() as session: session.add(message) session.commit() return True
def search(self, latitude={"atype": float, "adef": 0}, longitude={"atype": float, "adef": 0}, category_id={"atype": int, "adef": 0}, distance={"atype": int, "adef": 1000}, ): """ 查找消息 """ cond = {} if category_id: cond["category_id"] = category_id message_list = mongo.message.find({"loc": SON([("$near", [longitude, latitude]), ("$maxDistance", distance)])}) data = [] for message in message_list: message_id = message["id"] message_t = MessageModel.table() message_obj = MessageModel.object() cond = message_t.id == message_id if not message_obj.find(cond): continue message_info = {} message_info["id"] = message_obj.id message_info["user_id"] = message_obj.user_id message_info["latitude"] = message_obj.latitude message_info["longitude"] = message_obj.longitude message_info["content"] = message_obj.content message_info["with_sku_id"] = message_obj.with_sku_id message_info["with_sku_type"] = message_obj.with_sku_type message_info["category_id"] = message_obj.category_id message_info["tags"] = message_obj.tags message_info["create_on"] = to_utf8(message_obj.create_on) message_info["nice"] = message_obj.nice point_user = (longitude, latitude) point_message = (message_info["longitude"], message_info["latitude"]) message_info["distance"] = calculate_distance(point_user, point_message) data.append(message_info) return self._response(PyobjectList(Error.success, data))
def put(self, message_id): title = request.form.get('title') body = request.form.get('body') message = MessageModel.find_message_by_id(message_id) if message is None: message = MessageModel(title, body, author_id) else: if title: message.title = title if body: message.body = body message.save_to_db() return {'message': message.to_json()}
def add(self, user_id={"atype": int, "adef": 0}, latitude={"atype": float, "adef": 0.0}, longitude={"atype": float, "adef": 0.0}, content={"atype": unicode, "adef": ""}, resources={"atype": str, "adef":""}, routes={"atype":str, "adef":""}, category_id={"atype": int,"adef": 0}, tags={"atype":unicode,"adef": ""}, with_sku_id={"atype": int, "adef": 0}, with_sku_type={"atype": int, "adef": 0} ): """ 添加一条消息 """ content = content.encode("utf8") tags = tags.encode("utf8") if user_id == 0 or latitude == 0 or longitude == 0: return self._response(Pyobject(Error.param_error)) message_obj = MessageModel.object() message_obj.user_id = user_id message_obj.latitude = latitude message_obj.longitude = longitude message_obj.content = content message_obj.with_sku_id = with_sku_id message_obj.with_sku_type = with_sku_type message_obj.category_id = category_id message_obj.tags = tags message_id = message_obj.insert() resourceIds = resources.split(",") for i in xrange(0,len(resourceIds)): sql = "insert into message_resources(message_id,resource_id) values(%s,%s)" % (message_id,resourceIds[i]) mysql_conn.execsql(sql) routes = resources.split(",") for i in xrange(0,len(routes)): sql = "insert into message_routes(message_id,route_id) values(%s,%s)" % (message_id,routes[i]) mysql_conn.execsql(sql) # 插入mongodb mongo.message.insert({"id": int(message_id), "loc": [longitude, latitude], "category_id": category_id}) return self._response(Pyobject(Error.success, message_id))
def get(self, message_id): message = MessageModel.find_message_by_id(message_id) if message: return {'message': message.to_json()}, 200 return {'message': 'message not found'}, 404
def delete(self, message_id): message = MessageModel.find_message_by_id(message_id) if message: message.delete_from_db() return {'message': 'item deleted'}
def post(self): message = MessageModel(request.form.get('title'), request.form.get('body'), request.form.get('author_id')) message.save_to_db() return {'message': message.to_json()}, 201