<commit_before>from .serializers import StarSerializer, StarSmallSerializer
from .serializers import StarEmployeesSubcategoriesSerializer, StarTopEmployeeLists
from .serializers import StarKeywordList
from .models import Star
from employees.models import Employee
from categories.models import Category, Keyword, Subcategory
from django.db.models import Count
from django.shortcuts import get_object_or_404
from rest_framework import status
from rest_framework.authentication import SessionAuthentication, BasicAuthentication
from rest_framework.decorators import api_view, authentication_classes
from rest_framework.exceptions import APIException
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response


class CsrfExemptSessionAuthentication(SessionAuthentication):

    def enforce_csrf(self, request):
        return  # To not perform csrf check


@api_view(['POST', ])
@authentication_classes((CsrfExemptSessionAuthentication, BasicAuthentication))
def give_star_to(request, from_employee_id, to_employee_id):
    """
    This endpoint saves stars on both employees (from and to).
    ---
    response_serializer: StarSerializer
    responseMessages:
    - code: 400
      message: Bad request
    - code: 404
      message: Not found (from_employee_id or to_employee_id or category or subcategory)
    - code: 406
      message: User is unable to give stars to itself.
    parameters:
    - name: body
      description: JSON Object containing two or three parameters = category(id), subcategory(id)  and text(optional).
      required: true
      paramType: body
      pytype: stars.serializers.StarSwaggerSerializer
    """
    # Set None as initial values for variables in content json
    category_id = None
    subcategory_id = None
    keyword_id = None
    text = None

    if from_employee_id == to_employee_id:
        content = {'detail': 'User is unable to give stars to itself.'}
        return Response(content, status=status.HTTP_406_NOT_ACCEPTABLE)
    elif request.method == 'POST':
        # Set values from request.data from POST
        text = (request.data['text'] if 'text' in request.data.keys() else None)
        from_user = get_object_or_404(Employee, pk=from_employee_id)
        to_user = get_object_or_404(Employee, pk=to_employee_id)
        category_id = (request.data['category'] if request.data['category'] else None)
        category = get_object_or_404(Category, pk=category_id)
        subcategory_id = (request.data['subcategory'] if request.data['subcategory'] else None)
        subcategory = get_object_or_404(Subcategory, pk=subcategory_id)
        keyword_id = (request.data['keyword'] if request.data['keyword'] else None)
        keyword = get_object_or_404(Keyword, pk=keyword_id)

        # Create data object to save
        data = {"category": category.id,
                "subcategory": subcategory.id,
                "keyword": keyword_id,
                "text": text,
                "from_user": from_user.id,
                "to_user": to_user.id}

        # Validate serializer with data provided.
        serializer = StarSerializer(data=data)
        if serializer.is_valid():
            # Save recommendation
            serializer.save()

            # Add 1 point to from_user
            from_user.total_score += 1
            from_user.current_month_score += 1
            from_user.evaluate_level()
            from_user.save()

            # Add points to to_user according category weight
            to_user.total_score += category.weight
            to_user.current_month_score += category.weight
            to_user.evaluate_level()
            to_user.save()

            return Response(serializer.data, status=status.HTTP_201_CREATED)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


@api_view(['GET', ])<commit_msg>Refactor star post view in order to keep simple category, subcategory and keyword values<commit_after>from .serializers import StarSerializer, StarSmallSerializer
from .serializers import StarEmployeesSubcategoriesSerializer, StarTopEmployeeLists
from .serializers import StarKeywordList
from .models import Star
from employees.models import Employee
from categories.models import Category, Keyword, Subcategory
from django.db.models import Count
from django.shortcuts import get_object_or_404
from rest_framework import status
from rest_framework.authentication import SessionAuthentication, BasicAuthentication
from rest_framework.decorators import api_view, authentication_classes
from rest_framework.exceptions import APIException
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response


class CsrfExemptSessionAuthentication(SessionAuthentication):

    def enforce_csrf(self, request):
        return  # To not perform csrf check


@api_view(['POST', ])
@authentication_classes((CsrfExemptSessionAuthentication, BasicAuthentication))
def give_star_to(request, from_employee_id, to_employee_id):
    """
    This endpoint saves stars on both employees (from and to).
    ---
    response_serializer: StarSerializer
    responseMessages:
    - code: 400
      message: Bad request
    - code: 404
      message: Not found (from_employee_id or to_employee_id or category or subcategory)
    - code: 406
      message: User is unable to give stars to itself.
    parameters:
    - name: body
      description: JSON Object containing two or three parameters = category(id), subcategory(id)  and text(optional).
      required: true
      paramType: body
      pytype: stars.serializers.StarSwaggerSerializer
    """
    # Set None as initial values for variables in content json
    category_id = None
    subcategory_id = None
    keyword_id = None
    text = None

    if from_employee_id == to_employee_id:
        content = {'detail': 'User is unable to give stars to itself.'}
        return Response(content, status=status.HTTP_406_NOT_ACCEPTABLE)
    elif request.method == 'POST':
        # Set values from request.data from POST
        text = (request.data['text'] if 'text' in request.data.keys() else None)
        from_user = get_object_or_404(Employee, pk=from_employee_id)
        to_user = get_object_or_404(Employee, pk=to_employee_id)
        category = get_object_or_404(Category, pk=request.data['category'])
        subcategory = get_object_or_404(Subcategory, pk=request.data['subcategory'])
        keyword = get_object_or_404(Keyword, pk=request.data['keyword'])

        # Create data object to save
        data = {"category": category.id,
                "subcategory": subcategory.id,
                "keyword": keyword.id,
                "text": text,
                "from_user": from_user.id,
                "to_user": to_user.id}

        # Validate serializer with data provided.
        serializer = StarSerializer(data=data)
        if serializer.is_valid():
            # Save recommendation
            serializer.save()

            # Add 1 point to from_user
            from_user.total_score += 1
            from_user.current_month_score += 1
            from_user.evaluate_level()
            from_user.save()

            # Add points to to_user according category weight
            to_user.total_score += category.weight
            to_user.current_month_score += category.weight
            to_user.evaluate_level()
            to_user.save()

            return Response(serializer.data, status=status.HTTP_201_CREATED)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


@api_view(['GET', ])
{'fraction_non_alphanumeric': 0.07476635514018691, 'fraction_numerical': 0.010267210741082005, 'mean_word_length': 3.2613572630398204, 'pattern_counts': {'":': 12, '<': 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': '2076445', 'n_tokens_mistral': 2119, 'n_tokens_neox': 1998, 'n_words': 569}