Skip to content

Instantly share code, notes, and snippets.

@ianseyer
Created September 25, 2014 17:26
Show Gist options
  • Select an option

  • Save ianseyer/81079ee14e99e86bddf7 to your computer and use it in GitHub Desktop.

Select an option

Save ianseyer/81079ee14e99e86bddf7 to your computer and use it in GitHub Desktop.
#foreign imports
from flask import Flask
#domestic imports
from modules.users import users, app_security, principals, login_manager, mail, user_datastore
from modules.homes import homes
from modules.admin import admin
from modules.the_db import db
"""
This file is the master configuration file.
It configures flask, composes the app, and deploys it [via ifmain]
"""
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql+psycopg2://postgres:postgres@localhost/homeopcost'
app.config['SECURITY_POST_LOGIN'] = '/'
app.config.update(
#EMAIL SETTINGS
MAIL_SERVER='server',
MAIL_PORT=465,
MAIL_USE_SSL=True,
MAIL_USERNAME = 'the email',
MAIL_PASSWORD = 'the password',
DEFAULT_MAIL_SENDER = 'the sender',
SECURITY_POST_LOGIN_VIEW='/',)
app.secret_key = 'the secret key'
#init the db
db.init_app(app)
#flask-security
app_security.init_app(app, user_datastore)
@app_security.login_context_processor
def login_context_processor():
form = LoginForm()
return dict(form=form)
@login_manager.user_loader
def load_user(id):
try:
user = User.objects.filter_by(id=id).first()
return user
except:
return AnonymousUser
@login_manager.request_loader
def load_user_from_request(request):
#try to login with api (request.args.get('api_key'))
#then try to login with normal email
pass
#end security
principals.init_app(app)
login_manager.init_app(app)
#flask-mail
mail.init_app(app)
"""
This file also handles blueprint registration, composing the entire `app`
"""
app.register_blueprint(homes)
app.register_blueprint(users)
app.register_blueprint(admin)
"""
It also handles errors.
"""
"""@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'), 404
@app.errorhandler(500)
def server_error(e):
return render_template('500.html'), 500"""
if __name__ == '__main__':
app.run(debug=True)
#foreign
from flask.ext.sqlalchemy import SQLAlchemy, BaseQuery
from slugify import slugify
import hashlib, random, requests
from sqlalchemy_searchable import make_searchable, SearchQueryMixin
from sqlalchemy_utils.types import TSVectorType
from sqlalchemy.ext.declarative import declarative_base
#domestic
from modules.the_db import db
"""
This contains all the database objects required for a Homes.
"""
make_searchable()
class HomeQuery(BaseQuery, SearchQueryMixin):
pass
class Home(db.Model):
### THE FIELDS
#foreign
from flask import Blueprint
from flask.ext.security import login_required
#domestic
from models.homes import *
from models.users import *
from models.ads import *
from forms import *
from the_db import db
homes = Blueprint('home_blueprint', __name__)
"""
This file handles all actions related to homes.
"""
#JUST US HARMLESS ROUTES HERE
File "/Users/ianseyer/Documents/programming/web/homeopcost-flask/env/lib/python2.7/site-packages/flask_security/core.py", line 439, in __getattr__
return getattr(self._state, name, None)
File "/Users/ianseyer/Documents/programming/web/homeopcost-flask/env/lib/python2.7/site-packages/flask_security/core.py", line 439, in __getattr__
return getattr(self._state, name, None)
File "/Users/ianseyer/Documents/programming/web/homeopcost-flask/env/lib/python2.7/site-packages/flask_security/core.py", line 439, in __getattr__
return getattr(self._state, name, None)
File "/Users/ianseyer/Documents/programming/web/homeopcost-flask/env/lib/python2.7/site-packages/flask_security/core.py", line 439, in __getattr__
return getattr(self._state, name, None)
File "/Users/ianseyer/Documents/programming/web/homeopcost-flask/env/lib/python2.7/site-packages/flask_security/core.py", line 439, in __getattr__
return getattr(self._state, name, None)
File "/Users/ianseyer/Documents/programming/web/homeopcost-flask/env/lib/python2.7/site-packages/flask_security/core.py", line 439, in __getattr__
return getattr(self._state, name, None)
File "/Users/ianseyer/Documents/programming/web/homeopcost-flask/env/lib/python2.7/site-packages/flask_security/core.py", line 439, in __getattr__
return getattr(self._state, name, None)
File "/Users/ianseyer/Documents/programming/web/homeopcost-flask/env/lib/python2.7/site-packages/flask_security/core.py", line 439, in __getattr__
return getattr(self._state, name, None)
File "/Users/ianseyer/Documents/programming/web/homeopcost-flask/env/lib/python2.7/site-packages/flask_security/core.py", line 439, in __getattr__
return getattr(self._state, name, None)
File "/Users/ianseyer/Documents/programming/web/homeopcost-flask/env/lib/python2.7/site-packages/flask_security/core.py", line 439, in __getattr__
return getattr(self._state, name, None)
RuntimeError: maximum recursion depth exceeded while calling a Python object
#foreign
from flask import Blueprint, redirect, render_template, abort
from flask.ext.security import Security, SQLAlchemyUserDatastore, UserMixin, RoleMixin, login_required, roles_required, current_user, AnonymousUser
from flask.ext.security.utils import *
from flask.ext.security.confirmable import *
from flask.ext.principal import Principal, Permission, RoleNeed
from flask.ext.login import LoginManager
from flask.ext.mail import Mail, Message
#domestic
from models.users import *
from forms import *
"""
This file handles all user, non-home-related functionality.
"""
users = Blueprint('user_blueprint', __name__)
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
#user_datastore.create_role(name='user', description="A most basic user.")
#user_datastore.create_role(name='owner', description='Owner of a home. Verified via CC or otherwise.')
#user_datastore.create_role(name='rater', description='Third-party rater. Can be hired to rate homes.')
#user_datastore.create_role(name='admin', description='An admin of HomeOpCost.')
login_manager = LoginManager()
login_manager.login_view = 'login' #after login, there is a "next" variable in the query string that indicates where the user was trying to access
login_manager.login_message = "You must logged in to do that."
mail = Mail()
principals = Principal()
can_enter_custom_data = Permission(RoleNeed('owner'))
can_edit_data = Permission(RoleNeed('rater'))
app_security = Security()
#foreign
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.security import SQLAlchemyUserDatastore, UserMixin, RoleMixin, login_required, current_user
from flask.ext.security.utils import *
from sqlalchemy.orm import mapper
import random, hashlib
#domestic
from modules.the_db import db
from models.homes import *
"""
For login/registration/permissions we will be using flask-security. This is a conglomeration of several independently powerful flask libraries. Flask-Login, Flask-Authentication, Flask-OAuthLib, WTF-Flask, etc.
It gives us some useful methods:
flask_security.utils.verify_password(password, hash_password) checks an entered password against a stored and hashed password
flask_security.utils.encrypt_password(plaintext_password)
flask_security.utils.login_user(user, remember=None)
*.logout_user() Cleans out session
user_datastore.add_role_to_user(user, role)
user_datastore.create_user(**kwargs)
user_datastore.get_user(id_or_email)
More can be found here: https://pythonhosted.org/Flask-Security/api.html#flask_security.datastore.SQLAlchemyUserDatastore.activate_user
"""
#map Users to Roles
roles_users = db.Table('roles_users',
db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
db.Column('role_id', db.Integer(), db.ForeignKey('role.id')))
class Role(db.Model, RoleMixin):
"""
Roles for determining User permissions and access levels.
"""
#__tablename__ = 'roles'
id = db.Column(db.Integer(), primary_key=True)
name = db.Column(db.String(80), unique=True)
description = db.Column(db.String(255))
class User(db.Model, UserMixin):
"""
A User of the system.
"""
#THE FIELDS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment