def post(self, request): userId = request.POST.get('userId') if is_none_or_empty(userId): return EmptyResponse() user = userId2user(userId) if user: try: userinfo = UserInfo.objects.get(username=user.username) except UserInfo.DoesNotExist: userinfo = None if userinfo: data = { 'username': userinfo.username, 'name': userinfo.name, 'gender': userinfo.gender, 'grade': userinfo.grade, 'college': userinfo.college, 'professional': userinfo.professional, 'classname': userinfo.classname, 'dormitory': userinfo.dormitory, 'number': userinfo.number } return SuccessResponse('获取用户数据成功', data) return ErrorResponse('暂时没有当前用户的信息') else: return UserIdErrorResponse()
def post(self, request): try: data = request.data customer = checkIsExists(Customers, id=data['customer_id']) product = checkIsExists(Products, id=data['product_id']) assert customer, "customer not found::404" assert product, "product not found::404" already_exist = checkIsExists(Carts, customer_id=customer.id, product_id=product.id) if already_exist: varian_exist = json.loads(already_exist.varian) varian_data = data['varian'] # check if data already exist with the same varian if self.checkVarianExist(varian_data, varian_exist): current_quantity = already_exist.quantity already_exist.quantity += current_quantity already_exist.save() else: self.saveData(data, product) else: self.saveData(data, product) return SuccessResponse({'message': 'data successfully added!'}) except AssertionError as error: return AssertionErrorResponse(str(error)) except Exception as error: print(error) return ErrorResponse("Internal Server Error", 500)
def get(self, request): try: params = request.GET assert 'customer_id' in params and params[ 'customer_id'] != '', "parameter customer_id not found::404" customer_id = params['customer_id'] customer = checkIsExists(Customers, id=int(customer_id)) assert customer, "customer not found::404" carts = Carts.objects.filter(customer_id=customer_id) data = getPaginate(request, carts, pageQueryName='page', sizeQueryName='page_size') return SuccessResponse({ 'data': CartSerializer(data['data'], many=True).data, 'meta': data['meta'] }) except AssertionError as error: return AssertionErrorResponse(str(error)) except Exception as error: print(error) return ErrorResponse("Internal Server Error", 500)
def get(self, request): try: data_raw = Merchants.objects.all() data = MerchantSerializer(data_raw, many=True).data return SuccessResponse({"data": data}) except AssertionError as error: return AssertionErrorResponse(str(error))
def post(self, request): userId = request.POST.get('userId') nickName = request.POST.get('nickName') avatarUrl = request.POST.get('avatarUrl') gender = request.POST.get('gender') if is_none_or_empty(userId, avatarUrl, gender): return EmptyResponse() user = userId2user(userId) if self.user_exits(user): return SuccessResponse('已存在当前用户数据') else: UserWeChatInfo.objects.create(user=user, nickName=nickName, avatarUrl=avatarUrl, gender=gender) return SuccessResponse('上传微信信息成功')
def put(self, request, board_id): board_data = request.json_data board = request.board board.title = board_data.get("title") board.data = ( board_data.get("data", {}) if "data" in board_data.keys() else board.data ) board.save() return SuccessResponse()
def signin(request): user_info = request.json_data user = authenticate(username=user_info["email"], password=user_info["password"]) if user is not None: login(request, user) response = {"email": user.email} return SuccessResponse(response) else: return ErrorResponse(status=HTTPStatus.UNAUTHORIZED)
def post(self, request): userId = request.POST.get('userId') if is_none_or_empty(userId): return EmptyResponse() user = userId2user(userId) if user: scheduleOption.update_schedule(user) return SuccessResponse('课表更新成功') else: return UserIdErrorResponse()
def get(self, request): update_logs_jsons = [] update_logs = UpdateLog.objects.order_by('-create_time') for update_log in update_logs: update_info_json = { 'version_name': update_log.version_name, 'version_desc': update_log.version_desc.split('-'), 'create_time': update_log.create_time.strftime("%Y-%m-%d"), } update_logs_jsons.append(update_info_json) return SuccessResponse('获取更新数据成功', update_logs_jsons)
def post(self, request): loveId = request.POST.get('loveId') if is_none_or_empty(loveId): return EmptyResponse() if len(request.FILES) > 0: file_content = ContentFile(request.FILES['file'].read()) love_image = LoveImage.objects.create(love=Love.objects.get( id=loveId)) love_image.image.save(request.FILES['file'].name, file_content) return SuccessResponse('上传成功', 1)
def post(self, request, id): try: data = request.data merchant = checkIsExists(Merchants, id=id) assert merchant, "merchant id is not found::404" saved = Categories.objects.create(merchant_id=id, name=data['name']) return SuccessResponse({"message": "data successfully created!"}) except AssertionError as error: return AssertionErrorResponse(str(error))
def post(self, request): userId = request.POST.get('userId') time = request.POST.get('time') if is_none_or_empty(userId, time): return EmptyResponse() user = userId2user(userId) if user: data = resultsOption.query_results(user, time) return SuccessResponse('成绩查询获取成功', data) else: return UserIdErrorResponse()
def post(self, request): areaId = request.POST.get('areaId') architectureId = request.POST.get('architectureId') floorId = request.POST.get('floorId') roomId = request.POST.get('roomId') if is_none_or_empty(areaId, architectureId, floorId, roomId): return EmptyResponse() data = dormitoryOption.query_electricity(areaId, architectureId, floorId, roomId) return SuccessResponse('查询电费成功', data)
def post(self, request): state = request.POST.get('state') userId = request.POST.get('userId') if is_none_or_empty(state, userId): return EmptyResponse() user = userId2user(userId) if user: data = attendanceOption.query_state(user, state) return SuccessResponse('考勤数据获取成功', data) else: return UserIdErrorResponse()
def post(self, request): userId = request.POST.get('userId') time = request.POST.get('time') if is_none_or_empty(userId, time): return EmptyResponse() user = userId2user(userId) if user: data = examOption.query_exam_arrangement(user, time) return SuccessResponse('考试安排查询成功', data) else: return UserIdErrorResponse()
def post(self, request): taskNos = request.POST.get('taskNos') userId = request.POST.get('userId') week = request.POST.get('week') if is_none_or_empty(taskNos, userId, week): return EmptyResponse() user = userId2user(userId) if user: if teachingOption.one_key(user, week, taskNos): return SuccessResponse('一键评教成功') else: return UserIdErrorResponse()
def get(self, request): loves = Love.objects.filter(status=2) # 过滤审核通过的表白 resultList = [] for love in loves: loveImages = LoveImage.objects.filter(love=love) resultList.append({ 'weChatNickName': love.user_weChat_info.nickName, 'weChatAvatarUrl': love.user_weChat_info.avatarUrl, 'content': love.content, 'images': self.loveImgaes_to_list(loveImages), 'dataTime': love.create_time.strftime("%Y-%m-%d") }) return SuccessResponse('获取表白成功', resultList)
def post(self, request): userId = request.POST.get('userId') if is_none_or_empty(userId): return EmptyResponse() user = userId2user(userId) if user: has_schedule = scheduleOption.exist_schedule_for_semester(user) if has_schedule: return SuccessResponse("本学期已存在课表哦") else: return ErrorResponse('本学期没有导入课表哦', 2) else: return UserIdErrorResponse()
def post(self, request): userId = request.POST.get('userId') if is_none_or_empty(userId): return EmptyResponse() user = userId2user(userId) if user: if scheduleOption.exist_schedule_for_semester(user): return ErrorResponse('本学期课表已经存在或者正在导入中') scheduleOption.import_schedule(user) # 导入课表 if scheduleOption.exist_schedule_for_semester(user): # 检查课表是否导入成功 return SuccessResponse('导入课表成功') else: return ErrorResponse('导入课表失败') else: return UserIdErrorResponse()
def get(self, request, id): try: params = request.GET if 'ouput' in params and params['ouput'] == 'data': pass categories = Categories.objects.filter(merchant_id=id) paginated = getPaginate(request, categories, pageQueryName='page', sizeQueryName='page_size') data = CategorySerializer(paginated['data'], many=True).data return SuccessResponse({"data": data, "meta": paginated['meta']}) except AssertionError as error: return AssertionErrorResponse(str(error))
def post(self, request): try: data = request.data serializer = MerchantSerializer(data=data) if serializer.is_valid(): is_exist = checkIsExists(Sellers, customer_id=data['customer_id']) assert is_exist, "customer not found::404" saved = serializer.save() return SuccessResponse( {"data": MerchantSerializer(saved).data}) else: raise AssertionError("invalid data::400") except AssertionError as error: return AssertionErrorResponse(str(error))
def get(self, request): try: params = request.GET products = Products.objects.all() paginated = getPaginate(request, products, pageQueryName='page', sizeQueryName='page_size') return SuccessResponse({ "data": ProductSerializer(paginated['data'], many=True).data, "meta": paginated['meta'] }) except AssertionError as error: return AssertionErrorResponse(str(error))
def post(self, request): userId = request.POST.get('userId') love = request.POST.get('love') if is_none_or_empty(userId, love): return EmptyResponse() user = userId2user(userId) if not user: return UserIdErrorResponse() userWeChatInfo = userId2UserWeChatInfo(userId) if not userWeChatInfo: # 当前用户未授权用户 return ErrorResponse('当前用户未授权微信信息', 2) love = Love.objects.create( user=user, user_weChat_info=userWeChatInfo, content=love, ) return SuccessResponse('发布表白成功,请等待审核哦!', love.id)
def post(self, request): userId = request.POST.get('userId') username = request.POST.get('username') password = request.POST.get('password') if is_none_or_empty(userId, username, password): return EmptyResponse() # 检测用户名和密码是否正确 if userOption.check_username_and_password(username, password): user = userId2user(userId) if user: user.is_active = 1 user.username = username user.password = password user.save() return SuccessResponse('当前用户已激活') return UserIdErrorResponse() else: return ErrorResponse('用户名或密码错误')
def post(self, request): try: data = request.data data_length = data['length'] path = [] for i in range(int(data_length)): attribute = 'file_{}'.format(i + 1) file = data[attribute] print(file) saved = saveFile(file) print('saved file -> ', saved) if saved: path.append(saved) return SuccessResponse({"data": path}) except AssertionError as error: return AssertionErrorResponse(str(error))
def signup(request): user_info = request.json_data if User.objects.filter(email=user_info["email"]).exists(): return ErrorResponse(status=HTTPStatus.CONFLICT) new_user = User() new_user.email = user_info["email"] new_user.username = user_info["email"] new_user.set_password(user_info["password"]) new_user.save() user = authenticate(username=new_user.email, password=user_info["password"]) if user is not None: login(request, user) response = {"email": new_user.email} return SuccessResponse(response, status=HTTPStatus.CREATED) else: return ErrorResponse(status=HTTPStatus.BAD_REQUEST)
def post(self, request): userId = request.POST.get('userId') if is_none_or_empty(userId): return EmptyResponse() user = userId2user(userId) if user: try: user_wechat_info = UserWeChatInfo.objects.get(user=user) except UserWeChatInfo.DoesNotExist: user_wechat_info = None if user_wechat_info: data = { 'avatarUrl': user_wechat_info.avatarUrl, 'gender': user_wechat_info.gender } return SuccessResponse('获取用户数据成功', data) return ErrorResponse('暂时没有当前用户的信息') else: return UserIdErrorResponse()
def post(self, request): try: data = request.data serializer = CustomerSerializer(data=data) if serializer.is_valid(): saved = serializer.save() saved.password = generateHash(data['password']) saved.save() include_address = False address_saved = None if 'address' in data and data['address'] != {}: address = data['address'] if not isinstance(address, dict): temp = json.dumps(address) address = json.loads(temp) address_serializer = CustomerAddressSerializer( data=address) if address_serializer.is_valid(): address_saved = address_serializer.save() address_saved.default = True address_saved.customer_id = saved.id address_saved.save() include_address = True resp = { "message": "data successfully added!", "data": CustomerSerializer(saved, many=False).data } if include_address: resp['address'] = CustomerAddressSerializer( address_saved, many=False).data return SuccessResponse(resp) else: raise except AssertionError as error: return AssertionErrorResponse(str(error)) except: return ErrorResponse("Bad Request")
def post(self, request): try: data = request.data customer_id = data['customer_id'] customer = checkIsExists(Customers, id=customer_id) assert customer, "customer_id not found::404" serializer = SellerSerializer(data=data) if serializer.is_valid(): saved = serializer.save() customer.is_seller = True customer.save() return SuccessResponse({"message": "data successfully added!"}) else: raise except AssertionError as error: return AssertionErrorResponse(str(error)) except: return ErrorResponse("Bad Request")
def get(self, request, id): try: sql = """ SELECT products.id, products.name, products.description, products.price, products.stocks, products.varian, products.media, products.date_created, products.time_created, merchants.id as merchant_id, merchants.name as merchant_name, merchants.address as merchant_address, product_categories.id as category_id, product_categories.name as categories FROM ((products INNER JOIN merchants ON products.merchant_id = merchants.id) INNER JOIN product_categories ON products.category_id = product_categories.id) WHERE products.id = '{}' """.format(id) result = query(sql, False) result['varian'] = json.loads(result['varian']) result['media'] = json.loads(result['media']) spec_sql = """ SELECT * FROM product_specifications WHERE product_id = '{}' """.format(id) result['specifications'] = query(spec_sql) result['tags'] = query( "SELECT * FROM product_tags WHERE product_id = '{}'".format( id)) return SuccessResponse({'data': result}) except AssertionError as error: return AssertionErrorResponse(str(error))