Skip to content
Newer
Older
100644 118 lines (99 sloc) 3.92 KB
81a81e1 @progrium adding errors for form validation, full notifications, and minor edit…
progrium authored Apr 18, 2010
1 from google.appengine.api import urlfetch, memcache
2 from django.utils import simplejson
b879063 @mdhancher Respect the local timezone (Pacific), and tidy up related imports.
mdhancher authored May 13, 2010
3 from datetime import datetime
d8e72b5 @chrismeyer Added checking for phone numbers as well as a unit testing framework.
chrismeyer authored Jun 22, 2010
4 import re
b879063 @mdhancher Respect the local timezone (Pacific), and tidy up related imports.
mdhancher authored May 13, 2010
5 import pytz
6
7 LOCAL_TZ = 'America/Los_Angeles'
81a81e1 @progrium adding errors for form validation, full notifications, and minor edit…
progrium authored Apr 18, 2010
8
9 # Hacker Dojo Domain API helper with caching
10 def dojo(path):
c4de329 @progrium making sure to use the canonical domain app host
progrium authored Jul 13, 2010
11 base_url = 'http://domain.hackerdojo.com'
81a81e1 @progrium adding errors for form validation, full notifications, and minor edit…
progrium authored Apr 18, 2010
12 cache_ttl = 3600
13 resp = memcache.get(path)
14 if not resp:
15 resp = urlfetch.fetch(base_url + path, deadline=10)
16 try:
17 resp = simplejson.loads(resp.content)
18 except Exception, e:
19 resp = []
20 cache_ttl = 10
21 memcache.set(path, resp, cache_ttl)
22 return resp
7ebb2cd @christopherb Cleaned up the style of the files some. Mostly standardizing on singl…
christopherb authored Jun 23, 2010
23
24
6157814 @progrium refactoring to_sentence, moving to utils
progrium authored Jun 22, 2010
25 def to_sentence_list(lst):
26 lst = map(str, lst)
27 count = len(lst)
28 if count == 0:
29 return ''
30 elif count == 1:
31 return lst[0]
32 else:
33 if count > 2:
34 pre_and = ', '.join(lst[:-1])
35 else:
36 pre_and = lst[0]
37 return ' and '.join([pre_and, lst[-1]])
7ebb2cd @christopherb Cleaned up the style of the files some. Mostly standardizing on singl…
christopherb authored Jun 23, 2010
38
39
81a81e1 @progrium adding errors for form validation, full notifications, and minor edit…
progrium authored Apr 18, 2010
40 def username(user):
41 return user.nickname().split('@')[0] if user else None
42
7ebb2cd @christopherb Cleaned up the style of the files some. Mostly standardizing on singl…
christopherb authored Jun 23, 2010
43
81a81e1 @progrium adding errors for form validation, full notifications, and minor edit…
progrium authored Apr 18, 2010
44 def human_username(user):
45 if user:
46 nick = user.nickname().split('@')[0]
324fb3b @novas0x2a Capitalize first/middle/last name, not just first.
novas0x2a authored May 13, 2010
47 return nick.replace('.', ' ').title()
81a81e1 @progrium adding errors for form validation, full notifications, and minor edit…
progrium authored Apr 18, 2010
48 else:
49 return None
50
7ebb2cd @christopherb Cleaned up the style of the files some. Mostly standardizing on singl…
christopherb authored Jun 23, 2010
51
81a81e1 @progrium adding errors for form validation, full notifications, and minor edit…
progrium authored Apr 18, 2010
52 def set_cookie(headers, name, value):
53 headers.add_header('Set-Cookie', '%s=%s;' % (name, simplejson.dumps(value)))
b879063 @mdhancher Respect the local timezone (Pacific), and tidy up related imports.
mdhancher authored May 13, 2010
54
2e08a66 @progrium cleaning things up and adding json representations for jon
progrium authored Jun 22, 2010
55
b879063 @mdhancher Respect the local timezone (Pacific), and tidy up related imports.
mdhancher authored May 13, 2010
56 def local_today():
7ebb2cd @christopherb Cleaned up the style of the files some. Mostly standardizing on singl…
christopherb authored Jun 23, 2010
57 """Return a datetime object representing the start of today, local time."""
b879063 @mdhancher Respect the local timezone (Pacific), and tidy up related imports.
mdhancher authored May 13, 2010
58 utc_now = pytz.utc.localize(datetime.utcnow())
59 local_now = utc_now.astimezone(pytz.timezone(LOCAL_TZ))
60 return datetime(*local_now.timetuple()[:3])
7ebb2cd @christopherb Cleaned up the style of the files some. Mostly standardizing on singl…
christopherb authored Jun 23, 2010
61
62
d8e72b5 @chrismeyer Added checking for phone numbers as well as a unit testing framework.
chrismeyer authored Jun 23, 2010
63 def get_phone_parts( in_phone, international_okay=False ):
7ebb2cd @christopherb Cleaned up the style of the files some. Mostly standardizing on singl…
christopherb authored Jun 23, 2010
64 """Return the different parts of a phone number: area code, trunk, number, extension, and optionally international code"""
d8e72b5 @chrismeyer Added checking for phone numbers as well as a unit testing framework.
chrismeyer authored Jun 23, 2010
65 phone_pattern = '((\d{3})\D*)?(\d{3})\D*(\d{4})(\D+(\d+))?$'
66 if international_okay:
67 phone_pattern = '(\+?\d{1-3})?\D*' + phone_pattern
68 phone_re = re.compile( '^' + phone_pattern )
69 try:
766e87c @christopherb Added UserRights class.
christopherb authored Jun 26, 2010
70 seg = phone_re.search( in_phone ).groups()
d8e72b5 @chrismeyer Added checking for phone numbers as well as a unit testing framework.
chrismeyer authored Jun 23, 2010
71 except AttributeError:
766e87c @christopherb Added UserRights class.
christopherb authored Jun 26, 2010
72 return [ None, None, None, None, None ]
d8e72b5 @chrismeyer Added checking for phone numbers as well as a unit testing framework.
chrismeyer authored Jun 23, 2010
73 if international_okay:
74 return [ seg[ 2 ], seg[ 3 ], seg[ 4 ], seg[ 6 ], seg[ 0 ] ]
75 else:
76 return [ seg[ 1 ], seg[ 2 ], seg[ 3 ], seg[ 5 ] ]
77
7ebb2cd @christopherb Cleaned up the style of the files some. Mostly standardizing on singl…
christopherb authored Jun 23, 2010
78
d8e72b5 @chrismeyer Added checking for phone numbers as well as a unit testing framework.
chrismeyer authored Jun 23, 2010
79 def is_phone_valid( in_phone, area_code_required=True, international_okay=True ):
7ebb2cd @christopherb Cleaned up the style of the files some. Mostly standardizing on singl…
christopherb authored Jun 23, 2010
80 """Check to make sure a given phone number is valid"""
d8e72b5 @chrismeyer Added checking for phone numbers as well as a unit testing framework.
chrismeyer authored Jun 23, 2010
81 parts = get_phone_parts( in_phone, international_okay )
82 out = True
83 if area_code_required and ( parts[ 0 ] == None or len( parts[ 0 ] ) != 3 ):
84 out = False
85 if parts[ 1 ] == None or parts[ 2 ] == None or len( parts[ 1 ] ) != 3 or len( parts[ 2 ] ) != 4:
86 out = False
766e87c @christopherb Added UserRights class.
christopherb authored Jun 26, 2010
87 return out
88
89 class UserRights(object):
90 def __init__(self, user=None, event=None):
91 """Constructor.
92
93 Args:
94 user: User() object that you want to perform the check on.
95 event: Event() object that you want to perform the check against if applicable.
96 """
97 self.user = user
98 self.event = event
99 self.is_admin = False
100 self.is_owner = False
101 self.can_approve = False
102 self.can_cancel = False
103 self.can_edit = False
104 self.can_staff = False
105 self.can_unstaff = False
106
107 if self.user:
108 self.is_admin = username(self.user) in dojo('/groups/events')
109 if self.event:
110 self.is_owner = (self.user == self.event.member)
111 self.can_approve = (self.event.status in ['pending'] and self.is_admin
112 and not self.is_owner)
113 self.can_cancel = self.is_admin or self.is_owner
114 self.can_edit = self.is_admin or self.is_owner
115 self.can_staff = (self.event.status in ['pending', 'understaffed', 'approved']
22b8be8 @dustball Removed staff logic
dustball authored Aug 17, 2010
116 and self.user not in self.event.staff)
766e87c @christopherb Added UserRights class.
christopherb authored Jun 26, 2010
117 self.can_unstaff = (self.event.status not in ['canceled', 'deleted']
22b8be8 @dustball Removed staff logic
dustball authored Aug 18, 2010
118 and self.user in self.event.staff)
Something went wrong with that request. Please try again.