Skip to content

Instantly share code, notes, and snippets.

@egealpay
Created September 8, 2018 09:08
Show Gist options
  • Save egealpay/5e82c50f3c29331be0246cf6916382d9 to your computer and use it in GitHub Desktop.
Save egealpay/5e82c50f3c29331be0246cf6916382d9 to your computer and use it in GitHub Desktop.
from database.db import db
class UserModel(db.Model):
__tablename__ = "users"
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80))
password = db.Column(db.String())
def __init__(self, username, password):
self.username = username
self.password = password
def json(self):
return {
"id": self.id,
"username": self.username
}, 200
# Method to save user to DB
def save_to_db(self):
db.session.add(self)
db.session.commit()
# Method to remove user from DB
def remove_from_db(self):
db.session.delete(self)
db.session.commit()
# Class method which finds user from DB by username
@classmethod
def find_user_by_username(cls, username):
return cls.query.filter_by(username=username).first()
# Class method which finds user from DB by id
@classmethod
def find_user_by_id(cls, _id):
return cls.query.filter_by(id=_id).first()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment