import json import math import uuid from django.contrib.auth.models import User from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage from django.http import HttpResponse from django.shortcuts import render # Create your views here. # 成员管理 from django.views.decorators.csrf import csrf_exempt from dashboard.models import UserProfile, Task, Jurisdiction, Findings, Sampletrees, Samplebush, Sampleherbal, \ Simplesoil, Barcodes, Weather, Landuse, Investigators, Sampleinterlayerplants, Simplebiomass, Community, \ Verificationcode, Special, Area_code_2020, Edificator, Dominantspecies, Associated, Dronephotos, Scenephoto, \ Accessory @csrf_exempt def jurisdiction(request): if request.method == 'POST': obj = json.loads(request.body.decode('utf-8')) username = obj['username'] if username is not None: user = User.objects.get(username=username) if user.is_superuser is True: jurisdiction = 0 else: jurisdiction = UserProfile.objects.get(user_id=user.id).jurisdiction.jurisdiction return HttpResponse(json.dumps({ "status": "1", "jurisdiction": jurisdiction })) return HttpResponse("未获取到权限信息") @csrf_exempt def member_management(request): obj = json.loads(request.body.decode('utf-8')) username = obj['username'] login_user = User.objects.get(username=username) userjurisdiction = UserProfile.objects.get(user_id=login_user.id).jurisdiction.jurisdiction task_id = UserProfile.objects.get(user_id=login_user.id).task_id special_id = UserProfile.objects.get(user_id=login_user.id).special_id jurisdiction = None user = None if login_user.is_superuser is True: jurisdiction = 0 user = User.objects.all().order_by('-date_joined') elif userjurisdiction == 0: jurisdiction = 0 user = User.objects.all().order_by('-date_joined') elif userjurisdiction == 1: jurisdiction = 1 user = User.objects.filter(userprofile__task_id=task_id).order_by('-date_joined') elif userjurisdiction == 2: jurisdiction = 2 elif jurisdiction == 3: jurisdiction = 3 user = User.objects.filter(userprofile__special_id=special_id).order_by('-date_joined') elif userjurisdiction == 4: jurisdiction = 4 results = [] paginator = Paginator(user, 10) page = int(request.GET.get('page', 1)) try: user_p = paginator.page(page) except PageNotAnInteger: user_p = paginator.page(1) except EmptyPage: user_p = paginator.page(paginator.num_pages) for u in user_p: o = dict() o['id'] = str(u.id) o['name'] = u.username o['phone'] = u.first_name o['email'] = u.email o['organization'] = u.last_name try: o['task'] = UserProfile.objects.get(user_id=u.id).task.taskname o['jurisdiction'] = UserProfile.objects.get(user_id=u.id).jurisdiction.explain except: print("666") results.append(o) return HttpResponse(json.dumps({ "status": "1", "user": results, "count": len(user), "page_count": math.ceil(len(user) / 10), "jurisdiction": '0' })) @csrf_exempt def member_management_update(request, pk): user = User.objects.get(id=pk) if request.method == 'POST': obj = json.loads(request.body.decode('utf-8')) username = obj['username'] phone = obj['phone'] email = obj['email'] organization = obj['organization'] jurisdiction = obj['jurisdiction'] j_n = Jurisdiction.objects.get(explain=jurisdiction) name = obj['name'] User.objects.filter(id=pk).update(username=username, first_name=phone, email=email, last_name=organization) UserProfile.objects.filter(user_id=user.id).update(jurisdiction=j_n.id, name=name) return HttpResponse(json.dumps({ "status": "1", "message": "修改成功" })) else: manageusername = request.GET.get('manageusername') u = User.objects.get(username=manageusername) profile = UserProfile.objects.get(user_id=u.id) j = Jurisdiction.objects.get(id=profile.jurisdiction_id) task = Task.objects.all() if u.is_superuser is True or j.jurisdiction == 0: jurisdiction = Jurisdiction.objects.filter(jurisdiction__in=[1, 2, 3, 4]) else: jurisdiction = Jurisdiction.objects.filter(jurisdiction__in=[2, 3, 4]) resultstask = [] resultsjurisdiction = [] for t in task: o = dict() o['id'] = str(t.id) o['taskname'] = t.taskname o['taskcode'] = t.taskcode o['remark'] = t.remark resultstask.append(o) for j in jurisdiction: o = dict() o['id'] = str(j.id) o['jurisdiction'] = j.jurisdiction o['explain'] = j.explain resultsjurisdiction.append(o) usertask = None if user.is_superuser is True: j = str(Jurisdiction.objects.get(explain='系统管理员').explain) userProfile = UserProfile.objects.all() for userprofile in userProfile: if userprofile.user_id == user.id: usertask = str(UserProfile.objects.get(user_id=user.id).task) else: j = str(UserProfile.objects.get(user_id=user.id).jurisdiction.explain) # usertask = str(UserProfile.objects.get(user_id=user.id).task.id) return HttpResponse(json.dumps({ "status": "1", "username": user.username, "phone": user.first_name, "email": user.email, "organization": user.last_name, "jurisdiction": j, "name": UserProfile.objects.get(user_id=user.id).name, # "task": usertask, "resultstask": resultstask, "resultsjurisdiction": resultsjurisdiction, })) def member_management_delete(request, pk): user = User.objects.get(id=pk) userProfile = UserProfile.objects.get(user_id=pk) user.delete() userProfile.delete() return HttpResponse(json.dumps({ "status": "1", "message": "删除成功" })) @csrf_exempt def member_management_add(request): flag = False if request.method == 'POST': obj = json.loads(request.body.decode('utf-8')) username = obj['username'] email = obj['email'] password = obj['password'] confirm_password = obj['confirm_password'] login_username = obj['login_username'] login_user = User.objects.get(username=login_username) phone = obj['phone'] organization = obj['organization'] jurisdiction = obj['jurisdiction'] name = obj['name'] task_id = UserProfile.objects.get(user_id=login_user.id).task_id if password is not None and confirm_password is not None: if password == confirm_password: flag = True else: return HttpResponse('{"status":"0","message":"两次输入的密码不一致,请重新输入"}') filter_result = User.objects.filter(email=email) filter_result_username = User.objects.filter(username=username) if len(filter_result) > 0 or len(filter_result_username) > 0: return HttpResponse('{"status":"0","message":"用户已存在"}') if username is not None and password is not None and confirm_password is not None and email is not None and flag: user = User.objects.create_user(username, email, password) user.first_name = phone user.last_name = organization user.is_active = True user.is_staff = True user.save() userprofile = UserProfile(user_id=user.id, jurisdiction_id=jurisdiction, name=name, task_id=task_id) userprofile.save() verificationcode = Verificationcode(user_id=user.id, verificationcode='0000') verificationcode.save() return HttpResponse('{"status":"1","message":"添加成功"}') else: manageusername = request.GET.get('manageusername') u = User.objects.get(username=manageusername) profile = UserProfile.objects.get(user_id=u.id) j = Jurisdiction.objects.get(id=profile.jurisdiction_id) task = Task.objects.all() if u.is_superuser is True or j.jurisdiction == 0: jurisdiction = Jurisdiction.objects.filter(jurisdiction__in=[1, 2, 3, 4]) else: jurisdiction = Jurisdiction.objects.filter(jurisdiction__in=[2, 3, 4]) resultstask = [] resultsjurisdiction = [] for t in task: o = dict() o['id'] = str(t.id) o['organization'] = t.organization o['taskname'] = t.taskname o['taskcode'] = t.taskcode o['remark'] = t.remark resultstask.append(o) for j in jurisdiction: o = dict() o['id'] = str(j.id) o['jurisdiction'] = j.jurisdiction o['explain'] = j.explain resultsjurisdiction.append(o) return HttpResponse(json.dumps({ "status": "1", "resultstask": resultstask, "resultsjurisdiction": resultsjurisdiction, })) return HttpResponse('{"status":"0","message":"请使用post请求"}') # 课题组管理 @csrf_exempt def task_management(request): obj = json.loads(request.body.decode('utf-8')) username = obj['username'] user = User.objects.get(username=username) task = None jurisdiction = None userjurisdiction = UserProfile.objects.get(user_id=user.id).jurisdiction.jurisdiction task_id = UserProfile.objects.get(user_id=user.id).task_id if user.is_superuser is True: jurisdiction = 0 task = Task.objects.all().order_by('-created') elif userjurisdiction == 0: jurisdiction = 0 task = Task.objects.all().order_by('-created') elif userjurisdiction == 1: jurisdiction = 1 task = Task.objects.filter(id=task_id).order_by('-created') elif userjurisdiction == 3: jurisdiction = 3 task = Task.objects.filter(id=task_id).order_by('-created') results = [] for t in task: o = dict() o['id'] = str(t.id) o['taskname'] = t.taskname o['taskcode'] = t.taskcode o['organization'] = t.organization o['remark'] = t.remark results.append(o) return HttpResponse(json.dumps({ "status": "1", "task": results, "jurisdiction": jurisdiction })) def task_management_delete(request, pk): task = Task.objects.get(id=pk) task.delete() return HttpResponse(json.dumps({ "status": "1", "message": "删除成功" })) @csrf_exempt def task_management_update(request, pk): if request.method == 'POST': obj = json.loads(request.body.decode('utf-8')) taskname = obj['taskname'] taskcode = obj['taskcode'] organization = obj['organization'] principal = obj['principal'] remark = obj['remark'] Task.objects.filter(id=pk).update(taskname=taskname, taskcode=taskcode, organization=organization, remark=remark, principal_id=principal) return HttpResponse(json.dumps({ "status": "1", "message": "修改成功" })) else: task = Task.objects.get(id=pk) user = User.objects.all() results = [] for u in user: o = {} o['username'] = u.username o['id'] = str(u.id) results.append(o) return HttpResponse(json.dumps({ "status": "1", "id": str(task.id), "taskname": task.taskname, "taskcode": task.taskcode, "organization": task.organization, "remark": task.remark, "user": results })) @csrf_exempt def task_management_add(request): if request.method == 'POST': obj = json.loads(request.body.decode('utf-8')) taskname = obj['taskname'] taskcode = obj['taskcode'] organization = obj['organization'] remark = obj['remark'] principal = obj['principal'] task = Task(taskname=taskname, taskcode=taskcode, organization=organization, remark=remark, principal_id=principal) task.save() return HttpResponse(json.dumps({ "status": "1", "message": "添加成功" })) else: user = User.objects.all() results = [] for u in user: o = {} o['username'] = u.username o['id'] = u.id results.append(o) return HttpResponse(json.dumps({ "status": "1", "user": results })) # 调查点 def findings(request): findings = None username = request.GET.get('username') user = User.objects.get(username=username) task_id = UserProfile.objects.get(user_id=user.id).task_id special_id = UserProfile.objects.get(user_id=user.id).special_id if user.is_superuser is True: findings = Findings.objects.all().order_by('-created') elif user.userprofile.jurisdiction.jurisdiction == 0: findings = Findings.objects.all().order_by('-created') elif user.userprofile.jurisdiction.jurisdiction == 1: print(str(task_id) + "111111111111111111111111111") findings = Findings.objects.filter(task_id=task_id).order_by('-created') elif user.userprofile.jurisdiction.jurisdiction == 3 or user.userprofile.jurisdiction.jurisdiction == 2: findings = Findings.objects.filter(special_id=special_id).order_by('-created') results = [] paginator = Paginator(findings, 10) page = int(request.GET.get('page', 1)) try: findings_p = paginator.page(page) except PageNotAnInteger: findings_p = paginator.page(1) except EmptyPage: findings_p = paginator.page(paginator.num_pages) for f in findings_p: count_list = [] sampletrees = Sampletrees.objects.filter(findings_id=f.id).count() samplebush = Samplebush.objects.filter(findings_id=f.id).count() sampleherbal = Sampleherbal.objects.filter(findings_id=f.id).count() simplesoil = Simplesoil.objects.filter(findings_id=f.id).count() weather = Weather.objects.filter(findings_id=f.id).count() landuse = Landuse.objects.filter(findings_id=f.id).count() dronephotos = Dronephotos.objects.filter(findings_id=f.id).count() scenephoto = Scenephoto.objects.filter(findings_id=f.id).count() accessory = Accessory.objects.filter(findings_id=f.id).count() count_list.append(sampletrees) count_list.append(samplebush) count_list.append(sampleherbal) count_list.append(simplesoil) count_list.append(weather) count_list.append(landuse) count_list.append(dronephotos) count_list.append(scenephoto) print(count_list) index = 8 - int(count_list.count(0)) o = dict() o['id'] = str(f.id) o['findingscode'] = f.findingscode o['resultssampletrees'] = sampletrees o['resultssamplebush'] = samplebush o['resultssampleherbal'] = sampleherbal o['resultssimplesoil'] = simplesoil o['resultsweather'] = weather o['resultslanduse'] = landuse o['resultsdronephotos'] = dronephotos o['resultsscenephoto'] = scenephoto o['resultsaccessory'] = accessory o['index'] = str(index) + '/8' results.append(o) return HttpResponse(json.dumps({ "status": "1", "findings": results, "count": len(findings), "page_count": math.ceil(len(findings) / 10), })) def findings_0(request): findings = None username = request.GET.get('username') user = User.objects.get(username=username) task_id = UserProfile.objects.get(user_id=user.id).task_id special_id = UserProfile.objects.get(user_id=user.id).special_id if user.is_superuser is True: findings = Findings.objects.all().order_by('findingscode') elif user.userprofile.jurisdiction.jurisdiction == 0: findings = Findings.objects.all().order_by('findingscode') elif user.userprofile.jurisdiction.jurisdiction == 1: print(str(task_id) + "111111111111111111111111111") findings = Findings.objects.filter(task_id=task_id).order_by('findingscode') elif user.userprofile.jurisdiction.jurisdiction == 3 or user.userprofile.jurisdiction.jurisdiction == 2: findings = Findings.objects.filter(special_id=special_id).order_by('findingscode') results = [] paginator = Paginator(findings, 10) page = int(request.GET.get('page', 1)) try: findings_p = paginator.page(page) except PageNotAnInteger: findings_p = paginator.page(1) except EmptyPage: findings_p = paginator.page(paginator.num_pages) for f in findings_p: print(f.findingscode) count_list = [] sampletrees = Sampletrees.objects.filter(findings_id=f.id).count() samplebush = Samplebush.objects.filter(findings_id=f.id).count() sampleherbal = Sampleherbal.objects.filter(findings_id=f.id).count() simplesoil = Simplesoil.objects.filter(findings_id=f.id).count() weather = Weather.objects.filter(findings_id=f.id).count() landuse = Landuse.objects.filter(findings_id=f.id).count() dronephotos = Dronephotos.objects.filter(findings_id=f.id).count() scenephoto = Scenephoto.objects.filter(findings_id=f.id).count() accessory = Accessory.objects.filter(findings_id=f.id).count() count_list.append(sampletrees) count_list.append(samplebush) count_list.append(sampleherbal) count_list.append(simplesoil) count_list.append(weather) count_list.append(landuse) count_list.append(dronephotos) count_list.append(scenephoto) print(count_list) index = 8 - int(count_list.count(0)) o = dict() o['id'] = str(f.id) o['findingscode'] = f.findingscode o['resultssampletrees'] = sampletrees o['resultssamplebush'] = samplebush o['resultssampleherbal'] = sampleherbal o['resultssimplesoil'] = simplesoil o['resultsweather'] = weather o['resultslanduse'] = landuse o['resultsdronephotos'] = dronephotos o['resultsscenephoto'] = scenephoto o['resultsaccessory'] = accessory o['index'] = str(index) + '/8' results.append(o) return HttpResponse(json.dumps({ "status": "1", "findings": results, "count": len(findings), "page_count": math.ceil(len(findings) / 10), })) def findings_1(request): findings = None username = request.GET.get('username') user = User.objects.get(username=username) task_id = UserProfile.objects.get(user_id=user.id).task_id special_id = UserProfile.objects.get(user_id=user.id).special_id if user.is_superuser is True: findings = Findings.objects.all().order_by('-findingscode') elif user.userprofile.jurisdiction.jurisdiction == 0: findings = Findings.objects.all().order_by('-findingscode') elif user.userprofile.jurisdiction.jurisdiction == 1: print(str(task_id) + "111111111111111111111111111") findings = Findings.objects.filter(task_id=task_id).order_by('-findingscode') elif user.userprofile.jurisdiction.jurisdiction == 3 or user.userprofile.jurisdiction.jurisdiction == 2: findings = Findings.objects.filter(special_id=special_id).order_by('-findingscode') results = [] paginator = Paginator(findings, 10) page = int(request.GET.get('page', 1)) try: findings_p = paginator.page(page) except PageNotAnInteger: findings_p = paginator.page(1) except EmptyPage: findings_p = paginator.page(paginator.num_pages) for f in findings_p: print(666) count_list = [] sampletrees = Sampletrees.objects.filter(findings_id=f.id).count() samplebush = Samplebush.objects.filter(findings_id=f.id).count() sampleherbal = Sampleherbal.objects.filter(findings_id=f.id).count() simplesoil = Simplesoil.objects.filter(findings_id=f.id).count() weather = Weather.objects.filter(findings_id=f.id).count() landuse = Landuse.objects.filter(findings_id=f.id).count() dronephotos = Dronephotos.objects.filter(findings_id=f.id).count() scenephoto = Scenephoto.objects.filter(findings_id=f.id).count() accessory = Accessory.objects.filter(findings_id=f.id).count() count_list.append(sampletrees) count_list.append(samplebush) count_list.append(sampleherbal) count_list.append(simplesoil) count_list.append(weather) count_list.append(landuse) count_list.append(dronephotos) count_list.append(scenephoto) print(count_list) index = 8 - int(count_list.count(0)) o = dict() o['id'] = str(f.id) o['findingscode'] = f.findingscode o['resultssampletrees'] = sampletrees o['resultssamplebush'] = samplebush o['resultssampleherbal'] = sampleherbal o['resultssimplesoil'] = simplesoil o['resultsweather'] = weather o['resultslanduse'] = landuse o['resultsdronephotos'] = dronephotos o['resultsscenephoto'] = scenephoto o['resultsaccessory'] = accessory o['index'] = str(index) + '/8' results.append(o) return HttpResponse(json.dumps({ "status": "1", "findings": results, "count": len(findings), "page_count": math.ceil(len(findings) / 10), })) @csrf_exempt def findings_add(request): if request.method == 'POST': obj = json.loads(request.body.decode('utf-8')) form = obj['form'] if form['special'] == '0': return HttpResponse(json.dumps({ "status": "0", "message": "专题组不能为空" })) investigators = form['investigators'] if len(investigators) == 0: return HttpResponse(json.dumps({ "status": "0", "message": "请选择调查者" })) if len(obj['provincs']) == 0: return HttpResponse(json.dumps({ "status": "0", "message": "请选择省" })) if len(obj['citis']) == 0: return HttpResponse(json.dumps({ "status": "0", "message": "请选择市" })) provincs = Area_code_2020.objects.get(code=obj['provincs']).name citis = Area_code_2020.objects.get(code=obj['citis']).name if len(obj['districts']) == 0: districts = '' else: districts = Area_code_2020.objects.get(code=obj['districts']).name if len(obj['countries']) == 0: countries = '' else: countries = Area_code_2020.objects.get(code=obj['countries']).name if len(obj['villages']) == 0: villages = '' else: villages = Area_code_2020.objects.get(code=obj['villages']).name # provincs = obj['provincs'] # print(provincs) arrit = obj['arrit'] arredificator = obj['arredificator'] arrassociated = obj['arrassociated'] findingscode = form['findingscode'] if len(findingscode) == 0: return HttpResponse(json.dumps({ "status": "0", "message": "请输入调查点编号" })) results = Findings.objects.filter(findingscode=findingscode) if len(results) > 0: return HttpResponse(json.dumps({ "status": "0", "message": "调查点编号重复" })) if len(form['vegetationsubtypes']) == 0: return HttpResponse(json.dumps({ "status": "0", "message": "植被亚型不能为空" })) if len(form['formationgroup']) == 0: return HttpResponse(json.dumps({ "status": "0", "message": "群系组不能为空" })) if len(form['formation']) == 0: return HttpResponse(json.dumps({ "status": "0", "message": "群落不能为空" })) site = str(provincs + '-' + citis + '-' + districts + '-' + countries + '-' + villages) findings = None if len(form['lat']) != 0 and len(form['lon']) != 0: findings = Findings(findingscode=findingscode, investigators_id=investigators, date=form['date'], site=site, locationdescription=form['locationdescription'], terrain=form['terrain'], elevation=form['elevation'], gradient=form['gradient'], exposure=form['exposure'], river=form['river'], riverlength=form['riverlength'], riverwidth=form['riverwidth'], riverdeep=form['riverdeep'], rivershape=form['rivershape'], lon=form['lon'], lat=form['lat'], vegetationtypegroups=form['vegetationtypegroups'], vegetation=form['vegetation'], vegetationsubtypes=form['vegetationsubtypes'], formationgroup=form['formationgroup'], formation=form['formation'], task_id=form['task'], special_id=form['special'] ) for r in arredificator: if r['edificatorchinese'] == '' or r['edificatorlatin'] == '': return HttpResponse(json.dumps({ "status": "0", "message": "建群种不能为空" })) for t in arrit: if t['dominantspecieschinese'] == '' or t['dominantspecieslatin'] == '': return HttpResponse(json.dumps({ "status": "0", "message": "优势种不能为空" })) findings.save() elif len(form['d1']) != 0 and len(form['f1']) != 0 and len(form['m1']) != 0 and len(form['d2']) != 0 and len( form['f2']) != 0 and len(form['m2']) != 0: d1 = form['d1'] f1 = form['f1'] m1 = form['m1'] lon = int(d1) + float(int(f1) / 60) + float(int(m1) / 3600) d2 = form['d2'] f2 = form['f2'] m2 = form['m2'] lat = int(d2) + float(int(f2) / 60) + float(int(m2) / 3600) findings = Findings(findingscode=form['findingscode'], investigators_id=form['investigators'], date=form['date'], site=site, locationdescription=form['locationdescription'], terrain=form['terrain'], elevation=form['elevation'], gradient=form['gradient'], exposure=form['exposure'], river=form['river'], riverlength=form['riverlength'], riverwidth=form['riverwidth'], riverdeep=form['riverdeep'], rivershape=form['rivershape'], lon=lon, lat=lat, vegetationtypegroups=form['vegetationtypegroups'], vegetation=form['vegetation'], vegetationsubtypes=form['vegetationsubtypes'], formationgroup=form['formationgroup'], formation=form['formation'], task_id=form['task'], special_id=form['special']) for r in arredificator: if r['edificatorchinese'] == '' or r['edificatorlatin'] == '': return HttpResponse(json.dumps({ "status": "0", "message": "建群种不能为空" })) for t in arrit: if t['dominantspecieschinese'] == '' or t['dominantspecieslatin'] == '': return HttpResponse(json.dumps({ "status": "0", "message": "优势种不能为空" })) findings.save() else: return HttpResponse(json.dumps({ "status": "0", "message": "请输入地理位置信息" })) for r in arredificator: if r['edificatorchinese'] != '' or r['edificatorlatin'] != '': edificator = Edificator(edificatorchinese=r['edificatorchinese'], edificatorlatin=r['edificatorlatin'], findings_id=findings.id) edificator.save() else: return HttpResponse(json.dumps({ "status": "0", "message": "建群种不能为空" })) for t in arrit: if t['dominantspecieschinese'] != '' or t['dominantspecieslatin'] != '': dominantspecies = Dominantspecies(dominantspecieschinese=t['dominantspecieschinese'], dominantspecieslatin=t['dominantspecieslatin'], findings_id=findings.id) dominantspecies.save() else: return HttpResponse(json.dumps({ "status": "0", "message": "优势种不能为空" })) for d in arrassociated: associated = Associated(associatedchinese=d['associatedchinese'], associatedlatin=d['associatedlatin'], findings_id=findings.id) associated.save() return HttpResponse(json.dumps({ "status": "1", "message": "添加成功" })) else: username = request.GET.get('username') task = Task.objects.get(userprofile__user__username=username) investigators = Investigators.objects.all() results = [] for i in investigators: o = dict() o['name'] = i.name o['phone'] = i.phone o['email'] = i.email o['organization'] = i.organization o['remark'] = i.remark o['id'] = str(i.id) results.append(o) return HttpResponse(json.dumps({ "investigators": results })) @csrf_exempt def findings_update(request, pk): if request.method == 'POST': obj = json.loads(request.body.decode('utf-8')) form = obj['form'] if form['special'] == '0': return HttpResponse(json.dumps({ "status": "0", "message": "专题组不能为空" })) # provincs = Area_code_2020.objects.get(code=obj['provincs']) # citis = Area_code_2020.objects.get(code=obj['citis']) # districts = Area_code_2020.objects.get(code=obj['districts']) # countries = Area_code_2020.objects.get(code=obj['countries']) # villages = Area_code_2020.objects.get(code=obj['villages']) provincs = obj['provincs'] citis = obj['citis'] districts = obj['districts'] countries = obj['countries'] villages = obj['villages'] if provincs != '' and provincs.isdigit(): province_r = Area_code_2020.objects.get(code=provincs).name else: province_r = provincs if citis != '' and citis.isdigit(): city_r = Area_code_2020.objects.get(code=citis).name else: city_r = citis if districts != '' and districts.isdigit(): district_r = Area_code_2020.objects.get(code=districts).name else: district_r = districts if countries != '' and countries.isdigit(): town_r = Area_code_2020.objects.get(code=countries).name else: town_r = countries if villages != '' and villages.isdigit(): village_r = Area_code_2020.objects.get(code=villages).name else: village_r = villages arrit = obj['arrit'] arredificator = obj['arredificator'] arrassociated = obj['arrassociated'] print(arredificator) print(arrassociated) site = str(province_r + '-' + city_r + '-' + district_r + '-' + town_r + '-' + village_r) findings = None if len(form['lat']) != 0 and len(form['lon']) != 0: Findings.objects.filter(id=pk).update(findingscode=form['findingscode'], investigators_id=form['investigators'], date=form['date'], site=site, locationdescription=form['locationdescription'], terrain=form['terrain'], elevation=form['elevation'], gradient=form['gradient'], exposure=form['exposure'], river=form['river'], riverlength=form['riverlength'], riverwidth=form['riverwidth'], riverdeep=form['riverdeep'], rivershape=form['rivershape'], lon=form['lon'], lat=form['lat'], vegetationtypegroups=form['vegetationtypegroups'], vegetation=form['vegetation'], vegetationsubtypes=form['vegetationsubtypes'], formationgroup=form['formationgroup'], formation=form['formation'], task_id=form['task'], special_id=form['special']) else: d1 = form['d1'] f1 = form['f1'] m1 = form['m1'] lon = int(d1) + float(int(f1) / 60) + float(int(m1) / 3600) d2 = form['d2'] f2 = form['f2'] m2 = form['m2'] lat = int(d2) + float(int(f2) / 60) + float(int(m2) / 3600) Findings.objects.get(id=pk).updated(findingscode=form['findingscode'], investigators_id=form['investigators'], date=form['date'], site=site, locationdescription=form['locationdescription'], terrain=form['terrain'], elevation=form['elevation'], gradient=form['gradient'], exposure=form['exposure'], river=form['river'], riverlength=form['riverlength'], riverwidth=form['riverwidth'], riverdeep=form['riverdeep'], rivershape=form['rivershape'], lon=lon, lat=lat, vegetationtypegroups=form['vegetationtypegroups'], vegetation=form['vegetation'], vegetationsubtypes=form['vegetationsubtypes'], formationgroup=form['formationgroup'], formation=form['formation'], task_id=form['task'], special_id=form['special']) f = Findings.objects.get(id=pk) for r in arredificator: if len(str(r).split('id')) != 1: Edificator.objects.filter(id=r['id']).update(edificatorchinese=r['edificatorchinese'], edificatorlatin=r['edificatorlatin'], findings_id=f.id) else: edificator = Edificator(edificatorchinese=r['edificatorchinese'], edificatorlatin=r['edificatorlatin'], findings_id=f.id) edificator.save() for t in arrit: if len(str(t).split('id')) != 1: Dominantspecies.objects.filter(id=t['id']).update(dominantspecieschinese=t['dominantspecieschinese'], dominantspecieslatin=t['dominantspecieslatin'], findings_id=f.id) else: dominantspecies=Dominantspecies(dominantspecieschinese=t['dominantspecieschinese'], dominantspecieslatin=t['dominantspecieslatin'], findings_id=f.id) dominantspecies.save() for d in arrassociated: if len(str(d).split('id')) != 1: Associated.objects.filter(id=d['id']).update(associatedchinese=d['associatedchinese'], associatedlatin=d['associatedlatin'], findings_id=f.id) else: associated = Associated(associatedchinese=d['associatedchinese'], associatedlatin=d['associatedlatin'], findings_id=f.id) associated.save() return HttpResponse(json.dumps({ "status": "1", "message": "修改成功" })) else: investigators = Investigators.objects.all() results = [] for i in investigators: o = dict() o['name'] = i.name o['phone'] = i.phone o['email'] = i.email o['organization'] = i.organization o['remark'] = i.remark o['id'] = str(i.id) results.append(o) findings = Findings.objects.get(id=pk) edificator_list = Edificator.objects.filter(findings_id=findings.id) dominantspecies_list = Dominantspecies.objects.filter(findings_id=findings.id) associated_list = Associated.objects.filter(findings_id=findings.id) edificator_results = [] dominantspecies_results = [] associated_results = [] for e in edificator_list: o = dict() o['id'] = str(e.id) o['edificatorchinese'] = e.edificatorchinese o['edificatorlatin'] = e.edificatorlatin edificator_results.append(o) for d in dominantspecies_list: o = dict() o['id'] = str(d.id) o['dominantspecieschinese'] = d.dominantspecieschinese o['dominantspecieslatin'] = d.dominantspecieslatin dominantspecies_results.append(o) for a in associated_list: o = dict() o['id'] = str(a.id) o['associatedchinese'] = a.associatedchinese o['associatedlatin'] = a.associatedlatin associated_results.append(o) special_list = [] special = Special.objects.filter(id=findings.special_id) for s in special: i = dict() i['id'] = str(s.id) i['specialname'] = s.specialname i['specialcode'] = s.specialcode i['remark'] = s.remark special_list.append(i) return HttpResponse(json.dumps({ "status": "1", "investigators": results, "id": str(findings.id), "findingscode": findings.findingscode, "date": findings.date, "site1": findings.site.split('-')[0], "site2": findings.site.split('-')[1], "site3": findings.site.split('-')[2], "site4": findings.site.split('-')[3], "site5": findings.site.split('-')[4], "locationdescription": findings.locationdescription, "terrain": findings.terrain, "lon": findings.lon, "lat": findings.lat, "elevation": findings.elevation, "gradient": findings.gradient, "exposure": findings.exposure, "river": findings.river, "riverlength": findings.riverlength, "riverwidth": findings.riverwidth, "riverdeep": findings.riverdeep, "rivershape": findings.rivershape, "investigators_id": str(findings.investigators_id), "edificator_results": edificator_results, "dominantspecies_results": dominantspecies_results, "associated_results": associated_results, "vegetationtypegroups": findings.vegetationtypegroups, "vegetation": findings.vegetation, "vegetationsubtypes": findings.vegetationsubtypes, "formationgroup": findings.formationgroup, "formation": findings.formation, "task": str(findings.task_id), "special": str(findings.special_id), "special_list": special_list, })) def findings_delete(request, pk): if pk: findings = Findings.objects.get(id=pk) findings.delete() return HttpResponse('{"status":"1","message":"删除成功"}') else: return HttpResponse('{"status":"0","message":"请传入正确的id"}') # //数据统计 def data_management_0(request): sampletrees = Sampletrees.objects.all().count() samplebush = Samplebush.objects.all().count() sampleherbal = Sampleherbal.objects.all().count() simplesoil = Simplesoil.objects.all().count() barcodes = Barcodes.objects.all().count() weather = Weather.objects.all().count() landuse = Landuse.objects.all().count() sampleinterlayerplants = Sampleinterlayerplants.objects.all().count() simplebiomass = Simplebiomass.objects.all().count() community = Community.objects.all().count() findings = Findings.objects.all().count() task_count = Task.objects.all().count() count = int(sampletrees) + int(samplebush) + int(sampleherbal) + int(simplesoil) + int(barcodes) + int( weather) + int(landuse) + int(sampleinterlayerplants) + int(simplebiomass) + int(community) # 课题组数据统计 task = Task.objects.all().order_by('-created') task_results = [] for t in task: o = dict() task_findings = Findings.objects.filter(task_id=t.id).count() task_sampletreescount = Sampletrees.objects.filter(findings__task_id=t.id).count() task_samplebushcount = Samplebush.objects.filter(findings__task_id=t.id).count() task_sampleherbalcount = Sampleherbal.objects.filter(findings__task_id=t.id).count() task_simplesoilcount = Simplesoil.objects.filter(findings__task_id=t.id).count() task_barcodescount = Barcodes.objects.all().count() task_weathercount = Weather.objects.filter(findings__task_id=t.id).count() task_landusecount = Landuse.objects.filter(findings__task_id=t.id).count() task_sampleinterlayerplantscount = Sampleinterlayerplants.objects.filter(findings__task_id=t.id).count() task_simplebiomasscount = Simplebiomass.objects.filter(findings__task_id=t.id).count() task_communitycount = Community.objects.filter(findings__task_id=t.id).count() o['taskname'] = t.taskname o['task_findings'] = task_findings o['task_sampletreescount'] = task_sampletreescount o['task_samplebushcount'] = task_samplebushcount o['task_sampleherbalcount'] = task_sampleherbalcount o['task_simplesoilcount'] = task_simplesoilcount o['task_barcodescount'] = task_barcodescount o['task_weathercount'] = task_weathercount o['task_landusecount'] = task_landusecount o['task_sampleinterlayerplantscount'] = task_sampleinterlayerplantscount o['task_simplebiomasscount'] = task_simplebiomasscount o['task_communitycount'] = task_communitycount task_results.append(o) # 调查点数据统计 findings_s = Findings.objects.all().order_by('-created') paginator = Paginator(findings_s, 6) page = int(request.GET.get('page', 1)) try: findings_s_p = paginator.page(page) except PageNotAnInteger: findings_s_p = paginator.page(1) except EmptyPage: findings_s_p = paginator.page(paginator.num_pages) res = [] findings_results = [] for f in findings_s_p: o = dict() findings_sampletreescount = Sampletrees.objects.filter(findings__task_id=f.id).count() findings_samplebushcount = Samplebush.objects.filter(findings__task_id=f.id).count() findings_sampleherbalcount = Sampleherbal.objects.filter(findings__task_id=f.id).count() findings_simplesoilcount = Simplesoil.objects.filter(findings__task_id=f.id).count() findings_barcodescount = Barcodes.objects.all().count() findings_weathercount = Weather.objects.filter(findings__task_id=f.id).count() findings_landusecount = Landuse.objects.filter(findings__task_id=f.id).count() findings_sampleinterlayerplantscount = Sampleinterlayerplants.objects.filter(findings__task_id=f.id).count() findings_simplebiomasscount = Simplebiomass.objects.filter(findings__task_id=f.id).count() findings_communitycount = Community.objects.filter(findings__task_id=f.id).count() o['findingscode'] = f.findingscode o['taskname'] = Task.objects.get(id=f.task_id).taskname o['findings_sampletreescount'] = findings_sampletreescount o['findings_samplebushcount'] = findings_samplebushcount o['findings_sampleherbalcount'] = findings_sampleherbalcount o['findings_simplesoilcount'] = findings_simplesoilcount o['findings_barcodescount'] = findings_barcodescount o['findings_weathercount'] = findings_weathercount o['findings_landusecount'] = findings_landusecount o['findings_sampleinterlayerplantscount'] = findings_sampleinterlayerplantscount o['findings_simplebiomasscount'] = findings_simplebiomasscount o['findings_communitycount'] = findings_communitycount findings_results.append(o) # 专题组数据统计 special = Special.objects.all().order_by('-created') paginator = Paginator(special, 3) page = int(request.GET.get('page', 1)) try: special_p = paginator.page(page) except PageNotAnInteger: special_p = paginator.page(1) except EmptyPage: special_p = paginator.page(paginator.num_pages) special_results = [] for s in special_p: o = dict() findings_count = Findings.objects.filter(special_id=s.id).count() sampletrees_count = Sampletrees.objects.filter(findings__special_id=s.id).count() samplebush_count = Samplebush.objects.filter(findings__special_id=s.id).count() sampleherbal_count = Sampleherbal.objects.filter(findings__special_id=s.id).count() simplesoil_count = Simplesoil.objects.filter(findings__special_id=s.id).count() barcodes_count = Barcodes.objects.all().count() weather_count = Weather.objects.filter(findings__special_id=s.id).count() landuse_count = Landuse.objects.filter(findings__special_id=s.id).count() o['specialname'] = s.specialname o['specialcode'] = s.specialcode o['task'] = s.task.taskname o['findings_count'] = findings_count o['sampletrees_count'] = sampletrees_count o['samplebush_count'] = samplebush_count o['sampleherbal_count'] = sampleherbal_count o['simplesoil_count'] = simplesoil_count o['barcodes_count'] = barcodes_count o['weather_count'] = weather_count o['landuse_count'] = landuse_count special_results.append(o) return HttpResponse(json.dumps({ "status": "1", "count": count, "task": task_count, "findings": findings, "sampletrees": sampletrees, "samplebush": samplebush, "sampleherbal": sampleherbal, "simplesoil": simplesoil, "barcodes": barcodes, "weather": weather, "landuse": landuse, "sampleinterlayerplants": sampleinterlayerplants, "simplebiomass": simplebiomass, "community": community, "task_results": task_results, "findings_results": findings_results, "special_results": special_results, "count_special": len(special), "page_count_special": math.ceil(len(special) / 3), "count_findings_s": len(findings_s), "page_count_findings_s": math.ceil(len(findings_s) / 6), })) def data_management_1(request): username = request.GET.get('username') user_id = User.objects.get(username=username).id task = Task.objects.get(userprofile__user_id=user_id) # task = Task.objects.get(principal_id=user_id) sampletrees = Sampletrees.objects.filter(findings__investigators__task_id=task.id).count() samplebush = Samplebush.objects.filter(findings__investigators__task_id=task.id).count() sampleherbal = Sampleherbal.objects.filter(findings__investigators__task_id=task.id).count() simplesoil = Simplesoil.objects.filter(findings__investigators__task_id=task.id).count() barcodes = Barcodes.objects.all().count() weather = Weather.objects.filter(findings__investigators__task_id=task.id).count() landuse = Landuse.objects.filter(findings__investigators__task_id=task.id).count() sampleinterlayerplants = Sampleinterlayerplants.objects.filter(findings__investigators__task_id=task.id).count() simplebiomass = Simplebiomass.objects.filter(findings__investigators__task_id=task.id).count() community = Community.objects.filter(findings__investigators__task_id=task.id).count() findings = Findings.objects.filter(investigators__task_id=task.id).count() count = int(sampletrees) + int(samplebush) + int(sampleherbal) + int(simplesoil) + int(barcodes) + int( weather) + int(landuse) + int(sampleinterlayerplants) + int(simplebiomass) + int(community) # 调查点数据统计 findings_s = Findings.objects.filter(investigators__task_id=task.id) findings_results = [] for f in findings_s: o = dict() findings_sampletreescount = Sampletrees.objects.filter(findings__id=f.id).count() findings_samplebushcount = Samplebush.objects.filter(findings__id=f.id).count() findings_sampleherbalcount = Sampleherbal.objects.filter(findings__id=f.id).count() findings_simplesoilcount = Simplesoil.objects.filter(findings_id=f.id).count() findings_barcodescount = Barcodes.objects.all().count() findings_weathercount = Weather.objects.filter(findings_id=f.id).count() findings_landusecount = Landuse.objects.filter(findings_id=f.id).count() findings_sampleinterlayerplantscount = Sampleinterlayerplants.objects.filter(findings__id=f.id).count() findings_simplebiomasscount = Simplebiomass.objects.filter(findings__id=f.id).count() findings_communitycount = Community.objects.filter(findings_id=f.id).count() o['findingscode'] = f.findingscode o['taskname'] = Task.objects.get(investigators__findings__id=f.id).taskname o['findings_sampletreescount'] = findings_sampletreescount o['findings_samplebushcount'] = findings_samplebushcount o['findings_sampleherbalcount'] = findings_sampleherbalcount o['findings_simplesoilcount'] = findings_simplesoilcount o['findings_barcodescount'] = findings_barcodescount o['findings_weathercount'] = findings_weathercount o['findings_landusecount'] = findings_landusecount o['findings_sampleinterlayerplantscount'] = findings_sampleinterlayerplantscount o['findings_simplebiomasscount'] = findings_simplebiomasscount o['findings_communitycount'] = findings_communitycount findings_results.append(o) return HttpResponse(json.dumps({ "status": "1", "findings": findings, "sampletrees": sampletrees, "samplebush": samplebush, "sampleherbal": sampleherbal, "simplesoil": simplesoil, "barcodes": barcodes, "weather": weather, "landuse": landuse, "sampleinterlayerplants": sampleinterlayerplants, "simplebiomass": simplebiomass, "community": community, "count": count, "findings_results": findings_results })) # 专题组管理 @csrf_exempt def special_management(request): special = None obj = json.loads(request.body.decode('utf-8')) username = obj['username'] id = User.objects.get(username=username).id userjurisdiction = User.objects.get(id=id) if userjurisdiction.is_superuser is True: jurisdiction = 0 else: jurisdiction = UserProfile.objects.get(user_id=id).jurisdiction.jurisdiction if jurisdiction == 0: special = Special.objects.all().order_by('-created') elif jurisdiction == 1: task_id = UserProfile.objects.get(user_id=id).task_id task = Task.objects.get(id=task_id) special = Special.objects.filter(task_id=task.id).order_by('-created') elif jurisdiction == 3 or jurisdiction == 2: special_id = UserProfile.objects.get(user_id=id).special_id special = Special.objects.filter(id=special_id).order_by('-created') special_list = [] paginator = Paginator(special, 10) page = int(request.GET.get('page', 1)) try: special_p = paginator.page(page) except PageNotAnInteger: special_p = paginator.page(1) except EmptyPage: special_p = paginator.page(paginator.num_pages) for t in special_p: o = dict() o['id'] = str(t.id) o['specialname'] = t.specialname o['specialcode'] = t.specialcode o['remark'] = t.remark o['task_name'] = t.task.taskname o['task_id'] = str(t.task.id) special_list.append(o) return HttpResponse(json.dumps({ "status": '1', "special": special_list, "jurisdiction": jurisdiction, "count": len(special), "page_count": math.ceil(len(special) / 10), })) @csrf_exempt def special_management_add(request): if request.method == 'POST': obj = json.loads(request.body.decode('utf-8')) ruleForm = obj['ruleForm'] taskname = obj['taskname'] results = Special.objects.filter(specialcode=ruleForm['specialcode']) if len(results) != 0: return HttpResponse(json.dumps({ "status": "0", "message": "该专题组已经存在" })) special = Special(specialname=ruleForm['specialname'], specialcode=ruleForm['specialcode'], remark=ruleForm['remark'], task_id=taskname) special.save() return HttpResponse(json.dumps({ "status": "1", "message": "添加成功" })) else: username = request.GET.get('username') user = User.objects.get(username=username) userprofile = UserProfile.objects.get(user_id=user.id) task = Task.objects.get(id=userprofile.task_id) results = [] o = dict() o['taskname'] = task.taskname o['taskcode'] = task.taskcode o['id'] = str(task.id) results.append(o) return HttpResponse(json.dumps({ "status": "1", "task": results, "results": results, # "taskname": task.taskname, # "taskcode": task.taskcode, # "id": str(task.id) })) def special_delete(request, pk): if pk: special = Special.objects.get(id=pk) special.delete() return HttpResponse('{"status":"1","message":"删除成功"}') else: return HttpResponse('{"status":"0","message":"请传入正确的id"}') @csrf_exempt def special_management_update(request, pk): if request.method == 'POST': obj = json.loads(request.body.decode('utf-8')) form = obj['form'] taskname = obj['taskname'] Special.objects.filter(id=pk).update(specialname=form['specialname'], specialcode=form['specialcode'], remark=form['remark'], task_id=taskname) return HttpResponse(json.dumps({ "status": "1", "message": "修改成功" })) else: special = Special.objects.get(id=pk) username = request.GET.get('username') user = User.objects.get(username=username) userprofile = UserProfile.objects.get(user_id=user.id) task = Task.objects.get(id=userprofile.task_id) principal = User.objects.filter(userprofile__task_id=task.id) results = [] o = {} o['taskname'] = task.taskname o['id'] = str(task.id) results.append(o) return HttpResponse(json.dumps({ "status": "1", "id": str(special.id), "specialname": special.specialname, "specialcode": special.specialcode, "remark": special.remark, "task": str(special.task.id), "task_list": results, }))