comparison rhodecode/model/db.py @ 1758:a87aa385f21c beta

fixed repo_create permission by adding missing commit statements - added few tests for checking permission in UserModel - added __json__() into get_dict() to fetch from it hybrid_properties and any additional custom properties - code garden
author Marcin Kuzminski <marcin@python-works.com>
date Tue, 06 Dec 2011 04:06:01 +0200
parents 2aa7f454204e
children 6c86c987cf93
comparison
equal deleted inserted replaced
1757:2aa7f454204e 1758:a87aa385f21c
80 return obj.__json__() 80 return obj.__json__()
81 else: 81 else:
82 return json.JSONEncoder.default(self, obj) 82 return json.JSONEncoder.default(self, obj)
83 83
84 class BaseModel(object): 84 class BaseModel(object):
85 """Base Model for all classess 85 """
86 86 Base Model for all classess
87 """ 87 """
88 88
89 @classmethod 89 @classmethod
90 def _get_keys(cls): 90 def _get_keys(cls):
91 """return column names for this model """ 91 """return column names for this model """
92 return class_mapper(cls).c.keys() 92 return class_mapper(cls).c.keys()
93 93
94 def get_dict(self): 94 def get_dict(self, serialized=False):
95 """return dict with keys and values corresponding 95 """
96 to this model data """ 96 return dict with keys and values corresponding
97 to this model data
98 """
97 99
98 d = {} 100 d = {}
99 for k in self._get_keys(): 101 for k in self._get_keys():
100 d[k] = getattr(self, k) 102 d[k] = getattr(self, k)
103
104 # also use __json__() if present to get additional fields
105 if hasattr(self, '__json__'):
106 for k,val in self.__json__().iteritems():
107 d[k] = val
101 return d 108 return d
102 109
103 def get_appstruct(self): 110 def get_appstruct(self):
104 """return list with keys and values tupples corresponding 111 """return list with keys and values tupples corresponding
105 to this model data """ 112 to this model data """
346 def update_lastlogin(self): 353 def update_lastlogin(self):
347 """Update user lastlogin""" 354 """Update user lastlogin"""
348 self.last_login = datetime.datetime.now() 355 self.last_login = datetime.datetime.now()
349 Session.add(self) 356 Session.add(self)
350 log.debug('updated user %s lastlogin', self.username) 357 log.debug('updated user %s lastlogin', self.username)
358
359
360 def __json__(self):
361 return dict(email=self.email,
362 full_name=self.full_name)
351 363
352 364
353 class UserLog(Base, BaseModel): 365 class UserLog(Base, BaseModel):
354 __tablename__ = 'user_logs' 366 __tablename__ = 'user_logs'
355 __table_args__ = {'extend_existing':True} 367 __table_args__ = {'extend_existing':True}