feat: Added user profile and creation of profile
This commit is contained in:
parent
4a4d693d72
commit
545b31a15f
2 changed files with 31 additions and 1 deletions
|
|
@ -1,5 +1,7 @@
|
||||||
|
from models.UserProfile.user_profile import UserProfile
|
||||||
from werkzeug.security import generate_password_hash, check_password_hash
|
from werkzeug.security import generate_password_hash, check_password_hash
|
||||||
from models import db
|
from models import db
|
||||||
|
from sqlalchemy import event
|
||||||
|
|
||||||
class User(db.Model):
|
class User(db.Model):
|
||||||
__tablename__ = 'users'
|
__tablename__ = 'users'
|
||||||
|
|
@ -8,6 +10,8 @@ class User(db.Model):
|
||||||
username = db.Column(db.String(80), unique=True, nullable=False)
|
username = db.Column(db.String(80), unique=True, nullable=False)
|
||||||
_password = db.Column("password", db.String(255), nullable=False)
|
_password = db.Column("password", db.String(255), nullable=False)
|
||||||
|
|
||||||
|
profile = db.relationship('UserProfile', back_populates='user', uselist=False, cascade="all, delete-orphan")
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def password(self):
|
def password(self):
|
||||||
return self._password
|
return self._password
|
||||||
|
|
@ -20,4 +24,16 @@ class User(db.Model):
|
||||||
self._password = raw_password
|
self._password = raw_password
|
||||||
|
|
||||||
def check_password(self, password):
|
def check_password(self, password):
|
||||||
return check_password_hash(self._password, password)
|
return check_password_hash(self._password, password)
|
||||||
|
|
||||||
|
@event.listens_for(User, 'after_insert')
|
||||||
|
def create_user_profile(mapper, connection, target):
|
||||||
|
connection.execute(
|
||||||
|
UserProfile.__table__.insert().values (
|
||||||
|
user_id = target.id,
|
||||||
|
first_name = "",
|
||||||
|
last_name = "",
|
||||||
|
bio = "",
|
||||||
|
profile_picture = ""
|
||||||
|
)
|
||||||
|
)
|
||||||
14
models/UserProfile/user_profile.py
Normal file
14
models/UserProfile/user_profile.py
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
from models import db
|
||||||
|
|
||||||
|
class UserProfile(db.Model):
|
||||||
|
__tablename__ = 'user_profile'
|
||||||
|
|
||||||
|
id = db.Column(db.Integer, primary_key = True)
|
||||||
|
user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable = False)
|
||||||
|
first_name = db.Column(db.String(80), nullable = False)
|
||||||
|
last_name = db.Column(db.String(80), nullable = False)
|
||||||
|
bio = db.Column(db.Text, nullable = True)
|
||||||
|
profile_picture = db.Column(db.String(255), nullable = True)
|
||||||
|
|
||||||
|
user = db.relationship('User', back_populates='profile')
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue