def add_device(request): if request.method == 'POST': form = DevicesForm(request.POST) # 如果请求为POST,并且Form校验通过,把新添加的学员信息写入数据库 if form.is_valid(): s1 = Device(name=request.POST.get('name'), ip_address=request.POST.get('ip_address'), ro_community=request.POST.get('ro_community'), rw_community=request.POST.get('rw_community'), username=request.POST.get('username'), password=request.POST.get('password'), enable_password=request.POST.get('enable_password'), device_type=request.POST.get('device_type'), vendor=request.POST.get('vendor')) s1.save() form = DevicesForm() #messages.success(request, '设备添加成功') return render(request, 'add_device.html', { 'form': form, 'successmessage': '设备添加成功' }) else: # 如果Form校验失败,返回客户在Form中输入的内容和报错信息 # 如果检查到错误,会添加错误内容到form内,例如:<ul class="errorlist"><li>QQ号码已经存在</li></ul> return render(request, 'add_device.html', {'form': form}) else: # 如果不是POST,就是GET,表示为初始访问, 显示表单内容给客户 form = DevicesForm() return render(request, 'add_device.html', {'form': form})
def bind(msg): wx_device_id = msg.device_id open_id = msg.open_id wx_wechatoriginalid = msg.device_type userinfo = getuserinfo(open_id) userinfo = json.dumps(userinfo) url = FOGPATH + '/wechat/bind/' data = { "wx_device_id": wx_device_id, "originalid": wx_wechatoriginalid, "open_id": open_id, "userinfo": userinfo } response = requests.post(url, data=data, verify=False) fog_response = json.loads(response.text) if not Enduser.objects.filter(open_id=open_id): en = Enduser() en.fog_enduser_id = fog_response['data']['fog_enduser_id'] en.open_id = open_id en.fog_product_id = fog_response['data']['fog_product_id'] en.save() try: device = Device.objects.get(wx_device_id=wx_device_id) device.note = fog_response['data']['devicename'] device.fog_product_id = fog_response['data']['fog_product_id'] device.save() except Device.DoesNotExist: device = Device() device.note = fog_response['data']['devicename'] device.fog_product_id = fog_response['data']['fog_product_id'] device.fog_device_id = fog_response['data']['fog_device_id'] device.wx_device_id = wx_device_id device.save() return json.loads(response.text)
def update_connected(name): # {"connected":"True"} body = request.get_json() ori = Device.objects.get(name=name) if (body['connected'] == 'True'): ori.update(connected=True) elif (body['connected'] == 'False'): ori.update(connected=False) return Response(Device.objects().get(name=name).to_json(), mimetype="application/json", status=200)
def post(self, request): try: de = Device.objects.get( fog_device_id=request.data['fog_device_id']) de.wx_device_id = request.data['wx_device_id'] de.ai_device_id = request.data['ai_device_id'] note = getdeviceidfromfog(request.data['wx_device_id']) if note['meta']['code'] == 0: de.note = note['data']['wx_device_alias'] de.fog_product_id = note['data']['fog_product_id'] de.save() else: data = requests.query_params meta = {'code': 27081, 'message': note['meta']['message']} return Response({ 'meta': meta, 'data': data }, status=status.HTTP_200_OK) except Device.DoesNotExist: de = Device() de.wx_device_id = request.data['wx_device_id'] de.ai_device_id = request.data['ai_device_id'] note = getdeviceidfromfog(requests.data['wx_device_id']) if note['meta']['code'] == 0: de.note = note['data']['wx_device_alias'] de.fog_product_id = note['data']['fog_product_id'] de.save() else: data = requests.query_params meta = {'code': 27081, 'message': note['meta']['message']} return Response({ 'meta': meta, 'data': data }, status=status.HTTP_200_OK) data = self.serializer_class(de).data meta = {'code': 0, 'message': 'successful'} return Response({ 'meta': meta, 'data': data }, status=status.HTTP_200_OK)
def owner_device_add(request): if request.method == 'POST': device = Device() device.device_name = request.POST['devicename'] device.owner = request.session['username'] device.owner_phone = request.POST['phone'] # device.user = '' # device.start = timezone.now().date() # device.due = timezone.now().date() device.location = request.POST['location'] device.addition = request.POST['addition'] device.valid = 'on_order' device.reason = request.POST['reason'] device.save() # shelf_order shelf_order = ShelfOrder() shelf_order.device_id = device.id shelf_order.owner_name = device.owner shelf_order.reason = device.reason shelf_order.state = 'waiting' shelf_order.start_time = timezone.now().date() shelf_order.save() add_dialog('设备管理者{}把设备{}添加进设备申请列表'.format(request.session['username'], request.POST['devicename'])) return JsonResponse({'state': 1})
def delete_allDevices(): Device.objects().delete() RegisterQueue.objects().delete() return Response(Device.objects().to_json(), mimetype="application/json", status=200)
def get_devices(): devices = Device.objects().to_json() return Response(devices, mimetype="application/json", status=200)
def handle_register(json_str): data = json.loads(json_str) Device(**data).save()