Skip to content

Commit 539a4a2

Browse files
committed
Add email views
1 parent 7584f82 commit 539a4a2

File tree

5 files changed

+45
-5
lines changed

5 files changed

+45
-5
lines changed

interp/urls.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@
3030
url(r'^setting/$', Setting.as_view(), name='setting'),
3131
url(r'^$', FirstPage.as_view(), name='firstpage'),
3232
url(r'^get_trans_pdf/$', GetTranslatePDF.as_view(), name='gettranspdf'),
33+
url(r'^mail_trans_pdf/$', MailTranslatePDF.as_view(), name='mailtranspdf'),
3334
url(r'^get_trans_preview/$', GetTranslatePreview.as_view(), name='gettranspreview'),
3435
url(r'^get_task_pdf/$', GetTaskPDF.as_view(), name='gettaskpdf'),
36+
url(r'^mail_task_pdf/$', MailTaskPDF.as_view(), name='mailtaskpdf'),
3537

3638
url(r'^notifications/$', Notifications.as_view(), name='notifications'),
3739

interp/views/task.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import markdown
2+
from django.core.mail.message import EmailMultiAlternatives
3+
from django.http.response import HttpResponseRedirect
24

35
from django.shortcuts import render, redirect
46
from django.views.generic import View
@@ -99,7 +101,7 @@ class GetTaskPDF(LoginRequiredMixin, PDFTemplateView):
99101
'margin-right': '0.75in',
100102
'margin-bottom': '0.75in',
101103
'margin-left': '0.75in',
102-
'zoom': 15,
104+
# 'zoom': 15,
103105
'javascript-delay': 500,
104106
}
105107

@@ -118,3 +120,19 @@ def get_context_data(self, **kwargs):
118120
context['content'] = content
119121
context['title'] = self.filename
120122
return context
123+
124+
125+
class MailTaskPDF(GetTaskPDF):
126+
def get(self, request, *args, **kwargs):
127+
response = super(MailTaskPDF, self).get(request, *args, **kwargs)
128+
response.render()
129+
130+
subject, from_email, to = 'hello', '[email protected]', '[email protected]'
131+
text_content = 'Test'
132+
html_content = '<p>This is an <strong>TEST</strong> message.</p>'
133+
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
134+
msg.attach_alternative(html_content, "text/html")
135+
msg.attach('file.pdf', response.content, 'application/pdf')
136+
msg.send()
137+
138+
return HttpResponseRedirect(request.META['HTTP_REFERER'])

interp/views/translate.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import markdown
2+
from django.core.mail import send_mail
3+
from django.core.mail.message import EmailMessage, EmailMultiAlternatives
4+
from django.http.response import HttpResponseRedirect
25
from django.utils import timezone
36

47
from django.views.generic import View
@@ -17,7 +20,7 @@ def get(self, request, *args, **kwargs):
1720
tasks = Task.objects.filter(is_published=True).values_list('id', 'title')
1821
tasks = []
1922
for task in Task.objects.filter(is_published=True):
20-
translation = Translation.objects.get(user=user, task=task)
23+
translation = Translation.objects.filter(user=user, task=task).first()
2124
is_editing = translation and is_translate_in_editing(translation)
2225
tasks.append((task.id, task.title, is_editing))
2326

@@ -170,7 +173,7 @@ class GetTranslatePDF(LoginRequiredMixin, PDFTemplateView):
170173
'margin-right': '0.75in',
171174
'margin-bottom': '0.75in',
172175
'margin-left': '0.75in',
173-
'zoom': 15,
176+
# 'zoom': 15,
174177
'javascript-delay': 500,
175178
}
176179

@@ -184,7 +187,7 @@ def get_context_data(self, **kwargs):
184187
# TODO
185188
return None
186189

187-
trans = Translation.objects.get(user=user, task=task)
190+
trans = Translation.objects.filter(user=user, task=task).first()
188191
if trans is None:
189192
# TODO
190193
return None
@@ -195,3 +198,19 @@ def get_context_data(self, **kwargs):
195198
context['content'] = content
196199
context['title'] = self.filename
197200
return context
201+
202+
203+
class MailTranslatePDF(GetTranslatePDF):
204+
def get(self, request, *args, **kwargs):
205+
response = super(MailTranslatePDF, self).get(request, *args, **kwargs)
206+
response.render()
207+
208+
subject, from_email, to = 'hello', '[email protected]', '[email protected]'
209+
text_content = 'Test'
210+
html_content = '<p>This is an <strong>TEST</strong> message.</p>'
211+
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
212+
msg.attach_alternative(html_content, "text/html")
213+
msg.attach('file.pdf', response.content, 'application/pdf')
214+
msg.send()
215+
216+
return HttpResponseRedirect(request.META['HTTP_REFERER'])

logs/.gitkeep

Whitespace-only changes.

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ six==1.10.0
1010
websocket-client==0.35.0
1111
django-wkhtmltopdf
1212
python-markdown-math
13-
django-redis
13+
django-redis
14+
ipython

0 commit comments

Comments
 (0)