#16 made /register endpoint
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
from app import db
|
||||
from flask_login import UserMixin
|
||||
from datetime import datetime
|
||||
from werkzeug.security import generate_password_hash, check_password_hash
|
||||
|
||||
|
||||
class User(UserMixin, db.Model):
|
||||
@@ -8,14 +9,25 @@ class User(UserMixin, db.Model):
|
||||
username = db.Column(db.String(64), index=True, unique=True)
|
||||
email = db.Column(db.String(120), index=True, unique=True)
|
||||
password_hash = db.Column(db.String(128))
|
||||
about_me = db.Column(db.String(140))
|
||||
last_seen = db.Column(db.DateTime, default=datetime.utcnow)
|
||||
token = db.Column(db.String(32), index=True, unique=True)
|
||||
|
||||
def set_password(self, password):
|
||||
self.password_hash = generate_password_hash(password)
|
||||
|
||||
def check_password(self, password):
|
||||
return check_password_hash(self.password_hash, password)
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
"id": self.id,
|
||||
"username": self.username,
|
||||
"email": self.email,
|
||||
"about_me": self.about_me,
|
||||
}
|
||||
|
||||
def from_dict(self, data, new_user=False):
|
||||
for field in ['username', 'email']:
|
||||
if field in data:
|
||||
setattr(self, field, data[field])
|
||||
if new_user and 'password' in data:
|
||||
self.set_password(data['password'])
|
||||
|
||||
Reference in New Issue
Block a user