# Path: api/serializers/request_serializers.py
# class SaveSupportMessageRequest(EmailSerializer):
#     text = serializers.CharField(max_length=5000)
# 
# Path: api/utils.py
# def invalid_data_response(request_serializer):
#     """
#     Returns the error message depending on missing/invalid data in serializer.
#     """
#     return Response({'detail': request_serializer.errors,
#                      'data': {}}, status=status.HTTP_400_BAD_REQUEST)
# 
# def validate_api_secret_key(secret_key):
#     """
#     Checks if request received from known origin Application. If not returns 404 (not found) status
#     """
#     if secret_key != settings.API_SECRET_KEY:
#         logger.info('Incorrect API key: "{}"'.format(secret_key))
#         raise Http404('The API key isn\t correct!')
# 
# Path: app/models.py
# class SupportMessage(models.Model):
#     """
#     Class for managing support messages.
#     """
#     email = models.EmailField()
#     text = models.TextField(max_length=5000)
#     posted_date = models.DateTimeField(auto_now_add=True)
#     is_checked = models.BooleanField(default=False)


import logging
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response
from ..serializers.request_serializers import SaveSupportMessageRequest
from ..utils import invalid_data_response, validate_api_secret_key
from app.models import SupportMessage
# -*- coding: utf-8 -*-




logger = logging.getLogger('changes')


# ----------------------------------------------------------------------------------------------------------------------
@api_view(['POST'])
def save_support_message(request):
    """
    Saves a support message if validated.
    """
    validate_api_secret_key(request.data.get('app_key'))
    request_serializer = SaveSupportMessageRequest(data=request.data)

    if request_serializer.is_valid():
        message = request.data.get('text')

        SupportMessage.objects.create(email=request.data.get('email'), text=message)

        return Response({'detail': 'successful',
                         'data': {}},
                        status=status.HTTP_200_OK)
    else:

# Path: api/serializers/request_serializers.py
# class SaveSupportMessageRequest(EmailSerializer):
#     text = serializers.CharField(max_length=5000)
# 
# Path: api/utils.py
# def invalid_data_response(request_serializer):
#     """
#     Returns the error message depending on missing/invalid data in serializer.
#     """
#     return Response({'detail': request_serializer.errors,
#                      'data': {}}, status=status.HTTP_400_BAD_REQUEST)
# 
# def validate_api_secret_key(secret_key):
#     """
#     Checks if request received from known origin Application. If not returns 404 (not found) status
#     """
#     if secret_key != settings.API_SECRET_KEY:
#         logger.info('Incorrect API key: "{}"'.format(secret_key))
#         raise Http404('The API key isn\t correct!')
# 
# Path: app/models.py
# class SupportMessage(models.Model):
#     """
#     Class for managing support messages.
#     """
#     email = models.EmailField()
#     text = models.TextField(max_length=5000)
#     posted_date = models.DateTimeField(auto_now_add=True)
#     is_checked = models.BooleanField(default=False)


# Path: api/views/additional_views.py
import logging
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response
from ..serializers.request_serializers import SaveSupportMessageRequest
from ..utils import invalid_data_response, validate_api_secret_key
from app.models import SupportMessage

# -*- coding: utf-8 -*-




logger = logging.getLogger('changes')


# ----------------------------------------------------------------------------------------------------------------------
@api_view(['POST'])
def save_support_message(request):
    """
    Saves a support message if validated.
    """
    validate_api_secret_key(request.data.get('app_key'))
    request_serializer = SaveSupportMessageRequest(data=request.data)

    if request_serializer.is_valid():
        message = request.data.get('text')

        SupportMessage.objects.create(email=request.data.get('email'), text=message)

        return Response({'detail': 'successful',
                         'data': {}},
                        status=status.HTTP_200_OK)
    else:

        return invalid_data_response(request_serializer)