def login_user(request): if 'username' in request.POST and 'password' in request.POST: user = authenticate(username=request.POST[ 'username'], password=request.POST['password']) if user is not None: if user.is_active: login(request, user) return create_json_response({"login": "******", "user": request.user.get_full_name()}, request) else: return create_json_response({"login": "******", "user": request.user.get_full_name()}, request) else: return create_json_response({"login": "******"}, request) else: return create_json_response({"login": "******"}, request)
def list_notifications(request, start, end): if not request.user.is_authenticated(): raise PermissionDenied start = 0 if start is None else start end = 25 if end is None else end if request.user.is_superuser: notifications = Notification.objects.all() else: notifications = Notification.objects.filter( threshold__show_manager=True) notifications = notifications.select_related() output = {'total': len(notifications), 'notifications': []} for notification in notifications.order_by( '-sensor_value__timestamp')[int(start):int(end)]: output['notifications'].append({ 'id': notification.id, 'threshold': model_to_dict(notification.threshold), 'sensor_value': model_to_dict(notification.sensor_value), 'read': notification.read, 'target': notification.target, }) return create_json_response(output, request)
def list_notifications(request, start, end): if not request.user.is_authenticated(): raise PermissionDenied start = 0 if start is None else start end = 25 if end is None else end if request.user.is_superuser: notifications = Notification.objects.all() else: notifications = Notification.objects.filter( threshold__show_manager=True) notifications = notifications.select_related() output = { 'total': len(notifications), 'notifications': [] } for notification in notifications.order_by('-sensor_value__timestamp')[int(start):int(end)]: output['notifications'].append({ 'id': notification.id, 'threshold': model_to_dict(notification.threshold), 'sensor_value': model_to_dict(notification.sensor_value), 'read': notification.read, 'target': notification.target, }) return create_json_response(output, request)
def list_settings(request): if not request.user.is_authenticated(): raise PermissionDenied output = [] output += functions.get_configurations() output += functions.get_device_configurations() return create_json_response(dict(output), request)
def list_sensors(request): if not request.user.is_authenticated(): raise PermissionDenied sensors = Sensor.objects.filter(in_diagram=True).values( 'id', 'name', 'unit', 'device__name', 'aggregate_sum', 'aggregate_avg') # rename device__name to device for convenience output = [{'id': x['id'], 'name': x['name'], 'unit': x['unit'], 'device': x['device__name'], 'sum': x['aggregate_sum'], 'avg': x['aggregate_avg']} for x in sensors] return create_json_response(output, request)
def login_user(request): if 'username' in request.POST and 'password' in request.POST: user = authenticate(username=request.POST['username'], password=request.POST['password']) if user is not None: if user.is_active: login(request, user) return create_json_response( { "login": "******", "user": request.user.get_full_name() }, request) else: return create_json_response( { "login": "******", "user": request.user.get_full_name() }, request) else: return create_json_response({"login": "******"}, request) else: return create_json_response({"login": "******"}, request)
def status(request): output = [ ("system_status", functions.get_configuration("system_status", False))] output.append( ("system_mode", functions.get_configuration("system_mode", False))) output.append(("login", request.user.is_authenticated())) if request.user.is_authenticated(): output.append(("user", request.user.get_full_name())) output.append(("admin", request.user.is_superuser)) output.append( ("auto_optimization", functions.get_configuration("auto_optimization", False))) return create_json_response(dict(output), request)
def status(request): output = [("system_status", functions.get_configuration("system_status", False))] output.append( ("system_mode", functions.get_configuration("system_mode", False))) output.append(("login", request.user.is_authenticated())) if request.user.is_authenticated(): output.append(("user", request.user.get_full_name())) output.append(("admin", request.user.is_superuser)) output.append(("auto_optimization", functions.get_configuration("auto_optimization", False))) return create_json_response(dict(output), request)
def list_sensors(request): if not request.user.is_authenticated(): raise PermissionDenied sensors = Sensor.objects.filter(in_diagram=True).values( 'id', 'name', 'unit', 'device__name', 'aggregate_sum', 'aggregate_avg') # rename device__name to device for convenience output = [{ 'id': x['id'], 'name': x['name'], 'unit': x['unit'], 'device': x['device__name'], 'sum': x['aggregate_sum'], 'avg': x['aggregate_avg'] } for x in sensors] return create_json_response(output, request)
def logout_user(request): logout(request) return create_json_response({"logout": "successful"}, request)
def api_index(request): return create_json_response({'version': 0.2}, request)