Skip to content

Instantly share code, notes, and snippets.

@gajanan0707
Last active September 15, 2022 14:30
Show Gist options
  • Save gajanan0707/2f79d06fe5d48273e17cc72d3a2b1769 to your computer and use it in GitHub Desktop.
Save gajanan0707/2f79d06fe5d48273e17cc72d3a2b1769 to your computer and use it in GitHub Desktop.
"""Data models."""
import datetime
from flask_bcrypt import generate_password_hash, check_password_hash
from flask_sqlalchemy import SQLAlchemy
from server import db
# The User class is a data model for user accounts
class User(db.Model):
"""Data model for user accounts."""
__tablename__ = "users"
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), index=True, unique=True, nullable=False)
email = db.Column(db.String(80), index=True, unique=True, nullable=False)
password = db.Column(db.String(500), nullable=False)
created = db.Column(db.DateTime, default=datetime.datetime.utcnow, nullable=True)
def __init__(self, **kwargs):
"""
The function takes in a dictionary of keyword arguments and assigns the values to the class
attributes
"""
self.username = kwargs.get("username")
self.email = kwargs.get("email")
self.password = kwargs.get("password")
def __repr__(self):
"""
The __repr__ function is used to return a string representation of the object
:return: The username of the user.
"""
return "<User {}>".format(self.username)
def hash_password(self):
"""
It takes the password that the user has entered, hashes it, and then stores the hashed password in
the database
"""
self.password = generate_password_hash(self.password).decode("utf8")
def check_password(self, password):
"""
It takes a plaintext password, hashes it, and compares it to the hashed password in the database
:param password: The password to be hashed
:return: The password is being returned.
"""
return check_password_hash(self.password, password)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment