Created
April 30, 2020 16:07
-
-
Save taiwotman/a916ace8c328371d363960bafcc64f45 to your computer and use it in GitHub Desktop.
Flasklogin-Neo4j key files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Initialize app.""" | |
from flask import Flask | |
from .flask_py2neo import Py2Neo | |
from flask_login import LoginManager | |
from . import routes | |
from . import auth | |
db = Py2Neo() | |
login_manager = LoginManager() | |
def create_app(): | |
"""Construct the core application.""" | |
app = Flask(__name__, instance_relative_config=False) | |
# Application Configuration | |
app.config.from_object('config.Config') | |
# Initialize Plugins | |
db.init_app(app) | |
login_manager.init_app(app) | |
with app.app_context(): | |
from . import routes | |
from . import auth | |
# Register Blueprints | |
app.register_blueprint(routes.main_bp) | |
app.register_blueprint(auth.auth_bp) | |
# Create unique index constraint | |
try: | |
db.graph.schema.create_uniqueness_constraint("User", "email") | |
except: | |
pass | |
return app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import absolute_import | |
from flask import current_app | |
from py2neo import Graph | |
import os | |
username = os.environ.get('NEO4J_USERNAME') | |
password = os.environ.get('NEO4J_PASSWORD') | |
class Py2Neo(object): | |
def __init__(self, app=None): | |
self.app = app | |
if app is not None: | |
self.init_app(app) | |
def init_app(self, app): | |
"""This callback can be used to initialize an application for the user with this database setup. | |
""" | |
app.config.setdefault("PY2NEO_BOLT", False) | |
app.config.setdefault("PY2NEO_SECURE", False) | |
app.config.setdefault("PY2NEO_HOST", "localhost") | |
app.config.setdefault("PY2NEO_HTTP_PORT", 7474) | |
app.config.setdefault("PY2NEO_HTTPS_PORT", 7473) | |
app.config.setdefault("PY2NEO_BOLT_PORT", 7687) | |
app.config.setdefault("PY2NEO_USER", username) | |
app.config.setdefault("PY2NEO_PASSWORD", password) | |
config_params = { | |
"bolt": app.config["PY2NEO_BOLT"], | |
"secure": app.config["PY2NEO_SECURE"], | |
"host": app.config["PY2NEO_HOST"], | |
"http_port": app.config["PY2NEO_HTTP_PORT"], | |
"https_port": app.config["PY2NEO_HTTPS_PORT"], | |
"bolt_port": app.config["PY2NEO_BOLT_PORT"], | |
"user": app.config["PY2NEO_USER"], | |
"password": app.config["PY2NEO_PASSWORD"], | |
} | |
app.extensions["graph"] = Graph(**config_params) | |
def get_app(self, reference_app=None): | |
"""Helper method that implements the logic to look up an | |
application.""" | |
if reference_app is not None: | |
return reference_app | |
if current_app: | |
return current_app | |
if self.app is not None: | |
return self.app | |
raise RuntimeError( | |
"application not registered on db instance and no application" | |
"bound to current context" | |
) | |
@property | |
def graph(self, app=None): | |
app = self.get_app(app) | |
return app.extensions["graph"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""App entry point.""" | |
from application import create_app | |
app = create_app() | |
if __name__ == "__main__": | |
app.run(host='0.0.0.0', debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment