def post(self, request): questionId = request.data.get('questionId', None) reply = request.data.get('reply', None) Question.objects.filter(id=questionId).update(solve=reply, state=1) response = BaseResponse() response.code = 200 response.data = "Reply Succeeded!" return Response(response.dict)
def get(self, request): response = BaseResponse() redisConnect = redis.Redis(connection_pool=POOL) token = request.META.get("HTTP_AUTHENTICATION", "") redisConnect.delete(str(token)) logout(request) response.code = 200 response.data = "Logout successful!" return Response(response.dict)
def post(self, request): taskId = request.data.get('taskId', None) value = request.data.get('value', None) comment = request.data.get('comment', None) Task.objects.filter(id=taskId).update(achievement=value, comment=comment, state=1) response = BaseResponse() response.code = 200 response.data = "Successful Correction!" return Response(response.dict)
def post(self, request): response = BaseResponse() username, password = request.data.get('username'), request.data.get('password') try: user = Account.objects.create_user(username=username, password=password) Student.objects.create(student=user) response.code, response.data = 200, 'Register Successful!' return Response(response.dict) except IntegrityError: response.code, response.data = 500, 'Username Already Exists!' return Response(response.dict)
def put(self, request): response = BaseResponse() requestData = QueryDict(request.body) userToken = requestData.get('userToken', None) userId = CONNECT.get(str(userToken)) buyCourseList = requestData.get('buyCourseList', None)[:-1].split(',') for courseId in buyCourseList: key = SHOPPING_CAR_KEY % (userId, courseId) CONNECT.hset(key, 'state', 1) response.code = 200 response.data = 'Enter Settlement!' return JsonResponse(response.dict)
def delete(self, request): response = BaseResponse() requestData = QueryDict(request.body) userToken = requestData.get('userToken', None) userId = CONNECT.get(str(userToken)) deleteCourseList = requestData.get('deleteCourseList', None)[:-1].split(',') for courseId in deleteCourseList: key = SHOPPING_CAR_KEY % (userId, courseId) CONNECT.delete(key) response.code = 200 response.data = 'Item deleted successfully!' return JsonResponse(response.dict)
def get(self, request): """购物车商品列表""" response = BaseResponse() # 1.构建 Redis Key token = request.GET.get('token', None) userId = CONNECT.get(str(token)) shoppingCarKey = SHOPPING_CAR_KEY % (userId, '*') # 2.读取 Redis 中数据 response.code, response.data, allKeys = 200, [], CONNECT.scan_iter(shoppingCarKey) for key in allKeys: response.data.append(CONNECT.hgetall(key)) return JsonResponse(response.dict)
def post(self, request): response = BaseResponse() username, password = request.data.get("username", None), request.data.get("password", None) user = authenticate(request=request, username=username, password=password) if user: # 如果验证通过 login(request, user) redisConnect = redis.Redis(connection_pool=POOL) try: response.code = 200 token = uuid.uuid4() redisConnect.set(str(token), user.id) # 获取购物车商品数量 goods, shoppingCarKey = [], "SHOPPING_CAR_%s_%s" % (user.id, '*') for key in redisConnect.scan_iter(shoppingCarKey): goods.append(redisConnect.hgetall(key)) data = { "access_token": token, "username": user.username, "identity": user.identity, "avatar": f"http://127.0.0.1:8000/media/{str(user.avatar)}", "message": 'Login Successful!', } response.data = data except Exception as e: print(e) response.code = 501 response.error = "Token creation failed!" else: # 验证失败 response.code = 501 response.error = "ERROR Incorrect username or password!" return Response(response.dict)
def post(self, request, pk): token = request.GET.get('token', None) userId = CONNECT.get(str(token)) user = Student.objects.filter(student=Account.objects.filter(id=userId).first()).first() file = request.FILES.get('file', None) Task.objects.create( student_id=userId, teacher_id=user.teacher.id, chapter_id=pk, file=file, ) response = BaseResponse() response.code = 200 response.data = "File uploaded successfully!" return Response(response.dict)
def post(self, request): userId = CONNECT.get(str(request.data.get('token', None))) user = Student.objects.filter(student_id=userId).first() requestData = request.data.copy() requestData.pop('token') requestData['student'] = user.id teacher = Teacher.objects.filter(studentTeacher=user).first() requestData['teacher'] = teacher.id Question.objects.create( student=user, teacher=teacher, title=request.data.get('title', None), content=request.data.get('content', None), environment=request.data.get('environment', None), situation=request.data.get('situation', None), state=1, ) response = BaseResponse() response.code = 200 response.data = "Successful questioning, Please wait for the good news!" return Response(response.dict)
def post(self, request): response = BaseResponse() # 1.获取前端传送的数据 token = request.POST.get('token', '') userId = CONNECT.get(str(token)) courseId = request.POST.get('courseId', '') # 2.校验课程、价格策略的合法性 course = PracticalCourse.objects.filter(id=courseId).first() if not course: response.code = 501 response.error = 'The course id is not valid!' return Response(response.dict) # 3.构建 Redis Key key = SHOPPING_CAR_KEY % (userId, courseId) # 4.构建数据结构 shoppingCourseInfo = { 'id': course.id, 'userToken': token, 'title': course.title, 'price': course.price, 'courseImage': str(course.courseImage), # state 表示状态 0:加入购物车但是未结算,1:进入结算,2:结算完毕 'state': 0, } # 5.写入 Redis CONNECT.hmset(key, shoppingCourseInfo) response.code = 200 response.data = 'Add to cart successfully!' return JsonResponse(response.dict)