Created
August 31, 2013 10:19
-
-
Save yanmhlv/6397393 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BaseHandler(RequestHandler): | |
COOKIE_SESSION = 'sid' | |
def get_current_user(self): | |
sid = self.get_secure_cookie(COOKIE_SESSION) | |
if not sid: | |
sid = '%s' % uuid4() | |
db.sessions.insert({'sid': sid, 'date_created': datetime.datetime.now()) | |
return False | |
session = db.session.find_one({'sid': sid}) | |
return sid | |
class UserHandler(BaseHandler): | |
@authenticated | |
def get_user_profile(self): | |
session = self.get_current_user() | |
user = db.users.find_one({'email': session['email']}) | |
return user | |
@authenticated | |
def update_user_profile(self, **kwargs): | |
session = self.get_current_user() | |
db.users.update(**kwargs) | |
return True | |
# а ты тут используешь | |
class UserProfileRoute(UserHandler): | |
def get(self): | |
user_profile = self.get_user_profile() | |
self.render('profile/user_profile.html', **user_profile) | |
class UserProfileUpdateRoute(UserHandler): | |
def get(self): | |
update_fields = {arg: self.get_arguments(arg) for arg in ['email', 'birthday', 'about']} | |
self.update_user_profile(**update_fields) | |
self.write("success!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment