# Path: plenario/database.py
# def create_database(bind: Engine, database: str) -> None:
# def drop_database(bind: Engine, database: str) -> None:
# def create_extension(bind: Engine, extension: str) -> None:
# def psql(path: str) -> None:
# def postgres_session_context():
# def redshift_session_context():
# 
# Path: plenario/models/User.py
# class User(postgres_base):
#     __tablename__ = 'plenario_user'
# 
#     id = Column(String(36), default=get_uuid, primary_key=True)
#     name = Column(String, nullable=False, unique=True)
#     email = Column(String, nullable=False)
#     _password = Column('password', String(60), nullable=False)
# 
#     def _get_password(self):
#         return self._password
# 
#     def _set_password(self, value):
#         self._password = bcrypt.generate_password_hash(value).decode('utf-8')
# 
#     password = property(_get_password, _set_password)
#     password = synonym('_password', descriptor=password)
# 
#     def __init__(self, name, password, email):
#         self.name = name
#         self.password = password
#         self.email = email
# 
#     @classmethod
#     def get_by_username(cls, name):
#         return postgres_session.query(cls).filter(cls.name == name).first()
# 
#     @classmethod
#     def check_password(cls, name, value):
#         user = cls.get_by_username(name)
#         if not user:
#             return False
#         return bcrypt.check_password_hash(user.password, value)
# 
#     def is_authenticated(self):
#         return True
# 
#     def is_active(self):
#         return True
# 
#     def is_anonymous(self):
#         return False
# 
#     def get_id(self):
#         return self.id


import json
from functools import wraps
from flask import Blueprint, render_template, redirect, request, url_for, flash, session as flask_session
from flask_login import LoginManager, login_user, logout_user, login_required
from flask_wtf import Form, CSRFProtect
from wtforms import StringField, PasswordField
from wtforms.validators import DataRequired, Email
from plenario.database import postgres_session as db_session
from plenario.models import User
# TODO: refactor this whole massive kludge into an `auth` package -- too much going on in here





auth = Blueprint('auth', __name__)
login_manager = LoginManager()
csrf = CSRFProtect()


def check_admin_status():
    def decorator(f):
        @wraps(f)
        def decorated(*args, **kwargs):
            if flask_session.get('user_id'):
                api_key = flask_session['user_id']
            elif request.form.get('api_key'):
                api_key = request.form['api_key']
            elif request.args.get('api_key'):
                api_key = request.args['api_key']
            else:
                try:
                    api_key = json.loads(request.data).get('api_key')
                except ValueError:
                    api_key = None
            if api_key:

# Path: plenario/database.py
# def create_database(bind: Engine, database: str) -> None:
# def drop_database(bind: Engine, database: str) -> None:
# def create_extension(bind: Engine, extension: str) -> None:
# def psql(path: str) -> None:
# def postgres_session_context():
# def redshift_session_context():
# 
# Path: plenario/models/User.py
# class User(postgres_base):
#     __tablename__ = 'plenario_user'
# 
#     id = Column(String(36), default=get_uuid, primary_key=True)
#     name = Column(String, nullable=False, unique=True)
#     email = Column(String, nullable=False)
#     _password = Column('password', String(60), nullable=False)
# 
#     def _get_password(self):
#         return self._password
# 
#     def _set_password(self, value):
#         self._password = bcrypt.generate_password_hash(value).decode('utf-8')
# 
#     password = property(_get_password, _set_password)
#     password = synonym('_password', descriptor=password)
# 
#     def __init__(self, name, password, email):
#         self.name = name
#         self.password = password
#         self.email = email
# 
#     @classmethod
#     def get_by_username(cls, name):
#         return postgres_session.query(cls).filter(cls.name == name).first()
# 
#     @classmethod
#     def check_password(cls, name, value):
#         user = cls.get_by_username(name)
#         if not user:
#             return False
#         return bcrypt.check_password_hash(user.password, value)
# 
#     def is_authenticated(self):
#         return True
# 
#     def is_active(self):
#         return True
# 
#     def is_anonymous(self):
#         return False
# 
#     def get_id(self):
#         return self.id


# Path: plenario/auth.py
import json
from functools import wraps
from flask import Blueprint, render_template, redirect, request, url_for, flash, session as flask_session
from flask_login import LoginManager, login_user, logout_user, login_required
from flask_wtf import Form, CSRFProtect
from wtforms import StringField, PasswordField
from wtforms.validators import DataRequired, Email
from plenario.database import postgres_session as db_session
from plenario.models import User

# TODO: refactor this whole massive kludge into an `auth` package -- too much going on in here





auth = Blueprint('auth', __name__)
login_manager = LoginManager()
csrf = CSRFProtect()


def check_admin_status():
    def decorator(f):
        @wraps(f)
        def decorated(*args, **kwargs):
            if flask_session.get('user_id'):
                api_key = flask_session['user_id']
            elif request.form.get('api_key'):
                api_key = request.form['api_key']
            elif request.args.get('api_key'):
                api_key = request.args['api_key']
            else:
                try:
                    api_key = json.loads(request.data).get('api_key')
                except ValueError:
                    api_key = None
            if api_key:

                db_session.query(User).get(api_key)