-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.py
119 lines (97 loc) · 4.23 KB
/
worker.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import logging
import os
import StringIO
import zipfile
import random
#import urllib
import cloudstorage as gcs
#import httplib2 as httplib2
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.api import app_identity
from google.appengine.api import mail
from google.appengine.api import urlfetch
from models import User
# Retry can help overcome transient urlfetch or GCS issues, such as timeouts.
my_default_retry_params = gcs.RetryParams(initial_delay=0.2,
max_delay=5.0,
backoff_factor=2,
max_retry_period=15)
gcs.set_default_retry_params(my_default_retry_params)
def get_random_string(length=12,
allowed_chars='abcdefghijklmnopqrstuvwxyz'
'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'):
return ''.join(random.choice(allowed_chars) for i in range(length))
class ArchiveHandler(webapp.RequestHandler):
def post(self):
user_id = self.request.headers['X-AppEngine-TaskName'].split('-')[0]
user = User.get_by_key_name(user_id)
default_bucket_name = app_identity.get_default_gcs_bucket_name()
bucket_name = os.environ.get('BUCKET_NAME', default_bucket_name)
bucket = '/' + bucket_name + '/'
object_name = ('archive/%s/cuoluodiary.archive.zip' %
get_random_string(32))
filename = bucket + object_name
path = os.path.join(os.path.dirname(__file__),
'templates', 'archive.html')
stringio = StringIO.StringIO()
zipstream = zipfile.ZipFile(stringio, 'w')
# Archive diary
for diary in user.diary:
template_values = {
'title': diary.title,
'content': diary.content.html,
'trashed': diary.status == 'trashed'
}
filecontent = template.render(path, template_values).encode('utf8')
zipstream.writestr('cuoluodiary/diary/%s.html' % diary.title,
filecontent)
# Archive notes
for note in user.notes:
template_values = {
'title': note.title,
'content': note.content.html,
'trashed': note.status == 'trashed'
}
filecontent = template.render(path, template_values).encode('utf8')
zipstream.writestr('cuoluodiary/note/%s.html' % note.title,
filecontent)
zipstream.close()
gcs_file = gcs.open(filename, 'w',
content_type='application/zip',
options={
str('x-goog-acl'): 'bucket-owner-full-control',
str('x-goog-meta-user'): user.email})
gcs_file.write(stringio.getvalue())
stringio.close()
gcs_file.close()
download_url = 'https://storage.cloud.google.com' + filename
#set_archive_acl(bucket_name, object_name, user_id)
setacl_by_email(user.email, filename)
send_archive_email(user.email, download_url)
logging.info(download_url)
def set_archive_acl(service, bucket_name, object_name, user_id):
req = service.objectAccessControls().insert(
bucket=bucket_name,
object=object_name,
body={'entity': 'user-' + user_id, 'role': 'READER'})
req.execute()
def setacl_by_email(email, filename):
url = "http://**secret_url**/%s%s" % (email, filename)
result = urlfetch.fetch(url)
logging.info(url)
return result
def send_archive_email(to, download_url):
"""docstring for send_archive_email"""
path = os.path.join(os.path.dirname(__file__),
'templates', 'archive.email.txt')
body = template.render(path, {'download_url': download_url}).encode('utf8')
sender = 'Cuoluo Diary <[email protected]>'
subject = 'Your Cuoluo Diary download is ready'
mail.send_mail(sender=sender, to=to, subject=subject, body=body)
app = webapp.WSGIApplication([
('/worker/archive', ArchiveHandler),
], debug=True)