# Path: server/apps/main/models.py
# class ContactMessage(models.Model):
# 
#     name = models.CharField(max_length=50)
#     email = models.EmailField(max_length=60)
#     phone = models.CharField(max_length=50, blank=True)
#     message = models.TextField()
# 
#     created_on = models.DateTimeField('created_on', auto_now_add=True)
# 
#     class Meta:
#         ordering = ['id']
# 
#     def __str__(self):
#         return self.name
# 
# Path: server/apps/main/permissions.py
# class ContactMessagePermission(permissions.BasePermission):
#     """
#     Custom permission to only allow owners of an object to access (read/write)
#     """
# 
#     def has_permission(self, request, view):
#         if request.method == 'POST':
#             return True
#         else:
#             return request.user.is_staff
# 
#     def has_object_permission(self, request, view, obj):
#         # Everybody can submit
#         return request.user.is_staff
# 
# Path: server/apps/main/serializers.py
# class ContactMessageSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = ContactMessage
#         fields = ('id', 'name', 'email', 'phone', 'message', 'created_on')
#         read_only_fields = ('created_on')
# 
# Path: server/apps/main/tasks.py
# def send_contact_me_notification(instance):
#     '''
#     Use SNS to notify Staff of person trying to contact
#     us via the Landing Page
# 
#     :param instance: ContactMessage object to email
#     :return: Nothing
#     '''
# 
#     message  = '==========================\n'
#     message += '%s\n' % instance.name
#     message += '%s\n' % instance.email
#     message += '==========================\n'
#     message += instance.message
#     message += '\n'
#     message += '==========================\n'
# 
#     mail_admins(subject='New Message', message=message)
# 
#     return True


import json
from django.conf import settings
from django.contrib.auth.models import User
from django.core.exceptions import PermissionDenied
from django.http import Http404, HttpResponse
from django.shortcuts import get_object_or_404
from rest_framework import generics, mixins, status, viewsets
from rest_framework.parsers import JSONParser
from rest_framework.permissions import IsAdminUser
from rest_framework.renderers import JSONRenderer
from rest_framework.response import Response
from rest_framework.views import APIView
from .models import ContactMessage
from .permissions import ContactMessagePermission
from .serializers import ContactMessageSerializer
from .tasks import send_contact_me_notification





class APIMessageViewSet(mixins.CreateModelMixin,
                                mixins.ListModelMixin,
                                mixins.RetrieveModelMixin,
                                viewsets.GenericViewSet):
    """
    This viewset automatically provides `list`, `create`, `retrieve`,
    `update` and `destroy` actions.

    Additionally we also provide an extra `highlight` action.
    """
    queryset = ContactMessage.objects.all()
    serializer_class = ContactMessageSerializer
    permission_classes = (ContactMessagePermission,)


    def get_queryset(self):
        """
        This view should return a list of all records
        for the currently authenticated user.
        """
        #user = self.request.user

# Path: server/apps/main/models.py
# class ContactMessage(models.Model):
# 
#     name = models.CharField(max_length=50)
#     email = models.EmailField(max_length=60)
#     phone = models.CharField(max_length=50, blank=True)
#     message = models.TextField()
# 
#     created_on = models.DateTimeField('created_on', auto_now_add=True)
# 
#     class Meta:
#         ordering = ['id']
# 
#     def __str__(self):
#         return self.name
# 
# Path: server/apps/main/permissions.py
# class ContactMessagePermission(permissions.BasePermission):
#     """
#     Custom permission to only allow owners of an object to access (read/write)
#     """
# 
#     def has_permission(self, request, view):
#         if request.method == 'POST':
#             return True
#         else:
#             return request.user.is_staff
# 
#     def has_object_permission(self, request, view, obj):
#         # Everybody can submit
#         return request.user.is_staff
# 
# Path: server/apps/main/serializers.py
# class ContactMessageSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = ContactMessage
#         fields = ('id', 'name', 'email', 'phone', 'message', 'created_on')
#         read_only_fields = ('created_on')
# 
# Path: server/apps/main/tasks.py
# def send_contact_me_notification(instance):
#     '''
#     Use SNS to notify Staff of person trying to contact
#     us via the Landing Page
# 
#     :param instance: ContactMessage object to email
#     :return: Nothing
#     '''
# 
#     message  = '==========================\n'
#     message += '%s\n' % instance.name
#     message += '%s\n' % instance.email
#     message += '==========================\n'
#     message += instance.message
#     message += '\n'
#     message += '==========================\n'
# 
#     mail_admins(subject='New Message', message=message)
# 
#     return True


# Path: server/apps/main/api_views.py
import json
from django.conf import settings
from django.contrib.auth.models import User
from django.core.exceptions import PermissionDenied
from django.http import Http404, HttpResponse
from django.shortcuts import get_object_or_404
from rest_framework import generics, mixins, status, viewsets
from rest_framework.parsers import JSONParser
from rest_framework.permissions import IsAdminUser
from rest_framework.renderers import JSONRenderer
from rest_framework.response import Response
from rest_framework.views import APIView
from .models import ContactMessage
from .permissions import ContactMessagePermission
from .serializers import ContactMessageSerializer
from .tasks import send_contact_me_notification






class APIMessageViewSet(mixins.CreateModelMixin,
                                mixins.ListModelMixin,
                                mixins.RetrieveModelMixin,
                                viewsets.GenericViewSet):
    """
    This viewset automatically provides `list`, `create`, `retrieve`,
    `update` and `destroy` actions.

    Additionally we also provide an extra `highlight` action.
    """
    queryset = ContactMessage.objects.all()
    serializer_class = ContactMessageSerializer
    permission_classes = (ContactMessagePermission,)


    def get_queryset(self):
        """
        This view should return a list of all records
        for the currently authenticated user.
        """
        #user = self.request.user

        return ContactMessage.objects.all()