def saveUser(request): id = request.REQUEST.get('id', '') if id: user = User.objects.get(pk=id) else: user = User() user.set_password('111111') user.username = request.REQUEST.get('username', '') if not user.username or User.objects.filter( username=user.username).count() > 0: return getResult(False, u'用户名已经存在', None) is_active = request.REQUEST.get('isaction', '') if is_active: if is_active == 'true': user.is_active = True else: user.is_active = False is_staff = request.REQUEST.get('ismanager', '') if is_staff: if is_staff == 'true': user.is_staff = True else: user.is_staff = False user.first_name = request.REQUEST.get('truename', u'游客') if request.REQUEST.has_key('password'): user.set_password(request.REQUEST.get('password')) user.save() return getResult( True, '', { 'username': user.username, 'truename': user.first_name, 'ismanager': user.is_staff, 'isaction': user.is_active, 'id': user.pk })
def getPaperByKind(request): kind = request.REQUEST.get('kind', None) if kind: paperlist = [] query = Paper.objects.filter( Q(title__icontains=kind) | Q(flag__icontains=kind)).order_by('title') for p in query: paperlist.append({ "id": p.pk, 'title': p.title, 'kmkind': p.kmkind_id, 'flag': p.flag, 'content': p.content, 'right_ztdm': p.right_ztdm, 'is_pub': p.is_pub, 'guan_id': p.guan_id, 'time': p.time }) return getResult( True, '', { 'result': paperlist, 'limit': query.count(), 'start': 0, 'total': query.count() }) else: return getResult(False, u'请提供关键字', None)
def updateKJKMKind(request): pk = request.REQUEST.get('id', '') if pk: kindForm = KMKindForm(request.POST, instance=KMKind.objects.get(pk=pk)) else: kindForm = KMKindForm(request.POST) if not kindForm.is_valid(): msg = kindForm.json_error() return getResult(False, msg, None) kind = kindForm.save() # l=[] if not pk: k = json.load(open("%skjkm.json" % STATIC_ROOT)) for oldkm in k: try: # l.append({'kmbh':km.name.split(":")[0],'name':km.name.split(":")[1]}) km = KM() km.kind = kind km.kmbh = oldkm.get("kmbh", "") km.name = oldkm.get("name", "") km.save() except: pass return getResult(True, u'保存会计科目信息成功', kind.pk)
def getTicketByUserRule(request): ruleid = request.REQUEST.get('ruleid', '') if not ruleid: return getResult(True, '', []) kindlist = [] kindidlist = set() kinddict = {} tids = set() for r in Relation.objects.filter(rule=ruleid).order_by('id'): tids.add(r.ticket_id) for ticket in Ticket.objects.filter(id__in=tids).order_by('id'): ticketFather(ticket, kinddict, kindidlist) for kid in kindidlist: kind = kinddict.get(str(kid)) if not kind['fatherid']: kindlist.append(kind) else: kinddict[str(kind.get('fatherid'))]['children'].append(kind) for kind in kinddict.values(): if len(kind['children']) == 0: del kind['children'] l = [] for k in kinddict.values(): if not hasattr(k, 'children'): l.append(k) return getResult(True, '', {"kindlist": kindlist, "kindalllist": l})
def getKJZDByKM(request): ruleid = request.REQUEST.get('ruleid', '') ticketid = request.REQUEST.get('ticketid', '') businessid = request.REQUEST.get('businessid', '') if Relation.objects.filter( rule=ruleid, ticket=ticketid, business=businessid).count() != 0: r = Relation.objects.get(rule=ruleid, ticket=ticketid, business=businessid) kjzdlist = [] sflist = [] sstslist = [] for kjzd in r.kjzds.all(): kjzdlist.append(kjzd.name) for sf in r.sf.all(): sflist.append(sf.name) for ssts in r.ssts.all(): sstslist.append(ssts.name) return getResult( True, '', { 'kjzd': '\n'.join(kjzdlist), 'sf': '\n'.join(sflist), 'ssts': '\n'.join(sstslist), 'id': r.pk }) else: return getResult(True, '', {'kjzd': '', 'sf': '', 'ssts': ''})
def getPZ(request): subjectid = request.REQUEST.get('subjectid','') subject = Subject.objects.get(pk=subjectid) pz = PZ.objects.filter(subject=subject)[:1] result = {'fllist':[],'pzdesc':'',"kindid":subject.kmkind_id} if len(pz)>0: pz = pz[0] fllist=[] for fl in FL.objects.filter(pz=pz).order_by('id'): f={'kjkm':fl.kmmc_id, 'fx':fl.fx, 'id':fl.pk, 'zy':fl.zy} if fl.fx: f['jje'] = str(fl.num) if not fl.num: f['jje']=0 else: f['dje'] = str(fl.num) if not fl.num: f['dje']=0 fllist.append(f) result['fllist']=fllist result['pzdesc']= pz.desc result['pzid']=pz.pk # result['imgurl']= pz.getImg() return getResult(True,'',result) else: return getResult(True,'',result)
def getHistory(request): content_type_id = request.REQUEST.get('object_type', None) object_id = request.REQUEST.get('object_id', None) uid = request.REQUEST.get('uid', None) logquery = LogEntry.objects.all().order_by('-id') start = request.REQUEST.get('start', None) end = request.REQUEST.get('end', None) if start and end: with c: startdate = datetime.datetime.strptime(start, "%Y/%m/%d") enddate = datetime.datetime.strptime( end, "%Y/%m/%d") + datetime.timedelta(days=1) logquery = logquery.filter(action_time__lt=enddate, action_time__gte=startdate) elif content_type_id and object_id: logquery = logquery.filter(object_id=object_id) else: return getResult(False, u'请提供查询范围') if content_type_id: logquery = logquery.filter(content_type=content_type_id) if object_id: logquery = logquery.filter(object_id=object_id) if uid: logquery = logquery.filter(user=uid) l = [] for log in logquery: l.append(log2dict(log)) return getResult(True, u'获取到日志', l)
def updateKJKMKind(request): pk = request.REQUEST.get('id', '') if pk: kindForm = KMKindForm(request.POST, instance=KMKind.objects.get(pk=pk)) else: kindForm = KMKindForm(request.POST) if not kindForm.is_valid(): msg = kindForm.json_error() return getResult(False, msg, None) kind = kindForm.save() # l=[] if not pk: k = json.load(open("%skjkm.json"%STATIC_ROOT)) for oldkm in k: try: # l.append({'kmbh':km.name.split(":")[0],'name':km.name.split(":")[1]}) km = KM() km.kind = kind km.kmbh = oldkm.get("kmbh", "") km.name = oldkm.get("name", "") km.save() except: pass return getResult(True, u'保存会计科目信息成功', kind.pk)
def getHistory(request): content_type_id = request.REQUEST.get('object_type', None) object_id = request.REQUEST.get('object_id',None) uid = request.REQUEST.get('uid', None) logquery = LogEntry.objects.all().order_by('-id') start = request.REQUEST.get('start', None) end = request.REQUEST.get('end', None) if start and end: with c: startdate = datetime.datetime.strptime(start, "%Y/%m/%d") enddate = datetime.datetime.strptime(end, "%Y/%m/%d") + datetime.timedelta(days=1) logquery = logquery.filter(action_time__lt=enddate, action_time__gte=startdate) elif content_type_id and object_id: logquery = logquery.filter(object_id=object_id) else: return getResult(False,u'请提供查询范围') if content_type_id: logquery=logquery.filter(content_type=content_type_id) if object_id: logquery = logquery.filter(object_id=object_id) if uid: logquery = logquery.filter(user=uid) l = [] for log in logquery: l.append(log2dict(log)) return getResult(True,u'获取到日志',l)
def getTicketByUserRule(request): ruleid = request.REQUEST.get('ruleid','') if not ruleid: return getResult(True,'',[]) kindlist = [] kindidlist = set() kinddict = {} tids = set() for r in Relation.objects.filter(rule=ruleid).order_by('id'): tids.add(r.ticket_id) for ticket in Ticket.objects.filter(id__in=tids).order_by('id'): ticketFather(ticket, kinddict, kindidlist) for kid in kindidlist: kind = kinddict.get(str(kid)) if not kind['fatherid']: kindlist.append(kind) else: kinddict[str(kind.get('fatherid'))]['children'].append(kind) for kind in kinddict.values(): if len(kind['children']) == 0: del kind['children'] l=[] for k in kinddict.values(): if not hasattr(k,'children'): l.append(k) return getResult(True, '', {"kindlist":kindlist,"kindalllist":l})
def saveUser(request): id = request.REQUEST.get('id', '') if id: user = User.objects.get(pk=id) else: user = User() user.set_password('111111') user.username = request.REQUEST.get('username', '') if not user.username or User.objects.filter(username=user.username).count() > 0: return getResult(False, u'用户名已经存在', None) is_active = request.REQUEST.get('isaction', '') if is_active: if is_active == 'true': user.is_active = True else: user.is_active = False is_staff = request.REQUEST.get('ismanager', '') if is_staff: if is_staff == 'true': user.is_staff = True else: user.is_staff = False user.first_name = request.REQUEST.get('truename', u'游客') if request.REQUEST.has_key('password'): user.set_password(request.REQUEST.get('password')) user.save() return getResult(True, '', {'username': user.username, 'truename': user.first_name, 'ismanager': user.is_staff, 'isaction': user.is_active, 'id': user.pk})
def delKJKM(request): pk = request.REQUEST.get('id', '') kind = KM.objects.get(pk=pk) if 0 == kind.fl_set.count(): kind.fl_set.all().delete() kind.delete() return getResult(True, u'会计科目删除成功', None) return getResult(False, u'会计科目删除失败,还有题目使用此会计科目', None)
def delKJKM(request): pk = request.REQUEST.get('id', '') kind = KM.objects.get(pk=pk) if 0==kind.fl_set.count(): kind.fl_set.all().delete() kind.delete() return getResult(True, u'会计科目删除成功',None) return getResult(False, u'会计科目删除失败,还有题目使用此会计科目', None)
def delRule(request): id = request.REQUEST.get('id', '') if id: rule = Rule.objects.get(pk=id) rule.delete() else: getResult(False, u'行业不存在', None) return getResult(True, '', None)
def delOption(request): id = request.REQUEST.get('id', '') if id: kind = Option.objects.get(pk=id) kind.delete() else: getResult(False, u'选项不存在', None) return getResult(True, u'选项删除成功', id)
def delKjkmKind(request): pk = request.REQUEST.get('id', '') kind = KMKind.objects.get(pk=pk) if 0==kind.subject_set.count(): kind.subject_set.all().delete() kind.km_set.all().delete() kind.delete() return getResult(True, u'会计科目分类删除成功',None) return getResult(False, u'会计科目分类删除失败,还有题目使用此会计科目', None)
def getSubjectByPaper(request): pid = request.REQUEST.get('pid') if pid: paper = Paper.objects.get(pk=pid) subjects = MyEncoder.default(paper.subjects.order_by('papersubject')) return getResult(True, u'获取到试题成功', subjects) else: return getResult(False, u'获取试题失败,请提供试卷id', None)
def delKjkmKind(request): pk = request.REQUEST.get('id', '') kind = KMKind.objects.get(pk=pk) if 0 == kind.subject_set.count(): kind.subject_set.all().delete() kind.km_set.all().delete() kind.delete() return getResult(True, u'会计科目分类删除成功', None) return getResult(False, u'会计科目分类删除失败,还有题目使用此会计科目', None)
def delTicket(request): id = request.REQUEST.get('id', '') if id: taxTicket = Ticket.objects.get(pk=id) cache.delete('allticket') taxTicket.delete() else: getResult(False, u'票据不存在', None) return getResult(True, '', None)
def regUser(request): result = saveUser(request) if result.get('success'): auth_login(request, User.objects.get(pk=result.get('result').get('pk'))) if request.session.test_cookie_worked(): request.session.delete_test_cookie() return getResult(True, '', None) else: return getResult(False, '注册失败', None)
def getSubjectByKind(request): kind = request.REQUEST.get('kind', None) if kind: sl = [] query = Subject.objects.filter(Q(title__icontains=kind)|Q(flag__icontains=kind)).order_by('title') for subject in query: sl.append(MyEncoder.default(subject)) return getResult(True, '', {'result': sl, 'limit': query.count(), 'start': 0, 'total': query.count()}) else: return getResult(False, u'请提供关键字', None)
def getPaper(request): ''' 根据 id 获取一张试卷的完整信息,包括题目 ''' id = request.REQUEST.get('id', '') if id: paper = Paper.objects.get(pk=id) pdict = MyEncoder.default(paper) return getResult(True, u'获取试卷成功', pdict) else: return getResult(False, u'试卷不存在', id)
def getGuan(request): ''' 根据 id 获取一张关卡的完整信息,包括试卷 ''' id = request.REQUEST.get('id', '') if id: guan = Guan.objects.get(pk=id) pdict = MyEncoder.default(guan) return getResult(True, u'获取关卡成功', pdict) else: return getResult(False, u'关卡不存在', id)
def getPaperByKind(request): kind = request.REQUEST.get('kind', None) if kind: paperlist = [] query = Paper.objects.filter(Q(title__icontains=kind) | Q(flag__icontains=kind)).order_by('title') for p in query: paperlist.append({"id": p.pk, 'title': p.title, 'kmkind': p.kmkind_id, 'flag': p.flag, 'content': p.content, 'right_ztdm': p.right_ztdm, 'is_pub': p.is_pub, 'guan_id': p.guan_id, 'time': p.time}) return getResult(True, '', {'result': paperlist, 'limit': query.count(), 'start': 0, 'total': query.count()}) else: return getResult(False, u'请提供关键字', None)
def deleteRule(request): ruleid = request.REQUEST.get('ruleid') rule = TaxRule.objects.get(pk=ruleid) try: if not rule.subject_set.exists(): rule.delete() return getResult(True, u'删除成功', None) else: raise 'error' except Exception, e: return getResult(False, u'规则被使用,无法删除', None)
def delPaper(request): ''' 删除一个试卷,设置为不公开 ''' id = request.REQUEST.get('id', '') if id: paper = Paper.objects.get(pk=id) paper.delete() else: return getResult(False, u'试卷不存在', id) return getResult(True, '', id)
def getSubjectById(request): id = request.REQUEST.get('id', '') if id: kind = Subject.objects.get(pk=id) k=MyEncoder.default(kind) k ['options']= [] for o in Option.objects.filter(subject=kind): k['options'].append(MyEncoder.default(o)) return getResult(True, u'获取考题成功', k) else: return getResult(False, u'考题不存在', None)
def saveTuZhang(request): name = request.REQUEST.get('name', '') id = request.REQUEST.get('id', '') if id: rule = TaxTuZhang.objects.get(pk=id) else: if TaxTuZhang.objects.filter(name=name).count() > 0: return getResult(False, u'图章模板名称不能重复', None) rule = TaxTuZhang() rule.name = name rule.save() return getResult(True, u'保存图章模板成功', {'id': rule.pk, 'name': rule.name})
def getPaperByGuan(request): ''' 获取关卡下的所有试卷 ''' pid = request.REQUEST.get('pid') if pid: guan = Guan.objects.get(pk=pid) subjects = MyEncoder.default(guan.paper_set.all()) return getResult(True, u'获取到关卡成功', subjects) else: return getResult(False, u'获取关卡失败,请提供关卡id', None)
def delImg(request): imgurl = request.REQUEST.get('imgurl','') if imgurl: if ImageInfo.objects.filter(img__iendswith = imgurl.split('/')[-1]).count()==0: return getResult(True,'') else: for img in ImageInfo.objects.filter(img__iendswith = imgurl.split('/')[-1]): cache.delete('all%s'%img.modelType) img.img.delete() img.delete() return getResult(True,'') return getResult(False,'')
def delSubject(request): if not request.user.is_staff: return getResult(False, u'权限不足', None) id = request.REQUEST.get('id', '') if id: kind = Subject.objects.get(pk=id) if kind.paper_set.exists(): return getResult(False, u'考题被使用无法删除', None) kind.delete() else: return getResult(False, u'考题不存在', None) return getResult(True, u'考题删除成功', id)
def imgUploaded(request): img = request.FILES.get('file','') if img: imginfo=ImageInfo() imginfo.img = img imginfo.index = 0 imginfo.modelId = request.REQUEST.get('mid') imginfo.modelType = request.REQUEST.get('mtype') imginfo.save() cache.delete('all%s'%imginfo.modelType) return getResult(True,u'上传成功',imginfo.img.url) else: return getResult(False,'',None)
def getOptionBySubject(request): sid = request.REQUEST.get('sid',None) if sid: subject = Subject.objects.get(pk=sid) options = MyEncoder.default(subject.option_set.all()) if subject.rule_id: templateid = subject.rule.taxtemplate_id ruleid = subject.rule_id else: templateid =None ruleid = None return getResult(True,u'获取试题信息',{'sid':sid,'options':options,'templateid':templateid,'ruleid':ruleid}) else: return getResult(False,u'获取试题信息失败,需要试题id',None)
def delRelation(request): ruleid = request.REQUEST.get('ruleid', '') ticketid = request.REQUEST.get('ticketid', '') businessid = request.REQUEST.get('businessid', '') kjzdstr = request.REQUEST.get('kjzd', '') sfstr = request.REQUEST.get('sf', '') if Relation.objects.filter(rule=ruleid, ticket=ticketid, business=businessid).count() != 0: r = Relation.objects.get(rule=ruleid, ticket=ticketid, business=businessid) r.delete() return getResult(True, u'删除成功') else: return getResult(True, u'关系不存在')
def delImg(request): imgurl = request.REQUEST.get('imgurl', '') if imgurl: if ImageInfo.objects.filter( img__iendswith=imgurl.split('/')[-1]).count() == 0: return getResult(True, '') else: for img in ImageInfo.objects.filter( img__iendswith=imgurl.split('/')[-1]): cache.delete('all%s' % img.modelType) img.img.delete() img.delete() return getResult(True, '') return getResult(False, '')
def imgUploaded(request): img = request.FILES.get('file', '') if img: imginfo = ImageInfo() imginfo.img = img imginfo.index = 0 imginfo.modelId = request.REQUEST.get('mid') imginfo.modelType = request.REQUEST.get('mtype') imginfo.save() cache.delete('all%s' % imginfo.modelType) return getResult(True, u'上传成功', imginfo.img.url) else: return getResult(False, '', None)
def updateGuan(request): ''' 修改一个关卡 ''' pk = request.REQUEST.get('id', '') if pk: guanform = GuanForm(request.POST, instance=Guan.objects.get(pk=pk)) else: guanform = GuanForm(request.POST) if not guanform.is_valid(): msg = guanform.json_error() return getResult(False, msg, None) guan = guanform.save() return getResult(True, u'保存关卡信息成功', guan.pk)
def delGuan(request): ''' 删除一个关卡,设置为不公开 ''' id = request.REQUEST.get('id', '') if id: guan = Guan.objects.get(pk=id) for paper in guan.paper_set.all(): paper.guan = None paper.save() guan.delete() else: return getResult(False, u'关卡不存在', id) return getResult(True, '', id)
def deleteTemplate(request): if not request.user.is_staff: return getResult(False, u'权限不足', None) id = request.REQUEST.get('templateid') t = TaxTemplate.objects.get(pk=id) try: if not t.taxrule_set.exists(): if t.img: t.img.delete() t.delete() return getResult(True, u'删除成功', None) else: raise 'error' except Exception, e: return getResult(False, u'模板被使用,无法删除', None)
def getAllPaper(request): ''' 获取所有试卷 ''' paperlist = [] is_pub = request.REQUEST.get('is_pub', '') title = request.REQUEST.get('title', '') all = request.REQUEST.get('all', '') limit = int(request.REQUEST.get('limit', '40')) start = int(request.REQUEST.get('start', '0')) paperquery = Paper.objects.all().order_by('title') if is_pub: if is_pub == 'true': paperquery = paperquery.filter(is_pub=True) else: paperquery = paperquery.filter(is_pub=False) if title: paperquery = paperquery.filter(title__icontains=title) totalnum = paperquery.count() if not all: paperquery = paperquery[start:start + limit] for p in paperquery: paperlist.append({"id": p.pk, 'title': p.title, 'kmkind': p.kmkind_id, 'flag': p.flag, 'content': p.content, 'right_ztdm': p.right_ztdm, 'is_pub': p.is_pub, 'guan_id': p.guan_id, 'time': p.time}) return getResult(True, '', {'result': paperlist, 'limit': limit, 'start': start, 'total': totalnum})
def getAllBusiness(request): cachename = 'allbussiness' taxkindstr = cache.get(cachename) if taxkindstr: return HttpResponse(taxkindstr) kindlist = [] kindidlist = [] kinddict = {} for business in Business.objects.all().order_by('num'): kinddict['%s' % business.pk] = {'id': business.pk, 'num': business.num, 'ywbh': business.ywbh(), 'businessname': '[%s] %s' % (business.ywbh(), business.name) , 'type': 'business', 'fatherid': business.fatherBusiness_id, 'name': business.name, 'children': []} # pzlist = [] # for pz in business.pz_set.filter(is_active=True).all(): # pzlist.append(pz.pk) # kinddict['%s' % business.pk]['pzlist']=pzlist kindidlist.append(business.pk) for kid in kindidlist: kind = kinddict.get(str(kid)) if not kind['fatherid']: kindlist.append(kind) else: kinddict[str(kind.get('fatherid'))]['children'].append(kind) for kind in kinddict.values(): if len(kind['children']) == 0: del kind['children'] return getResult(True, '', kindlist, cachename=cachename)
def getAllBusiness(request): cachename = 'allbussiness' taxkindstr = cache.get(cachename) if taxkindstr: return HttpResponse(taxkindstr) kindlist = [] kindidlist = [] kinddict = {} for business in Business.objects.all().order_by('num'): kinddict['%s' % business.pk] = { 'id': business.pk, 'num': business.num, 'ywbh': business.ywbh(), 'businessname': '[%s] %s' % (business.ywbh(), business.name), 'type': 'business', 'fatherid': business.fatherBusiness_id, 'name': business.name, 'children': [] } # pzlist = [] # for pz in business.pz_set.filter(is_active=True).all(): # pzlist.append(pz.pk) # kinddict['%s' % business.pk]['pzlist']=pzlist kindidlist.append(business.pk) for kid in kindidlist: kind = kinddict.get(str(kid)) if not kind['fatherid']: kindlist.append(kind) else: kinddict[str(kind.get('fatherid'))]['children'].append(kind) for kind in kinddict.values(): if len(kind['children']) == 0: del kind['children'] return getResult(True, '', kindlist, cachename=cachename)