Esempio n. 1
0
def create_app():
    app = Flask(__name__, template_folder='templates')
    CORS(app)
    CSRFProtect(app)
    config.init(os.environ['CONFIG_PATH'])
    app.config['SERVER_NAME'] = config.config['server_name']
    app.config['SECRET_KEY'] = config.config['secret_key']
    app.config['SQLALCHEMY_DATABASE_URI'] = config.config['db_url']
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
    app.config['SQLALCHEMY_ECHO'] = app.debug
    app.config.update(**config.config['mail'])
    app.config['MAIL_USERNAME'] = os.environ['APP_MAIL_USERNAME']
    app.config['MAIL_PASSWORD'] = os.environ['APP_MAIL_PASSWORD']
    login_manager.init_app(app)
    db.init_app(app)
    make_searchable(db.metadata)
    mail.init_app(app)
    migrate.init_app(app, db)
    from .auth import auth
    app.register_blueprint(auth, url_prefix='/auth')
    from .main import main
    app.register_blueprint(main, url_prefix='/')
    from .api import api
    app.register_blueprint(api, url_prefix='/api')

    return app
Esempio n. 2
0
 def __init__(self, database_conf: {}, meta_data=None):
     super().__init__()
     self.user = database_conf["db_user"]
     self.password = database_conf["db_pass"]
     self.host = database_conf["db_host"]
     self.database = database_conf["db_name"]
     self.port = database_conf["db_port"]
     make_searchable(meta_data)
Esempio n. 3
0
 def setup_method(self, method):
     self.engine = create_engine(CONNECTION_STRING)
     self.engine.execute('CREATE EXTENSION IF NOT EXISTS hstore')
     # self.engine.echo = True
     self.Base = declarative_base()
     make_searchable(self.Base.metadata)
     self.create_models()
     Session = sessionmaker(bind=self.engine)
     self.session = Session()
     sa.orm.configure_mappers()
     self.create_tables()
Esempio n. 4
0
 def setup_method(self, method):
     self.engine = create_engine(CONNECTION_STRING)
     self.engine.execute('CREATE EXTENSION IF NOT EXISTS hstore')
     # self.engine.echo = True
     self.Base = declarative_base()
     make_searchable(self.Base.metadata)
     self.create_models()
     Session = sessionmaker(bind=self.engine)
     self.session = Session()
     sa.orm.configure_mappers()
     self.create_tables()
Esempio n. 5
0
def db(app, request):
    from cal import db

    db.init_app(app)
    db.configure_mappers()
    db.create_all()
    make_searchable()

    yield db

    db.session.close()  # prevent py.test from hanging
    db.drop_all()
Esempio n. 6
0
    def __init__(self, config):
        super().__init__(config)
        self.engine = build_engine(sql_engine_uri=self.config.DATABASE_URI)
        self.session_factory = sessionmaker(bind=self.engine, autoflush=True)
        sa.orm.configure_mappers()  # IMPORTANT!
        make_searchable(metadata=metadata)

        metadata.create_all(self.engine)

        if config.AFFAIR_REPOSITORY == "ELASTIC":
            self.elastic_repositories = ElasticRepositories(config=self.config)
            self.affair = self.elastic_repositories.affair
        else:
            self.affair = InMemoryAffairRepository()
Esempio n. 7
0
def create_app(config_path=None, name=None):
    """Create a Flask application.

    Args:
        config_path (str, optional): The absolute or relative path to
            the JSON formatted configuration file. If not provided,
            defaults to *config.json*.
        name (str, optional): The name of the Flask application.
            Defaults to the name of the package.

    Returns:
        :class:`flask.Flask`: A Flask application instance.

    """
    if name is None:
        name = __name__.split('.')[0]
    app = flask.Flask(name, instance_relative_config=True)
    if config_path is None:
        config_path = os.getenv('RMFDB_CONFIG', default_config_filename)
    init_config(app, config_path=config_path)
    for key in app.config:
        envvar = key.upper()
        if envvar in os.environ:
            app.config[key] = os.getenv(envvar)
    app.logger.setLevel(logging.INFO)
    init_handlers(app)

    # middleware
    middleware.cache.init_app(app)
    middleware.db.init_app(app)
    middleware.limiter.init_app(app)
    middleware.ma.init_app(app)
    middleware.migrate.init_app(app, middleware.db)
    middleware.talisman.init_app(app, force_https=False)
    # fts
    make_searchable(middleware.db.metadata)
    middleware.db.configure_mappers()

    # blueprints
    app.register_blueprint(settings)
    app.register_blueprint(stigs)
    app.register_blueprint(controls)
    app.register_blueprint(search)

    return app
Esempio n. 8
0
def enable_full_text_search():
    """
    Add full-text search capabilities to the database models. This must be called prior to
    any database model creation or migration
    """
    from flask.ext.sqlalchemy import BaseQuery
    from sqlalchemy_searchable import SearchQueryMixin

    class OrganizationQuery(BaseQuery, SearchQueryMixin):
        pass

    from sqlalchemy_searchable import make_searchable

    from sqlalchemy_utils.types import TSVectorType

    #Update the models query_class if full-text search desired
    Organization.query_class = OrganizationQuery
    search_vector = db.Column(TSVectorType('legalName', 'description'))

    #enable full-text search of postgres. call after models are augmented.
    make_searchable()
Esempio n. 9
0
class Recipe(db.Model):
    """ Model for storing recipes """
    query_class = RecipeQuery
    __tablename__ = 'recipes'

    make_searchable()

    id = db.Column(UUID(as_uuid=True),
                   primary_key=True,
                   default=lambda: uuid.uuid4().hex)
    name = db.Column(db.String(255), unique=True, nullable=False)
    description = db.Column(db.Text)
    steps = db.Column(ARRAY(db.Text))
    image = db.Column(db.String(255), unique=False, nullable=True)
    rating = db.Column(db.Float)
    num_ratings = db.Column(db.Integer, default=0)
    creator_id = db.Column(UUID(as_uuid=True), db.ForeignKey('users.id'))
    creator = db.relationship('User', back_populates='created_recipes')
    search_vector = db.Column(TSVectorType('name', 'description'))

    raters = association_proxy('rating_users', 'user')

    ingredients = association_proxy('recipe_ingredients', 'ingredient')

    def __init__(self,
                 name,
                 description=None,
                 steps=None,
                 image=None,
                 rating=None,
                 creator=None,
                 creator_id=None):
        self.name = name
        self.description = description
        self.steps = steps
        self.image = image
        self.rating = rating
        self.creator_id = creator_id
        self.creator = creator
Esempio n. 10
0
from sqlalchemy import Unicode, Integer, Text, desc, Boolean
from sqlalchemy.sql import select, func
from sqlalchemy.orm import Query
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.dialects.postgresql import ARRAY

from sqlalchemy_searchable import SearchQueryMixin
from sqlalchemy_utils.types import TSVectorType
from sqlalchemy_searchable import make_searchable
#from flask_dance.consumer.backend.sqla import OAuthConsumerMixin
#from flask_login import UserMixin

from arguments.database import integer_pk, integer_fk, TimeStamp, rel, FK, C, Model, Table, bref, db_metadata

make_searchable(options={'regconfig': 'pg_catalog.german'})


class QuestionQuery(Query, SearchQueryMixin):
    pass


class UserGroup(Model):

    __tablename__ = "usergroup"

    id = integer_pk()
    name = C(Unicode)


user_to_usergroup = Table(
    "user_to_usergroup", db_metadata,
Esempio n. 11
0
        super(RedashSQLAlchemy, self).apply_pool_defaults(app, options)
        if settings.SQLALCHEMY_DISABLE_POOL:
            options['poolclass'] = NullPool
            # Remove options NullPool does not support:
            options.pop('max_overflow', None)


db = RedashSQLAlchemy(session_options={'expire_on_commit': False})
# Make sure the SQLAlchemy mappers are all properly configured first.
# This is required by SQLAlchemy-Searchable as it adds DDL listeners
# on the configuration phase of models.
db.configure_mappers()

# listen to a few database events to set up functions, trigger updates
# and indexes for the full text search
make_searchable(db.metadata, options={'regconfig': 'pg_catalog.simple'})


class SearchBaseQuery(BaseQuery, SearchQueryMixin):
    """
    The SQA query class to use when full text search is wanted.
    """


@vectorizer(db.Integer)
def integer_vectorizer(column):
    return db.func.cast(column, db.Text)


Column = functools.partial(db.Column, nullable=False)
Esempio n. 12
0
import flask_sqlalchemy
import sqlalchemy_searchable
from sqlalchemy_utils import TSVectorType


db = flask_sqlalchemy.SQLAlchemy()
"""SQLAlchemy: SQLAlchemy with Flask integration

The SQLAlchemy object handles interactions with the application's
database. the object provides the db.Model class to create Python
abstractions around resources as well as query execution functions
"""


sqlalchemy_searchable.make_searchable()
"""Prepares models to be indexable for later searching"""


class CustomQuery(flask_sqlalchemy.BaseQuery, sqlalchemy_searchable.SearchQueryMixin):
    """Mixin adds search method to query for each model"""
    pass


class Base(db.Model):
    """Abstract Model for defining shared columns between models"""

    __abstract__ = True

    query_class = CustomQuery
Esempio n. 13
0
from sqlalchemy_utils.types.choice import ChoiceType
from sqlalchemy_utils.types.url import URLType
from little_boxes.key import Key as LittleBoxesKey
from activitypub.utils import ap_url
from activitypub.vars import DEFAULT_CTX
from sqlalchemy.dialects.postgresql import UUID, JSONB
from sqlalchemy import text as sa_text
from little_boxes import activitypub as ap
from urllib.parse import urlparse
from authlib.flask.oauth2.sqla import OAuth2ClientMixin, OAuth2AuthorizationCodeMixin, OAuth2TokenMixin
import time
import uuid
from utils.defaults import Reel2bitsDefaults

db = SQLAlchemy()
make_searchable(db.metadata)

# #### Base ####


class CaseInsensitiveComparator(Comparator):
    def __eq__(self, other):
        return func.lower(self.__clause_element__()) == func.lower(other)


class Config(db.Model):
    __tablename__ = "config"

    id = db.Column(db.Integer, primary_key=True)
    app_name = db.Column(db.String(255), default=None)
    app_description = db.Column(db.Text)
Esempio n. 14
0
        super(RedashSQLAlchemy, self).apply_pool_defaults(app, options)
        if settings.SQLALCHEMY_DISABLE_POOL:
            options['poolclass'] = NullPool
            # Remove options NullPool does not support:
            options.pop('max_overflow', None)


db = RedashSQLAlchemy(session_options={'expire_on_commit': False})
# Make sure the SQLAlchemy mappers are all properly configured first.
# This is required by SQLAlchemy-Searchable as it adds DDL listeners
# on the configuration phase of models.
db.configure_mappers()

# listen to a few database events to set up functions, trigger updates
# and indexes for the full text search
make_searchable(options={'regconfig': 'pg_catalog.simple'})


class SearchBaseQuery(BaseQuery, SearchQueryMixin):
    """
    The SQA query class to use when full text search is wanted.
    """


@vectorizer(db.Integer)
def integer_vectorizer(column):
    return db.func.cast(column, db.Text)


Column = functools.partial(db.Column, nullable=False)
Esempio n. 15
0
            options["pool_pre_ping"] = True
        if settings.SQLALCHEMY_DISABLE_POOL:
            options["poolclass"] = NullPool
            # Remove options NullPool does not support:
            options.pop("max_overflow", None)


db = RedashSQLAlchemy(session_options={"expire_on_commit": False})
# Make sure the SQLAlchemy mappers are all properly configured first.
# This is required by SQLAlchemy-Searchable as it adds DDL listeners
# on the configuration phase of models.
db.configure_mappers()

# listen to a few database events to set up functions, trigger updates
# and indexes for the full text search
make_searchable(options={"regconfig": "pg_catalog.simple"})


class SearchBaseQuery(BaseQuery, SearchQueryMixin):
    """
    The SQA query class to use when full text search is wanted.
    """


@vectorizer(db.Integer)
def integer_vectorizer(column):
    return db.func.cast(column, db.Text)


Column = functools.partial(db.Column, nullable=False)
Esempio n. 16
0
    MANAGE_USERS = 0b1
    MANAGE_SEEDS = 0b10


class Anonymous(AnonymousUserMixin):
    """Anonymous user for flask-login that mocks some attributes of User."""
    def __init__(self):
        self.name = 'Guest'
        self.permissions = 0

    def can(self, permission=None):
        """Anonymous users can't do squat, always return False!"""
        return False

db = SQLAlchemy()
make_searchable(options={'remove_symbols': '@"<>-'})
mail = Mail()


login_manager = LoginManager()
login_manager.anonymous_user = Anonymous
login_manager.session_protection = 'basic'
login_manager.login_view = 'auth.login'


def create_app(config_name):
    """Create a Flask instance based on a Config subclass.

    Args:
        config_name (str): The config mode we want to load. Options for this
            are enumerated in the CONFIG dictionary in ``/config.py``.
Esempio n. 17
0
        super(RedashSQLAlchemy, self).apply_pool_defaults(app, options)
        if settings.SQLALCHEMY_DISABLE_POOL:
            options['poolclass'] = NullPool


db = RedashSQLAlchemy(session_options={
    'expire_on_commit': False
})
# Make sure the SQLAlchemy mappers are all properly configured first.
# This is required by SQLAlchemy-Searchable as it adds DDL listeners
# on the configuration phase of models.
db.configure_mappers()

# listen to a few database events to set up functions, trigger updates
# and indexes for the full text search
make_searchable(options={'regconfig': 'pg_catalog.simple'})


class SearchBaseQuery(BaseQuery, SearchQueryMixin):
    """
    The SQA query class to use when full text search is wanted.
    """


@vectorizer(db.Integer)
def integer_vectorizer(column):
    return db.func.cast(column, db.Text)


Column = functools.partial(db.Column, nullable=False)
Esempio n. 18
0
from flask import current_app
from string import split

from . import db, BaseNameMixin, BaseMixin

from flask_sqlalchemy import SQLAlchemy, BaseQuery
from sqlalchemy_searchable import SearchQueryMixin
from sqlalchemy_utils.types import TSVectorType
from sqlalchemy_searchable import make_searchable

__all__ = [
    'ORG_STATUS', 'ORG_STATUS_CHOICES', 'Organization', 'OrganizationInfo',
    'OrganizationWork', 'Campaign', 'is_org_name_exists', 'is_org_email_exists'
]

make_searchable(options={'remove_symbols': ''})


def is_org_name_exists(name):
    """Check the username exists in db.

    :param name `unicode`: name to check in db.
    """
    return Organization.query.filter_by(name=name).first()


def is_org_email_exists(email):
    """Check the email exists in db.

    :param email `unicode`: org info email to check in db.
    """
Esempio n. 19
0
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy_searchable import make_searchable

Base = declarative_base()
make_searchable(Base.metadata)

__all__ = [
    'Answer', 'Base', 'LogEntity', 'Post', 'Question', 'Tag', 'User',
    'VoteAnswer', 'VoteQuestion'
]

# Model import to expose in core.db.models
from core.db.models.log_entity import LogEntity
from core.db.models.post import Post
from core.db.models.answer import Answer
from core.db.models.question import Question
from core.db.models.tag import Tag
from core.db.models.user import User
from core.db.models.vote import VoteAnswer
from core.db.models.vote import VoteQuestion
Esempio n. 20
0
    app.config["CACHE_TYPE"] = "redis"
    app.config["CACHE_REDIS_URL"] = os.environ["REDIS_URL"]
    app.config["CACHE_DEFAULT_TIMEOUT"] = 60 * 60  # 1 hour

app.config["SQLALCHEMY_DATABASE_URI"] = url

# remove whitespaces from HTML
app.jinja_env.trim_blocks = True
app.jinja_env.lstrip_blocks = True


cache = Cache(app)

db = SQLAlchemy(app)

make_searchable(db.metadata, options={"regconfig": "pg_catalog.german"})


class DocumentQuery(BaseQuery, SearchQueryMixin):
    pass


class Document(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    year = db.Column(db.Integer)
    title = db.Column(db.String)
    jurisdiction = db.Column(db.String)
    file_url = db.Column(db.String, unique=True)
    num_pages = db.Column(db.Integer)

Esempio n. 21
0
import logging

from datetime import datetime
from sqlalchemy import Table, MetaData, Column, String, ForeignKey, Integer, TIMESTAMP, Enum
from sqlalchemy_searchable import make_searchable


logger = logging.getLogger(__name__)

metadata = MetaData()

make_searchable(metadata=metadata)
Esempio n. 22
0
                        func, Enum, CheckConstraint)
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.ext.mutable import MutableDict

from sqlalchemy.orm import relationship, backref, object_session
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy_searchable import make_searchable
from sqlalchemy_utils.types import TSVectorType, URLType, EmailType

from ekklesia_portal.database import Base, integer_pk, C
from ekklesia_portal.enums import ArgumentType, EkklesiaUserType, Majority, PropositionStatus, SupporterStatus, VotingType, VotingStatus, \
    VotingSystem, VoteByUser
from ekklesia_common.utils import cached_property

make_searchable(Base.metadata, options={'regconfig': 'pg_catalog.german'})


class Group(Base):
    __tablename__ = 'groups'
    id = Column(Integer, Sequence('id_seq', optional=True), primary_key=True)
    name = Column(String(64), unique=True, nullable=False)
    is_admin_group = Column(Boolean, nullable=False, server_default='false')
    members = association_proxy('group_members',
                                'member')  # <-GroupMember-> User


class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, Sequence('id_seq', optional=True), primary_key=True)
    name = Column(String(64), unique=True, nullable=False)
Esempio n. 23
0
from sqlalchemy.ext.declarative import declarative_base

# For full-text search on emails
from sqlalchemy_utils.types import TSVectorType
from sqlalchemy_searchable import make_searchable

# creates engine that stores data in local postgres server		
engine = create_engine('postgresql://*****:*****@localhost/eventador')
db_session = scoped_session(sessionmaker(autocommit=False, autoflush=False, bind=engine))

Base = declarative_base()
# Allow querying on models directly
Base.query = db_session.query_property()

# Use sqlalchemy-searchable
make_searchable()

class Email(Base):
  __tablename__ = 'emails'

  id = Column(Integer, primary_key=True)
  html = Column(Text)
  text = Column(CIText)
  domain = Column(Text)
  author_address = Column(Text)
  author_name = Column(Text)
  subject = Column(CIText)
  start_time = Column(DateTime)
  end_time = Column(DateTime)
  location = Column(Text)
  search_vector = Column(TSVectorType('text', 'subject', 'location'))
Esempio n. 24
0
 def initdb():
     db.drop_all()
     make_searchable()
     db.configure_mappers()
     db.create_all()
Esempio n. 25
0
from sqlalchemy_utils.types import TSVectorType
from sqlalchemy_searchable import make_searchable, SearchQueryMixin, search
from geoalchemy2 import Geometry, Geography, WKTElement

from flask.ext.script import Manager
from flask.ext.migrate import Migrate, MigrateCommand

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://*****:*****@localhost:5432/flask_fts_local'
db = SQLAlchemy(app)
migrate = Migrate(app, db)

manager = Manager(app)
manager.add_command('db', MigrateCommand)

make_searchable()

class DocumentQuery(BaseQuery, SearchQueryMixin):
    pass

class Document(db.Model):
    query_class = DocumentQuery
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(200), unique=True)
    description = db.Column(db.Text)
    search_vector = db.Column(TSVectorType('title', 'description'))

    def __init__(self, title, description):
        self.title = title
        self.description = description
Esempio n. 26
0
import sqlalchemy_searchable
import sqlalchemy.ext.declarative

Base: sqlalchemy.ext.declarative.declarative_base = sqlalchemy.ext.declarative.declarative_base(
)
"""
The declarative base for all tables in Mandarin.
"""

sqlalchemy_searchable.make_searchable(metadata=Base.metadata)

__all__ = ("Base", )