|
|
@ -82,6 +82,7 @@ class User(UserMixin, db.Model): |
|
|
|
about_me = db.Column(db.Text()) |
|
|
|
member_since = db.Column(db.DateTime(), default=datetime.utcnow) |
|
|
|
last_seen = db.Column(db.DateTime(), default=datetime.utcnow) |
|
|
|
avatar_hash = db.Column(db.String(32)) |
|
|
|
|
|
|
|
def __init__(self, **kwargs): |
|
|
|
super(User, self).__init__(**kwargs) |
|
|
@ -90,6 +91,8 @@ class User(UserMixin, db.Model): |
|
|
|
self.role = Role.query.filter_by(name='Administrator').first() |
|
|
|
else: |
|
|
|
self.role = Role.query.filter_by(default=True).first() |
|
|
|
if self.email is not None and self.avatar_hash is None: |
|
|
|
self.avatar_hash = self.gravatar_hash() |
|
|
|
|
|
|
|
@property |
|
|
|
def password(self): |
|
|
@ -155,6 +158,7 @@ class User(UserMixin, db.Model): |
|
|
|
if self.query.filter_by(email=new_email).first() is not None: |
|
|
|
return False |
|
|
|
self.email = new_email |
|
|
|
self.gravatar_hash = self.gravatar_hash() |
|
|
|
db.session.add(self) |
|
|
|
return True |
|
|
|
|
|
|
@ -169,9 +173,12 @@ class User(UserMixin, db.Model): |
|
|
|
db.session.add(self) |
|
|
|
db.session.commit() |
|
|
|
|
|
|
|
def gravatar_hash(self): |
|
|
|
return hashlib.md5(self.email.lower().encode('utf-8')).hexdigest() |
|
|
|
|
|
|
|
def gravatar(self, size=100, default='identicon', rating='g'): |
|
|
|
url = 'https://secure.gravatar.com/avatar' |
|
|
|
hash = hashlib.md5(self.email.lower().encode('utf-8')).hexdigest() |
|
|
|
hash = self.avatar_hash or self.gravatar_hash() |
|
|
|
return '{url}/{hash}?s={size}&d={default}&r={rating}'.format( |
|
|
|
url=url, hash=hash, size=size, default=default, rating=rating) |
|
|
|
|
|
|
|