-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbriefingAgenciesView.py
69 lines (57 loc) · 2.98 KB
/
briefingAgenciesView.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from django.contrib.auth import login, authenticate
from qvsta_server.qvsta_api.serializers.briefingSerializers.briefingAgenciesSerializer import BriefingAgenciesEmailSerializer, BriefingAgenciesSerializer, BriefingAgenciesWithBriefingIDSerializer
from qvsta_server.qvsta_api.models import BriefingAgencies, Companies
from rest_framework import status, response, mixins, generics
from rest_framework.response import Response
from rest_framework.decorators import api_view
from rest_framework.views import APIView
from django.http import Http404
from rest_framework.filters import SearchFilter
from django_filters.rest_framework import DjangoFilterBackend
from django.http import Http404
from django.core.mail import send_mail
from django.template.loader import get_template, render_to_string
from django.template import Context
class BriefingsAgenciesList(generics.ListCreateAPIView):
queryset = BriefingAgencies.objects.all()
serializer_class = BriefingAgenciesWithBriefingIDSerializer
filter_backends = (DjangoFilterBackend,)
filter_fields = ('briefingID',)
class BriefingsAgenciesDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = BriefingAgencies.objects.all()
serializer_class = BriefingAgenciesWithBriefingIDSerializer
class BriefingsAgenciesSendEmail(generics.ListCreateAPIView):
queryset = BriefingAgencies.objects.all()
serializer_class = BriefingAgenciesEmailSerializer
filter_backends = (DjangoFilterBackend,)
filter_fields = ('briefingID',)
def post(self, request, format = None):
serializer = BriefingAgenciesEmailSerializer(data=request.data)
if serializer.is_valid() == False:
print("invalid input")
return Response(status=status.HTTP_400_BAD_REQUEST)
if BriefingAgencies.objects.filter(briefingID = serializer.initial_data.get('briefingID')).exists():
briefingID = serializer.initial_data.get('briefingID')
else:
print("briefingID doesn't exist")
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
agencyIDs = BriefingAgencies.objects.filter(briefingID = briefingID).values_list('agencyID', flat = True)
agencyList = list(set(agencyIDs))
emailList = []
for agency in agencyList:
emailList.append(Companies.objects.get(companyID = agency).email)
print("valid or resending")
print(serializer.initial_data.get('email'))
print(serializer.initial_data.get('uniqueID'))
for agency in agencyList:
subject = "TEST FOR BRIEFINGAGENCIES"
from1 = '[email protected]'
send_mail(
subject,
'Hello',
from1,
[Companies.objects.get(companyID = agency).email],
fail_silently=False,
html_message= BriefingAgencies.objects.get(agencyID = agency, briefingID = briefingID).emailTemplate
)
return Response(serializer.data, status=status.HTTP_201_CREATED)