comparison bitsyblog/bitsyblog.py @ 87:67dd8e0aa6da

whitespace
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 17 Nov 2011 15:03:19 -0800
parents 777c47bae0ab
children a3a7ac9102dc
comparison
equal deleted inserted replaced
86:777c47bae0ab 87:67dd8e0aa6da
60 60
61 def __init__(self, kw, handler_args): 61 def __init__(self, kw, handler_args):
62 for key in self.defaults: 62 for key in self.defaults:
63 setattr(self, key, kw.get(key, self.defaults[key])) 63 setattr(self, key, kw.get(key, self.defaults[key]))
64 self.n_links = int(self.n_links) # could be a string from the .ini 64 self.n_links = int(self.n_links) # could be a string from the .ini
65 self.response_functions = { 'GET': self.get, 65 self.response_functions = {'GET': self.get,
66 'POST': self.post, 66 'POST': self.post,
67 'PUT': self.put 67 'PUT': self.put
68 } 68 }
69 69
70 # abstract attributes 70 # abstract attributes
71 from user import FilespaceUsers 71 from user import FilespaceUsers
72 72
73 self.users = FilespaceUsers(self.file_dir) 73 self.users = FilespaceUsers(self.file_dir)
109 if entry_point.name in self.post_handlers: 109 if entry_point.name in self.post_handlers:
110 try: 110 try:
111 handler = entry_point.load()(self, **handler_args.get(entry_point.name, {})) 111 handler = entry_point.load()(self, **handler_args.get(entry_point.name, {}))
112 self.handlers.append(handler) 112 self.handlers.append(handler)
113 except: 113 except:
114 print 'Cant load entry point %s' % entry_point.name 114 print 'Cant load entry point %s' % entry_point.name
115 raise 115 raise
116 116
117 ### methods dealing with HTTP 117 ### methods dealing with HTTP
118 118
119 def __call__(self, environ, start_response): 119 def __call__(self, environ, start_response):
359 if user not in self.users.users(): 359 if user not in self.users.users():
360 return exc.HTTPNotFound("No blog found for %s" % user) 360 return exc.HTTPNotFound("No blog found for %s" % user)
361 361
362 if len(path): 362 if len(path):
363 return exc.HTTPMethodNotAllowed("Not sure what you're trying to do") 363 return exc.HTTPMethodNotAllowed("Not sure what you're trying to do")
364 364
365 # find the dates + entries in the file 365 # find the dates + entries in the file
366 regex = '\[.*\]:' 366 regex = '\[.*\]:'
367 entries = re.split(regex, request.body)[1:] 367 entries = re.split(regex, request.body)[1:]
368 dates = [ date.strip().strip(':').strip('[]').strip() 368 dates = [ date.strip().strip(':').strip('[]').strip()
369 for date in re.findall(regex, request.body) ] 369 for date in re.findall(regex, request.body) ]
371 371
372 # write to the blog 372 # write to the blog
373 for i in range(len(entries)): 373 for i in range(len(entries)):
374 datestamp = utils.datestamp(dates[i]) 374 datestamp = utils.datestamp(dates[i])
375 self.blog.post(user, datestamp, entries[i], 'public') 375 self.blog.post(user, datestamp, entries[i], 'public')
376 376
377 return exc.HTTPOk("%s posts blogged" % len(entries)) 377 return exc.HTTPOk("%s posts blogged" % len(entries))
378 378
379 379
380 def error(self): 380 def error(self):
381 """deal with non-supported methods""" 381 """deal with non-supported methods"""
382 methods = ', '.join(self.response_functions.keys()[:1]) 382 methods = ', '.join(self.response_functions.keys()[:1])
383 methods += ' and %s' % self.response_functions.keys()[-1] 383 methods += ' and %s' % self.response_functions.keys()[-1]
384 return exc.HTTPMethodNotAllowed("Only %s operations are allowed" % methods) 384 return exc.HTTPMethodNotAllowed("Only %s operations are allowed" % methods)
385 385
386 ### auth/auth functions 386 ### auth/auth functions
387 387
388 def passwords(self): 388 def passwords(self):
389 return self.users.passwords() 389 return self.users.passwords()
390 390
391 def authenticated(self, request): 391 def authenticated(self, request):
392 """return authenticated user""" 392 """return authenticated user"""
393 # XXX needed?
394 return request.environ.get('REMOTE_USER') 393 return request.environ.get('REMOTE_USER')
395 394
396 def check_user(self, user, request): 395 def check_user(self, user, request):
397 """ 396 """
398 determine authenticated user 397 determine authenticated user
400 """ 399 """
401 authenticated = self.authenticated(request) 400 authenticated = self.authenticated(request)
402 if authenticated is None: 401 if authenticated is None:
403 return exc.HTTPUnauthorized('Unauthorized') 402 return exc.HTTPUnauthorized('Unauthorized')
404 elif user != authenticated: 403 elif user != authenticated:
405 return exc.HTTPForbidden("Forbidden") 404 return exc.HTTPForbidden("Forbidden")
406 405
407 def role(self, user, request): 406 def role(self, user, request):
408 """ 407 """
409 determine what role the authenticated member has 408 determine what role the authenticated member has
410 with respect to the user 409 with respect to the user
421 return 'public' 420 return 'public'
422 421
423 ### user methods 422 ### user methods
424 423
425 def userpath(self, request): 424 def userpath(self, request):
426 """user who's blog one is viewing""" 425 """user who's blog one is viewing"""
427 path = request.path_info.strip('/').split('/') 426 path = request.path_info.strip('/').split('/')
428 name = path[0] 427 name = path[0]
429 path = path[1:] 428 path = path[1:]
430 if name: 429 if name:
431 if name not in self.users: 430 if name not in self.users:
470 url = url.strip('x') 469 url = url.strip('x')
471 try: 470 try:
472 value = int(url, 16) 471 value = int(url, 16)
473 except ValueError: 472 except ValueError:
474 return None 473 return None
475 474
476 # XXX once one has a mangled url, one can obtain the secret 475 # XXX once one has a mangled url, one can obtain the secret
477 value /= self.users.secret(user) 476 value /= self.users.secret(user)
478 477
479 entry = str(value) 478 entry = str(value)
480 if self.isentry(entry): 479 if self.isentry(entry):
481 return self.blog.entry(user, entry, ['public', 'secret', 'private']) 480 return self.blog.entry(user, entry, ['public', 'secret', 'private'])
482 481
483 ### blog retrival methods 482 ### blog retrival methods
484 483
485 def isentry(self, string): # TODO -> blog.py 484 def isentry(self, string): # TODO -> blog.py
486 """returns whether the string is a blog entry""" 485 """returns whether the string is a blog entry"""
487 return (len(string) == len(''.join(utils.timeformat))) and string.isdigit() 486 return (len(string) == len(''.join(utils.timeformat))) and string.isdigit()
520 519
521 # entire blog 520 # entire blog
522 if not path: 521 if not path:
523 return self.blog(user, allowed, n_items) 522 return self.blog(user, allowed, n_items)
524 523
525 # mangled urls 524 # mangled urls
526 if len(path) == 1 and path[0].startswith('x'): 525 if len(path) == 1 and path[0].startswith('x'):
527 entry = self.unmangleurl(path[0], user) 526 entry = self.unmangleurl(path[0], user)
528 if entry: 527 if entry:
529 return [ entry ] 528 return [ entry ]
530 else: 529 else:
531 raise BlogPathException(notfound) 530 raise BlogPathException(notfound)
532 531
533 # individual blog entry 532 # individual blog entry
534 if (len(path) == 1) and self.isentry(path[0]): 533 if (len(path) == 1) and self.isentry(path[0]):
535 blog = self.blog.entry(user, path[0], allowed) 534 blog = self.blog.entry(user, path[0], allowed)
536 if not blog: 535 if not blog:
537 raise BlogPathException(notfound) 536 raise BlogPathException(notfound)
538 return [ blog ] 537 return [ blog ]
539 538
540 # parse the path into a date path 539 # parse the path into a date path
541 n_date_vals = 3 # year, month, date 540 n_date_vals = 3 # year, month, date
542 if len(path) > n_date_vals: 541 if len(path) > n_date_vals:
543 raise BlogPathException(notfound) 542 raise BlogPathException(notfound)
544 543