Skip to content
Browse files

refactoring to_sentence, moving to utils

  • Loading branch information...
1 parent 11c9b1c commit 6157814e5814450b2980cec4631d2a0ca3fda741 @progrium progrium committed Jun 22, 2010
Showing with 18 additions and 14 deletions.
  1. +3 −13 models.py
  2. +15 −1 utils.py
View
16 models.py
@@ -2,7 +2,7 @@
from google.appengine.api import urlfetch, memcache, users, mail
from datetime import datetime, timedelta
from icalendar import Calendar, Event as CalendarEvent
-from utils import human_username, local_today
+from utils import human_username, local_today, to_sentence_list
import logging
ROOM_OPTIONS = (
@@ -18,16 +18,6 @@
GUESTS_PER_STAFF = 25
PENDING_LIFETIME = 30 # days
-def to_sentence(aList):
- sentence = ', '.join([e for e in aList if aList.index(e) != len(aList) -1])
- if len(aList) > 1: sentence = '%s and %s' % (sentence, aList[-1])
- return sentence
-
-def to_name_list(aList):
- sentence = ', '.join([human_username(e) for e in aList if aList.index(e) != len(aList) -1])
- if len(aList) > 1: sentence = '%s and %s' % (sentence, human_username(aList[-1]))
- return sentence
-
class Event(db.Model):
status = db.StringProperty(required=True, default='pending', choices=set(
['pending', 'understaffed', 'approved', 'canceled', 'onhold', 'expired', 'deleted']))
@@ -67,10 +57,10 @@ def get_pending_list(cls):
.order('start_time')
def stafflist(self):
- return to_name_list(self.staff)
+ return to_sentence_list(map(human_username, self.staff))
def roomlist(self):
- return to_sentence(self.rooms)
+ return to_sentence_list(self.rooms)
def is_staffed(self):
return len(self.staff) >= self.staff_needed()
View
16 utils.py
@@ -19,7 +19,21 @@ def dojo(path):
cache_ttl = 10
memcache.set(path, resp, cache_ttl)
return resp
-
+
+def to_sentence_list(lst):
+ lst = map(str, lst)
+ count = len(lst)
+ if count == 0:
+ return ''
+ elif count == 1:
+ return lst[0]
+ else:
+ if count > 2:
+ pre_and = ', '.join(lst[:-1])
+ else:
+ pre_and = lst[0]
+ return ' and '.join([pre_and, lst[-1]])
+
def username(user):
return user.nickname().split('@')[0] if user else None

0 comments on commit 6157814

Please sign in to comment.
Something went wrong with that request. Please try again.