def createMenu(self, request): data = request_body_to_dict(request) article_menu = ArticleMenu() article_menu = dict_to_object(data, article_menu) article_menu.uuid = get_uuid() article_menu.save() return self.xml_response_for_json(self.success_response(msg='添加成功'))
def api_list_method(self, request, models_name): """ 获取模型列表数据(分页) :return: """ params = request_body_to_dict(request) model = return_models(models_name) print(params) # 分页准备 page = params.get('page') limit = params.get('limit') start = (int(page) - 1) * int(limit) end = int(page) * int(limit) # end分页准备 model_query = model.objects.filter() # 排序处理 if params and 'orderby' in params: model_query = model_query.order_by( * [v.strip() for v in params['orderby'].split(',') if v.strip()]) # end排序处理 if hasattr(model, 'filter_handler'): model_query = model.filter_handler(params, model_query) total = model_query.count() fields = model().get_fields(not_add_fields=False) fields = fields + ['id', 'created', 'updated', 'date'] hander = lambda item: object_to_dict(fields, item) model_query_instance = model_query[start:end] res = [hander(item) for item in model_query_instance] data = {'item': res, 'total': total} return self.xml_response_for_json( self.success_response(data=data, msg='获取成功'))
def getMenuForLive(self, request): data = request_body_to_dict(request) article_menu = ArticleMenu.objects.filter(parent=data.get('uuid')) fields = ArticleMenu().return_fields hander = lambda item: object_to_dict(fields, item) res = [hander(item) for item in article_menu] return self.xml_response_for_json( self.success_response(data=res, msg='获取成功'))
def getMenuTable(self, request): data = request_body_to_dict(request) print(data) article_menu = ArticleMenu.objects.filter() fields = ArticleMenu().return_fields fields = fields + ['id', 'created', 'updated'] hander = lambda item: object_to_dict(fields, item) res = [hander(item) for item in article_menu] return self.xml_response_for_json( self.success_response(data=res, msg='获取成功'))
def api_delete_method(self, request, models_name): """ 删除模型的数据 :param request: 参数结构举例 {'data': {'guid': 123}} :param models_name: :return: """ data = request_body_to_dict(request) model = return_models(models_name) model_instance = model.objects.filter(**data.get('data')).first() model_instance.delete() return self.xml_response_for_json(self.success_response(msg='删除成功'))
def api_create_method(self, request, models_name): """ 添加数据到模型 :param request: :param models_name: :return: """ data = request_body_to_dict(request) model = return_models(models_name) model_instance = model() model_instance = dict_to_object(data, model_instance) model_instance.guid = get_uuid() model_instance.save() return self.xml_response_for_json(self.success_response(msg='添加成功'))
def api_edit_method(self, request, models_name): """ 编辑模型数据 :param request: :param models_name: :return: """ data = request_body_to_dict(request) update_object = data.get('object') # 更新对象的标识,用来查询需要更新的记录 update = data.get('update') # 更新数据 model = return_models(models_name) model_instance = model.objects.filter(**update_object).first() model_instance = dict_to_object(update, model_instance) model_instance.save() return self.xml_response_for_json(self.success_response(msg='修改成功'))
def getArticleClassifyList(self, request, *args): """ 获取文章分类(通过关键字来搜索) :param request: :param models_name: :return: """ data = request_body_to_dict(request) name = data.get('name', '') fields = ArticleClassify().get_fields() hander = lambda item: object_to_dict(fields, item) query = ArticleClassify.objects.filter(name__contains=name) res = [hander(item) for item in query] data = {'items': res, 'count': query.count()} return self.xml_response_for_json( self.success_response(data=data, msg='获取成功'))
def getArticleClassifyForGUID(self, request, *args): """ 通过GUID获取当前分类下的子分类(不返回子分类的子分类) :param request: :param args: :return: """ params = request_body_to_dict(request) parent = params.get('parent') query = ArticleClassify.objects.filter(parent=parent) fields = [ 'name', 'guid', 'return_parents', 'return_all_children_count', 'isLeaf' ] hander = lambda item: object_to_dict(fields, item) res = [hander(item) for item in query] data = {'items': res} return self.xml_response_for_json( self.success_response(data=data, msg='获取成功'))
def Login(self, request, *args): """ 登陆接口 :param args: :return: """ params = request_body_to_dict(request) username = params.get('username', '') password = params.get('password', '') user = User.objects.filter(username=username).first() if not user or not user.check_password(password): return self.xml_response_for_json( self.error_response(msg='用户名或密码错误')) self.request.session['me'] = username print(self.request.COOKIES) # data = {'token': self.request.COOKIES.get(settings.SESSION_COOKIE_NAME)} data = {'token': self.request.COOKIES} # data = {} return self.xml_response_for_json( self.success_response(data=data, msg='登陆成功'))
def getArticleForGUID(self, request, *args): """ 通过GUID获取文章详情 :return: """ params = request_body_to_dict(request) guid = params.get('GUID') article = Article.objects.get(guid=guid) article.update_times() # 更新浏览次数 data = {} # 处理后台请求 if params.get('from_to_admin'): data = article.get_dict() # 处理前台请求 if not params.get('from_to_admin'): data = article.get_dict(not_add_fields=False, other_fields=['created', 'updated']) content_handler = ContentHandler(data['content']) data['menu'], data['content'] = content_handler.generate_menu_str() data['article_menu_label'] = article.return_article_classify_name return self.xml_response_for_json( self.success_response(data=data, msg='获取成功'))