-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathecal.py
136 lines (110 loc) · 4.15 KB
/
ecal.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python
#
# Copyright 2010 Eric Entzel <[email protected]>
#
import os
import random
import string
from google.appengine.api import app_identity
from google.appengine.ext import db
import jinja2
import webapp2
import settings
# Constant for datstore queries:
LOTS_OF_RESULTS = 999999
TEMPLATE_PATH=os.path.join(os.path.dirname(__file__), "templates")
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(TEMPLATE_PATH),
extensions=['jinja2.ext.autoescape'],
autoescape=True)
# TODO: @memoize
def get_environment(version=None):
app_id = app_identity.get_application_id()
environments = {
'testing': {
'base_url': 'http://%s' % (settings.HOST_NAME),
'secure_base_url': 'http://%s' % (settings.HOST_NAME),
'rsa_key': None },
'staging': {
'base_url':
'http://%s.%s.appspot.com' % ('staging', app_id),
'secure_base_url':
'https://%s.%s.appspot.com' % ('staging', app_id),
'rsa_key': None },
'master': {
'base_url': 'http://www.myeventbot.com',
'secure_base_url':
'https://%s.appspot.com' % (app_id),
'rsa_key': load_rsa_key() } }
if version is None:
version = os.environ['CURRENT_VERSION_ID'].split('.')[0]
return environments[version]
def load_rsa_key():
f = open(os.path.join(os.path.dirname(__file__), 'myrsakey.pem'))
rsa_key = f.read()
f.close()
return rsa_key
def random_address():
"""
Returns a random alphanumeric (lowercase) string of 9 digits.
Since there are 32 choices per digit (we exclude 'o', 'l', '0'
and '1' for readability), this gives:
32 ** 9 = 3.51843721 x 10 ** 13
possible results. When there are a million accounts active,
we need:
10 ** 6 x 10 ** 6 = 10 ** 12
possible results to have a one-in-a-million chance of a
collision, so this seems like a safe number.
"""
chars = string.lowercase + string.digits
chars = chars.translate(string.maketrans('', ''), 'ol01')
return ''.join([ random.choice(chars) for _ in range(9) ])
class EcalUser(db.Model):
@classmethod
def new(cls, **kwargs):
adr = random_address()
kwargs['schema_version'] = 2
kwargs['key_name'] = adr
kwargs['email_address'] = adr
return cls(**kwargs)
schema_version = db.IntegerProperty()
# the email address that the user sends events to:
email_address = db.StringProperty()
# the AuthSub token used to authenticate the user to gcal:
auth_token = db.StringProperty() # TODO: add indexed=False ?
date_added = db.DateTimeProperty(auto_now_add=True)
last_action = db.DateTimeProperty()
google_account = db.UserProperty(auto_current_user_add=True)
google_account_id = db.StringProperty()
send_emails = db.BooleanProperty(default=False)
class EcalAction(db.Model):
type = db.StringProperty()
time = db.DateTimeProperty(auto_now_add=True)
user = db.ReferenceProperty(EcalUser)
class EcalStat(db.Model):
type = db.StringProperty()
day = db.DateProperty()
value = db.IntegerProperty()
class EcalWSGIApplication(webapp2.WSGIApplication):
def __init__(self, url_mapping):
debug = os.environ['SERVER_SOFTWARE'].startswith('Dev')
super(EcalWSGIApplication, self).__init__(url_mapping, debug)
class EcalRequestHandler(webapp2.RequestHandler):
def canonical(self, path):
if self.request.environ['SERVER_PORT'] == '443':
server = get_environment()['secure_base_url']
else:
server = get_environment()['base_url']
return server + path
def global_template_vals(self):
return {
'canonical': self.canonical(self.request.path),
'auth_link': '/signup_disabled.html'
}
@webapp2.cached_property
def jinja2(self):
return jinja2.get_jinja2(app=self.app)
def respond_with_template(self, name, values):
all_values = self.global_template_vals()
template = JINJA_ENVIRONMENT.get_template(name)
self.response.write(template.render(dict(all_values.items() + values.items())))