Exemplo n.º 1
0
 def get(self, request, *args, **kwargs):
     ret = BaseResponse()
     try:
         # 获取id = 1的专题课,并打印该课程相关的所有章节
         obj = models.CourseChapter.objects.filter(course_id=1).all()
         ser = CoursesChapterViewModelSerializer(
             instance=obj, many=True, context={'request': request})
         ret.data = ser.data
     except Exception as e:
         ret.code = 500
         ret.error = '获取数据失败'
     return Response(ret.dict)
Exemplo n.º 2
0
 def get(self, request, *args, **kwargs):
     ret = BaseResponse()
     try:
         queryset = models.DegreeCourse.objects.all()
         page = PageNumberPagination()
         course_list = page.paginate_queryset(queryset, request, self)
         ser = DegreeCourseModelSerializer(instance=course_list, many=True)
         ret.data = ser.data
     except Exception as e:
         ret.code = 500
         ret.error = '获取数据失败'
     return Response(ret.dict)
Exemplo n.º 3
0
    def get(self, request, pk, *args, **kwargs):
        ret = BaseResponse()
        try:
            queryset = models.DegreeCourse.objects.get(id=pk)
            ser_quer = serializer_cmd.DegreeCourseModelSerializersPrice(
                instance=queryset)
            ret.data = ser_quer.data
        except Exception as e:
            ret.code = 500
            ret.error = '传输错误'

        return Response(ret.dict)
Exemplo n.º 4
0
    def authenticate(self, request):
        token = request.query_params.get('token')

        token = UserToken.objects.filter(token=token).first()
        if token:
            return token.user, token.token

        else:
            response = BaseResponse()
            response.code = 1001
            response.error = '认证失败'
            raise AuthenticationFailed(response.dict)
Exemplo n.º 5
0
 def get(self, request, *args, **kwargs):
     ret = BaseResponse()
     try:
         # 获取id = 1的专题课,并打印:课程名、级别(中文)、why_study、what_to_study_brief、所有recommend_courses
         DegreeCourse_info = Course.objects.filter(id=1)
         ser = DegreeCourseDetaileModelSerializer(
             instance=DegreeCourse_info, many=True)
         ret.data = ser.data
     except Exception as e:
         ret.code = 500
         ret.error = '获取数据失败'
     return Response(ret.dict)
Exemplo n.º 6
0
 def get(self, request, *args, **kwargs):
     ret = BaseResponse()
     try:
         # 查看id=1的学位课对应的所有模块名称
         DegreeCourse_list = Course.objects.filter(degree_course_id=1)
         ser = DegreeCourseTemplateModelSerializer(
             instance=DegreeCourse_list, many=True)
         ret.data = ser.data
     except Exception as e:
         ret.code = 500
         ret.error = '获取数据失败'
     return Response(ret.dict)
Exemplo n.º 7
0
 def get(self, request, *args, **kwargs):
     ret = BaseResponse()
     try:
         # 展示所有的专题课
         Course_list = Course.objects.filter(degree_course__isnull=True)
         ser = DegreeCourseScholarshiprModelSerializer(
             instance=Course_list, many=True, context={'request': request})
         ret.data = ser.data
     except Exception as e:
         ret.code = 500
         ret.error = '获取数据失败'
     return Response(ret.dict)
Exemplo n.º 8
0
    def login(self, request, *args, **kwargs):
        """
        用户登陆认证
        :param request:
        :param args:
        :param kwargs:
        :return:
        """
        response = BaseResponse()  # 默认状态
        try:
            user = request.data.get('username')
            pwd = request.data.get('password')
            print(user, pwd)
            # 验证用户和密码
            obj = models.Account.objects.filter(username=user,
                                                password=pwd).first()
            if not obj:
                response.code = 10002
                response.error = '用户名或密码错误'
            else:
                uid = str(uuid.uuid4())  # 生成唯一id
                # 保存到数据库中,update_or_create表示更新或者创建
                # user=obj,这个是判断条件。当条件成立,更新token字段,值为uid
                # 当条件不成立时,增加一条记录。注意:增加时,有2个字段,分别是user和token
                models.UserToken.objects.update_or_create(
                    user=obj, defaults={'token': uid})
                response.code = 1000
                response.data = uid

                # 增加session
                request.session['token'] = uid
                # request.session['userid'] = obj.id
                request.session['user'] = obj

        except Exception as e:
            response.code = 10005
            response.error = '操作异常'

        print(response.dict)
        return Response(response.dict)
Exemplo n.º 9
0
    def post(self, request):
        response = BaseResponse()

        receive = request.data
        if request.method == 'POST':
            is_valid = verify(receive)
            if is_valid:
                username = receive.get("username")
                password = receive.get("password")
                user = auth.authenticate(username=username, password=password)
                if user is not None:
                    # update the token
                    key = self.generate_key()
                    now = datetime.datetime.now()
                    Token.objects.update_or_create(user=user,
                                                   defaults={
                                                       "key": key,
                                                       "created": now
                                                   })
                    user_info = UserInfo.objects.get(pk=user.pk)
                    serializer = UserInfoSerializer(user_info)
                    data = serializer.data

                    response.msg = "验证成功!"
                    response.userinfo = data
                    response.token = key
                else:
                    try:
                        UserInfo.objects.get(username=username)
                        response.msg = "密码错误!"
                        response.code = 1002
                    except UserInfo.DoesNotExist:
                        response.msg = "用户不存在!"
                        response.code = 1003
            else:

                response.code = 1001
                response.msg = "请完成滑动验证!"

            return Response(response.dict)
Exemplo n.º 10
0
    def get(self, request, *args, **kwargs):
        ret = BaseResponse()
        try:
            queryset = Course.objects.filter(id=1)
            page = PageNumberPagination()
            course_list = page.paginate_queryset(queryset, request, self)
            ser = CourseSerializer6(instance=course_list, many=True)
            ret.data = ser.data
        except Exception as e:
            ret.code = 1001
            ret.error = '获取数据失败'

        return Response(ret.dict)
Exemplo n.º 11
0
 def get(self, request, *args, **kwargs):
     ret = BaseResponse()
     try:
         # a.查看所有学位课并打印学位课名称以及授课老师
         queryset = models.DegreeCourse.objects.all()
         # b.查看所有学位课并打印学位课名称以及学位课的奖学金
         # 见序列化类
         ser = degree.DegreeCourseSerializer(instance=queryset, many=True)
         ret.data = ser.data
     except Exception as e:
         ret.code = 0
         ret.errors = "数据获取失败"
     return Response(ret.dict)
Exemplo n.º 12
0
 def get(self, request, *args, **kwargs):
     ret = BaseResponse()
     try:
         # 获取id = 1的专题课,并打印该课程相关的所有常见问题
         obj = Course.objects.get(id=1)
         ask_list = obj.asked_question.all()
         ser = DegreeCourseOftenAskedQuestionModelSerializer(
             instance=ask_list, many=True)
         ret.data = ser.data
     except Exception as e:
         ret.code = 500
         ret.error = '获取数据失败'
     return Response(ret.dict)
Exemplo n.º 13
0
    def get(self, request, *args, **kwargs):
        ret = BaseResponse()
        try:
            # 查看所有学位课并打印学位课名称以及学位课的奖学金

            degree_course_scholarshipr = DegreeCourse.objects.all()
            ser = DegreeCourseScholarshiprModelSerializer(
                instance=degree_course_scholarshipr, many=True)
            ret.data = ser.data
        except Exception as e:
            ret.code = 500
            ret.error = '获取数据失败'
        return Response(ret.dict)
Exemplo n.º 14
0
 def get(self, request, *args, **kwargs):
     ret = BaseResponse()
     try:
         queryset = models.DegreeCourse.objects.all()
         print("queryset", queryset, "HHHHHHH1WWWWW")
         ser = degreeCourseWithSSModelSerializer(instance=queryset, many=True)
         print("ser", ser, "EEEEEERRRRRRRRREEEEE")
         ret.data = ser.data
     except Exception as e:
         print(e,'EEEEEEEEEEE')
         ret.code = 500
         ret.error = '获取数据失败'
     return Response( ret.dict)
Exemplo n.º 15
0
 def retrieve(self, request, pk, *args, **kwargs):
     """
         具體課程信息
         """
     ret = BaseResponse()
     try:
         course_data = models.Course.objects.get(pk=pk)
         ser = CourseSerializer(instance=course_data)
         ret.data = ser.data
     except Exception as e:
         ret.code = 1001
         ret.error = '获取数据失败'
     return Response(ret.dict)
Exemplo n.º 16
0
Arquivo: cpu.py Projeto: john966/CMDB
    def add(self, request, *args, **kwargs):  # 入库
        # res = {"code":0}
        res = BaseResponse()

        host = self.request.data.get('ip')  # 表单数据
        # 检测ip是否合格
        data = LegalIP(host).test_parameter()  # type:dict

        if data.get('error'):  # 判断结果
            res.code = 500
            res.error = data.get('error')
            return HttpResponse(json.dumps(res.__dict__))

        # 接收监控数据
        cpu = self.request.data.get('cpu')
        create_time = self.request.data.get('create_time')
        time_stamp = self.request.data.get('time_stamp')

        # 存入数据库中
        try:
            with transaction.atomic():  # 使用事务
                # 查询网卡ip,并查询出对应host表的id
                host = models.Network.objects.filter(ipaddr=host).values("host_id").first()
                if host:
                    # 获取主机id
                    host_id = host.get("host_id")
                    # CPU监控表中插入一条记录
                    models.CpuMonit.objects.create(cpu=cpu,create_time=create_time,time_stamp=time_stamp,host_id=host_id)
                    return HttpResponse(json.dumps(res.__dict__))
                else:
                    res.code = 500
                    res.error = "数据库找不到此ip"
                    return HttpResponse(json.dumps(res.__dict__))

        except Exception as e:
            print(e)
            res.code = 500
            res.error = "插入记录失败"
            return HttpResponse(json.dumps(res.__dict__))
Exemplo n.º 17
0
 def get(self, request, *args, **kwargs):
     ret = BaseResponse()
     try:
         # 获取id = 1的专题课,并打印该课程相关的课程大纲
         obj = models.Course.objects.get(
             id=1).coursedetail.courseoutline_set.all()
         ser = CoursesCourseoutlineViewModelSerializer(
             instance=obj, many=True, context={'request': request})
         ret.data = ser.data
     except Exception as e:
         ret.code = 500
         ret.error = '获取数据失败'
     return Response(ret.dict)
Exemplo n.º 18
0
    def update(self, request, *args, **kwargs):
        """
        修改用户选中的价格策略
        :param request:
        :param args:
        :param kwargs:
        :return:
        """
        """
        1. 获取课程ID、要修改的价格策略ID
        2. 校验合法性(去redis中)
        """
        response = BaseResponse()
        try:
            course_id = request.data.get('courseid')
            policy_id = str(request.data.get('policyid')) if request.data.get('policyid') else None

            # key = 'shopping_car_%s_%s' %(USER_ID,course_id,)
            key = settings.LUFFY_SHOPPING_CAR % (USER_ID, course_id,)

            if not CONN.exists(key):
                response.code = 10007
                response.error = '课程不存在'
                return Response(response.dict)

            price_policy_dict = json.loads(CONN.hget(key, 'price_policy_dict').decode('utf-8'))
            if policy_id not in price_policy_dict:
                response.code = 10008
                response.error = '价格策略不存在'
                return Response(response.dict)

            CONN.hset(key, 'default_price_id', policy_id)
            CONN.expire(key, 20 * 60)
            response.data = '修改成功'
        except Exception as e:
            response.code = 10009
            response.error = '修改失败'

        return Response(response.dict)
Exemplo n.º 19
0
    def destroy(self, request, *args, **kwargs):
        '''
         删除购物车中的某个课程
        :param request:
        :param args:
        :param kwargs:
        :return:
        '''
        response = BaseResponse()
        try:
            course_id = request.data.get('courseId')

            key = settings.LUFFY_SHOPPING_CAR % (1, course_id)  # 查出key值

            CONN.delete(key)  # 直接删除redis中key对应的整条记录
            response.code = 200
            response.data = '删除成功'
            return Response(response.dict)
        except Exception as e:
            response.code = 4008
            response.data = '删除失败'
            return Response(response.dict)
Exemplo n.º 20
0
 def get(self, request, pk, *args, **kwargs):
     ret = BaseResponse()
     print(pk)
     try:
         # courses_list = models.Course.objects.filter(pk=pk,egree_course__isnull=True).values('name')
         courses_list = models.Course.objects.filter(
             degree_course_id=1).values()
         ret.data = courses_list
         print(courses_list)
     except Exception as e:
         ret.code = 500
         ret.error = '获取数据失败'
     return Response(ret.dict)
Exemplo n.º 21
0
    def get(self, request, pk, *args, **kwargs):
        ret = BaseResponse()
        try:
            course = models.Course.objects.filter(id=pk)
            #序列化
            ser = CourseDetailSerializer(instance=course)
            ret.data = ser.data
            print(ret.data)
        except Exception as e:
            ret.code = 500
            ret.error = "获取数据失败"

        return Response(ret.dict)
Exemplo n.º 22
0
    def patch(self, request, *args, **kwargs):
        """
        修改课程的价格策略
        :param request:
        :param args:
        :param kwargs:
        :return:
        """
        ret = BaseResponse()
        try:
            # 1. 获取价格策略ID和课程ID
            course_id = int(request.data.get('courseid'))
            policy_id = str(request.data.get('policyid'))

            # 2. 拼接课程的key
            key = settings.SHOPPING_CAR_KEY %(request.auth.user_id,course_id,)
            #self.conn.exists(key):判断redis中是否有该key
            if not self.conn.exists(key):
                ret.code = 1002
                ret.error = "购物车中不存在此课程"
                return Response(ret.dict)
            # 3. 从redis中获取所有的价格策略
            policy_dict = json.loads(str(self.conn.hget(key,'policy'),encoding='utf-8'))
            if policy_id not in policy_dict:
                ret.code = 1003
                ret.error = "价格策略不合法"
                return Response(ret.dict)

            # 4. 在购物车中修改该课程的默认价格策略
            self.conn.hset(key,'default_policy',policy_id)

            ret.data = "修改成功"

        except Exception as e:
            ret.code = 1004
            ret.error = "修改失败"

        return Response(ret.dict)
Exemplo n.º 23
0
    def get(self, request, pk, *args, **kwargs):
        ret = BaseResponse()

        try:
            courses_list = models.Course.objects.filter(
                pk=pk, degree_course__isnull=False).values(
                    'coursedetail__courseoutline__title')
            ret.data = courses_list
            print(courses_list)
        except Exception as e:
            ret.code = 500
            ret.error = '获取数据失败'

        return Response(ret.dict)
Exemplo n.º 24
0
    def get(self, request, pk, *args, **kwargs):
        ret = BaseResponse()

        try:
            courses_list = models.Course.objects.filter(
                pk=pk, degree_course__isnull=False).first().asked_question.all(
                ).values('question')
            ret.data = courses_list
            print(courses_list)
        except Exception as e:
            ret.code = 500
            ret.error = '获取数据失败'

        return Response(ret.dict)
Exemplo n.º 25
0
    def get(self, request, *args, **kwargs):
        ret = BaseResponse()
        try:
            # 从数据库取数据
            queryset = models.DegreeCourse.objects.all()
            ser = DegreeCourseModelSerializer(instance=queryset, many=True)
            ret.data = ser.data
            print('_____________')
            print(ret.data)
        except Exception as e:
            ret.code = 500
            ret.error = '获取数据失败'

        return Response(ret.dict)
Exemplo n.º 26
0
 def get(self, request, *args, **kwargs):
     ret = BaseResponse()
     try:
         # 从数据库获取数据
         queryset = models.DegreeCourse.objects.order_by('id')
         # 分页
         page = PageNumberPagination()
         course_list = page.paginate_queryset(queryset, request, self)
         # 分页之后的结果执行序列化
         ser = DegreeCourseTeacherModelSerializer(instance=course_list, many=True)
         ret.data = ser.data
     except Exception as e:
         ret.code = 500
         ret.error = '获取数据失败'
     return Response(ret.dict)
Exemplo n.º 27
0
    def update(self,request,*args,**kwargs):
        response = BaseResponse()
        try:
            course_id = request.data.get('courseid')
            policy_id = str(request.data.get('policyid')) if request.data.get('policyid') else None
            key = settings.LUFFY_SHOPPING_CAR %(USER_ID,course_id)
            if not CONN.exists(key):
                response.code = 10007
                response.error = '课程不存在'
                return Response(response.dict)

            price_policy_dict = json.loads(CONN.hget(key,'price_policy_dict').decode('utf8'))
            if policy_id not in price_policy_dict:
                response.code = 10008
                response.error = '价格策略不存在'
                return Response(response.dict)
            CONN.hset(key,'default_price_id',policy_id)
            CONN.expire(key,20*60)
            response.data = '修改成功'
        except Exception as e:
            response.code = 10009
            response.error = '修改失败'

        return Response(response.dict)
Exemplo n.º 28
0
def get_pay_url(request):
    res = BaseResponse()
    order_number = request.GET.get("order_number")
    final_price = request.GET.get("final_price")
    stat = Order.objects.filter(order_number=order_number,
                                actual_amount=float(final_price))
    if stat:
        pay_url = ali_api.pay.get_pay_url(out_trade_no=order_number,
                                          total_amount=final_price,
                                          subject="test")
        res.data = {"pay_url": pay_url}
    else:
        res.code = 1402
        res.msg = '订单不存在'
    return JsonResponse(res.dict)
Exemplo n.º 29
0
    def add(self, request, *args, **kwargs):
        """
        添加Ansible组名
        :param request:
        :param args:
        :param kwargs:
        :return:
        """
        res = BaseResponse()
        group = self.request.data.get('group')  # 表单数据
        if not group:
            res.code = 500
            res.error = "组名不能为空"
            return HttpResponse(json.dumps(res.__dict__))

        has_group = models.AnsibleGroup.objects.filter(name=group)
        if has_group:
            res.code = 500
            res.error = "组名已经存在"
            return HttpResponse(json.dumps(res.__dict__))

        try:
            result = models.AnsibleGroup.objects.create(name=group)
            if not result:
                res.code = 500
                res.error = "添加失败"
                return HttpResponse(json.dumps(res.__dict__))

            res.url = "/web/ansible/list/"
            return HttpResponse(json.dumps(res.__dict__))

        except Exception as e:
            print(e)
            res.code = 500
            res.error = "添加异常"
            return HttpResponse(json.dumps(res.__dict__))
Exemplo n.º 30
0
    def get(self, request, *args, **kwargs):

        ret = BaseResponse()
        try:
            degree_list = models.DegreeCourse.objects.all()
            for row in degree_list:
                print(row.name)
                scholarship = row.scholarship_set.all()
                for item in scholarship:
                    print('---->', item.time_percent, item.value)

        except Exception as e:
            ret.code = 500
            ret.error = 'failed to get data'
        return Response(ret.dict)