<commit_before>from .models import Activity, Message
from .permissions import SendPushPermission
from .serializers import ActivitySerializer, MessageSerializer, NotificationSerializer
from constance import config
from employees.models import Employee, Location
from django.db.models import F, Q
from django.shortcuts import get_object_or_404
from itertools import chain
from rest_framework import status
from rest_framework.decorators import api_view, permission_classes
from rest_framework.pagination import PageNumberPagination
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from utils.send_messages import send_push_notification


@api_view(['POST', ])
@permission_classes((IsAuthenticated, SendPushPermission))
def send_message_all(request):
    """
    Send message to all employees
    ---
    response_serializer: employees.serializers.EmployeeDeviceSerializer
    parameters:
    - name: message
      type: string
      required: true
    responseMessages:
    - code: 401
      message: Unauthorized. Authentication credentials were not provided. Invalid token.
    - code: 403
      message: Forbidden.
    - code: 404
      message: Not found
    - code: 500
      message: Internal Server Error
    """
    if request.method == 'POST':
        if 'message' in request.data:
            employee_list = Employee.objects.all()
            message = Message(
                text=request.data['message'],
                from_user=request.user,
                to_user="all")
            message.save()
            for employee in employee_list:
                send_push_notification(employee, message.text)
            serializer = MessageSerializer(message)
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            content = {'detail': config.NO_MESSAGE}
            return Response(content, status=status.HTTP_400_BAD_REQUEST)


@api_view(['POST', ])
@permission_classes((IsAuthenticated, SendPushPermission))
def send_message_to(request, employee_username):
    """
    Send message to employee
    ---
    response_serializer: employees.serializers.EmployeeDeviceSerializer
    parameters:
    - name: message
      type: string
      required: true
    responseMessages:
    - code: 401
      message: Unauthorized. Authentication credentials were not provided. Invalid token.
    - code: 403
      message: Forbidden.
    - code: 404
      message: Not found
    - code: 500
      message: Internal Server Error
    """
    if request.method == 'POST':
        if 'message' in request.data:
            employee = get_object_or_404(Employee, username=employee_username)
            message = Message(
                text=request.data['message'],
                from_user=request.user,
                to_user=employee.username
            )
            message.save()
            send_push_notification(employee, message.text)
            serializer = MessageSerializer(message)
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            content = {'detail': config.NO_MESSAGE}
            return Response(content, status=status.HTTP_400_BAD_REQUEST)


@api_view(['POST', ])
@permission_classes((IsAuthenticated, SendPushPermission))
def send_message_location(request, location_id):
    """
    Send message to all employees by location
    ---
    response_serializer: employees.serializers.EmployeeDeviceSerializer
    parameters:
    - name: message
      type: string
      required: true
    responseMessages:
    - code: 401
      message: Unauthorized. Authentication credentials were not provided. Invalid token.
    - code: 403
      message: Forbidden.
    - code: 404
      message: Not found
    - code: 500
      message: Internal Server Error
    """
    if request.method == 'POST':
        if 'message' in request.data:
            employee_list = Employee.objects.all()
            location = get_object_or_404(Location, pk=location_id)
            message = Message(
                text=request.data['message'],
                from_user=request.user,
                to_user="location: " + location.name)
            message.save()
            for employee in employee_list:
                if employee.location == location:
                    send_push_notification(employee, message.text)
            serializer = MessageSerializer(message)
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            content = {'detail': config.NO_MESSAGE}
            return Response(content, status=status.HTTP_400_BAD_REQUEST)


@api_view(['GET', ])
@permission_classes((IsAuthenticated, SendPushPermission))
def get_messages(request, employee_id):
    """
    Get all messages for employee id
    ---
    response_serializer: activities.serializers.MessageSerializer
    responseMessages:<commit_msg>Remove location prefix from send message to location endpoint<commit_after>from .models import Activity, Message
from .permissions import SendPushPermission
from .serializers import ActivitySerializer, MessageSerializer, NotificationSerializer
from constance import config
from employees.models import Employee, Location
from django.db.models import F, Q
from django.shortcuts import get_object_or_404
from itertools import chain
from rest_framework import status
from rest_framework.decorators import api_view, permission_classes
from rest_framework.pagination import PageNumberPagination
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from utils.send_messages import send_push_notification


@api_view(['POST', ])
@permission_classes((IsAuthenticated, SendPushPermission))
def send_message_all(request):
    """
    Send message to all employees
    ---
    response_serializer: employees.serializers.EmployeeDeviceSerializer
    parameters:
    - name: message
      type: string
      required: true
    responseMessages:
    - code: 401
      message: Unauthorized. Authentication credentials were not provided. Invalid token.
    - code: 403
      message: Forbidden.
    - code: 404
      message: Not found
    - code: 500
      message: Internal Server Error
    """
    if request.method == 'POST':
        if 'message' in request.data:
            employee_list = Employee.objects.all()
            message = Message(
                text=request.data['message'],
                from_user=request.user,
                to_user="all")
            message.save()
            for employee in employee_list:
                send_push_notification(employee, message.text)
            serializer = MessageSerializer(message)
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            content = {'detail': config.NO_MESSAGE}
            return Response(content, status=status.HTTP_400_BAD_REQUEST)


@api_view(['POST', ])
@permission_classes((IsAuthenticated, SendPushPermission))
def send_message_to(request, employee_username):
    """
    Send message to employee
    ---
    response_serializer: employees.serializers.EmployeeDeviceSerializer
    parameters:
    - name: message
      type: string
      required: true
    responseMessages:
    - code: 401
      message: Unauthorized. Authentication credentials were not provided. Invalid token.
    - code: 403
      message: Forbidden.
    - code: 404
      message: Not found
    - code: 500
      message: Internal Server Error
    """
    if request.method == 'POST':
        if 'message' in request.data:
            employee = get_object_or_404(Employee, username=employee_username)
            message = Message(
                text=request.data['message'],
                from_user=request.user,
                to_user=employee.username
            )
            message.save()
            send_push_notification(employee, message.text)
            serializer = MessageSerializer(message)
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            content = {'detail': config.NO_MESSAGE}
            return Response(content, status=status.HTTP_400_BAD_REQUEST)


@api_view(['POST', ])
@permission_classes((IsAuthenticated, SendPushPermission))
def send_message_location(request, location_id):
    """
    Send message to all employees by location
    ---
    response_serializer: employees.serializers.EmployeeDeviceSerializer
    parameters:
    - name: message
      type: string
      required: true
    responseMessages:
    - code: 401
      message: Unauthorized. Authentication credentials were not provided. Invalid token.
    - code: 403
      message: Forbidden.
    - code: 404
      message: Not found
    - code: 500
      message: Internal Server Error
    """
    if request.method == 'POST':
        if 'message' in request.data:
            employee_list = Employee.objects.all()
            location = get_object_or_404(Location, pk=location_id)
            message = Message(
                text=request.data['message'],
                from_user=request.user,
                to_user=location.name)
            message.save()
            for employee in employee_list:
                if employee.location == location:
                    send_push_notification(employee, message.text)
            serializer = MessageSerializer(message)
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            content = {'detail': config.NO_MESSAGE}
            return Response(content, status=status.HTTP_400_BAD_REQUEST)


@api_view(['GET', ])
@permission_classes((IsAuthenticated, SendPushPermission))
def get_messages(request, employee_id):
    """
    Get all messages for employee id
    ---
    response_serializer: activities.serializers.MessageSerializer
    responseMessages:
{'fraction_non_alphanumeric': 0.08006486267355832, 'fraction_numerical': 0.012769838856795378, 'mean_word_length': 2.7463933181473044, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5281900', 'n_tokens_mistral': 2617, 'n_tokens_neox': 2460, 'n_words': 648}