Skip to content

Instantly share code, notes, and snippets.

@taiwotman
Created April 30, 2020 16:23
Show Gist options
  • Save taiwotman/bb6b934e8875709cc3ffffbf3311199e to your computer and use it in GitHub Desktop.
Save taiwotman/bb6b934e8875709cc3ffffbf3311199e to your computer and use it in GitHub Desktop.
"""Database models"""
from . import db
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import UserMixin
from py2neo import Node
from py2neo.ogm import GraphObject, Property
from datetime import datetime
class User(UserMixin, GraphObject):
"""Model for user accounts."""
name = Property()
email = Property()
password = Property()
website = Property()
created_on = Property()
def __init__(self, name, email, password,website):
self.name = name
self.email = email
self.password = password
self.website = website
self.created_on = datetime.now().strftime('%Y-%m-%d')
def find(self):
user = db.graph.find_one('User', 'email', self.email)
return user
def get_id(self):
query = 'match (n:User) where n.email={email} return ID(n)'
user = db.graph.find_one('User', 'email', self.email)
id = db.graph.run(query, parameters={'email': user['email']}).evaluate()
return id
def set_password(self, password):
"""Create hashed password."""
self.password = generate_password_hash(password, method='sha256')
if not self.find():
user = Node('User', name=self.name, email=self.email, password=self.password, created_on=self.created_on)
db.graph.create(user)
return True
else:
return False
def check_password(self, password):
"""Check hashed password."""
user = self.find()
if user:
return check_password_hash(user['password'], password)
else:
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment