def put(self, request, user_id): try: data = dict(request.data.items()) # 校验手机号和账号是否重复 if 'phone' in data: query_set = HyUsers.objects.filter(phone=data['phone']).exclude(id=user_id) if len(query_set) > 0: return util_response(code=ecode.ExistedPhone) if 'account' in data: query_set = HyUsers.objects.filter(account=data['account']).exclude(id=user_id) if len(query_set) > 0: return util_response(code=ecode.ExistedAccount) HyUsers.objects.filter(id=user_id).update(**data) # 删除缓存中的账号信息 query_set = HyUsers.objects.filter(id=user_id) phone = query_set[0].phone conn = get_redis_connection("default") # print('old', conn.hget(phone, 'right')) conn.delete(phone) # print('new', conn.hget(phone, 'right')) return util_response() # return Response({'code': 200, 'msg': 'ok'}) except Exception as e: logging.error(e) return util_response(code=ecode.ERROR)
def get(self, request): row = int(request.query_params.get('row', 10)) page = int(request.query_params.get('page', 1)) # 手动分页 offset = (page - 1) * row condition = dict() condition['customer_id__contains'] = self.request.query_params.get( 'customer_id', '') condition['name_short__contains'] = self.request.query_params.get( 'customer_short_name', '') condition['code_mnemonic__contains'] = self.request.query_params.get( 'code_mnemonic', '') condition['customer_category'] = self.request.query_params.get( 'customer_category', '') condition['lock_status'] = self.request.query_params.get( 'lock_status', '') condition['delete_state__exact'] = False query_condition = dict() for i in condition: if condition[i] != '': query_condition[i] = condition[i] try: query_set = HyCustomers.objects.filter(**query_condition) total = len(query_set) s = serializer.CustomerBigListSerializer(instance=query_set, many=True) data = dict() data['list'] = s.data[offset:offset + row] data['total'] = total return util_response(data) # return Response({'code': 200, 'msg': 'ok', 'data': data}) except Exception as e: logging.error(e) return util_response(code=ecode.ERROR)
def get(self, request): account = request.query_params.get('account') name = request.query_params.get('name') phone = request.query_params.get('phone') page = request.query_params.get('page', '1') row = request.query_params.get('row', '10') offset = (int(page) - 1) * int(row) limit = int(row) condition = dict() if account: condition['account__icontains'] = account if name: condition['name__icontains'] = name if phone: condition['phone__icontains'] = phone try: query_set = HyUsers.objects.filter(**condition) total = len(query_set) data = UserSerializer(query_set, many=True).data data = data[offset: offset+limit] data = {'list': data, 'total': total} return util_response(data) # return Response({'code': 200, 'msg': 'ok', 'data': {'list': data, 'total': total}}) except Exception as e: logging.error(e) return util_response(code=ecode.ERROR)
def get(self, request): site_id = request.query_params.get('site_id') if not site_id: util_response(http_status=status.HTTP_400_BAD_REQUEST) cache_key = cache.get_realtime_device_items_key(site_id) conn = get_redis_connection('default') device_items = conn.get(cache_key) if not device_items: device_items = {} else: device_items = json.loads(device_items) items = [] devices = Device.objects.filter(site_id=site_id) for device in devices: cache_key = cache.get_device_monitor_data_key(device.id) monitor_data = conn.get(cache_key) if not monitor_data: continue monitor_data = json.loads(monitor_data) for mdata in monitor_data['Data']: mdata['device_name'] = device.name mdata['device_id'] = device.id mdata['check'] = '1' if device_items.get( device.id, '') and mdata['name'] in device_items.get( device.id) else '0' items.append(mdata) return util_response(data=items)
def post(self, request): conn = get_redis_connection('default') phone = request.data.get('phone') code = request.data.get('code') new_password = request.data.get('pwd') try: r_code = conn.get(phone + '_') if not r_code: return util_response(code=ecode.InvalidCode) # return Response({"code": 500, 'msg': '验证码已失效,请重新验证!'}) if r_code == code: new_password = make_password(new_password, None, 'pbkdf2_sha1') else: return util_response(code=ecode.WrongCode) # return Response({"code": 500, 'msg': '验证码验证失败!'}) query_set = HyUsers.objects.filter(phone=phone) if len(query_set) == 0: return util_response(code=ecode.InvalidAccount) # return Response({"code": 500, "msg": "账号不存在!"}) update = {'password': new_password} HyUsers.objects.filter(phone=phone).update(**update) # 重置登录失败次数 conn.hset(phone, 'fail_times', '0') return util_response() # return Response({'code': 200, 'msg': 'ok'}) except Exception as e: logging.error(e) return util_response(code=ecode.ERROR)
def put(self, request, chart_id): name = request.data.get('name', '') chart_type = request.data.get('type', '1') time_type = request.data.get('time_type', '') items = request.data.get('items', {}) if not chart_id or not name or not chart_type or not time_type or not items: return util_response(http_status=status.HTTP_400_BAD_REQUEST) chart = AnalysisChart.objects.get(id=chart_id) chart.name = name chart.chart_type = chart_type chart.time_type = time_type chart.items = items chart.save() cache_key = cache.get_analysis_chart_items_key(chart_id) conn = get_redis_connection('default') conn.set( cache_key, json.dumps({ "items": items, "chart_type": chart_type, "time_type": time_type, "forecast": '1' if chart.forecast else '0' })) return util_response()
def get(self, request, chart_id): """ chart_type 0: 柱状图,1:折线图 2:条形图 3:饼图 time_type 0: 分 1: 时 2: 天 :param request: :param chart_id: :return: """ cache_key = cache.get_analysis_chart_items_key(chart_id) conn = get_redis_connection('default') cache_res = conn.get(cache_key) if not cache_res: return util_response( http_status=status.HTTP_500_INTERNAL_SERVER_ERROR) # {"items": items, "chart_type": chart_type, "time_type": time_type, "forecast": forecast} cache_res = json.loads(cache_res) data = dict() if cache_res['chart_type'] == '0': data = self.histogram_chart(cache_res['items'], cache_res['time_type'], cache_res['forecast']) elif cache_res['chart_type'] == '1': data = self.line_chart(cache_res['items'], cache_res['time_type'], cache_res['forecast']) elif cache_res['chart_type'] == '2': data = self.bar_chart(cache_res['items'], cache_res['time_type']) elif cache_res['chart_type'] == '3': data = self.pie_chart(cache_res['items'], cache_res['time_type']) return util_response(data=data)
def get(self, request): site_id = request.query_params.get('site_id') if not site_id: return util_response(http_status=status.HTTP_400_BAD_REQUEST) cache_key = cache.get_realtime_device_items_key(site_id) conn = get_redis_connection('default') device_items = conn.get(cache_key) if not device_items: return util_response(data=[]) device_items = json.loads(device_items) result = [] for device in list(device_items.keys()): cache_key = cache.get_device_monitor_data_key(device) monitor_data = conn.get(cache_key) if not monitor_data: continue monitor_data = json.loads(monitor_data) for item in device_items.get(device, []): for mdata in monitor_data['Data']: if item == mdata['name']: mdata['device_id'] = device result.append(mdata) break return util_response(data=result)
def post(self, request): site_id = request.query_params.get("site_id") customer_id = request.query_params.get("customer_id") forecast = request.query_params.get("type") == '1' if not site_id or not customer_id: return util_response(http_status=status.HTTP_400_BAD_REQUEST) name = request.data.get('name', '') chart_type = request.data.get('type', '1') time_type = request.data.get('time_type', '') items = request.data.get('items', {}) chart_id = generate_chart_id() obj = AnalysisChart(id=chart_id, name=name, chart_type=chart_type, time_type=time_type, customer_id=customer_id, site_id=site_id, items=items, forecast=forecast) obj.save() cache_key = cache.get_analysis_chart_items_key(chart_id) conn = get_redis_connection('default') conn.set( cache_key, json.dumps({ "items": items, "chart_type": chart_type, "time_type": time_type, "forecast": '1' if forecast else '0' })) return util_response()
def get(self, request, item_id): device_id = request.query_params.get('device_id') start = request.query_params.get("start_time") end = request.query_params.get("end_time") site_name = request.query_params.get("site_name", '') if not start or not end or not device_id or not item_id: return util_response(http_status=status.HTTP_400_BAD_REQUEST) db_conn = connections['default'] cursor = db_conn.cursor() order_state_sql = "select name, value, time from hy_monitor_data where device_id = %s and" \ " name = %s and time > %s and time < %s order by time desc" # device = Device.objects.get(pk=device_id) # 如果数据不存在, 会抛出异常报错 device = Device.objects.filter(pk=device_id) cursor.execute(order_state_sql, (device_id, item_id, start, end)) res = cursor.fetchall() data_list = [] title = ['时间', '数据名称', '数据值', '所属设备', '站点名称'] for x in res: data_list.append([ x[2].strftime("%Y-%m-%d %H:%M:%S"), x[0], x[1], device.name, site_name ]) data = { "title": title, "list": data_list, } return util_response(data)
def get(self, request): """ :param request: :return: """ device_id = request.query_params.get("device_id") item_id = request.query_params.get("device_item_id") start = request.query_params.get("start_time") end = request.query_params.get("end_time") if not start or not end or not device_id or not item_id: return util_response(http_status=status.HTTP_400_BAD_REQUEST) db_conn = connections['default'] cursor = db_conn.cursor() order_state_sql = "select name, value, time from hy_monitor_data where device_id = %s and" \ " name = %s and time > %s and time < %s order by time desc" cursor.execute(order_state_sql, (device_id, item_id, start, end)) res = cursor.fetchall() data_list = [] title = ['时间', '数据名称', '数据值'] for x in res: data_list.append([x[2].strftime("%Y-%m-%d %H:%M:%S"), x[0], x[1]]) data = { "title": title, "list": data_list, } return util_response(data)
def put(self, request, customer_id): try: data = dict(request.data.items()) business_license = data.get('business_license') if business_license: extension = os.path.splitext(business_license.name)[1] new = '{}{}'.format(customer_id, extension) old_business_license = HyCustomers.objects.get( customer_id=customer_id).business_license old = old_business_license.split('/')[-1] # print('old', old) # print('new', new) if old == new: delete = False else: delete = True path = os.path.join(os.path.dirname(BASE_DIR), "static/img/{}".format(new)) with open(path, 'wb') as file: file.write(bytes(business_license.read())) data['business_license'] = IMAGE_PATH + "img/{}".format(new) # 当新图片正常保存后再删除旧图片 if delete: old_path = os.path.join(os.path.dirname(BASE_DIR), 'static/img/{}'.format(old)) os.remove(old_path) # 返回影响的数据数 HyCustomers.objects.filter(customer_id=customer_id).update(**data) return util_response() # return Response({'code': 200, 'msg': 'ok'}) except Exception as e: logging.error(e) return util_response(code=ecode.ERROR)
def put(self, request): try: conn = get_redis_connection('default') customer_id = request.redis_cache['customer_id'] new_bg = request.FILES['img'] extension = os.path.splitext(new_bg.name)[1] new_bg_name = '{}_bg{}'.format(customer_id, extension) # print('new_bg_name', new_bg_name) old_bg = conn.hget(customer_id, 'bg') or new_bg_name old_bg_name = old_bg.split('/')[-1] # print('old_bg_name', old_bg_name) if old_bg_name == new_bg_name: delete = False else: delete = True new_bg_path = os.path.join(os.path.dirname(BASE_DIR), "static/img/{}".format(new_bg_name)) with open(new_bg_path, 'wb') as file: file.write(bytes(new_bg.read())) conn.hset(customer_id, 'bg', IMAGE_PATH + "img/{}".format(new_bg_name)) # 当新图片正常保存后再删除旧图片 if delete: old_bg_path = os.path.join(os.path.dirname(BASE_DIR), 'static/img/{}'.format(old_bg_name)) os.remove(old_bg_path) return util_response() # return Response({'code': 200, 'msg': 'ok'}) except Exception as e: logging.error(e) return util_response(code=ecode.ERROR)
def get(self, request): site_id = request.query_params.get("site_id") if not site_id: return util_response(http_status=status.HTTP_400_BAD_REQUEST) charts = AnalysisChart.objects.filter(site=site_id) data = [AnalisysChartSerializer(x).data for x in charts] return util_response(data=data)
def delete(self, request, id_): try: HyNotifier.objects.filter(id=id_).delete() return util_response() # return Response({'code': 200, 'msg': 'ok'}) except Exception as e: logging.error(e) return util_response(code=ecode.ERROR)
def delete(self, request, user_id): try: HyUsers.objects.filter(id=user_id).delete() return util_response() # return Response({'code': 200, 'msg': 'ok'}) except Exception as e: logging.error(e) return util_response(code=ecode.ERROR)
def delete(self, request, id_): try: delete = {'delete_state': True} HySites.objects.filter(id=id_).update(**delete) return util_response() # return Response({'code': 200, 'msg': 'ok'}) except Exception as e: logging.error(e) return util_response(code=ecode.ERROR)
def post(self, request, id_): try: data = dict(request.data.items()) HyAlarmLevel.objects.filter(id=id_).update(**data) return util_response() # return Response({'code': 200, 'msg': 'ok'}) except Exception as e: logging.error(e) return util_response(code=ecode.ERROR)
def post(self, request): img = request.FILES['img'] if not img: return util_response(http_status=status.HTTP_400_BAD_REQUEST) path = os.path.join(os.path.dirname(settings.BASE_DIR), "static/img/chart_gy") with open(path, 'wb') as file: file.write(bytes(img.read())) return util_response()
def post(self, request, customer_id): try: data = dict(request.data.items()) customer = HyCustomers.objects.get(customer_id=customer_id) customer.customer_notifier.all().update(**data) return util_response() # return Response({'code': 200, 'msg': 'ok'}) except Exception as e: logging.error(e) return util_response(code=ecode.ERROR)
def put(self, request, id_): # site_id try: data = dict(request.data.items()) # 返回影响的数据数 HySites.objects.filter(id=id_).update(**data) return util_response() # return Response({'code': 200, 'msg': 'ok'}) except Exception as e: logging.error(e) return util_response(code=ecode.ERROR)
def get(self, request): try: query_set = HyCustomers.objects.filter(delete_state__exact=False) data = serializer.CustomerListSerializer(instance=query_set, many=True).data return util_response(data) # return Response({'code': 200, 'msg': 'ok', 'data': data}) except Exception as e: logging.error(e) return util_response(code=ecode.ERROR)
def post(self, request, customer_id): try: # 返回影响的数据数 HyCustomers.objects.filter(customer_id=customer_id).update( lock_status='0') return util_response() # return Response({'code': 200, 'msg': 'ok'}) except Exception as e: logging.error(e) return util_response(code=ecode.ERROR)
def put(self, request): try: customer_id = request.redis_cache['customer_id'] conn = get_redis_connection('default') times = int(request.data.get('times')) conn.hset(customer_id, 'fail_times', str(times)) return util_response() # return Response({'code': 200, 'msg': 'ok'}) except Exception as e: logging.error(e) return util_response(code=ecode.ERROR)
def get(self, request): site_id = request.query_params.get("site_id") if not site_id: return util_response(http_status=status.HTTP_400_BAD_REQUEST) charts = Chart.objects.filter(site=site_id) data = [ChartSerializer(x).data for x in charts] return util_response(data={ "charts": data, "img": settings.STATIC_FILE_URL + "img/chart_gy" })
def put(self, request): try: customer_id = request.redis_cache['customer_id'] conn = get_redis_connection('default') name = request.data.get('name') conn.hset(customer_id, 'name', name) return util_response() # return Response({'code': 200, 'msg': 'ok'}) except Exception as e: logging.error(e) return util_response(code=ecode.ERROR)
def get(self, request, site_id): """ :param request: :param site_id: :return: """ if not site_id: return util_response(http_status=status.HTTP_400_BAD_REQUEST) devices = Device.objects.filter(site=site_id) devices = [DeviceList(x).data for x in devices] return util_response(data=devices)
def get(self, request, customer_id): try: query_set = HyCustomers.objects.filter(customer_id=customer_id) data = serializer.CustomersSerializer(instance=query_set, many=True).data if data: return util_response(data[0]) else: return util_response(dict()) # return Response({'code': 200, 'msg': 'ok', 'data': data}) except Exception as e: logging.error(e) return util_response(code=ecode.ERROR)
def get(self, request, event_id): cursor = connection.cursor() sql_1 = """ select t2.name, t1.device_id_id, t1.name, t1.id, coalesce(t3.level, '0'), to_char(t1.time,'yyyy-mm-dd hh24:mi:ss') from hy_alarm_event t1 left join hy_device t2 on t1.device_id_id = t2.id left join hy_alarm_level t3 on t3.device_id_id = t1.device_id_id and t3.name = t1.name where t1.id = '{}';""".format(event_id) sql_2 = """ select confirm_person, plan_description, problem, to_char(confirm_time,'yyyy-mm-dd hh24:mi:ss') from hy_confirm_alarm where event_id_id = '{}';""".format(event_id) target_1 = [ 'device_name', 'device_id', 'name', 'alarm_id', 'level', 'time' ] target_2 = [ 'confirm_person', 'plan_description', 'problem', 'confirm_time' ] try: cursor.execute(sql_1) tmp = cursor.fetchone() or list() basic_info = dict(zip(target_1, tmp)) cursor.execute(sql_2) tmp = cursor.fetchone() or list() alarm_solution = dict(zip(target_2, tmp)) data = {'basic_info': basic_info, 'alarm_solution': alarm_solution} return util_response(data) # return Response({'code': 200, 'msg': 'ok', 'data': data}) except Exception as e: logging.error(e) return util_response(code=ecode.ERROR)
def post(self, request): device_id = request.data.get("id") pwd = request.data.get('pwd') name = request.data.get('name') if not device_id or not pwd or not name: return util_response(http_status=status.HTTP_400_BAD_REQUEST) obj = Device(id=device_id, pwd=pwd, name=name, customer_id=request.data.get('customer_id'), site_id=request.data.get('site_id')) try: obj.save(force_insert=True) except IntegrityError as e: logging.error(e) return util_response(code=ecode.DeviceIDExist) return util_response()