def delete(self, request, cid): ''' 删除商品 :param request: :param cid: :return: ''' # noinspection PyBroadException try: commodity = Commodity.objects.filter(id=cid) if not commodity.exists(): return JsonResponse({ 'status': False, 'err': '找不到该内容' }, status=404) commodity = commodity[0] if not authCheck(['12', '515400'], request.session.get('login'), commodity): return JsonResponse({ 'status': False, 'err': '你没有权限' }, status=401) commodity.delete() return JsonResponse({ 'status': True, 'id': cid }) except: return JsonResponse({ 'status': False, 'err': '未知错误' }, status=403)
def post(self, requests): ''' 新增商品分类 :param requests: :return: ''' try: if not authCheck(['12', '515400'], requests.session.get('login')): return JsonResponse({ 'status': False, 'err': '你没有权限' }, status=401) param = requests.body jsonParams = json.loads(param) user = getUser(requests.session.get('login')) if Classification.objects.filter( name__exact=jsonParams.get('name')).exists(): return JsonResponse({ 'status': False, 'err': '分类名已存在' }, status=401) classification = Classification.objects.create( name=jsonParams.get('name'), create_man=user) return JsonResponse({'status': True, 'id': classification.id}) except: return JsonResponse({'status': False, 'err': '未知错误'}, status=403)
def put(self, request, pid): ''' 修改文章信息 :param request: :param pid: :return: ''' article = Article.objects.filter(id=pid) if not article.exists(): return JsonResponse({ 'status': False, 'err': '找不到该内容' }, status=404) article = article[0] if not authCheck(['123', '515400'], request.session.get('login'), article): return JsonResponse({ 'status': False, 'err': '你没有权限' }, status=401) params = request.body json_params = json.loads(params) try: if json_params.get('title') != None: article.title = json_params.get('title') if json_params.get('content') != None: article.content = json_params.get('content') if json_params.get('status') != None: article.content = json_params.get('status') if json_params.get('category') != None: category = Category.objects.filter(cid=json_params.get('category')) if not category.exists(): return JsonResponse({ 'status': False, 'err': '分类不存在', }, status=404) article.category = category if json_params.get('tags') != None: for tag in json_params.get('tags'): t = Tag.objects.filter(name=tag) if not t.exists(): t = Tag.objects.create(name=tag) article.tags.add(t) else: if not article.tags.filter(name=tag).exists(): t = t[0] article.tags.add(t) except: return JsonResponse({ 'status': False, 'err': '未知错误' }, status=403) article.save() return JsonResponse({ 'status': True, 'id': article.id })
def put(self, request, cid): ''' 修改商品内容 :param request: :param cid: :return: ''' params = request.body jsonParams = json.loads(params) try: commodity = Commodity.objects.filter(id=cid) if not commodity.exists(): return JsonResponse({ 'status': False, 'err': '找不到该内容' }, status=404) commodity = commodity[0] if not authCheck(['12', '515400'], request.session.get('login'), commodity): return JsonResponse({ 'status': False, 'err': '你没有权限' }, status=401) if jsonParams.get('classification'): try: commodity.classification = Classification.objects.get( name__exact=jsonParams.get('classification')) except: return JsonResponse({ 'status': False, 'err': '不存在此分类名' }, status=404) if jsonParams.get('status'): commodity.status = jsonParams.get('status') if jsonParams.get('detail'): commodity.detail = jsonParams.get('detail') if jsonParams.get('price'): commodity.price = jsonParams.get('price') commodity.last_mod_time = now() import datetime commodity.last_mod_date = datetime.datetime.now() commodity.save() return JsonResponse({ 'status': True, 'id': commodity.id, 'detail': commodity.detail, 'after_detail': commodity.detail, 'commodity_status': commodity.status, 'classification': commodity.classification.name }) except: return JsonResponse({ 'status': False, 'err': '意料之外的错误' }, status=403)
def get(self, request): ''' 获取用户列表 :param request: :return: ''' if authCheck(role='515400', email=request.session.get('login')): userList = User_Info.objects.all() return JsonResponse({ 'status': True, 'users': [model_to_dict(user, fields=self.INCLUDE_FIELDS) for user in userList] }) else: return JsonResponse({ 'status': False, 'err': '你没有权限' }, status=401)
def delete(self, request, pid): ''' 删除文章 :param request: :param pid: :return: ''' article = Article.objects.filter(id=pid) if not article.exists(): return JsonResponse({ 'status': False, 'err': '找不到该内容' }, status=404) article = article[0] if not authCheck(['123', '515400'], request.session.get('login'), article): return JsonResponse({ 'status': False, 'err': '你没有权限' }, status=401) article.delete() return JsonResponse({ 'status': True, 'id': pid })
def post(self, request): ''' 新增文章分裂 :param request: :return: ''' try: if not authCheck(['12', '515400'], request.session.get('login')): return JsonResponse({ 'err': '你没有权限', 'status': False }, status=401) params = json.loads(request.body) if Category.objects.filter(name=params.get('name')).exists(): return JsonResponse({ 'status': False, 'err': '分类名已存在' }, status=401) category = Category.objects.create(name=params.get('name')) return JsonResponse({'status': True, 'id': category.id}) except: return JsonResponse({'status': False, 'err': '出现未知错误'}, status=403)