-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
67 lines (59 loc) · 2.62 KB
/
helpers.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
""" This module provdes helpers.
Author: Joshua Holt
Date: 09-30-2009
Last Modified: 02-14-2010
"""
import time,datetime,hashlib,models
from google.appengine.ext import db
from models import User, Task, Project
#-----------------------------------------------------------------------------
# GENERAL JSON HELPERS
#-----------------------------------------------------------------------------
def apply_json_to_model_instance(model, jobj):
"""This is the generic method to apply the given json to the given model"""
for key in model.properties():
setattr(model, key, jobj[key] if jobj.has_key(key) else None)
return model
def build_list_json(list):
"""This method will build the users list in JSON"""
users_json = []
for user in list:
user_json = { "id": "%s" % user.key().id_or_name(),
"name": user.name,
"loginName": user.loginName, "role": user.role,
"preferences": {}, "email": user.email, "authToken": user.authToken if user.authToken != None else "",
"password": "password" if user.password != None and len(user.password) != 0 else "",
"createdAt": user.createdAt if user.createdAt != None else 0,
"updatedAt": user.updatedAt if user.updatedAt != None else 0 }
users_json.append(user_json)
return users_json
def generateAuthToken():
"""This method generates the authToken for a user every time they login"""
return hashlib.sha1("This--is--the--authToken--%s" % time.mktime(datetime.datetime.utcnow().timetuple())).hexdigest()
#-----------------------------------------------------------------------------
# AUTHORIZATION
#-----------------------------------------------------------------------------
# TODO: tighten up control to match GUI - Guests can only delete tasks they submitted
def authorized(userId, authToken, action):
"""This method checks the user's authToken against what's stored in the DB"""
key = db.Key.from_path('User', int(userId))
user = db.get(key)
retVal = False
if not user == None:
if user.authToken == authToken:
retVal = {
"createProject": lambda role: True if role == "_Manager" else False,
"updateProject": lambda role: True if role == "_Manager" else False,
"deleteProject": lambda role: True if role == "_Manager" else False,
"createTask": True,
"updateTask": True,
"deleteTask": lambda role: True if not role == "None" else False,
"createUser": lambda role: True if role == "_Manager" else False,
"updateUser": True,
"deleteUser": lambda role: True if role == "_Manager" else False
}[action](str(user.role))
else:
retVal = False
else:
retVal = False
return retVal