def process_exception(self, request, e): """ 程序遇到未捕获的异常,会在此处整理出错误信息,和代码行数 by:王健 at:2015-1-3 修改日志 配置 by:王健 at:2015-3-10 修改获取用户手机号 by:王健 at:2015-3-15 优化 日志输入 by:王健 at:2015-3-17 修改错误堆栈信息 获取方式 by:王健 at:2015-4-7 修改错误输出格式 by: 范俊伟 at:2015-04-19 修改log输出函数 by: 范俊伟 at:2015-05-05 :param request: :param e: :return: """ import time errorid = time.time() # client.user_context({ # "errorid": errorid, # "url": request.path, # "method": request.method, # "post": json.dumps(request.POST.items(), ensure_ascii=False), # "get": json.dumps(request.GET.items(), ensure_ascii=False), # "ENVIRONMENT": settings.ENVIRONMENT, # # }) # client.captureException() log = logging.getLogger('django') s = [u'错误码:%s' % errorid, u'%s:%s' % (request.method, request.path)] user = getattr(request, 'user', None) if hasattr(user, "tel"): s.append(u'用户:%s' % user.tel) else: s.append(u'未登录用户') s.append(u'出现以下错误:') # s.append(traceback.format_exc()) etype, value, tb = sys.exc_info() s.append(repr(value.message)) s.append(u'错误代码位置如下:') while tb is not None: f = tb.tb_frame lineno = tb.tb_lineno co = f.f_code filename = co.co_filename name = co.co_name s.append(u'File "%s", line %d, in %s' % (filename, lineno, name)) tb = tb.tb_next if not settings.DEBUG: log.exception('\n '.join(s)) return get_result(False, u'服务器端错误,请联系管理员,错误标记码:%s' % errorid, dialog=2) else: m = '\n '.join(s) log.exception(m) return get_result(False, u'服务器端错误,错误如下:\n%s' % (m), dialog=2)
def create_organization(request, name, icon_url): """ 创建组织 :param icon_url: :param name: :param request: :return: 创建组织 by:王健 at:2016-04-26 """ try: obj = Organization() obj.name = name obj.icon_url = icon_url obj.save() person = Person() person.org = obj person.manage_type = 2 person.user = request.user person.realname = request.user.realname person.email = request.user.email person.save() org_commend("org_change", obj.id, None) return get_result(True, None, obj) except Organization.DoesNotExist: return get_result(False, u'组织不存在')
def transfer_manager_org(request, org_id, user_id, person): """ 添加新的超级管理员 :param user_id: :param person: :param request: :param org_id: :return: 添加新的超级管理员 by:王健 at:2016-04-27 组织成员变动事件 by:王健 at:2016-05-03 """ if person.manage_type == 2: return get_result(False, u'只有超级管理员才能添加新的超级管理员') try: member = Person.objects.get(user_id=user_id, org_id=org_id, is_active=True) member.copy_old() member.manage_type = 2 create, diff = member.compare_old() if diff: member.save() org_commend("transfer_manager_org", org_id, u"%s 设置您为 %s 超级管理员身份" % (person.realname, person.org.name), [user_id]) member_ids = get_org_member_ids_by_manage_type(org_id, [1, 2]) member_ids.remove(user_id) org_commend("transfer_manager_org", org_id, u"%s 设置 %s 为 %s 超级管理员" % (person.realname, member.realname, person.org.name), member_ids) org_commend("org_member_change", org_id, None) return get_result(True, u'成功将用户设置成超级管理员', member) except Person.DoesNotExist: return get_result(False, u'用户不是该组织成员')
def change_password_by_code(request, username, newpassword, code): """ 通手机验证码修改密码 :param username: :param request: :param newpassword: :param code: :return: 通手机验证码修改密码 by:王健 at:2016-04-21 """ if code != request.session.get('smscode', 1234): return get_result(False, u'短信验证码输入错误,请核实!') if username != request.session.get('smsusername'): return get_result(False, u'手机号和短信验证码发送的手机号不一致,请核实!', None) try: user = get_user_model().objects.get(username=username) except get_user_model().DoesNotExist: return get_result(False, u'用户不存在') if not user.is_active: return get_result(False, u'用户已经停止使用。') user.set_password(newpassword) user.save(update_fields=['password']) return get_result(True, u'重置密码成功,请重新登录')
def send_sms_code(request, tel): """ 发送修改密码的验证码 :param request: :param tel: :return: 发送修改密码的验证码 by:王健 at:2016-04-21 """ # todo:虚假发送验证码,待完善 before_sms = request.session.get('smstime') if before_sms and time.time() - before_sms < 60: return get_result(False, u'每分钟只能发送一条验证码短信') num = request.session.get('smsnum', 0) if num > 10: return get_result(False, u'当天发送短信验证码太多') if get_user_model().objects.filter(username=tel).exists(): request.session['smsusername'] = tel request.session['smstime'] = time.time() request.session['smscode'] = 1234 request.session['smsnum'] = num + 1 else: return get_result(False, u'手机号不存在') return get_result(True, u'发送验证码成功')
def make_appinfo_permission(request, org_id, app_id, user_id, role_id, person): """ 从组织中删除应用 :param request: :param org_id: :return: 从组织中删除应用 by:王健 at:2016-04-27 """ try: org = person.org app = AppInfo.objects.get(pk=app_id, orgapp__org_id=org_id, is_active=True, orgapp__is_active=True) person = Person.objects.get(org_id=org_id, user_id=user_id, is_active=True) permission, created = Permissions.objects.get_or_create(org=org, person=person, app=app) permission.copy_old() if role_id is None: permission.is_active = False else: permission.role = AppRole.objects.get(app_id=app_id, pk=role_id, is_active=True) create, diff = permission.compare_old() if diff: permission.save() return get_result(True, u'修改用户在应用中的角色成功') except OrgApp.DoesNotExist: return get_result(False, u'组织中没有这个应用') except AppInfo.DoesNotExist: return get_result(False, u'应用不存在') except Person.DoesNotExist: return get_result(False, u'用户不是组织的成员') except AppRole.DoesNotExist: return get_result(False, u'应用中不存在这个角色')
def add_charge_group(request, org_id, group_id, user_id, person, group): """ 添加部门主管 :param request: :param org_id: :return: 添加部门主管 by:王健 at:2016-04-27 部门变动事件 by:王健 at:2016-05-03 """ if check_person_group_permiss(person, group, True): group.copy_old() try: group.charge = Person.objects.get(org_id=org_id, user_id=user_id, is_active=True) except Person.DoesNotExist: return get_result(False, u'用户不是当前组织的成员,不能设置成主管') created, diff = group.compare_old() if diff: group.save() org_commend("org_group_change", org_id, None) if group.talkgroup_id is not None: talkuser, created = TalkUser.objects.get_or_create(talkgroup_id=group.talkgroup_id, user_id=user_id) talkuser.copy_old() talkuser.is_active = True crea, diff = talkuser.compare_old() if created or diff: talkuser.save() talkuser.push_im_event(request.user) im_commend("im_group_change", group.talkgroup_id) return get_result(True, u'设置部门主管成功', group) else: return get_result(False, u'只有管理员、部门主管、父级部门主管、父级部门主管助手可以添加部门主管')
def create_product_error_bind_rule(request, org_id, diameter, weight_m, m_select, weight_min, weight_max, person): try: obj = ProductionErrorBindRule() obj.diameter = diameter diameter_obj = ProductionErrorBindRule.objects.filter( diameter=diameter, is_active=True).first() if weight_m and diameter_obj: return get_result(False, u'对应规格 米重已定义') elif not weight_m and diameter_obj: obj.weight_m = diameter_obj.weight_m elif not weight_m and not diameter_obj: return get_result(False, u'对应规格 米重未定义') else: obj.weight_m = weight_m obj.m_select = m_select obj.weight_min = weight_min obj.weight_max = weight_max obj.save() return get_result(True, u'创建标签信息成功', obj) except IntegrityError: return get_result(False, u'对应规格 {}米重已存在'.format(m_select))
def add_talkgroup(request, talkgroup_id, user_id, talkuser): """ 拉人入群 :param talkuser: :param user_id: :param request: :param talkgroup_id: :return: 拉人入群 by:王健 at:2016-04-25 """ if not talkuser.talkgroup.is_add and talkuser.talkgroup.owner_id != request.user.id and talkuser.role != 1: return get_result(False, u'只有群主和管理员才能拉人入群') member, created = TalkUser.objects.get_or_create(talkgroup_id=talkgroup_id, user_id=user_id) member.copy_old() member.is_active = True member.read_timeline = int(time.time()) member.is_muted = False member.compare_old() member.save() member.push_im_event(request.user) talkgroup = talkuser.talkgroup talkgroup.copy_old() talkgroup.make_md5_flag() created, diff = talkgroup.compare_old() if diff: talkgroup.save() im_commend("im_group_change", talkgroup.id) return get_result(True, u'踢出群成功')
def reject_organization(request, org_id, orgapply_id, person): """ 拒绝加入组织 :param orgapply_id: :param person: :param request: :param org_id: :return: 拒绝加入组织 by:王健 at:2016-04-27 拒绝加入事件 by:王健 at:2016-05-03 """ try: apporg = OrgApply.objects.get(id=orgapply_id, org_id=org_id) if apporg.status != 0: if apporg.status == 1: return get_result(False, u'该申请已经被 %s 通过' % apporg.checker) elif apporg.status == 2: return get_result(False, u'该申请已经被 %s 拒绝' % apporg.checker) else: return get_result(False, u'该申请已经被 %s 处理' % apporg.checker) apporg.copy_old() apporg.status = 2 apporg.compare_old() apporg.save() org_commend("reject_organization", org_id, u"您被拒绝加入 %s" % apporg.org.name, [apporg.user_id]) return get_result(True, u'已经绝用户的加入组织申请', apporg) except OrgApply.DoesNotExist: return get_result(False, u'这不是发给您的组织的申请,您不能处理')
def remove_manager_org(request, org_id, user_id, person): """ 移除管理员 :param user_id: :param person: :param request: :param org_id: :return: 移除管理员 by:王健 at:2016-04-27 组织成员变动事件 by:王健 at:2016-05-03 """ if person.manage_type == 2: return get_result(False, u'只有超级管理员才能移除管理员') try: member = Person.objects.get(user_id=user_id, org_id=org_id, is_active=True) member.copy_old() member.manage_type = 0 create, diff = member.compare_old() if diff: member.save() org_commend("remove_manager_org", org_id, u"%s 取消您 %s 管理员身份" % (person.realname, person.org.name), [user_id]) org_commend("org_member_change", org_id, None) return get_result(True, u'成功将用户设置成管理员', member) except Person.DoesNotExist: return get_result(False, u'用户不是该组织成员')
def update_person_group(request, org_id, user_id, realname, title, email, is_gaoguan, is_show_tel, is_show_email, person): """ 修改组织成员信息 :param group: :param person: :param user_id: :param request: :param org_id: :return: 部门加人 by:王健 at:2016-04-27 """ if person.manage_type in [1, 2]: try: member = Person.objects.get(org_id=org_id, user_id=user_id, is_active=True) member.copy_old() member.realname = realname member.email = email member.title = title member.is_gaoguan = is_gaoguan member.is_show_tel = is_show_tel member.is_show_email = is_show_email created, diff = member.compare_old() if diff: member.save() org_commend("org_group_change", org_id, None) return get_result(True, u'成员信息修改成功', member) except Person.DoesNotExist: return get_result(False, u'成员不存在') else: return get_result(False, u'只有管理员可以设置成员信息')
def remove_group(request, org_id, person, group): """ 删除部门 :param group: :param person: :param request: :param org_id: :return: 删除部门 by:王健 at:2016-04-27 部门变动事件 by:王健 at:2016-05-03 """ if check_person_group_permiss(person, group, True): if group.members.all().exist(): return get_result(False, u'部门内还有成员, 请先移除成员') group.copy_old() group.is_active = False created, diff = group.compare_old() if diff: group.save() org_commend("org_group_change", org_id, None) return get_result(True, u'删除部门成功', group) else: return get_result(False, u'只有管理员、部门主管、父级部门主管、父级部门主管助手可以删除部门')
def add_person_group(request, org_id, user_id, person, group): """ 部门加人 :param group: :param person: :param user_id: :param request: :param org_id: :return: 部门加人 by:王健 at:2016-04-27 部门变动事件 by:王健 at:2016-05-03 """ if check_person_group_permiss(person, group): try: group.members.add(Person.objects.get(org_id=org_id, user_id=user_id, is_active=True)) except Person.DoesNotExist: return get_result(False, u'用户不是当前组织的成员,不能加入部门') clean_organization_groups_cache(org_id) org_commend("org_group_change", org_id, None) if group.talkgroup_id: talkuser, created = TalkUser.objects.get_or_create(talkgroup_id=group.talkgroup_id, user_id=user_id) talkuser.copy_old() talkuser.is_active = True crea, diff = talkuser.compare_old() if created or diff: talkuser.save() talkuser.push_im_event(request.user) im_commend("im_group_change", group.talkgroup_id) return get_result(True, u'部门加人成功') else: return get_result(False, u'只有管理员、部门主管、部门主管助手、父级部门主管、父级部门主管助手可以添加部门成员')
def update_group(request, org_id, group_id, name, icon_url, parent_id, person, group): """ 修改部门的信息 :param request: :param org_id: :return: 修改部门的信息 by:王健 at:2016-04-27 部门变动事件 by:王健 at:2016-05-03 """ if not check_person_group_permiss(person, group): return get_result(False, u'只有管理员、部门主管、部门主管助手、父级部门主管、父级部门主管助手可以修改部门信息') try: if parent_id is not None: parentgroup = Group.objects.get(org_id=org_id, pk=parent_id, is_active=True) except Group.DoesNotExist: return get_result(False, u'设置的父级部门不存在,无法修改') group.copy_old() if name: group.name = name if icon_url: group.icon_url = icon_url if parent_id: group.parent_id = parent_id created, diff = group.compare_old() if diff: group.save() org_commend("org_group_change", org_id, None) return get_result(True, u'修改部门信息成功', group)
def update_product_item_fu_cha(request, org_id, aim_org_id, product_item_id, fc_remark, bind_status, person): """ 修改成品捆负差备注 :param request: :param org_id: :param aim_org_id: :param product_item_id: :param fc_remark: :param bind_status: :param person: :return: by:唐政 at:2021-01-07 """ if not aim_org_id: aim_org_id = org_id obj = ProductItem.objects.using(aim_org_id).filter( is_active=True, pk=product_item_id).first() if not obj: return get_result(False, u'成品捆不存在') obj.bind_status = bind_status obj.fc_remark = fc_remark obj.save(update_fields=['bind_status', 'fc_remark']) return get_result(True, u'修改成品捆负差成功')
def pass_friendapply(request, friendapply_id): """ 修改好友申请 :param request: :param friendapply_id: :return: 修改好友申请 by:王健 at:2016-04-24 增加好友变动的事件 by:王健 at:2016-05-03 """ try: friendapply = FriendApply.objects.get(id=friendapply_id, owner=request.user, is_active=True) friendapply.copy_old() friendapply.status = 1 created, diff = friendapply.compare_old() if diff: friend, created = Friend.objects.get_or_create(friend=friendapply.friend, owner=request.user) if not created: friend.is_active = True friend.save() friend, created = Friend.objects.get_or_create(owner=friendapply.friend, friend=request.user) if not created: friend.is_active = True friend.save() im_friend_commend("pass_friendapply", friendapply.owner_id, friendapply.friend_id, friendapply.toJSON()) return get_result(True, u'好友申请处理成功', friendapply) else: return get_result(False, u'已经处理过的申请,不能再次处理', friendapply) except FriendApply.DoesNotExist: return get_result(False, u'好友申请,不是发给您的,您无权处理')
def remove_person_org(request, org_id, user_id, person): """ 把用户移出组织 :param user_id: :param person: :param request: :param org_id: :return: 把用户移出组织 by:王健 at:2016-04-27 组织成员变动事件 by:王健 at:2016-05-03 """ try: member = Person.objects.get(user_id=user_id, org_id=org_id) member.copy_old() member.is_active = False create, diff = member.compare_old() if diff: member.save() org_commend("remove_person_org", org_id, u"%s 将您移出 %s " % (person.realname, person.org.name), [user_id]) org_commend("org_member_change", org_id, None) return get_result(True, u'成功将用户移出组织', member) except Person.DoesNotExist: return get_result(False, u'用户不是该组织成员')
def remove_talkgroup(request, talkgroup_id, user_id, talkuser): """ 踢人出群 :param talkuser: :param user_id: :param request: :param talkgroup_id: :return: 踢出群 by:王健 at:2016-04-25 """ if talkuser.talkgroup.owner_id != request.user.id and talkuser.role != 1: return get_result(False, u'只有群主和管理员才能踢人出群') member = TalkUser.objects.get(talkgroup_id=talkgroup_id, user_id=user_id) member.copy_old() member.is_active = False member.compare_old() member.save() member.push_im_event(request.user) talkgroup = talkuser.talkgroup talkgroup.copy_old() talkgroup.make_md5_flag() created, diff = talkgroup.compare_old() if diff: talkgroup.save() im_commend("im_group_change", talkgroup.id) return get_result(True, u'踢出群成功')
def test(request, *args, **kwargs): if not request.user.is_anonymous(): if request.user.is_active: return func(request, *args, **kwargs) else: return get_result(False, u'用户已被禁用。', None, 5) else: return get_result(False, u'请先登录', None, 1)
def get_userinfo(request, user_id): """ 获取用户信息 :param request: :return: 获取用户信息 by:范俊伟 at:2016-04-30 """ try: user = LYUser.objects.get(id=user_id) except: return get_result(False, '', '不存在此用户') return get_result(True, u'', user.toJSON())
def get_appinfo(request, appinfo_id): """ 查询应用信息 :param appinfo_id: :param request: :return: 查询应用信息 by:王健 at:2016-04-21 """ try: obj = AppInfo.objects.get_serializer(pk=appinfo_id) return get_result(True, None, obj) except AppInfo.DoesNotExist: return get_result(False, u'应用不存在')
def get_organization(request, org_id): """ 查询组织信息信息 :param org_id: :param request: :return: 查询组织信息信息 by:王健 at:2016-04-26 """ try: obj = Organization.objects.get_serializer(pk=org_id) return get_result(True, None, obj) except Organization.DoesNotExist: return get_result(False, u'组织不存在')
def remove_person_group(request, org_id, user_id, person, group): """ 部门加人 :param group: :param person: :param user_id: :param request: :param org_id: :return: 部门加人 by:王健 at:2016-04-27 部门变动事件 by:王健 at:2016-05-03 """ if check_person_group_permiss(person, group): try: group.copy_old() member = Person.objects.get(org_id=org_id, user_id=user_id, is_active=True) if group.charge_id == member.id: if group.aide_id == person.id: return get_result(False, u'主管助理,不能移除主管') group.charge = None if group.aide_id == member.id: group.aide = None group.members.remove(member) created, diff = group.compare_old() if diff: group.save() clean_organization_groups_cache(org_id) org_commend("org_group_change", org_id, None) if group.talkgroup_id: talkuser, created = TalkUser.objects.get_or_create(talkgroup_id=group.talkgroup_id, user_id=user_id) talkuser.copy_old() talkuser.is_active = False crea, diff = talkuser.compare_old() if diff: talkuser.save() talkuser.push_im_event(request.user) im_commend("im_group_change", group.talkgroup_id) except Person.DoesNotExist: pass return get_result(True, u'部门成员移出成功') else: return get_result(False, u'只有管理员、部门主管、部门主管助手、父级部门主管、父级部门主管助手可以移出部门成员')
def create_talkgroup(request, name, group_type, member_ids): """ 创建群 :param member_ids: :param group_type: :param request: :param name: :return: 创建群 by:王健 at:2016-04-24 """ talkgroup = TalkGroup() talkgroup.name = name talkgroup.group_type = group_type talkgroup.flag = '' talkgroup.owner = request.user talkgroup.save() for uid in member_ids: talkuser = TalkUser() talkuser.copy_old() talkuser.talkgroup = talkgroup talkuser.user_id = uid talkuser.role = 0 talkuser.read_timeline = int(time.time()) talkuser.save() talkuser.push_im_event(request.user) im_commend("im_group_change", talkgroup.id) talkgroup.make_md5_flag() talkgroup.save() return get_result(True, u'创建群成功', talkgroup)
def query_all_app_list(request, page_index, page_size): """ 查询所有的app list信息 :param page_size: :param page_index: :param request: :param : :return: 查询所有的app list信息 by:王健 at:2016-04-21 改造成分页返回值 by:王健 at:2016-04-23 """ query_app = AppInfo.objects.list_json().filter(is_active=True).order_by('name') app_list = [] app_dict = {} for app in query_app: app_list.append(app) app_dict[app['id']] = app app['apilist'] = [] api_dict = {} for api in AppApi.objects.list_json(ex_parms=['app_id']).filter(app__is_active=True, is_active=True).order_by( 'create_time'): if app_dict.has_key(api['app_id']): app_dict[api['app_id']]['apilist'].append(api) api_dict[api['id']] = api api['is_confirm'] = False for apicare in AppApiCareUser.objects.values('api_id').filter(is_confirm=True, api__is_active=True, api__app__is_active=True): if api_dict.has_key(apicare['api_id']): apicare['api_id']['is_confirm'] = True return get_result(True, u'', page_obj_query(app_list, page_index, page_size))
def check_response(request, *args, **kwargs): response = func(request, *args, **kwargs) if not settings.DEBUG: return response errors = [] url = request.META.get('PATH_INFO') if url.rfind('_list') == len(url) - 5: response_type = 'list' else: response_type = 'dict' if isinstance(response, JSONHttpResponse): # 如果接口返回不成功,则不校验返回值 if not response.json['success']: return response result = response.json['result'] data = None if isinstance(result, dict): if result.has_key('list') and result.has_key('page_index') and result.has_key('page_count'): if len(result['list']) > 0: data = result['list'][0] if response_type != 'list': errors.append('返回值为list,但是url没有以_list结尾') else: data = result if response_type != 'dict': errors.append('返回值为dict,但是url却以_list结尾') elif isinstance(result, Page): if len(result) > 0: data = result[0] if response_type != 'list': errors.append('返回值为list,但是url没有以_list结尾') elif isinstance(result, list): if len(result) > 0: data = result[0] if response_type != 'list': errors.append('返回值为list,但是url没有以_list结尾') if data: for key, parm in checks.items(): name = parm[0] name = '(%s:%s)' % (key, name) value = data.get(key, None) error, v = request_parmes_value_check(name, data.has_key(key), 'r') if error: errors.append(error) check_args = parm[1].split(',') for check in check_args: error, v = request_parmes_value_check(name, value, check) if error: errors.append(error) lkey = list(set(data.keys()) - set(checks.keys())) if lkey: errors.append('缺少字段校验:%s' % ','.join(lkey)) if len(checks) > 0 and len(errors) == 0: response.is_response_suggest = False if errors: if settings.DEBUG: print '%s:%s' % (url, ','.join(errors)) return get_result(False, '%s:%s' % (url, ','.join(errors)), None) return response
def add_person_org(request, org_id, user_id, person): """ 把用户加入组织,无需申请 :param user_id: :param person: :param request: :param org_id: :return: 把用户加入组织,无需申请 by:王健 at:2016-04-27 组织成员变动事件 by:王健 at:2016-05-03 """ member, created = Person.objects.get_or_create(user_id=user_id, org_id=org_id) member.copy_old() member.realname = member.user.realname member.email = member.user.email member.is_active = True member.manage_type = 0 create, diff = member.compare_old() if diff: member.save() org_commend("add_person_org", org_id, u"%s 加入 %s" % (member.realname, person.org.name), [user_id]) org_commend("org_member_change", org_id, None) return get_result(True, u'成功将用户加入组织', member)
def change_password(request, newpassword, oldpassword): """ 通过原密码修改新密码 :param oldpassword: :param request: :param newpassword: :return: 通过原密码修改新密码 by:王健 at:2016-04-21 """ if not request.user.is_active: return get_result(False, u'用户已经停止使用。') if not request.user.check_password(oldpassword): return get_result(False, u'密码错误。') request.user.set_password(newpassword) request.user.save(update_fields=['password']) return get_result(True, u'重置密码成功', None)
def my_userinfo(request): """ 获取我的个人信息 :param request: :return: 获取我的个人信息 by:王健 at:2016-04-21 """ user = request.user.toJSON() return get_result(True, u'', user)
def logout(request): """ 退出账号 :param request: :return: 退出 by:王健 at:2016-04-21 """ auth_logout(request) return get_result(True, '')
def query_friendapply_list(request, page_index, page_size): """ 查询好友申请,分页 :param request: :param page_index: :param page_size: :return: """ query = FriendApply.objects.list_json().filter(friend=request.user).filter(is_active=True).order_by('-create_time') return get_result(True, None, query.get_page(page_index, page_size))
def update_product_error_bind_rule(request, org_id, error_bind_rule_id, diameter, weight_m, m_select, weight_min, weight_max, person): try: if diameter and weight_m: query = ProductionErrorBindRule.objects.filter(diameter=diameter, is_active=True) update_list = [] if weight_m: for q in query: q.weight_m = weight_m update_list.append(q) obj = query.filter(m_select=m_select).first() obj.weight_min = weight_min obj.weight_max = weight_max obj.save() return get_result(True, u'创建标签信息成功', obj) except IntegrityError: return get_result(False, u'对应规格 {}米重已存在'.format(m_select))
def query_api_url_list_api(request): query = ApiUrl.objects.values('id', 'name', 'url').filter(is_active=True) result_list = [] for each_api_url in query: _dict = { 'id': each_api_url['id'], 'name': each_api_url['name'].strip(), 'url': each_api_url['url'], } result_list.append(_dict) return get_result(True, u'查询url列表成功', page_obj_query(result_list, 1, False))
def auto_create_apiurl_api(request): api_func_list = get_all_url_views('^', settings.ROOT_URLCONF) update_list = [] create_list = [] result_list = [] for url, views_ in api_func_list: if views_.find('django') == 0: continue # ('phonegap/get_latest_code', u'phonegap.views.get_latest_code') views_list = views_.split('.') func_name = views_list[-1] file_path = os.path.join(settings.BASE_DIR, *views_list[:-1]) code_list = file('%s.py' % file_path).readlines() key_list = [] # 找出关键点, 首行不缩进的 以 特定字符开头的 都是关键点 for i, code in enumerate(code_list): code_first_char = code[0] if code_first_char in ['@', 'd', 'c', 'f', 'i']: key_list.append((i, code_first_char, code)) # 找出函数体开始和结束的点, 重复函数要排除 fun_end = 0 fun_start = 0 for i, (index, code_first_char, code) in enumerate(key_list): if code_first_char == 'd' and code.find( 'def %s(' % func_name) >= 0: if len(key_list) == i + 1: fun_end = len(code_list) else: fun_end = key_list[i + 1][0] key_list = key_list[:i + 1] break key_list.reverse() for i, (index, code_first_char, code) in enumerate(key_list): if i > 0 and code_first_char in ['d', 'c', 'f', 'i']: fun_start = key_list[i - 1][0] key_list = key_list[:i] break key_list.reverse() # 找出doc信息的位置 fun_doc_start = 0 fun_doc_end = 0 for i in range(fun_start, fun_end): code = code_list[i] if code.find('"""') > 0 or code.find("'''") > 0: if fun_doc_start == 0: fun_doc_start = i + 1 else: fun_doc_end = i break fun_doc = code_list[fun_doc_start:fun_doc_end] if len(fun_doc) > 0: api_name = fun_doc[0].decode('utf8').strip() else: api_name = '' url_part = url.split('/') if len(url_part) > 1: last_str = url_part[-1] auth = 0 if "query" in last_str else 1 else: auth = 0 api_url = ApiUrl.objects.filter(url=url).first() if api_url: api_url.copy_old() api_url.name = api_name api_url.is_auth = auth create, diff = api_url.compare_old() if diff: update_list.append(api_url) else: api_url = ApiUrl() api_url.name = api_name api_url.url = url api_url.is_auth = auth create_list.append(api_url) result_list.append({ 'id': api_url.id, 'name': api_url.name, 'url': api_url.url, 'is_auth': api_url.is_auth, }) from util.django_bulk_update.helper import bulk_update from django.db import transaction with transaction.atomic(): bulk_update(update_list, update_fields=['name', 'is_auth']) models.ApiUrl.objects.bulk_create(create_list, batch_size=100) pass return get_result(True, u'同步接口成功', page_obj_query(result_list, 1, False))
def query_product_item_list_api(request, aim_org_id, page_index, page_size): record_query = ProductionRecord.objects.values( 'id', 'plan_item_id', 'plan_item__roll_no', 'plan_item__gongyi__name', 'plan_item__prod_thick', 'plan_item__weight', 'plan_item__num', 'plan_item__trade_no_id', 'plan_item__trade_no__name', ). \ filter( org_id=aim_org_id, is_active=True).order_by("-create_time") if record_query is None: return get_result(False, u'对应ProductionRecord 不存在') if len(record_query) >= 2: record_query = record_query[:2] else: record_query = record_query[:1] result_list = [] for i, record in enumerate(record_query): item_query = ProductItem.objects.values( 'id', 'bind_no', 'merge_no', 'product_time', 'shift_name', 'shift_flag', 'num', 'diameter', 'fixed_length', 'truck_no', 'create_time'). \ using(aim_org_id).filter(is_active=True, org_id=aim_org_id, roll_no=record['plan_item__roll_no']) record_item_query = ProductionRecordItem.objects.values( 'id', 'real_weight' ).using(aim_org_id). \ filter(org_id=aim_org_id, production_record_id=record['id'], is_active=True). \ exclude(conclusion=ProductionRecordItem.Conclusion_None, ) for item, record_item in zip(item_query, record_item_query): result_dict = { 'product_record_id': record['id'], 'record_item_id': record_item['id'], 'item_id': item['id'], 'roll_no': record['plan_item__roll_no'], 'bind_no': item['bind_no'], 'merge_no': item['merge_no'], 'product_time': item['product_time'], 'num': item['num'], 'steel_type_id': record['plan_item__trade_no_id'], 'steel_type_name': record['plan_item__trade_no__name'], 'diameter': int(item['diameter']), 'fixed_length': item['fixed_length'], 'create_time': item['create_time'], 'gongyi': record['plan_item__gongyi__name'], 'sj_weight': record_item['real_weight'], 'll_weight': record['plan_item__weight'] / record['plan_item__num'] if record['plan_item__num'] else 0, 'truck_no': item['truck_no'] } result_list.append(result_dict) return get_result(True, u'查询数据成功', page_obj_query(result_list, page_index, page_size))
def analyse_product_fu_cha_result(request, org_id, aim_org_id, bind_no, truck_no, time_interval, steel_type_id, prod_thick, roll_no, shift_name, shift_flag, page_index, page_size, person): """ :param request: :param org_id: :param aim_org_id: :param bind_no: :param truck_no: :param time_interval: :param steel_type_id: :param prod_thick: :param roll_no: :param shift_name: :param shift_flag: :param page_index: :param page_size: :param person: :return: by:唐政 at:2021-1-05 """ if not time_interval[0] or not time_interval[1]: return get_result(False, u"请提供正确的时间区间") if time_interval[1] < time_interval[0]: return get_result(False, u"结束时间不能小于起始时间") if aim_org_id is None: aim_org_id = org_id record_query = ProductionRecord.objects.values( 'id', 'plan_item_id', 'plan_item__roll_no', 'plan_item__heat_no', 'plan_item__gongyi__name', 'plan_item__prod_thick', 'plan_item__weight', 'plan_item__num', 'plan_item__trade_no_id', 'plan_item__trade_no__name', ). \ filter( org_id=aim_org_id, create_time__gte=time_interval[0], create_time__lte=time_interval[1] + datetime.timedelta(days=1), is_active=True).order_by("-create_time") if steel_type_id: record_query = record_query.filter( plan_item__trade_no_id=steel_type_id) if prod_thick: record_query = record_query.filter(plan_item__prod_thick=prod_thick) if roll_no: record_query = record_query.filter(plan_item__roll_no=roll_no) roller_query = RollerSpecification.objects.values( 'id', 'name', 'diameter', ).filter(org__id=org_id) flag_list = [ 'zha_gang_fu_cha_target_{}_{}'.format(int(roller['diameter']), org_id) for roller in roller_query ] setting_query = PlanExcelImportSetting.objects.filter(is_active=True, flag__in=flag_list) diameter_dict = {} for setting in setting_query: try: diameter_dict[str(setting.flag.split('_')[-2])] = json.loads( setting.content) except: diameter_dict[str(setting.flag.split('_')[-2])] = None result_list = [] update_list = [] dis_list = [] for i, record in enumerate(record_query): item_query = ProductItem.objects.values( 'id', 'bind_no', 'merge_no', 'heat_no', 'product_time', 'shift_name', 'shift_flag', 'num', 'diameter', 'fixed_length', 'status', 'shape_code', 'truck_no', 'bind_status'). \ using(aim_org_id).filter(is_active=True, org_id=aim_org_id, roll_no=record['plan_item__roll_no']) if bind_no: item_query = item_query.filter(bind_no=bind_no) if truck_no: item_query = item_query.filter(truck_no=truck_no) if shift_name: item_query = item_query.filter(shift_name=shift_name) if shift_flag: item_query = item_query.filter(shift_flag=shift_flag) record_item_query = ProductionRecordItem.objects.values( 'id', 'real_weight' ).using(aim_org_id). \ filter(org_id=aim_org_id, production_record_id=record['id'], is_active=True). \ exclude(conclusion=ProductionRecordItem.Conclusion_None, ) for item, record_item in zip(item_query, record_item_query): result_dict = {} truck_query = ProductItem.objects.values('bind_no').using( aim_org_id).filter(truck_no=item['truck_no'], is_active=True) result_dict['product_record_id'] = record['id'] result_dict['roll_no'] = record['plan_item__roll_no'] result_dict['steel_type_id'] = record['plan_item__trade_no_id'] result_dict['steel_type_name'] = record[ 'plan_item__trade_no__name'] result_dict['gongyi'] = record['plan_item__gongyi__name'] result_dict['ll_weight'] = record['plan_item__weight'] / record[ 'plan_item__num'] if record['plan_item__num'] else 0 result_dict['item_id'] = item['id'] result_dict['record_item_id'] = record_item['id'] result_dict['bind_no'] = item['bind_no'] result_dict['merge_no'] = item['merge_no'] result_dict['product_time'] = item['product_time'] result_dict['shift_name'] = item['shift_name'] result_dict['shift_flag'] = item['shift_flag'] result_dict['num'] = item['num'] result_dict['diameter'] = int(item['diameter']) result_dict['fixed_length'] = item['fixed_length'] result_dict['status'] = item['status'] result_dict['shape_code'] = item['shape_code'] result_dict['sj_weight'] = record_item['real_weight'] fc_calculate = (result_dict['ll_weight'] - result_dict['sj_weight']) * 100 / result_dict['sj_weight'] if \ result_dict['sj_weight'] else 0 result_dict['diff'] = round(fc_calculate, 2) result_dict['diff_standard'] = diameter_dict.get( str(result_dict['diameter']), None) if result_dict['diff_standard']: if result_dict['diff_standard'].get( 'min_value', 0 ) <= result_dict['diff'] <= result_dict['diff_standard'].get( 'max_value', 0): bind_status = 0 else: bind_status = 1 else: bind_status = 0 if item['id'] == 4648155: print item['fc_remark'] print item['fc_remark'] is None if bind_status != item['bind_status'] and item['fc_remark'] is None: obj = ProductItem.objects.using(aim_org_id).get(pk=item['id'], is_active=True) obj.bind_status = bind_status update_list.append(obj) result_dict['bind_status'] = bind_status else: result_dict['bind_status'] = item['bind_status'] result_dict['fc_remark'] = item['fc_remark'] result_dict['truck_no'] = item['truck_no'] result_dict['truck_no_list'] = [ truck['bind_no'] for truck in truck_query ] if result_dict['item_id'] in dis_list: pass else: dis_list.append(result_dict['item_id']) result_list.append(result_dict) from util.django_bulk_update.helper import bulk_update from django.db import transaction with transaction.atomic(): bulk_update(update_list, update_fields=['bind_status'], using=str(aim_org_id)) return get_result(True, u'分析数据成功', page_obj_query(result_list, page_index, page_size))
def analyse_product_bind_result(request, org_id, aim_org_id, bind_no, truck_no, time_interval, steel_type_id, prod_thick, roll_no, shift_name, shift_flag, page_index, page_size, person): """ 错捆预警分析 :param request: :param org_id: :param aim_org_id: :param bind_no: :param truck_no: :param time_interval: :param steel_type_id: :param prod_thick: :param roll_no: :param shift_name: :param shift_flag: :param page_index: :param page_size: :param person: :return: by:唐政 at:2021-1-08 """ if not time_interval[0] or not time_interval[1]: return get_result(False, u"请提供正确的时间区间") if time_interval[1] < time_interval[0]: return get_result(False, u"结束时间不能小于起始时间") if aim_org_id is None: aim_org_id = org_id record_query = ProductionRecord.objects.values( 'id', 'plan_item_id', 'plan_item__roll_no', 'plan_item__gongyi__name', 'plan_item__prod_thick', 'plan_item__weight', 'plan_item__num', 'plan_item__trade_no_id', 'plan_item__trade_no__name', ). \ filter( org_id=aim_org_id, create_time__gte=time_interval[0], create_time__lte=time_interval[1] + datetime.timedelta(days=1), is_active=True).order_by("-create_time") if steel_type_id: record_query = record_query.filter( plan_item__trade_no_id=steel_type_id) if prod_thick: record_query = record_query.filter(plan_item__prod_thick=prod_thick) if roll_no: record_query = record_query.filter(plan_item__roll_no=roll_no) result_list = [] dist_list = [] up_item_dict = {} deal_flag = 0 for i, record in enumerate(record_query): if record['plan_item__roll_no'] in dist_list: continue else: dist_list.append(record['plan_item__roll_no']) item_query = ProductItem.objects.values( 'id', 'bind_no', 'merge_no', 'product_time', 'shift_name', 'shift_flag', 'num', 'diameter', 'fixed_length', 'truck_no', 'bind_status'). \ using(aim_org_id).filter(is_active=True, org_id=aim_org_id, roll_no=record['plan_item__roll_no']) if bind_no: item_query = item_query.filter(bind_no=bind_no) if truck_no: item_query = item_query.filter(truck_no=truck_no) if shift_name: item_query = item_query.filter(shift_name=shift_name) if shift_flag: item_query = item_query.filter(shift_flag=shift_flag) record_item_query = ProductionRecordItem.objects.values( 'id', 'real_weight' ).using(aim_org_id). \ filter(org_id=aim_org_id, production_record_id=record['id'], is_active=True). \ exclude(conclusion=ProductionRecordItem.Conclusion_None, ) for item, record_item in zip(item_query, record_item_query): result_dict = {} truck_query = ProductItem.objects.values('bind_no').using( aim_org_id).filter(truck_no=item['truck_no'], is_active=True) result_dict['product_record_id'] = record['id'] result_dict['item_id'] = item['id'] result_dict['record_item_id'] = record_item['id'] result_dict['roll_no'] = record['plan_item__roll_no'] result_dict['bind_no'] = item['bind_no'] result_dict['merge_no'] = item['merge_no'] result_dict['product_time'] = item['product_time'] result_dict['shift_name'] = item['shift_name'] result_dict['shift_flag'] = item['shift_flag'] result_dict['num'] = item['num'] result_dict['steel_type_id'] = record['plan_item__trade_no_id'] result_dict['steel_type_name'] = record[ 'plan_item__trade_no__name'] result_dict['diameter'] = int(item['diameter']) result_dict['fixed_length'] = item['fixed_length'] result_dict['gongyi'] = record['plan_item__gongyi__name'] result_dict['sj_weight'] = record_item['real_weight'] result_dict['ll_weight'] = record['plan_item__weight'] / record[ 'plan_item__num'] if record['plan_item__num'] else 0 fc_calculate = (result_dict['ll_weight'] - result_dict['sj_weight'] ) * 100 / result_dict['sj_weight'] if result_dict[ 'sj_weight'] else 0 result_dict['bind_fu_cha'] = round(fc_calculate, 2) result_dict['bind_status'] = item['bind_status'] result_dict['truck_no'] = item['truck_no'] result_dict['truck_no_list'] = [ truck['bind_no'] for truck in truck_query ] if up_item_dict: if item['bind_status'] == ProductItem.BIND_DEAL: # 待处理 转换首件确认 if up_item_dict['steel_type_id'] != result_dict[ 'steel_type_id'] or up_item_dict[ 'diameter'] != result_dict[ 'diameter'] or up_item_dict[ 'fixed_length'] != result_dict[ 'fixed_length'] and up_item_dict[ 'gongyi'] != result_dict[ 'gongyi']: # 首件处理 print up_item_dict['item_id'], up_item_dict[ 'steel_type_id'], up_item_dict[ 'diameter'], up_item_dict[ 'fixed_length'], up_item_dict['gongyi'] print result_dict['item_id'], result_dict[ 'steel_type_id'], result_dict[ 'diameter'], result_dict[ 'fixed_length'], result_dict['gongyi'] result_dict[ 'bind_status'] = ProductItem.BIND_FIRST_CONFIRMED obj = ProductItem.objects.using(aim_org_id).get( pk=item['id'], is_active=True) obj.bind_status = ProductItem.BIND_FIRST_CONFIRMED obj.save() else: deal_rule = [{ 'diameter': 10, 'weight_m': 0.617, 'weight_9m': 5.553, 'kg_range_9m': [-4, 4], 'weight_12m': 7.404, 'kg_range_12m': [-5, 5], }] # 非首件处理 # 无异常 ProductItem.BIND_NORMAL # 异常待确认 ProductItem.BIND_CONFIRMED pass elif item['bind_status'] == ProductItem.BIND_NORMAL: # 正常 up_item_dict['item_id'] = item['id'] up_item_dict['steel_type_id'] = result_dict[ 'steel_type_id'] up_item_dict['diameter'] = result_dict['diameter'] up_item_dict['fixed_length'] = result_dict['fixed_length'] up_item_dict['gongyi'] = result_dict['gongyi'] # 处理 elif item['bind_status'] == ProductItem.BIND_CONFIRMED: # 异常待确认不覆盖 pass elif item['bind_status'] == ProductItem.BIND_FIRST_CONFIRMED: # 首件确认 up_item_dict['item_id'] = item['id'] up_item_dict['steel_type_id'] = result_dict[ 'steel_type_id'] up_item_dict['diameter'] = result_dict['diameter'] up_item_dict['fixed_length'] = result_dict['fixed_length'] up_item_dict['gongyi'] = result_dict['gongyi'] elif item['bind_status'] == ProductItem.BIND_ABNORMAL: # 支数异常不覆盖 pass else: return get_result( True, u'错误捆支数状态 < ProductItem#{} > need in {}'.format( item['id'], [tup[0] for tup in ProductItem.BIND_STATUS_CHOICE])) else: up_item_dict['item_id'] = item['id'] up_item_dict['steel_type_id'] = result_dict['steel_type_id'] up_item_dict['diameter'] = result_dict['diameter'] up_item_dict['fixed_length'] = result_dict['fixed_length'] up_item_dict['gongyi'] = result_dict['gongyi'] result_list.append(result_dict) return get_result(True, u'查询数据成功', page_obj_query(result_list, page_index, page_size))
def query_product_bind_result(request, org_id, aim_org_id, bind_no, truck_no, time_interval, steel_type_id, prod_thick, roll_no, shift_name, shift_flag, page_index, page_size, person): """ :param request: :param org_id: :param aim_org_id: :param bind_no: :param truck_no: :param time_interval: :param steel_type_id: :param prod_thick: :param roll_no: :param shift_name: :param shift_flag: :param page_index: :param page_size: :param person: :return: by:唐政 at:2021-1-08 """ if not time_interval[0] or not time_interval[1]: return get_result(False, u"请提供正确的时间区间") if time_interval[1] < time_interval[0]: return get_result(False, u"结束时间不能小于起始时间") if aim_org_id is None: aim_org_id = org_id record_query = ProductionRecord.objects.values( 'id', 'plan_item_id', 'plan_item__roll_no', 'plan_item__heat_no', 'plan_item__gongyi__name', 'plan_item__prod_thick', 'plan_item__weight', 'plan_item__num', 'plan_item__trade_no_id', 'plan_item__trade_no__name', ). \ filter( org_id=aim_org_id, create_time__gte=time_interval[0], create_time__lte=time_interval[1] + datetime.timedelta(days=1), is_active=True).order_by("-create_time") if steel_type_id: record_query = record_query.filter( plan_item__trade_no_id=steel_type_id) if prod_thick: record_query = record_query.filter(plan_item__prod_thick=prod_thick) if roll_no: record_query = record_query.filter(plan_item__roll_no=roll_no) result_list = [] dist_list = [] for i, record in enumerate(record_query): if record['plan_item__roll_no'] in dist_list: continue else: dist_list.append(record['plan_item__roll_no']) item_query = ProductItem.objects.values( 'id', 'bind_no', 'merge_no', 'heat_no', 'product_time', 'shift_name', 'shift_flag', 'num', 'diameter', 'fixed_length', 'status', 'shape_code', 'truck_no', 'bind_status'). \ using(aim_org_id).filter(is_active=True, org_id=aim_org_id, roll_no=record['plan_item__roll_no']) if bind_no: item_query = item_query.filter(bind_no=bind_no) if truck_no: item_query = item_query.filter(truck_no=truck_no) if shift_name: item_query = item_query.filter(shift_name=shift_name) if shift_flag: item_query = item_query.filter(shift_flag=shift_flag) record_item_query = ProductionRecordItem.objects.values( 'id', 'real_weight' ).using(aim_org_id). \ filter(org_id=aim_org_id, production_record_id=record['id'], is_active=True). \ exclude(conclusion=ProductionRecordItem.Conclusion_None, ) for item, record_item in zip(item_query, record_item_query): result_dict = {} truck_query = ProductItem.objects.values('bind_no').using( aim_org_id).filter(truck_no=item['truck_no'], is_active=True) result_dict['product_record_id'] = record['id'] result_dict['item_id'] = item['id'] result_dict['record_item_id'] = record_item['id'] result_dict['roll_no'] = record['plan_item__roll_no'] result_dict['bind_no'] = item['bind_no'] result_dict['merge_no'] = item['merge_no'] result_dict['product_time'] = item['product_time'] result_dict['shift_name'] = item['shift_name'] result_dict['shift_flag'] = item['shift_flag'] result_dict['num'] = item['num'] result_dict['steel_type_id'] = record['plan_item__trade_no_id'] result_dict['steel_type_name'] = record[ 'plan_item__trade_no__name'] result_dict['diameter'] = int(item['diameter']) result_dict['fixed_length'] = item['fixed_length'] result_dict['gongyi'] = record['plan_item__gongyi__name'] result_dict['sj_weight'] = record_item['real_weight'] result_dict['ll_weight'] = record['plan_item__weight'] / record[ 'plan_item__num'] if record['plan_item__num'] else 0 fc_calculate = (result_dict['ll_weight'] - result_dict['sj_weight'] ) * 100 / result_dict['sj_weight'] if result_dict[ 'sj_weight'] else 0 result_dict['bind_fu_cha'] = round(fc_calculate, 2) result_dict['bind_status'] = item['bind_status'] result_dict['truck_no'] = item['truck_no'] result_dict['truck_no_list'] = [ truck['bind_no'] for truck in truck_query ] result_list.append(result_dict) return get_result(True, u'查询数据成功', page_obj_query(result_list, page_index, page_size))