Esempio n. 1
0
    def post(self, request):
        json_dict = get_json_data(request)

        j_id = request.GET.get('id')
        js_code = json_dict.get('js_code')
        description = json_dict.get('description')
        fornum_id = json_dict.get('fornum_id')

        description = '' if description is None else description

        if not all([j_id, js_code, fornum_id]):
            return JsonResponse({'code': RET.PARAMERR, 'message': '参数错误'})

        try:
            js = JavaScriptCode.objects.get(id=int(j_id))
        except Exception as e:
            logger.debug(e)
            return JsonResponse({'code': RET.PARAMERR, 'message': 'ID存在错误'})

        try:
            fornum = Fornum.objects.get(id=int(fornum_id))
        except Exception as e:
            logger.debug(e)
            return JsonResponse({'code': RET.DBERR, 'message': '指定修改的论坛错误'})

        try:
            js.description = description
            js.fornum = fornum
            js.js_code = js_code
            js.save()
        except Exception as e:
            logger.error(e)
            return JsonResponse({'code': RET.DBERR, 'message': '数据库错误'})

        return JsonResponse({'code': RET.OK, 'message': '成功'})
Esempio n. 2
0
    def get(self, request):
        """
        fornum_code     论坛编号
        p_version       程序版本号
        """
        fornum_code = request.GET.get('fornum_code')
        p_version = request.GET.get('p_version')

        if not all([fornum_code, p_version]):
            return JsonResponse({'code': RET.PARAMERR, 'message': '参数错误'})

        try:
            fornum = Fornum.objects.get(title=fornum_code)
        except Exception as e:
            logger.debug(e)
            return JsonResponse({'code': RET.DBERR, 'message': '论坛编号不存在'})

        try:
            program = Program.objects.get(fornum=fornum, version=p_version)
        except Exception as e:
            logger.debug(e)
            return JsonResponse({'code': RET.PARAMERR, 'message': '程序版本号不存在'})
        try:
            data = {
                'title': program.title,
                'fornum_id': program.fornum.id,
                'fornum_name': program.fornum.fornum_name,
                'fornum_code': program.fornum.title,
                'version': program.version,
                'is_active': program.is_active,
                'description': program.description
            }
        except Exception as e:
            logger.error(e)
        return JsonResponse({'code': RET.OK, 'message': '获取程序信息成功', "data": data})
Esempio n. 3
0
    def get(self, request):

        """
        通过QueryString传入所有要进行操作的id,键名为id,类型为int或数组
        op为操作标志为参数,也通过QueryString进行传入,该参数值如果为'ban'即为禁用操作,否则为删除操作
        :return:
        """

        # id 进行处理
        del_ids = request.GET.get('id')
        # 删除或者禁用操作
        operate = request.GET.get('op')

        if del_ids is None:
            return JsonResponse({'code': RET.PARAMERR, 'message': '参数错误'})

        try:
            del_ids = json.loads(del_ids)
        except Exception as e:
            logger.debug(e)
            return JsonResponse({'code': RET.PARAMERR, 'message': '参数错误'})

        # 非可迭代对象和字符串把其变成单元素的列表
        if (not isinstance(del_ids, Iterable)) or isinstance(del_ids, str):
            del_ids = [del_ids]

        # 过滤掉非整形数据
        del_ids = list(filter(filter_null, del_ids))

        if len(del_ids) == 0:
            return JsonResponse({'code': RET.PARAMERR, 'message': '参数错误'})

        machines = Machine.objects.filter(id__in=del_ids)

        if len(machines) == 0:
            return JsonResponse({'code': RET.PARAMERR, 'message': '该id不存在'})

        if len(machines) != len(del_ids):
            return JsonResponse({'code': RET.PARAMERR, 'message': 'id存在错误'})

        if operate == "ban":
            try:
                # 如果是禁用操作
                for machine in machines:
                    machine.is_active = not machine.is_active
                    machine.save()
            except Exception as e:
                logger.error(e)
        else:
            for m in machines:
                m.is_delete = True
                m.save()

        return JsonResponse({'code': RET.OK, 'message': '操作成功'})
Esempio n. 4
0
    def post(self, request):
        json_dict = get_json_data(request)
        p_id = request.GET.get('id')
        if json_dict is None:
            return JsonResponse({'code': RET.PARAMERR, 'message': '请使用json数据格式'})

        description = json_dict.get('description')
        fornum_id = json_dict.get('fornum_id')
        version = json_dict.get('version')
        title = json_dict.get('title')

        if not p_id:
            return JsonResponse({'code': RET.PARAMERR, 'message': '缺少必要参数'})

        if fornum_id:
            try:
                fornum = Fornum.objects.get(id=fornum_id)
            except Fornum.DoesNotExist:
                logger.debug('Fornum Does Not Exist')
                return JsonResponse({'code': RET.DBERR, 'message': '论坛不存在'})

        try:
            program = Program.objects.get(id=p_id)
            if description:
                program.description = description
            if title:
                program.title = title
            if version:
                program.version = version
            if fornum_id:
                program.fornum = fornum

            program.update_person = request.user.id
            program.update_time = datetime.now()

            program.save()

        except Exception as e:
            logger.error(e)
            return JsonResponse({'code': RET.DBERR, 'message': '数据库错误,修改失败'})

        return JsonResponse({'code': RET.OK, 'message': '修改成功'})
Esempio n. 5
0
    def post(self, request):
        json_dict = get_json_data(request)
        if json_dict is None:
            return JsonResponse({
                'code': RET.PARAMERR,
                'message': '请使用json数据格式'
            })

        js_code = json_dict.get('js_code')
        fornum_id = json_dict.get('fornum_id')
        title = json_dict.get('title')
        description = json_dict.get('description')

        if not all([js_code, fornum_id, title]):
            return JsonResponse({'code': RET.PARAMERR, 'message': '参数错误'})

        # 默认值设置为''
        description = '' if description is None else description

        try:
            fornum = Fornum.objects.get(id=fornum_id)
        except Exception as e:
            logger.debug(e)
            return JsonResponse({'code': RET.DBERR, 'message': '论坛不存在'})

        try:
            js = JavaScriptCode.objects.create(
                fornum=fornum,
                js_code=js_code,
                title=title,
                description=description,
                update_person=request.user.id,
                founder=request.user.id,
            )

            js.save()

        except Exception as e:
            logger.error(e)
            return JsonResponse({'code': RET.DBERR, 'message': '添加js代码失败'})

        return JsonResponse({'code': RET.OK, 'message': '添加成功'})
Esempio n. 6
0
    def post(self, request):
        json_dict = get_json_data(request)
        if json_dict is None:
            return JsonResponse({
                'code': RET.PARAMERR,
                'message': '请使用json数据格式'
            })

        description = json_dict.get('description')
        fornum_name = json_dict.get('fornum_name')
        title = json_dict.get('title')
        app_type = json_dict.get('app_type')

        if description is None: description = ''
        if title is None: title = ''
        if app_type is None: app_type = 0

        if fornum_name is None:
            return JsonResponse({'code': RET.PARAMERR, 'message': '缺少必要参数'})
        try:
            if int(app_type) not in Fornum.APP_TYPE.keys():
                return JsonResponse({
                    'code': RET.PARAMERR,
                    'message': '应用类型错误'
                })
        except Exception:
            return JsonResponse({'code': RET.PARAMERR, 'message': '应用类型错误'})

        try:
            fornum = Fornum.objects.create(title=title,
                                           fornum_name=fornum_name,
                                           description=description,
                                           update_person=request.user.id,
                                           founder=request.user.id,
                                           app_type=app_type)
            fornum.save()
        except Exception as e:
            logger.error(e)
            return JsonResponse({'code': RET.DBERR, 'message': '数据库错误,创建失败'})

        return JsonResponse({'code': RET.OK, 'message': '成功'})
Esempio n. 7
0
    def get(self, request):
        uuid = request.GET.get('uuid')
        last_uuid = request.GET.get('last_uuid')
        if not uuid:
            # 参数不全,返回错误信息
            return JsonResponse({'errorcode': RET.PARAMERR, 'errormsg': "参数错误"})
        # text:验证码的文本信息   image:验证码的图片一张
        image, text = Captcha.gene_code()

        redis_conn = get_redis_connection('default')

        try:
            # 将图片的UUID存储到redis数据库中,过期时间设置为300秒
            redis_conn.set('ImageCode:' + uuid, text, settings.IMAGE_CODE_REDIS_EXPIRES)
        except Exception as e:
            logger.error(e)
            return JsonResponse({'code': RET.DBERR, 'message': "数据库错误"})

        if last_uuid is not None:
            # 删除之前的图片验证码的UUID
            redis_conn.delete('ImageCode:' + last_uuid)

        # 设置响应的内容类型为图片
        return HttpResponse(image, content_type='image/jpeg')
Esempio n. 8
0
    def post(self, request):
        json_dict = get_json_data(request)
        if json_dict is None:
            return JsonResponse({'code': RET.PARAMERR, 'message': '请使用json数据格式'})

        description = json_dict.get('description')
        fornum_id = json_dict.get('fornum_id')
        version = json_dict.get('version')
        title = json_dict.get('title')

        if not all([version, fornum_id, title]):
            return JsonResponse({'code': RET.PARAMERR, 'message': '缺少必要参数'})

        if description is None: description = ''

        try:
            fornum = Fornum.objects.get(id=fornum_id)
        except Fornum.DoesNotExist:
            logger.debug('Fornum Does Not Exist')
            return JsonResponse({'code': RET.DBERR, 'message': '论坛不存在'})

        try:
            program = Program.objects.create(
                title=title,
                fornum=fornum,
                version=version,
                description=description,
                update_person=request.user.id,
                founder=request.user.id
            )
            program.save()
        except Exception as e:
            logger.error(e)
            return JsonResponse({'code': RET.DBERR, 'message': '数据库错误,创建失败'})

        return JsonResponse({'code': RET.OK, 'message': '成功'})
Esempio n. 9
0
    def get(self, request):
        d_id = request.GET.get('id')

        if d_id is None:
            return JsonResponse({'code': RET.PARAMERR, 'message': '缺少必要参数'})

        try:
            d_id = json.loads(d_id)
        except Exception:
            return JsonResponse({'code': RET.PARAMERR, 'message': '参数错误'})

        # 不是可迭代对象
        if not isinstance(d_id, Iterable) or isinstance(d_id, str):
            d_id = [d_id]

        # 将不能转换为整型的过滤掉
        d_id = list(filter(filter_null, d_id))

        if len(d_id) == 0:
            return JsonResponse({'code': RET.PARAMERR, 'message': '参数错误'})

        js = JavaScriptCode.objects.filter(id__in=d_id).all()

        if len(js) != len(d_id):
            logger.debug('js脚本{}存在错误'.format(d_id))
            return JsonResponse({'code': RET.PARAMERR, 'message': '传入的参数有误'})

        try:
            for j in js:
                j.is_delete = True
                j.save()
        except Exception as e:
            logger.error(e)
            return JsonResponse({'code': RET.DBERR, 'message': '数据库错误,删除失败'})

        return JsonResponse({'code': RET.OK, 'message': '删除成功'})
Esempio n. 10
0
    def post(self, request):
        """
        参数:
                mac_address  MAC地址参数
                user_name    用户名
                password    密 码
                fornum_code    论坛编号
        """

        json_dict = get_json_data(request)

        if json_dict is None:
            return JsonResponse({"code": RET.PARAMERR, "message": "请使用json格式数据"})

        # 用户名
        username = json_dict.get("user_name")
        # 密码  要md5加密不然不匹配
        password = json_dict.get("password")
        # mac地址
        mac_add = json_dict.get("mac_address")
        # 论坛编号
        fornum_code = json_dict.get('fornum_code')

        # 校验完整性
        if not all([username, password, mac_add, fornum_code]):
            logger.debug('登陆参数不完整')
            return JsonResponse({"code": RET.PARAMERR, "message": "参数不完整"})

        if len(password) != 32:
            return JsonResponse({'code': RET.PARAMERR, 'message': '密码或账户名错误'})

        if len(username) < 4:
            return JsonResponse({"code": RET.USERERR, "message": "密码或账户名错误"})

        # 检查用户名密码是否错误
        user = authenticate(username=username.upper(), password=password)

        if user is None or user.is_delete:
            return JsonResponse({"code": RET.USERERR, "message": "密码或账户名错误"})

        if not user.is_active:
            # 判断账户是否过期
            return JsonResponse({'code': RET.USERERR, "message": "账户已停用"})

        # 校验程序对应论坛用户是否有使用权限
        try:
            fornum = Fornum.objects.get(title=fornum_code)
        except Exception as e:
            logger.debug('论坛编号错误')
            return JsonResponse({'code': RET.PARAMERR, 'message': '论坛代号错误'})

        uf_ids = [uf.fornum.id for uf in UserFornumRelation.objects.filter(user=user).all()]

        if fornum.id not in uf_ids:
            logger.debug('账户没有使用该软件的权限')
            return JsonResponse({'code': RET.USERERR, 'message': '账户没有使用该软件的权限'})

        try:
            mem = MemberShip.objects.get(slave=user)

            if user.role == 6:
                if user.last_login is None:
                    mem.end_time = datetime.now() + timedelta(hours=3)
                    mem.save()

            if mem.end_time < datetime.now():
                # 过期了切换账号状态
                user.is_active = False
                user.save()
                return JsonResponse({'code': RET.USERERR, "message": "账户已停用"})
        except Exception as e:
            logger.warning('%s没有隶属关系记录' % user.username)
            pass

        # 校验用户使用的机器部分
        ip_add = getIPFromDJangoRequest(request)

        machines = Machine.objects.filter(user=user, mac_add=mac_add).all()
        # 不是新的机器要更新ip,是新的用户要校验是否满足机器个数的限制,记录机器的mac地址和ip
        if machines.count():
            # 不是0,是已经登陆过的机器,记录IP地址,每个机器,对应单独用户只记录一次
            try:
                machine = machines.first()
                if not machine.is_active:
                    return JsonResponse({'code': RET.USERERR, 'message': '该机器已被禁用'})
                machine.last_ip_add = ip_add
                machine.last_login = datetime.now()
                machine.save()
            except Exception as e:
                logger.error(e)
                return JsonResponse({'code': RET.DBERR, 'message': '数据库错误'})
        else:
            # 是0,说明是新机器,需要校验账户是否有足够的权限进行登陆操作
            if user.role in [3, 5, 6]:
                # 代理商和临时账号,广告操作员
                if Machine.objects.filter(user=user).all().count():
                    return JsonResponse({'code': RET.USERERR, 'message': '使用中设备已达上限'})
            elif user.role != 1:
                # 广告主和后台操作员
                used_machine_count = Machine.objects.filter(user=user).all().count()
                try:
                    user_info = UserInfo.objects.get(user=user)
                    if user_info.accounts <= used_machine_count:
                        return JsonResponse({'code': RET.USERERR, 'message': '可用设备已达上限'})
                except Exception as e:
                    logger.error("用户%s,遇到%s" % (user.username, e))
                    return JsonResponse({'code': RET.DBERR, 'message': '数据库错误', })

            # 走到这里说明账号可以使用新机器,向machine表中插入一条数据
            try:
                machine = Machine.objects.create(
                    user=user,
                    mac_add=mac_add,
                    last_ip_add=ip_add,
                    last_login=datetime.now(),
                    is_active=True
                )
                machine.save()
            except Exception as e:
                logger.error(e)
                return JsonResponse({'code': RET.DBERR, 'message': '数据库出了点问题'})

        try:
            ul = UserLogs.objects.create(
                user=user,
                softwareType=fornum.id,
                load_ip=getIPFromDJangoRequest(request),
                mac_add=mac_add
            )
            ul.save()
        except Exception as e:
            logger.warn(e)

        # 记录登录状态
        login(request, user)

        return JsonResponse({
            "code": RET.OK,
            "message": "登陆成功"
        })
Esempio n. 11
0
    def get(self, request):
        q = request.GET.get('q')
        page = request.GET.get('page')
        if request.user.role <= 2:
            users = User.objects.filter(models.Q(name__icontains=q) | models.Q(username__icontains=q)).all()
        else:
            mems = MemberShip.objects.filter(master=request.user).all()
            mems = mems.filter(models.Q(slave__name__icontains=q) | models.Q(slave__username__icontains=q))
            users = [mem.slave for mem in mems]

        data = []

        try:
            page = int(page)
        except TypeError:
            page = 1

        # 每页10条数据
        paginator = Paginator(users, settings.PER_PAGE_COUNTS)

        if (page > paginator.num_pages) or (page < 0):
            page = 1

        users = paginator.page(page)

        for user in users:

            # 论坛信息展示
            ufrs = UserFornumRelation.objects.filter(user=user).all()
            u_fornums = []
            for ufr in ufrs:
                f = {
                    'fornum_name': ufr.fornum.fornum_name,
                    'fornum_id': ufr.fornum.id,
                    'fornum_code': ufr.fornum.title
                }
                u_fornums.append(f)

            try:
                mem = MemberShip.objects.get(slave=user)
                remain_days = (mem.end_time - datetime.now()).days
                remain_days = remain_days if remain_days >= 0 else -1
                end_date = mem.end_time.strftime("%Y年%m月%d日 %H:%M")
                parent_name = mem.master.name
                parent_id = mem.master.id
                parent_role = mem.master.role
                remarks = user.userinfo_set.first().remarks
            except MemberShip.DoesNotExist:
                logger.info("%s没有隶属关系信息" % user.username)
                remain_days = -1
                end_date = -1
                parent_id = -1
                parent_name = ''
                parent_role = -1
                remarks = ""
            try:
                d = {
                    'parent_name': parent_name,
                    'parent_role': parent_role,
                    'parent_id': parent_id,
                    'id': user.id,
                    'username': user.username,
                    'name': user.name,
                    'email': user.email,
                    'role': user.role,
                    'remain_days': remain_days,
                    'remarks': remarks,
                    'join_date': user.date_joined.strftime("%Y年%m月%d日 %H:%M"),
                    'end_date': end_date,
                    'is_in_use': user.is_active,
                    'fornums': u_fornums
                }
            except Exception as e:
                logger.error(e)
                continue
            data.append(d)

        return JsonResponse({'code': RET.OK, 'message': '成功', 'data': data, 'pages': paginator.num_pages,
                             'data_length': paginator.count})
Esempio n. 12
0
    def get(self, request):
        page = request.GET.get('page')

        # 代理商和广告主只能处理自己的隶属账户
        if request.user.role in [3, 4]:
            slaves = MemberShip.objects.filter(master=request.user).order_by('-slave__is_active', '-slave__date_joined')
        else:
            slaves = MemberShip.objects.all().order_by('-slave__is_active', '-slave__date_joined')

        try:
            page = int(page)
        except TypeError:
            page = 1

        # 每页10条数据
        paginator = Paginator(slaves, settings.PER_PAGE_COUNTS)

        if (page > paginator.num_pages) or (page < 0):
            page = 1

        rel_page = paginator.page(page)

        data = []
        for slave in rel_page:
            if slave.slave.is_delete:
                continue

            remain_days = (slave.end_time - datetime.now()).days
            remain_days = remain_days if remain_days >= 0 else -1
            # 论坛信息展示
            ufrs = UserFornumRelation.objects.filter(user=slave.slave)
            u_fornums = []
            for ufr in ufrs:
                f = {
                    'fornum_name': ufr.fornum.fornum_name,
                    'fornum_id': ufr.fornum.id,
                    'fornum_code': ufr.fornum.title,
                    'app_type':ufr.fornum.app_type
                }
                u_fornums.append(f)

            try:
                d = {
                    'parent_role': slave.master.role,
                    'parent_name': slave.master.name,
                    'parent_id': slave.master.id,
                    'id': slave.slave.id,
                    'username': slave.slave.username,
                    'name': slave.slave.name,
                    'email': slave.slave.email,
                    'role': slave.slave.role,
                    'remarks': slave.slave.userinfo_set.first().remarks,
                    'remain_days': remain_days,
                    'join_date': slave.slave.date_joined.strftime("%Y年%m月%d日 %H:%M"),
                    'end_date': slave.end_time.strftime("%Y年%m月%d日 %H:%M"),
                    'is_in_use': slave.slave.is_active,
                    'fornums': u_fornums
                }
            except Exception as e:
                logger.error(e)
                continue
            data.append(d)

        # 按剩余使用天数排序
        return JsonResponse({'code': RET.OK, 'message': '成功', 'data': data, 'pages': paginator.num_pages,
                             'data_length': paginator.count})
Esempio n. 13
0
    def get(self, request):
        # 获取页码
        page = request.GET.get('page')

        if request.user.role in [3, 4]:
            relations = MemberShip.objects.filter(master=request.user).order_by('-slave__is_active',
                                                                                '-slave__date_joined')
        else:
            relations = MemberShip.objects.exclude(slave=request.user).order_by('-slave__is_active',
                                                                                '-slave__date_joined')

        try:
            page = int(page)
            logger.debug('页码错误')
        except TypeError:
            page = 1

        # 每页10条数据
        paginator = Paginator(relations, settings.PER_PAGE_COUNTS)

        if (page > paginator.num_pages) or (page < 0):
            page = 1

        rel_page = paginator.page(page)

        data = []
        for rel in rel_page:
            if rel.slave.is_delete:
                continue

            days = (rel.end_time - datetime.now()).days
            # 如果为负数
            days = days if days >= 0 else -1
            can_use = '可用' if days != -1 else '不可用'
            # 论坛信息展示
            ufrs = UserFornumRelation.objects.filter(user=rel.slave)
            u_fornums = []
            for ufr in ufrs:
                f = {
                    'fornum_name': ufr.fornum.fornum_name,
                    'fornum_id': ufr.fornum.id,
                    'fornum_code': ufr.fornum.title,
                    'app_type':ufr.fornum.app_type
                }
                u_fornums.append(f)

            try:
                d = {
                    'parent_role': rel.master.role,
                    'parent_name': rel.master.name,
                    'parent_id': rel.master.id,
                    'slave_name': rel.slave.name,
                    'slave_username': rel.slave.username,
                    'slave_id': rel.slave.id,
                    'role': rel.slave.role,
                    'create_time': rel.create_time,
                    'remarks': rel.slave.userinfo_set.first().remarks,
                    'update_time': rel.update_time,
                    'end_time': rel.end_time.strftime("%Y年%m月%d日 %H:%M"),
                    'remain_days': days,
                    'can_use': can_use,
                    'fornums': u_fornums
                }
            except Exception as e:
                logger.error(e)
                continue
            data.append(d)

        return JsonResponse({'code': RET.OK, 'message': '成功', 'data': data, 'pages': paginator.num_pages,
                             'data_length': paginator.count})
Esempio n. 14
0
    def post(self, request):
        u_id = request.GET.get('id')
        json_dict = get_json_data(request)
        if json_dict is None:
            return JsonResponse({'code': RET.PARAMERR, 'message': '请使用json格式数据'})

        password = json_dict.get('password')
        email = json_dict.get('email')
        role = json_dict.get('adminRole')
        gender = json_dict.get('sex')
        remarks = json_dict.get('remarks')
        telephone = json_dict.get('phone')
        accounts = json_dict.get('accounts')
        end_time = json_dict.get('end_time')
        fornum_id = json_dict.get('fornum_id')
        name = json_dict.get('name')

        if not all([role, u_id]):
            return JsonResponse({"code": RET.PARAMERR, 'message': '参数错误'})

        # 是否修改密码的标志位
        is_update_password = not (password == '' or password is None)

        # 如果密码传过来没有做md5加密
        if is_update_password and len(password) != 32:
            logger.warning('密码没有md5加密传输')
            encoder = hashlib.md5()
            encoder.update(password.encode(encoding='utf-8'))
            password = password.hexdigest().lower()

        if accounts is None:
            accounts = 0

        try:
            gender = int(gender)
        except Exception:
            gender = 0
        # 校验数字类型参数
        try:
            role = int(role)
            accounts = int(accounts)
        except Exception as e:
            logger.debug(e)
            return JsonResponse({'code': RET.PARAMERR, 'message': '参数类型错误'})

        if role > 6 or role < 1:
            logger.warning('用户要修改的role不合法')
            return JsonResponse({'code': RET.PARAMERR, 'message': '身份不合法'})

        try:
            # 转换为时间为python日期类型对象
            end_time = datetime.strptime(end_time, '%Y-%m-%d %H:%M:%S')
        except Exception:
            logger.warning('修改的日期时间不合法')
            end_time = datetime.now() + timedelta(hours=settings.ACCOUNTS_VALID_HOURS)

        try:
            user = User.objects.get(id=u_id)
        except Exception as e:
            logger.warning(e)
            return JsonResponse({'code': RET.DBERR, 'message': '修改用户不存在'})

        is_alert_fornum = False

        if request.user.role in [1, 2]:
            is_alert_fornum = True
        # 代理商
        if request.user.role == 3:
            if user.id == request.user.id:
                # 改自己的
                role = 3
            else:
                is_alert_fornum = True
                role = 6

            accounts = 0

        # 广告主
        if request.user.role == 4:
            if user.id == request.user.id:
                # 改自己的,不能该身份,所以身份不变
                role = 4
            else:
                is_alert_fornum = True
                role = 5

            accounts = 0

            try:
                # 广告主设置的有效期不能大于自己的有效期
                self_end_time = MemberShip.objects.get(slave=request.user).end_time
                end_time = self_end_time if end_time > self_end_time else end_time
            except Exception as e:
                logger.error(e)
                return JsonResponse({'code': RET.DBERR, 'message': '数据库错误'})

        # 可以修改论坛,并且传入了要修改的论坛的id值
        if fornum_id and is_alert_fornum:
            if not isinstance(fornum_id, Iterable) or isinstance(fornum_id, str):
                fornum_id = [fornum_id]

            try:
                fornum_id = list(filter(filter_null, fornum_id))
                fornum_id = [int(i) for i in fornum_id]
            except Exception:
                return JsonResponse({'code': RET.PARAMERR, 'message': '用户论坛相关参数错误'})

            if len(fornum_id) == 0:
                return JsonResponse({'code': RET.PARAMERR, 'message': '用户论坛相关参数错误'})

            uf_rels = UserFornumRelation.objects.filter(user=user).all()

            uf_f_ids = [uf_rel.fornum.id for uf_rel in uf_rels]

            if uf_f_ids != fornum_id:
                # 改前和改后一致,不做修改,否则要修改

                # 旧的不在新的里就是要删除的
                del_cols = [i for i in uf_f_ids if i not in fornum_id]
                # 新的不在旧的里就是要创建的
                create_cols = [i for i in fornum_id if i not in uf_f_ids]

                if request.user.role > 2:

                    f_ids = [i.fornum.id for i in UserFornumRelation.objects.filter(user=request.user).all()]

                    for c in create_cols:
                        if c not in f_ids:
                            return JsonResponse({'code': RET.PARAMERR, 'message': '论坛相关参数错误'})

                try:
                    with transaction.atomic():
                        # 删除
                        uf_rels.filter(fornum_id__in=del_cols).delete()

                        # 新增
                        for i in create_cols:
                            try:
                                fornum = Fornum.objects.get(id=i)
                            except Fornum.DoesNotExist:
                                logger.warning('论坛id错误')
                                return JsonResponse({'code': RET.PARAMERR, 'message': '论坛id错误'})

                            new_uf_rel = UserFornumRelation.objects.create(
                                user=user,
                                fornum=fornum,
                                founder=request.user.id,
                                update_person=request.user.id,
                                update_time=datetime.now()
                            )
                            new_uf_rel.save()

                except Exception as e:
                    logger.error(e)
                    return JsonResponse({'code': RET.DBERR, 'message': '数据库错误,用户论坛修改失败'})

        try:
            #  超级用户可能没有以下两张表的数据的
            user_info = UserInfo.objects.get(user=user)

            if request.user.role in [3, 4] and user.id != request.user.id:
                try:
                    mem = MemberShip.objects.get(master=request.user, slave=user)
                except Exception:
                    logger.warning('用户修改了非自己的从属账户,修改人id为%d' % request.user.id)
                    return JsonResponse({'code': RET.USERERR, "message": "只能修改自己的从属账户"})
            else:
                mem = MemberShip.objects.get(slave=user)

            user_info.update_time = datetime.now()
            user_info.update_person = request.user.id
            if telephone:
                user_info.telephone = telephone

            user_info.accounts = accounts
            user_info.gender = gender
            user_info.remarks = remarks
            if request.user.role != 3:
                logger.warning('代理商企图修改截止时间,修改人id为:%d' % request.user.id)
                # 代理商无法修改有效期
                mem.end_time = end_time
            mem.update_person = request.user.id
            mem.update_time = datetime.now()

            user_info.save()
            mem.save()

        except Exception as e:
            logger.error(e)
            pass

        try:
            if is_update_password:
                user.password = make_password(password)

            # 修改时间和修改人
            user.update_person = request.user.id
            user.update_time = datetime.now()
            user.role = role

            if name:
                user.name = name
            if email:
                user.email = email
            user.save()

        except Exception as e:
            logger.error(e)
            return JsonResponse({'code': RET.DBERR, 'message': '数据库错误'})

        return JsonResponse({'code': RET.OK, 'message': '成功'})
Esempio n. 15
0
    def get(self, request):
        """
        查询字符串传了id参数就返回指定id的用户的机器信息
        没传id,传了all参数就返回全部程序信息(不分页)
        没传id,也没传all参数,就返回分页的程序信息(page参数不传或者错误就返回第一页)
        """
        page = request.GET.get('page')
        u_id = request.GET.get('id')
        is_all = request.GET.get('all')
        try:
            page = int(page)
        except TypeError:
            logger.debug('Page Error')
            page = 1

        if u_id is None:
            machines = Machine.objects.all().order_by('-create_time')
        else:
            try:
                user = User.objects.get(id=u_id)
                if user.role == 3:
                    # 代理商机器过滤,要显示它的子账户的机器登陆信息
                    slaves = MemberShip.objects.filter(master=user)
                    slaves = [slave.slave for slave in slaves]
                    slaves.append(user)
                    machines = Machine.objects.filter(user__in=slaves)
                else:
                    machines = Machine.objects.filter(user=user).order_by('-create_time')

            except User.DoesNotExist:
                logger.debug('User Does Not Exist')
                return JsonResponse({'code': RET.PARAMERR, 'message': '用户id参数有误'})

        pages = 1
        data_length = 0 if is_all is None and u_id is None else 1

        if len(machines) > 1:
            paginator = Paginator(machines, settings.PER_PAGE_COUNTS)
            pages = paginator.num_pages
            if page > pages:
                return JsonResponse({'code': RET.PARAMERR, 'message': '页码错误'})
            data_length = paginator.count
            machines_page = paginator.page(page)
        else:
            try:
                machines_page = [machines.first()]
            except Exception:
                logger.debug('Machine Does Not Exist')
                return JsonResponse({'code': RET.PARAMERR, 'message': '没有该机器信息'})

        data = []

        if len(machines) == 0:
            machines_page = []

        if is_all is not None and u_id is None:
            machines_page = machines

        for machine in machines_page:
            try:
                d = {
                    'id': machine.id,
                    'user_id': machine.user.id,
                    'user_name': machine.user.username,
                    'user_role': machine.user.role,
                    'mac_add': machine.mac_add,
                    'last_ip_add': machine.last_ip_add,
                    'last_login': machine.last_login.strftime("%Y-%m-%d %H:%M:%S"),
                    'is_active': machine.is_active
                }
            except Exception as e:
                logger.error(e)
                continue

            data.append(d)

        return JsonResponse({'code': RET.OK, 'message': '成功', 'data': data, 'pages': pages,
                             'data_length': data_length})
Esempio n. 16
0
    def get(self, request):
        """
        查询字符串传了id参数就返回指定id的版本信息
        没传id,传了all参数就返回全部程序信息(不分页)
        没传id,也没传all参数,就返回分页的程序信息(page参数不传或者错误就返回第一页)
        """
        page = request.GET.get('page')
        f_id = request.GET.get('fornum_id')
        p_id = request.GET.get('id')
        is_all = request.GET.get('all')
        try:
            page = int(page)
        except TypeError:
            logger.debug('Page Error')
            page = 1
        if f_id is None and p_id is None:
            programs = Program.objects.all().order_by('-update_time')
        elif p_id is None and f_id is not None:
            try:
                fornum = Fornum.objects.get(id=f_id)
            except Fornum.DoesNotExist:
                logger.debug('Fornum Does Not Exist')
                return JsonResponse({'code': RET.PARAMERR, 'message': '论坛id参数错误'})
            programs = Program.objects.filter(fornum=fornum)
        else:
            programs = Program.objects.filter(id=p_id)

        pages = 1
        data_length = 0 if is_all is None and f_id is None else 1

        if len(programs) > 1:
            paginator = Paginator(programs, settings.PER_PAGE_COUNTS)
            pages = paginator.num_pages
            if page > pages:
                return JsonResponse({'code': RET.PARAMERR, 'message': '页码错误'})
            data_length = paginator.count
            programs_page = paginator.page(page)
        else:
            try:
                programs_page = [programs.first()]
            except Exception as e:
                logger.debug(e)
                return JsonResponse({'code': RET.PARAMERR, 'message': '没有该程序信息'})

        data = []

        if len(programs) == 0:
            programs_page = []

        if is_all is not None and f_id is None:
            programs_page = programs

        for program in programs_page:
            try:
                d = {
                    'id': program.id,
                    'title': program.title,
                    'fornum_name': program.fornum.fornum_name,
                    'fornum_id': program.fornum.id,
                    'version': program.version,
                    'is_active': program.is_active,
                    'description': program.description,
                    'join_date': program.create_time.strftime("%Y-%m-%d")
                }
            except Exception as e:
                logger.error(e)
                continue

            data.append(d)
        return JsonResponse({'code': RET.OK, 'message': '成功', 'data': data, 'pages': pages,
                             'data_length': data_length})
Esempio n. 17
0
    def post(self, request):
        """
        通过用户role获取用户信息
        post方式,Json格式传递
        role参数,可为数组或int类型,数组类型为多个角色
        """
        page = request.GET.get('page')
        json_dict = get_json_data(request)
        if json_dict is None:
            return JsonResponse({'code': RET.PARAMERR, 'message': '请使用json格式数据'})

        role = json_dict.get('role')

        if request.user.role < 2:

            if not role:
                return JsonResponse({'code': RET.PARAMERR, 'message': '缺少必要参数'})

            if not isinstance(role, Iterable) or isinstance(role, str):
                role = [role]

            role = list(filter(filter_null, role))
            role = [int(i) for i in role]

            if not role:
                return JsonResponse({'code': RET.PARAMERR, 'message': '参数错误'})

            users = User.objects.filter(role__in=role).order_by('-is_active', '-date_joined')
        else:
            mems = MemberShip.objects.filter(master=request.user)
            users = [mem.slave for mem in mems]

        try:
            page = int(page)
        except TypeError:
            logger.info('页码错误')
            page = 1

        # 每页10条数据
        paginator = Paginator(users, settings.PER_PAGE_COUNTS)

        if (page > paginator.num_pages) or (page < 0):
            page = 1

        users = paginator.page(page)

        data = []

        for user in users:

            if user.is_delete:
                continue

            # 论坛信息展示
            ufrs = UserFornumRelation.objects.filter(user=user)
            u_fornums = []
            for ufr in ufrs:
                f = {
                    'fornum_name': ufr.fornum.fornum_name,
                    'fornum_id': ufr.fornum.id,
                    'fornum_code': ufr.fornum.title,
                    'app_type':ufr.fornum.app_type,
                }
                u_fornums.append(f)

            try:
                mem_info = MemberShip.objects.get(slave=user)
            except Exception:
                remain_days = -1
                end_date = -1
            else:
                remain_days = (mem_info.end_time - datetime.now()).days
                end_date = mem_info.end_time.strftime("%Y年%m月%d日 %H:%M")
                remain_days = remain_days if remain_days >= 0 else -1

            # 父级账户信息展示
            try:
                mem = MemberShip.objects.get(slave=user)
                parent_name = mem.master.name
                parent_id = mem.master.id
                parent_role = mem.master.role
                remarks = user.userinfo_set.first().remarks
            except MemberShip.DoesNotExist:
                logger.info('%d缺少隶属关系' % user.id)
                parent_name = ''
                parent_id = -1
                parent_role = -1
                remarks = ''
            try:
                d = {
                    'parent_role': parent_role,
                    'parent_name': parent_name,
                    'parent_id': parent_id,
                    'id': user.id,
                    'username': user.username,
                    'name': user.name,
                    'email': user.email,
                    'role': user.role,
                    'remarks': remarks,
                    'remain_days': remain_days,
                    'join_date': user.date_joined.strftime("%Y年%m月%d日 %H:%M"),
                    'end_date': end_date,
                    'is_in_use': user.is_active,
                    'fornums': u_fornums
                }
            except Exception as e:
                logger.error(e)
                continue

            data.append(d)

        return JsonResponse({'code': RET.OK, 'message': '成功', 'data': data, 'pages': paginator.num_pages,
                             'data_length': paginator.count})
Esempio n. 18
0
    def post(self, request):
        """
        创建用户接口
        """
        json_dict = get_json_data(request)
        if json_dict is None:
            return JsonResponse({"code": RET.PARAMERR, "message": "请使用json格式数据"})

        user_name = json_dict.get('adminName')
        name = json_dict.get('name')
        password = json_dict.get('password')
        email = json_dict.get('email')
        role = json_dict.get('adminRole')
        gender = json_dict.get('sex')
        remarks = json_dict.get('remarks')
        telephone = json_dict.get('phone')
        accounts = json_dict.get('accounts')
        end_time = json_dict.get('end_time')

        try:
            # 转换为时间格式
            end_time = datetime.strptime(end_time, '%Y-%m-%d %H:%M:%S')
        except Exception:
            end_time = datetime.now() + timedelta(hours=settings.ACCOUNTS_VALID_HOURS)

        # 可以缺省的参数,给定默认值
        email = '*****@*****.**' if email is None else email
        remarks = "" if remarks is None else remarks
        telephone = '13011111111' if telephone is None else telephone
        gender = 0 if gender is None else gender
        role = 6 if role is None else role
        accounts = 0 if accounts is None else accounts

        name = name if name else user_name

        # 代理商只能开临时账户,临时账户有效期为3个小时
        if request.user.role == 3:
            role = 6
            accounts = 0
            end_time = datetime.now() + timedelta(hours=3)

        # 广告主只能开广告操作员,广告操作员账户有效期要小于等于开户的广告主的有效时间
        if request.user.role == 4:
            role = 5
            accounts = 0
            try:
                slef_end_time = MemberShip.objects.get(slave=request.user).end_time
                # 如果广告操作员有效时间超出广告主有效时间,将其置为广告主的有效操作时间
                if slef_end_time < end_time:
                    end_time = slef_end_time
            except Exception as e:
                logger.error(e)
                return JsonResponse({'code': RET.DBERR, 'message': '数据库错误'})

        # 广告主默认开户数为5
        if role == 4:
            accounts = settings.ACCOUNTS_CREATE_COUNT if accounts is None else accounts

        # 校验参数
        if not all([user_name, password]):
            return JsonResponse({"code": RET.PARAMERR, "message": "参数不完整"})

        # 如果密码传过来没有做md5加密
        if len(password) != 32:
            logger.info('密码没有md5加密传输')
            encoder = hashlib.md5()
            encoder.update(password.encode(encoding='utf-8'))
            password = password.hexdigest().lower()

        if len(user_name) < settings.USERNAME_MIN_LENGTH:
            return JsonResponse({'code': RET.PARAMERR, 'message': '用户名长度不足'})

        # 改大写
        user_name = user_name.upper()

        try:
            gender = int(gender)
            role = int(role)
            accounts = int(accounts)
        except Exception as e:
            logger.debug(e)
            return JsonResponse({'code': RET.PARAMERR, 'message': '参数格式错误'})

        if role > 6 or role < 1:
            logger.warning('用户的role参数错误')
            return JsonResponse({'code': RET.PARAMERR, 'message': '角色参数为[1,6]的数字'})

        f_id = json_dict.get('fornum_id')

        if f_id is None:
            return JsonResponse({'code': RET.PARAMERR, 'message': '参数不足'})

        if not isinstance(f_id, Iterable) or isinstance(f_id, str):
            f_id = [f_id]

        try:
            f_id = list(filter(filter_null, f_id))
        except Exception:
            return JsonResponse({'code': RET.PARAMERR, 'message': '参数错误'})

        if gender not in [0, 1]:
            return JsonResponse({'code': RET.PARAMERR, 'message': '性别必须为0和1'})

        # 检测用户名是否已经存在
        try:
            user = User.objects.get(username=user_name)
        except User.DoesNotExist:
            user = None
        if user is not None:
            logger.debug('用户名重复')
            return JsonResponse({"code": RET.USERERR, "message": "用户名已存在"})

        is_super_user = False
        # 检查用户可以开户个数
        try:
            curr_user = UserInfo.objects.get(user=request.user)
            curr_accounts = curr_user.accounts
            has_accounts = curr_user.has_accounts
        except Exception:
            curr_accounts = -1
            has_accounts = -1
            is_super_user = True

        if curr_accounts == 0:
            logger.warning('越权创建账户')
            return JsonResponse({'code': RET.USERERR, 'message': '您没有权限创建账户'})

        # 检查已开户数是否满足要求
        if curr_accounts > 0:
            # 当前用户创建的在有效期内的账户数量
            # has_accounts = MemberShip.objects.filter(master=request.user, end_time__gt=datetime.now()).count()
            # 当前用户的所有账户数量
            has_accounts = MemberShip.objects.filter(master=request.user).all().count()

            if has_accounts >= curr_accounts:
                logger.debug('超过创建用户的限制')
                # 超出了允许拥有的账户数量
                return JsonResponse({'code': RET.USERERR, 'message': '用户可开户账户数已到上限'})

        try:

            # 开启事务,防止创建表失败还添加了新用户
            with transaction.atomic():
                # 保存用户主表
                user = User.objects.create_user(
                    user_name,
                    email,
                    password,
                    role=role,
                    founder=request.user.id,
                    name=name,
                    update_person=request.user.id
                )
        except Exception as e:
            logger.info(e)
            return JsonResponse({'code': RET.PARAMERR, 'message': '用户名已存在'})

        try:
            with transaction.atomic():
                # 保存用户信息表
                user_info = UserInfo.objects.create(
                    user=user,
                    remarks=remarks,
                    telephone=str(telephone),
                    gender=gender,
                    accounts=accounts,
                    founder=request.user.id,
                    update_person=request.user.id
                )

                # 保存用户关系表
                mem_ship = MemberShip.objects.create(
                    master=request.user,
                    slave=user,
                    end_time=end_time,
                    founder=request.user.id,
                    update_person=request.user.id,
                )

                # 如果不是创建工作人员,记录用户指定的论坛

                fornums = Fornum.objects.filter(id__in=f_id).all()
                if len(fornums) != len(f_id):
                    raise Exception('程序id参数有误')

                if request.user.role > 2:
                    # 操作者不是工作人员,校验其自身的论坛
                    user_ps = UserFornumRelation.objects.filter(user=request.user).all()
                    can_f_ids = [r.fornum.id for r in user_ps]
                    for i in f_id:
                        if int(i) not in can_f_ids:
                            raise Exception('程序id参数有误')

                for fornum in fornums:
                    user_p = UserFornumRelation.objects.create(
                        user=user,
                        fornum=fornum,
                        founder=request.user.id,
                        update_person=request.user.id,
                        update_time=datetime.now()
                    )
                    user_p.save()

                user.save()
                user_info.save()
                mem_ship.save()

                if not is_super_user:
                    # 更新当前用户拥有的账户数
                    curr_user.has_accounts = has_accounts + 1
                    curr_user.save()

        except Exception as e:
            logger.error('创建用户失败:%s' % e)
            return JsonResponse({'code': RET.DBERR, 'message': '数据库错误'})

        return JsonResponse({'code': RET.OK, 'message': '添加成功!'})