def videos_controller(request: wsgi.WSGIRequest): response = HttpResponse() if request.method == 'GET': user_name = session.get_user_name(request) user = User.get_user_by_user_name(user_name) videos = models.Video.objects.filter(user_id=user.id) ret = [] for video in videos: ret.append(convert_video_model_to_dict(video)) utils.serve_data_response(response, ret) elif request.method == 'POST': data = json.loads(request.body) if not isinstance(data, dict): response.status_code = 400 response.content = 'wrong parameters: check the data' else: video_id = data.get('video_id') video_url = data.get('video_url') if video_id is None or video_url is None: utils.serve_400_response( 'wrong parameters: check the video_id and the video_url cannot be empty' ) else: models.Video.objects.filter(id=video_id).update( video_url=video_url) utils.serve_data_response(response, '') return response
def overview(request): ret_vars = {} if session.check_is_login(request): user_name = session.get_user_name(request) ret_vars['username'] = user_name user = models.User.objects.get(name=user_name) user_message = models.User_Message.objects.values('message_id').filter( user_id=user.id) user_message_list = [i['message_id'] for i in user_message] message_list = models.Message.objects.exclude(id__in=user_message_list) message_count = models.Message.objects.count() device_count = models.Device.objects.count() devices = models.Device.objects.all() device_points = [] for d in devices: device_points.append( [d.location_x, d.location_y, d.video_url, d.hint]) ret_vars['message_list'] = serializers.serialize('json', message_list) ret_vars['device_count'] = device_count ret_vars['un_deal'] = message_count - len(user_message) ret_vars['user_deal'] = len(user_message) ret_vars['device_points'] = json.dumps(device_points) print(user_message) return render(request, 'overview.html', ret_vars) return redirect('/login')
def approve_message_controller(request: wsgi.WSGIRequest): response = HttpResponse() if request.method == "POST": data = json.loads(request.body) params = data.get('params') print(params) if not isinstance(params, dict): response.content = 'wrong parameters : params' response.status_code = 400 message_ids = params.get('message_ids') approve_status = params.get('approve_status') print(message_ids) print(approve_status) if message_ids is not None and approve_status is not None and isinstance(approve_status, bool): user_name = session.get_user_name(request) user = user_model.get_user_by_user_name(user_name) for message_id in message_ids: message = get_message_by_message_model(message_id) if models.User_Message.objects.filter(user_id=user.id, message_id=message_id).exists(): models.User_Message.objects.filter(user_id=user.id, message_id=message_id).update( true_or_false=approve_status) else: models.User_Message.objects.create(user_id=user, message_id=message, true_or_false=approve_status) utils.serve_data_response(response,'') else: response.content = 'wrong parameters : message_ids or approve_status is empty or incorrect' response.status_code = 400 return response
def check_is_login_and_get_username_controller(request): response = HttpResponse() if session.check_is_login(request): response.status_code = 200 user_name = session.get_user_name(request) print(user_name) utils.serve_data_response(response, user_name) else: response.status_code = 400 response.content = 'Not login' return response
def get_user_info_controller(request: wsgi.WSGIRequest): response = HttpResponse() if request.method == 'GET': utils.serve_data_response( response, convert_user_model_to_dict( get_user_by_user_name(session.get_user_name(request)))) else: response.status_code = 400 return response
def monitor(request): ret_vars = {} if session.check_is_login(request): user_name = session.get_user_name(request) ret_vars['username'] = user_name user = models.User.objects.get(name=user_name) video_list = models.Video.objects.filter(user_id=user.id) ret_vars['video_list'] = serializers.serialize('json', video_list) ret_vars['default_layout'] = user.layout return render(request, 'monitor.html', ret_vars) return redirect('/login')
def get_message_info_controller(request: wsgi.WSGIRequest): response = HttpResponse() if request.method == "GET": user_name = session.get_user_name(request) utils.serve_data_response(response, { "total": len(get_all_messages()), "unread": len(get_messages_unread_by_user_name(user_name)) }) else: response.status_code = 400 response.content = 'wrong http method' return response
def dealFalse(request): response = HttpResponse() if not session.check_is_login(request): response.status_code = 400 response.content = '没有登陆' return response if request.method == 'POST': id_array = request.POST['data'].split(',') print(id_array) user = models.User.objects.get(name=session.get_user_name(request)) try: for id in id_array: message = models.Message.objects.get(id=id) if models.User_Message.objects.filter( user_id=user, message_id=message).exists(): models.User_Message.objects.filter( user_id=user, message_id=message).update(true_or_false=False) else: models.User_Message.objects.create( user_id=user, message_id=models.Message.objects.get(id=id), true_or_false=False) except: response.status_code = 500 raise Exception() else: response.status_code = 200 return response else: response.status_code = 404 response.content = '找不到页面' return response
def warning(request): print("This is the type") print(type(request)) ret_vars = {} if session.check_is_login(request): user_name = session.get_user_name(request) ret_vars['username'] = user_name user = models.User.objects.get(name=user_name) user_message = models.User_Message.objects.values( 'message_id', 'true_or_false').filter(user_id=user.id) print(user_message) user_message_dict = {} for i in user_message: print(i) user_message_dict[i['message_id']] = 'true' print(user_message_dict) message_list = models.Message.objects.all() user_message_list = [{ 'id': i.id, 'content': i.content, 'img_url': i.img_url, 'device_number': i.serial_number.id, 'device_hint': i.serial_number.hint, 'deal': i.id in user_message_dict, 'c_time': i.c_time.strftime('%Y-%m-%d') } for i in message_list] print(user_message_list) ret_vars['user_message_list'] = json.dumps(user_message_list) # ret_vars['message_list'] = serializers.serialize('json', message_list) return render(request, 'warning.html', ret_vars) return redirect('/login')
def messages(request: wsgi.WSGIRequest): response = HttpResponse() if request.method == "GET": user_name = session.get_user_name(request) offset = int(request.GET.get('offset', 0)) limit = int(request.GET.get('limit', - 1)) the_type = request.GET.get('type', 'all') response.status_code = 200 if the_type == 'all': utils.serve_data_response(response, get_all_messages_by_user_name(user_name, limit, offset)) elif the_type == 'unread': utils.serve_data_response(response, get_messages_unread_by_user_name(user_name, limit, offset)) else: response.content = "wrong parameter 'type' " response.status_code = 400 elif request.method == "POST": data = json.loads(request.body) params = data.get('params') if not isinstance(params, dict): response.content = "wrong prameters params" response.status_code = 400 return response messages_id = params.get('id') try: if messages_id is not None: if models.Message.objects.filter(id=messages_id).exists(): models.Message.objects.filter(id=messages_id).update(content=params.get('content'), img_url=params.get('img_url')) except db.DatabaseError: utils.serve_error_response(response, status_code.MESSAGE_MODEL_CANNOT_UPDATE, str(db.DatabaseError)) except: utils.serve_error_response(response, status_code.MESSAGE_MODEL_CANNOT_UPDATE, "Error: post message") else: utils.serve_data_response(response, params.get('id')) return response