-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
166 lines (126 loc) · 4.51 KB
/
utils.py
File metadata and controls
166 lines (126 loc) · 4.51 KB
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
import random
import string
import hashlib
import datetime
import time
import Cookie
import json
import urllib2
import urllib
import re
SECRET = "soghrat"
home_site = "http://madeatmyhome.appspot.com"
#home_site = "http://localhost:8083"
COOKIE_RE = re.compile(r'.+=;\s+Path=/')
def valid_cookie(cookie):
return cookie and COOKIE_RE.match(cookie)
USER_RE = re.compile(r"^[a-zA-Z0-9_-]{3,20}$")
Address_RE = re.compile(r"^.{3,200}$")
PASSWORD_RE = re.compile(r"^.{3,20}$")
EMAIL_RE = re.compile(r"^[\S]+@[\S]+\.[\S]+$")
PHONE_RE = re.compile(r"^[0-9]{10,16}$")
def valid_name(username):
return USER_RE.match(username)
def valid_address(user_address):
return Address_RE.match(user_address)
def valid_pass(password):
return PASSWORD_RE.match(password)
def valid_email(email):
return EMAIL_RE.match(email)
def valid_phone(phone):
return PHONE_RE.match(phone)
def escape_html(s):
s = s.replace('&',"&")
s = s.replace(">",">")
s = s.replace("<","<")
s = s.replace('"',""")
s = s.replace('%20'," ")
s = s.replace(' '," ")
s = s.replace(' '," ")
return s
def unescape_html(s):
s = s.replace("&",'&')
s = s.replace(">",">")
s = s.replace("<","<")
s = s.replace(""",'"')
s = s.replace(" ",' ')
s = s.replace(" ",' ')
return s
def print_json(self,blog_post):
time_str = time.strftime("%a %b %d %H:%M:%S %Y",
time.gmtime(time.mktime(time.strptime(blog_post.created_date.strftime("%Y-%m-%d %H:%M:%S"),
"%Y-%m-%d %H:%M:%S"))))
post_dictionary = {}
post_dictionary['content'] = blog_post.content
post_dictionary['subject'] = blog_post.title
post_dictionary['created'] = time_str
return post_dictionary
def make_salt():
output = []
for i in range(0,5):
output.append(random.choice(string.letters))
return(string.join(output,""))
def valid_hash_cookie(h):
temp = h.split('|',3)
hashed_val = temp[0]
user = temp[1]
if (hashed_val == hashlib.sha256(user+SECRET).hexdigest()):
return user
else:
return ""
def make_hashed_pw(name, pw):
salt = make_salt()
return hashlib.sha256(name + pw + salt).hexdigest() + '|' + salt
def make_hashed_cookie(username):
return hashlib.sha256(str(username)+SECRET).hexdigest() + '|' + str(username)
def valid_hash_pw(name, pw, h):
temp = h.split('|',3)
hashed_val = temp[0]
salt = temp[1]
if (hashed_val == hashlib.sha256(name + pw + salt).hexdigest()):
return hashed_val
else:
return hashed_val
def print_json(self,blog_post):
self.response.out.write("{")
self.response.out.write("\"content\": \"")
self.response.out.write(blog_post.content)
self.response.out.write("\",")
self.response.out.write("\"created\": \"")
time_str = time.strftime("%a %b %d %H:%M:%S %Y",
time.gmtime(time.mktime(time.strptime(blog_post.created_date.strftime("%Y-%m-%d %H:%M:%S"),
"%Y-%m-%d %H:%M:%S"))))
self.response.out.write(time_str)
self.response.out.write("\",")
self.response.out.write("\"subject\": \"")
self.response.out.write(blog_post.title)
self.response.out.write("\"")
self.response.out.write("}")
def get_geolocation(postal_address):
if postal_address:
postal_address2 = urllib.quote_plus(postal_address)
url_geo = "http://maps.googleapis.com/maps/api/geocode/json?address=%s&sensor=false" % postal_address2
try:
geo_content = urllib2.urlopen(url_geo).read()
except URLError:
return
if geo_content:
q = json.loads(geo_content)
if q['status'] == 'OK':
lat = str(q['results'][0]['geometry']['location']['lat'])
lng = str(q['results'][0]['geometry']['location']['lng'])
else:
return q['status']
return q['status']
GMAPS_URL = "http://maps.googleapis.com/maps/api/staticmap?size=380x263&sensor=false"
def gmaps_img(point):
temp = ""
if point:
temp = temp + '&markers=%s,%s' %(point.lat,point.lon)
return temp
def remove_duplicate(input_list):
output_list = []
for item in input_list:
if item.lower() not in output_list:
output_list.append(item.lower())
return output_list