import logging from django.shortcuts import render, get_object_or_404 from django.core.mail import EmailMessage from django.template.loader import render_to_string # Create your views here. from rest_framework import viewsets from rest_framework.response import Response from rest_framework.permissions import IsAuthenticatedOrReadOnly, IsAuthenticated from rest_framework.decorators import api_view, permission_classes, action from companies.models import Company from companies.serializers import CompanySerializer from back_latienda.permissions import IsCreator class CompanyViewSet(viewsets.ModelViewSet): queryset = Company.objects.all() serializer_class = CompanySerializer permission_classes = [IsAuthenticatedOrReadOnly, IsCreator] def perform_create(self, serializer): serializer.save(creator=self.request.user) @action(detail=True, methods=['POST', ]) def email_manager(self, request, **kwargs): """ Send email to company.creator """ try: queryset = self.get_custom_queryset(request) instance = queryset.filter(pk=kwargs['pk']).first() if instance: data = json.loads(request.body) if request.user.is_authenticated: # send email to manager company_message = render_to_string('company_contact.html', { 'company': instance, 'email': request.user.email, 'full_name': request.user.full_name, 'quantity': data['quantity'], 'phone_number': data.get('phone_number'), 'comments': data['comments'], 'product_info': data['product_info'], }) user_message = render_to_string('confirm_company_contact.html', { 'company': instance, 'username': request.user.full_name, 'data': data, }) # send email to company subject = "Contacto de usuario" email = EmailMessage(subject, company_message, to=[instance.creator.email]) email.send() logging.info(f"Email sent to {instance.creator.email} as manager of {instance.name}") # send confirmation email to user subject = 'Confirmación de contacto' email = EmailMessage(subject, message, to=[request.user.email]) email.send() logging.info(f"Contact confirmation email sent to {request.user.email}") else: # for unauthenticated users company_message = render_to_string('company_contact.html', { 'company': instance, 'email': data['email'], 'full_name': data['full_name'], 'quantity': data['quantity'], 'phone_number': data.get('phone_number'), 'comments': data['comments'], 'product_info': data['product_info'], }) user_message = render_to_string('confirm_company_contact.html', { 'company': instance, 'username': data['full_name'], }) # send email to company email = EmailMessage(subject, company_message, to=[instance.creator.email]) email.send() logging.info(f"Email sent to {instance.creator.email} as manager of {instance.name}") # send confirmation email to user email = EmailMessage(subject, user_message, to=[data['email']]) email.send() logging.info(f"Contact confirmation email sent to anonymous user {data['email']}") # TODO: create statslog instance to rgister interaction return Response(data=data) else: return Response({"errors":{"details": f"No instance of company with id {kwargs['pk']}",}}) except Exception as e: return Response({"errors":{"details": str(e),}}, status=500) @api_view(['GET',]) @permission_classes([IsAuthenticated,]) def my_company(request): qs = Company.objects.filter(creator=request.user) company_serializer = CompanySerializer(qs) return Response(data=company_serializer.data)