forked from CNBorn/checknerds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.py
272 lines (214 loc) · 8.59 KB
/
base.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# -*- coding: utf-8 -*-
# ****************************************************************
# CheckNerds - www.checknerds.com
# version 1.1, codename California
# - base.py
# Author: CNBorn, 2008-2009
# http://cnborn.net, http://twitter.com/CNBorn
#
# Basic Fundamental Objects
#
# ****************************************************************
from google.appengine.ext import webapp
from google.appengine.api import users
from google.appengine.ext import db
from modules import tarsusaUser, Tag, AppModel
#from modules import *
import cgi
#import logging
#import tarsusaCore, memcache
# {{{ Base class
class tarsusaRequestHandler(webapp.RequestHandler):
def __init__(self):
pass
def initialize(self, request, response):
webapp.RequestHandler.initialize(self, request, response)
self.login_user = users.get_current_user()
self.is_login = (self.login_user != None)
if self.is_login:
self.user = tarsusaUser.all().filter('user = ', self.login_user).get() or tarsusaUser(user = self.login_user)
else:
self.user = None
try:
self.referer = self.request.headers['referer']
except:
self.referer = None
def param(self, name, **kw):
return self.request.get(name, **kw)
def write(self, s):
self.response.out.write(s)
def get_login_url(self, from_referer=False):
if from_referer:
dst = self.referer
if not dst : dst = '/m'
return users.create_login_url(dst)
else:
return users.create_login_url(self.request.uri)
def get_logout_url(self, from_referer=False):
if from_referer:
dst = self.referer
if not dst : dst = '/m'
return users.create_logout_url(dst)
else:
return users.create_logout_url(self.request.uri)
def chk_login(self, redirect_url='/'):
self.login_user = users.get_current_user()
self.is_login = (self.login_user != None)
if self.is_login:
# added register code here
CurrentUser = self.get_user_db()
if CurrentUser == None:
# Create a User
CurrentUser = tarsusaUser(user=users.get_current_user(), urlname=cgi.escape(users.get_current_user().nickname()))
CurrentUser.put()
## Added userid property.
CurrentUser.userid = CurrentUser.key().id()
CurrentUser.dispname = users.get_current_user().nickname()
CurrentUser.put()
else:
## DB Model Patch
## These code for registered user whose information are not fitted into the new model setting.
#Run DB Model Patch when User Logged in.
#DBPatcher.chk_dbmodel_update(CurrentUser)
#Run this at every ViewItem event
## Added userid here.
if CurrentUser.userid == None:
CurrentUser.userid = CurrentUser.key().id()
CurrentUser.put()
return True
else:
#self.redirect(redirect_url)
return False
def get_user_db(self):
q = db.GqlQuery("SELECT * FROM tarsusaUser WHERE user = :1", users.get_current_user())
return q.get()
def verify_api(self):
#Verifiction of external API calls.
#Verify the AppModel first.
apiappid = self.request.get('apiappid')
apiservicekey = self.request.get('servicekey')
if apiappid == "" or apiservicekey == "":
self.write("403 Not enough parameters.")
return False
#logging.info(apiservicekey)
verified = tarsusaCore.verify_AppModel(int(apiappid), apiservicekey)
apiuserid = self.request.get('apiuserid')
apikey = self.request.get('apikey')
userid = self.request.get('userid')
APIUser = tarsusaUser.get_by_id(int(apiuserid))
#Exception.
if APIUser == None:
self.write("403 No Such User")
#self.error(403)
return False
if verified == False:
self.write("403 Application Verifiction Failed.")
#self.error(403)
return False
#--- verified AppApi Part.
if tarsusaCore.verify_UserApi(int(apiuserid), apikey) == False:
self.write("403 UserID Authentication Failed.")
return False
try:
if APIUser.apikey == None:
return False
except:
return False
return True
def verify_api_limit(self):
#Check with API Usage.
apiappid = self.request.get('apiappid')
apiservicekey = self.request.get('servicekey')
AppApiUsage = memcache.get_item("appapiusage", int(apiappid))
ThisApp = AppModel.get_by_id(int(apiappid))
if ThisApp == None:
self.write("404 Application does not exist.")
return False
if AppApiUsage >= ThisApp.api_limit:
#Api Limitation exceed.
self.write('403 API Limitation exceed. ')
return False
elif AppApiUsage == None: #memcache.get_item returns None if there is not such var.
AppApiUsage = 0
AppApiUsage += 1
memcache.set_item("appapiusage", AppApiUsage, int(apiappid))
return True
def response_status(self, status_code, status_info=None, return_value=False):
'''
Set the response status & its info, and return the specify result state
mainly used in service.py, sample usage:
return self.response_status(404, 'No such Item', False)
#This line returns False, because of the third para in the function
'''
self.response.set_status(status_code)
self.write(status_info)
return return_value
## These 2 below functions are derived from Plog.
##
## Now Tag is a new type of model
def split_tags(s):
tags = list(set([t.strip() for t in re.split('[,;\\/\\\\]*', s) if t != ''])) #uniq
return tags
def update_tag_count(old_tags = None, new_tags = None):
if old_tags == None and new_tags == None:
tags = []
posts = Post.all()
for post in posts:
tags += post.tags
tags_set = set(tags)
for tag in tags_set:
t = Tag.all().filter('name = ', tag).get()
if t:
t.count = tags.count(tag)
else:
t = Tag(name = tag, count = tags.count(tag))
t.put()
else:
added = [t for t in new_tags if not t in old_tags]
deleted = [t for t in old_tags if not t in new_tags]
for tag in added:
t = Tag.all().filter('name = ', tag).get()
if t:
t.count = t.count + 1
else:
t = Tag(name = tag, count = 1)
t.put()
for tag in deleted:
t = Tag.all().filter('name = ', tag).get()
if t:
t.count = t.count - 1
if t.count == 0:
t.delete()
else:
t.put()
else:
t = Tag(name = tag, count = 1)
t.put()
## Import this function from tarsusa R6
def printExpireTimeGap(timeOne,timeTwo):
ReturnString = ''
#My Original Design, Returning a list with ReturnString in [0] and the following is Days Hours Minutes Seconds...
#ReturnList = []
if str(timeTwo - timeOne).find(" days,") <> -1:
#Projects has more than two days.
WorkString = str(timeTwo - timeOne).split(" days,",1)
TimeString = WorkString[1].split(":")
elif str(timeTwo - timeOne).find(" day,") <> -1:
#Projects has more than one day.
WorkString = str(timeTwo - timeOne).split("day,",1)
TimeString = WorkString[1].split(":")
else:
#Projects has less than one day.
TimeString = str(timeTwo - timeOne).split(":")
if str(timeTwo - timeOne)[0] == "-":
WorkString = ['-0']
else:
WorkString = ['0']
if WorkString[0][0] == "-":
ReturnString = "Past" + WorkString[0][1:] + "Days" + TimeString[0] + "Hours" + TimeString[1] + "Mins"
else:
ReturnString = "To GO " + WorkString[0] + "Days" + TimeString[0] + "Hours" + TimeString[1] + "Mins"
return ReturnString
## Used for API setting.
global_vars = {}
global_vars['apilimit'] = 400