def replace_options_agent_console(request): "Tries to update the agent console options" permission_obj = PermissionValidation(request) validation = permission_obj.validate('replace_options_agent_console') if validation['status']: data = request.data.copy() url = data['CRM_URL'] redirection_time = data['REDIRECT_TIME'] try: option_url = AgentConsoleOptions.objects.get(option='CRM_URL') except AgentConsoleOptions.DoesNotExist: option_url = AgentConsoleOptions() option_url.option = 'CRM_URL' try: option_redirection = AgentConsoleOptions.objects.get( option='REDIRECT_TIME') except AgentConsoleOptions.DoesNotExist: option_redirection = AgentConsoleOptions() option_redirection.option = 'REDIRECT_TIME' option_url.value = url option_redirection.value = redirection_time option_url.save() option_redirection.save() return Response({"success": True}, status=status.HTTP_200_OK, content_type='application/json') return PermissionValidation.error_response_webservice(validation, request)
def get_options_agent_console(request): "Return a JSON response with user data for the given id" permission_obj = PermissionValidation(request) validation = permission_obj.validate('get_options_agent_console') if validation['status']: try: option_url = AgentConsoleOptions.objects.get(option='CRM_URL') except AgentConsoleOptions.DoesNotExist: option_url = AgentConsoleOptions() option_url.option = 'CRM_URL' try: option_redirection = AgentConsoleOptions.objects.get( option='REDIRECT_TIME') except AgentConsoleOptions.DoesNotExist: option_redirection = AgentConsoleOptions() option_redirection.option = 'REDIRECT_TIME' options = { option_url.option: option_url.value, option_redirection.option: option_redirection.value } data = {"success": True, "data": options} return Response(data, status=status.HTTP_200_OK, content_type='application/json') return PermissionValidation.error_response_webservice(validation, request)
def list_profile(request): """ Returns a JSON response containing registered users""" permission_obj = PermissionValidation(request) validation = permission_obj.validate('list_profile') if validation['status']: sent_data = request.data draw = int(sent_data['draw']) start = int(sent_data['start']) length = int(sent_data['length']) search = sent_data['search[value]'] records_total = Profile.objects.count() if search != '': queryset = Profile.profiles_listing_filter(search, start, length) records_filtered = Profile.profiles_listing_filter( search, start, length, True) else: queryset = Profile.objects.all()[start:start + length] records_filtered = records_total result = ProfileSerializer(queryset, many=True) data = { 'draw': draw, 'recordsTotal': records_total, 'recordsFiltered': records_filtered, 'data': result.data } return Response(data, status=status.HTTP_200_OK, content_type='application/json') return PermissionValidation.error_response_webservice(validation, request)
def add_profile(request): """Tries to create an profile and returns the result""" permission_obj = PermissionValidation(request) validation = permission_obj.validate('add_profile') if validation['status']: json_actions = request.data['actions'] profile_serializer = ProfileSerializer(data=request.data) actions = json.loads(json_actions) if profile_serializer.is_valid(): profile_serializer.save() profile_id = profile_serializer.data['id'] for x in range(0, len(actions)): actions[x]['profile'] = profile_id permission_serializer = ProfilePermissionsSerializer( data=actions[x]) if permission_serializer.is_valid(): permission_serializer.save() return Response({"success": True}, status=status.HTTP_201_CREATED, content_type='application/json') data = error_data(profile_serializer) return Response(data, status=status.HTTP_400_BAD_REQUEST, content_type='application/json') return PermissionValidation.error_response_webservice(validation, request)
def auto_generate_users(request): "Return a JSON response with user data for the given id" permission_obj = PermissionValidation(request) validation = permission_obj.validate('auto_generate_users') if validation['status']: try: GenerateUsers.bulk_create_users() return Response( { 'success': True, 'message': 'Usuarios Generados con exito' }, status=status.HTTP_200_OK, content_type='application/json') except: return Response( { 'success': False, 'message': 'Ocurrio un error al intentar generar los usuarios' }, status=status.HTTP_200_OK, content_type='application/json') return PermissionValidation.error_response_webservice(validation, request)
def listing_profile(request): "Returns the rendered template for profile listing." permission_obj = PermissionValidation(request) validation = permission_obj.validate('listing_profile') if validation['status']: return render(request, 'profiles/listing.html', {'username': permission_obj.user.name}) return PermissionValidation.error_response_view(validation, request)
def set_user_agent(request): """Tries to create an user and returns the result""" permission_obj = PermissionValidation(request) validation = permission_obj.validate('set_user_agent') if validation['status']: data = request.data user_id = data['user'] agent_id = data['agent'] if agent_id == "": return Response({"success": True}, status=status.HTTP_200_OK, content_type='application/json') else: try: query = UserAgent.objects.filter(user=user_id) if len(query) > 0: for obj in query: model = obj if agent_id == "": model.delete() return Response( { "success": True, "message": "UserAgent deleted" }, status=status.HTTP_200_OK, content_type='application/json') serializer = UserAgentSerializer(instance=model, data=data) else: serializer = UserAgentSerializer(data=data) if serializer.is_valid(): serializer.save() return Response({"success": True}, status=status.HTTP_201_CREATED, content_type='application/json') data = error_data(serializer) return Response(data, status=status.HTTP_400_BAD_REQUEST, content_type='application/json') except Agent.DoesNotExist: data = { 'success': False, 'message': 'El agente se borro del servidor de telefonía, intente con otro agente' } return Response(data, status=status.HTTP_200_OK, content_type='application/json') return PermissionValidation.error_response_webservice(validation, request)
def delete_profile(request, profile_id): """Tries to delete an profile and returns the result.""" permission_obj = PermissionValidation(request) validation = permission_obj.validate('delete_profile') if validation['status']: profile_obj = Profile.objects.get(id=profile_id) profile_obj.delete() data = {"success": True, "message": "Perfil elminado exitosamente"} return Response(data, status=status.HTTP_200_OK, content_type='application/json') return PermissionValidation.error_response_webservice(validation, request)
def picker_search_agent(request): "Returns a JSON response with user data for a selectpicker." permission_obj = PermissionValidation(request) validation = permission_obj.validate('picker_search_agent') if validation['status']: value = request.data['value'] queryset = Agent.agent_picker_filter(value) serializer = AgentSerializer(queryset, many=True) result = serializer.data data = {"success": True, "result": result} return Response(data, status=status.HTTP_200_OK, content_type='application/json') return PermissionValidation.error_response_webservice(validation, request)
def options_form(request): "Returns the rendered template for the given user." permission_obj = PermissionValidation(request) validation = permission_obj.validate('options_form') if validation['status']: data = { "success": "True", 'username': permission_obj.user.name } return render( request, 'agent_console/options_form.html', data ) return PermissionValidation.error_response_view(validation, request)
def form_profile(request, profile_id=0): "Returns the rendered template for the given profile." permission_obj = PermissionValidation(request) validation = permission_obj.validate('form_profile') if validation['status']: if profile_id == 0: action = "Crear" else: action = "Actualizar" result = actions_by_app() return render( request, 'profiles/form.html', { 'id': profile_id, 'action': action, 'result': result, 'username': permission_obj.user.name }) return PermissionValidation.error_response_view(validation, request)
def form_user_agent(request, user_id=0): "Returns the rendered template for the given user." permission_obj = PermissionValidation(request) validation = permission_obj.validate('form_user_agent') if validation['status']: if user_id == 0: action = "Crear" else: action = "Actualizar" return render( request, 'agent_console/form.html', { 'id':user_id, 'action':action, 'username': permission_obj.user.name } ) return PermissionValidation.error_response_view(validation, request)
def toggle_profile(request, profile_id): """Toogles the active state for a given profile""" permission_obj = PermissionValidation(request) validation = permission_obj.validate('toggle_profile') if validation['status']: profile_obj = Profile.objects.get(id=profile_id) previous = profile_obj.active if previous: message = "Perfil desactivado con exito" else: message = "Perfil activado con exito" profile_obj.active = not profile_obj.active profile_obj.save() data = {"success": True, "message": message} return Response(data, status=status.HTTP_200_OK, content_type='application/json') return PermissionValidation.error_response_webservice(validation, request)
def get_agent(request, user_id): "Return a JSON response with agent data for the given id" permission_obj = PermissionValidation(request) validation = permission_obj.validate('get_agent') if validation['status']: try: user_agent_obj = UserAgent.objects.get(user=user_id) agent_id = user_agent_obj.agent agent_obj = Agent.objects.get(id=agent_id) agent_serializer = AgentSerializer(agent_obj) agent_data = agent_serializer.data.copy() data = {"success": True, "data": agent_data} except: data = {"success": False} return Response(data, status=status.HTTP_200_OK, content_type='application/json') return PermissionValidation.error_response_webservice(validation, request)
def get_crm_url(request): """Gets the agent state""" permission_obj = PermissionValidation(request) validation = permission_obj.validate('get_crm_url') if validation['status']: try: option = AgentConsoleOptions.objects.get(option='CRM_URL') data = {'success': True, 'url': option.value} except AgentConsoleOptions.DoesNotExist: data = { 'success': False, 'url': "No se ha especificado un valor para la url de redirección" } return Response(data, status=status.HTTP_200_OK, content_type='application/json') return PermissionValidation.error_response_webservice(validation, request)
def get_profile(request, profile_id): "Return a JSON response with profile data for the given id" permission_obj = PermissionValidation(request) validation = permission_obj.validate('get_profile') if validation['status']: profile_obj = Profile.objects.get(id=profile_id) profile_serializer = ProfileSerializer(profile_obj) permission_list = ProfilePermissions.objects.filter( profile=profile_obj.id) permissions_serializer = ProfilePermissionsSerializer(permission_list, many=True) data = { "success": True, "data": profile_serializer.data, "actions": permissions_serializer.data } return Response(data, status=status.HTTP_200_OK, content_type='application/json') return PermissionValidation.error_response_webservice(validation, request)
def agent_console(request): "Returns the rendered template for the given user." permission_obj = PermissionValidation(request) validation = permission_obj.validate('agent_console') if validation['status']: id_user = permission_obj.user.id try: user_agent = UserAgent.objects.get(user=id_user) id_agent = user_agent.agent except UserAgent.DoesNotExist: id_agent = "null" data = { 'id_agent': id_agent, 'username': permission_obj.user.name } return render( request, 'agent_console/agent_console.html', data ) return PermissionValidation.error_response_view(validation, request)
def replace_profile(request, profile_id): "Tries to update an user and returns the result" permission_obj = PermissionValidation(request) validation = permission_obj.validate('replace_profile') if validation['status']: profile_obj = Profile.objects.get(id=profile_id) profile_serializer = ProfileSerializer(profile_obj, data=request.data) json_actions = request.data['actions'] actions = json.loads(json_actions) if profile_serializer.is_valid(): profile_serializer.save() profile_id = profile_serializer.data['id'] for element in range(0, len(actions)): action_id = actions[element]['action'] try: permission_obj = ProfilePermissions.objects.get( profile=profile_id, action=action_id) actions[element]['profile'] = profile_id permission_serializer = ProfilePermissionsSerializer( permission_obj, data=actions[element]) except: actions[element]['profile'] = profile_id permission_serializer = ProfilePermissionsSerializer( data=actions[element]) if permission_serializer.is_valid(): permission_serializer.save() else: print(permission_serializer.errors) return Response({"success": True}, status=status.HTTP_200_OK, content_type='application/json') data = error_data(profile_serializer) return Response(data, status=status.HTTP_400_BAD_REQUEST, content_type='application/json') return PermissionValidation.error_response_webservice(validation, request)
def agent_state(request): """Gets the agent state""" permission_obj = PermissionValidation(request) validation = permission_obj.validate('agent_state') if validation['status']: agent_state_obj = AgentState(False) data = request.data previous_state = data['previous_state'] id_agent = data['id_agent'] previous_call = data['previous_call'] state, current_call = agent_state_obj.check_state(id_agent) if (state == 4 and current_call.uniqueid != previous_call ) or state != previous_state: answer = agent_state_obj.get_answer(state, id_agent, current_call) else: answer = {'update': False} return Response(answer, status=status.HTTP_200_OK, content_type='application/json') return PermissionValidation.error_response_webservice(validation, request)
def login(request): """ Returns the render for the login page""" permission_obj = PermissionValidation(request) timezone = pytz.timezone("America/Bogota") date_aware = timezone.localize(datetime.now()) if permission_obj.login_session is None or permission_obj.login_session.life < date_aware: now = datetime.now() return render( request, 'maingui/login.html', { 'year': str(now.year) + '@' } ) return redirect('index')
def index(request): """ Returns the render for the index page""" permission_obj = PermissionValidation(request) if permission_obj.login_session is not None: return redirect('agent_console') return redirect('login')