Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 113 lines (93 sloc) 4.002 kb
81a81e1 @progrium adding errors for form validation, full notifications, and minor edits t...
progrium authored
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
3 from datetime import datetime
d8e72b5 @chrismeyer Added checking for phone numbers as well as a unit testing framework.
chrismeyer authored
4 import re
b879063 @mdhancher Respect the local timezone (Pacific), and tidy up related imports.
mdhancher authored
5 import pytz
6
92e7bc4 @progrium replacing the dojo function with the shared api domain function
progrium authored
7 from shared.api import domain
81a81e1 @progrium adding errors for form validation, full notifications, and minor edits t...
progrium authored
8
92e7bc4 @progrium replacing the dojo function with the shared api domain function
progrium authored
9 LOCAL_TZ = 'America/Los_Angeles'
7ebb2cd @christopherb Cleaned up the style of the files some. Mostly standardizing on single q...
christopherb authored
10
92e7bc4 @progrium replacing the dojo function with the shared api domain function
progrium authored
11 # TODO: replace use of dojo function with directly using domain from shared lib
12 dojo = domain
7ebb2cd @christopherb Cleaned up the style of the files some. Mostly standardizing on single q...
christopherb authored
13
6157814 @progrium refactoring to_sentence, moving to utils
progrium authored
14 def to_sentence_list(lst):
15 lst = map(str, lst)
16 count = len(lst)
17 if count == 0:
18 return ''
5459516 @billsaysthis [#40] add not approved status and list view
billsaysthis authored
19 elif count == 1:
6157814 @progrium refactoring to_sentence, moving to utils
progrium authored
20 return lst[0]
21 else:
22 if count > 2:
23 pre_and = ', '.join(lst[:-1])
24 else:
25 pre_and = lst[0]
26 return ' and '.join([pre_and, lst[-1]])
7ebb2cd @christopherb Cleaned up the style of the files some. Mostly standardizing on single q...
christopherb authored
27
28
81a81e1 @progrium adding errors for form validation, full notifications, and minor edits t...
progrium authored
29 def username(user):
30 return user.nickname().split('@')[0] if user else None
31
7ebb2cd @christopherb Cleaned up the style of the files some. Mostly standardizing on single q...
christopherb authored
32
81a81e1 @progrium adding errors for form validation, full notifications, and minor edits t...
progrium authored
33 def human_username(user):
34 if user:
35 nick = user.nickname().split('@')[0]
324fb3b @novas0x2a Capitalize first/middle/last name, not just first.
novas0x2a authored
36 return nick.replace('.', ' ').title()
81a81e1 @progrium adding errors for form validation, full notifications, and minor edits t...
progrium authored
37 else:
38 return None
39
7ebb2cd @christopherb Cleaned up the style of the files some. Mostly standardizing on single q...
christopherb authored
40
81a81e1 @progrium adding errors for form validation, full notifications, and minor edits t...
progrium authored
41 def set_cookie(headers, name, value):
42 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
43
2e08a66 @progrium cleaning things up and adding json representations for jon
progrium authored
44
b879063 @mdhancher Respect the local timezone (Pacific), and tidy up related imports.
mdhancher authored
45 def local_today():
7ebb2cd @christopherb Cleaned up the style of the files some. Mostly standardizing on single q...
christopherb authored
46 """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
47 utc_now = pytz.utc.localize(datetime.utcnow())
48 local_now = utc_now.astimezone(pytz.timezone(LOCAL_TZ))
49 return datetime(*local_now.timetuple()[:3])
7ebb2cd @christopherb Cleaned up the style of the files some. Mostly standardizing on single q...
christopherb authored
50
51
d8e72b5 @chrismeyer Added checking for phone numbers as well as a unit testing framework.
chrismeyer authored
52 def get_phone_parts( in_phone, international_okay=False ):
7ebb2cd @christopherb Cleaned up the style of the files some. Mostly standardizing on single q...
christopherb authored
53 """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
54 phone_pattern = '((\d{3})\D*)?(\d{3})\D*(\d{4})(\D+(\d+))?$'
55 if international_okay:
56 phone_pattern = '(\+?\d{1-3})?\D*' + phone_pattern
57 phone_re = re.compile( '^' + phone_pattern )
58 try:
766e87c @christopherb Added UserRights class.
christopherb authored
59 seg = phone_re.search( in_phone ).groups()
d8e72b5 @chrismeyer Added checking for phone numbers as well as a unit testing framework.
chrismeyer authored
60 except AttributeError:
766e87c @christopherb Added UserRights class.
christopherb authored
61 return [ None, None, None, None, None ]
d8e72b5 @chrismeyer Added checking for phone numbers as well as a unit testing framework.
chrismeyer authored
62 if international_okay:
63 return [ seg[ 2 ], seg[ 3 ], seg[ 4 ], seg[ 6 ], seg[ 0 ] ]
64 else:
65 return [ seg[ 1 ], seg[ 2 ], seg[ 3 ], seg[ 5 ] ]
66
7ebb2cd @christopherb Cleaned up the style of the files some. Mostly standardizing on single q...
christopherb authored
67
d8e72b5 @chrismeyer Added checking for phone numbers as well as a unit testing framework.
chrismeyer authored
68 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 single q...
christopherb authored
69 """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
70 parts = get_phone_parts( in_phone, international_okay )
71 out = True
72 if area_code_required and ( parts[ 0 ] == None or len( parts[ 0 ] ) != 3 ):
73 out = False
74 if parts[ 1 ] == None or parts[ 2 ] == None or len( parts[ 1 ] ) != 3 or len( parts[ 2 ] ) != 4:
75 out = False
766e87c @christopherb Added UserRights class.
christopherb authored
76 return out
77
2ad290e @billsaysthis add logging model and obscure list view
billsaysthis authored
78 def user_is_admin():
79 user = users.get_current_user()
80 access_rights = UserRights(user)
81 return access_rights.is_admin
82
766e87c @christopherb Added UserRights class.
christopherb authored
83 class UserRights(object):
84 def __init__(self, user=None, event=None):
85 """Constructor.
86
87 Args:
88 user: User() object that you want to perform the check on.
89 event: Event() object that you want to perform the check against if applicable.
90 """
91 self.user = user
92 self.event = event
93 self.is_admin = False
94 self.is_owner = False
95 self.can_approve = False
96 self.can_cancel = False
97 self.can_edit = False
98 self.can_staff = False
99 self.can_unstaff = False
5459516 @billsaysthis [#40] add not approved status and list view
billsaysthis authored
100
766e87c @christopherb Added UserRights class.
christopherb authored
101 if self.user:
219203b @dustball Cache better
dustball authored
102 self.is_admin = username(self.user) in dojo('/groups/events',force=False)
766e87c @christopherb Added UserRights class.
christopherb authored
103 if self.event:
104 self.is_owner = (self.user == self.event.member)
5459516 @billsaysthis [#40] add not approved status and list view
billsaysthis authored
105 self.can_approve = ((self.event.status in ['pending'] or self.event.status in ['onhold'] or self.event.status in ['not_approved'] ) and self.is_admin
766e87c @christopherb Added UserRights class.
christopherb authored
106 and not self.is_owner)
5459516 @billsaysthis [#40] add not approved status and list view
billsaysthis authored
107 self.can_not_approve = self.event.status not in ['not_approved'] and self.is_admin
766e87c @christopherb Added UserRights class.
christopherb authored
108 self.can_cancel = self.is_admin or self.is_owner
109 self.can_edit = self.is_admin or self.is_owner
110 self.can_staff = (self.event.status in ['pending', 'understaffed', 'approved']
22b8be8 @dustball Removed staff logic
dustball authored
111 and self.user not in self.event.staff)
5459516 @billsaysthis [#40] add not approved status and list view
billsaysthis authored
112 self.can_unstaff = (self.event.status not in ['canceled', 'deleted']
22b8be8 @dustball Removed staff logic
dustball authored
113 and self.user in self.event.staff)
Something went wrong with that request. Please try again.