Exemple #1
0
    def test_get(self):
        client = get_tempstoreclient()
        opbeat = Opbeat(self.app, client=client)
        response = self.client.get('/an-error/?foo=bar')
        self.assertEquals(response.status_code, 500)
        self.assertEquals(len(client.events), 1)

        event = client.events.pop(0)

        self.assertTrue('http' in event)
        http = event['http']
        self.assertEquals(http['url'], 'http://localhost/an-error/')
        self.assertEquals(http['query_string'], 'foo=bar')
        self.assertEquals(http['method'], 'GET')
        self.assertEquals(http['data'], {})
        self.assertTrue('headers' in http)
        headers = http['headers']
        self.assertTrue('Content-Length' in headers, headers.keys())
        self.assertEquals(headers['Content-Length'], '0')
        self.assertTrue('Content-Type' in headers, headers.keys())
        self.assertEquals(headers['Content-Type'], '')
        self.assertTrue('Host' in headers, headers.keys())
        self.assertEquals(headers['Host'], 'localhost')
        env = http['env']
        self.assertTrue('SERVER_NAME' in env, env.keys())
        self.assertEquals(env['SERVER_NAME'], 'localhost')
        self.assertTrue('SERVER_PORT' in env, env.keys())
        self.assertEquals(env['SERVER_PORT'], '80')
Exemple #2
0
    def test_post(self):
        client = get_tempstoreclient()
        opbeat = Opbeat(self.app, client=client)
        response = self.client.post('/an-error/?biz=baz', data={'foo': 'bar'})
        self.assertEquals(response.status_code, 500)
        self.assertEquals(len(client.events), 1)

        event = client.events.pop(0)

        self.assertTrue('http' in event)
        http = event['http']
        self.assertEquals(http['url'], 'http://localhost/an-error/')
        self.assertEquals(http['query_string'], 'biz=baz')
        self.assertEquals(http['method'], 'POST')
        self.assertEquals(http['data'], {'foo': 'bar'})
        self.assertTrue('headers' in http)
        headers = http['headers']
        self.assertTrue('Content-Length' in headers, headers.keys())
        self.assertEquals(headers['Content-Length'], '7')
        self.assertTrue('Content-Type' in headers, headers.keys())
        self.assertEquals(headers['Content-Type'], 'application/x-www-form-urlencoded')
        self.assertTrue('Host' in headers, headers.keys())
        self.assertEquals(headers['Host'], 'localhost')
        env = http['env']
        self.assertTrue('SERVER_NAME' in env, env.keys())
        self.assertEquals(env['SERVER_NAME'], 'localhost')
        self.assertTrue('SERVER_PORT' in env, env.keys())
        self.assertEquals(env['SERVER_PORT'], '80')
 def test_get_debug(self):
     client = get_tempstoreclient()
     opbeat = Opbeat(self.app, client=client)
     self.app.config['DEBUG'] = True
     self.app.config['TESTING'] = False
     self.assertRaises(ValueError,
                       self.app.test_client().get, '/an-error/?foo=bar')
     self.assertEquals(len(client.events), 0)
Exemple #4
0
    def test_error_handler(self):
        client = get_tempstoreclient()
        opbeat = Opbeat(self.app, client=client)
        response = self.client.get('/an-error/')
        self.assertEquals(response.status_code, 500)
        self.assertEquals(len(client.events), 1)

        event = client.events.pop(0)

        self.assertTrue('exception' in event)
        exc = event['exception']
        self.assertEquals(exc['type'], 'ValueError')
        self.assertEquals(exc['value'], 'hello world')
        self.assertEquals(event['level'], "error")
        self.assertEquals(event['message'], 'ValueError: hello world')
        self.assertEquals(event['culprit'], 'tests.contrib.flask.flask_tests.an_error')
Exemple #5
0
def setup_error_emails(app):
    """Sets up emails on errors if the SMTP server is specified in the settings.
    """
    if 'OPBEAT' in app.config:
        app.logger.info("setting up opbeat")
        config = app.config['OPBEAT']
        opbeat = Opbeat(app,
                        organization_id=config['ORGANIZATION_ID'],
                        app_id=config['APP_ID'],
                        secret_token=config['SECRET_TOKEN'])

    if app.debug:
        return

    if 'ERROR_EMAIL_RECIPIENTS' not in app.config:
        return

    sender = app.config.get("MAIL_DEFAULT_SENDER")
    recipients = app.config['ERROR_EMAIL_RECIPIENTS'].split(",")
    mail_on_500(app, recipients, sender=sender)
Exemple #6
0
def configure(app, admin=None):
    if app.config.get('DEBUG_TOOLBAR_ENABLED'):
        try:
            DebugToolbarExtension(app)
        except TypeError:
            raise ImportError('You must install flask_debugtoolbar')

    if app.config.get('OPBEAT'):
        try:
            Opbeat(app,
                   logging=app.config.get('OPBEAT', {}).get('LOGGING', False))
            app.logger.info('opbeat configured!!!')
        except TypeError:
            raise ImportError('You must install opbeat')

    if app.config.get('SENTRY_ENABLED', False):
        try:
            app.sentry = Sentry(app)
        except TypeError:
            raise ImportError('You must install raven (Sentry)')
Exemple #7
0
def configure(app):
    if app.config.get('DEBUG_TOOLBAR_ENABLED'):
        try:
            from flask_debugtoolbar import DebugToolbarExtension
            DebugToolbarExtension(app)
        except ImportError:
            app.logger.info('flask_debugtoolbar is not installed')

    if app.config.get('OPBEAT'):
        try:
            from opbeat.contrib.flask import Opbeat
            Opbeat(app,
                   logging=app.config.get('OPBEAT', {}).get('LOGGING', False))
            app.logger.info('opbeat configured!!!')
        except ImportError:
            app.logger.info('opbeat is not installed')

    if app.config.get('SENTRY_ENABLED', False):
        try:
            from raven.contrib.flask import Sentry
            app.sentry = Sentry(app)
        except ImportError:
            app.logger.info('sentry, raven is not installed')
Exemple #8
0
toolbar = DebugToolbarExtension()
db = SQLAlchemy()
bootstrap = Bootstrap()

lm = LoginManager()
lm.login_view = 'main.login'

mail = Mail()
moment = Moment()

celery = Celery(__name__, broker=Config.CELERY_BROKER_URL)

csrf = CsrfProtect()

opbeat = Opbeat(
    organization_id=Config.OPBEAT_CREDENTIALS.get('organization_id'),
    app_id=Config.OPBEAT_CREDENTIALS.get('app_id'),
    secret_token=Config.OPBEAT_CREDENTIALS.get('secret_token'))


def create_app(config_name):

    app = Flask(__name__)
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)

    db.init_app(app)
    lm.init_app(app)
    toolbar.init_app(app)
    mail.init_app(app)
    bootstrap.init_app(app)
    moment.init_app(app)
Exemple #9
0
    content_security_policy_report_only=app.config['CSP_REPORT_ONLY'],
    content_security_policy_report_uri=app.config['CSP_REPORT_URI'],
)

db.init_app(app)

Migrate(app, db)

admin.init_app(app)
admin.add_view(JazzbandModelView(User, db.session))
admin.add_view(JazzbandModelView(Project, db.session))
admin.add_view(JazzbandModelView(EmailAddress, db.session))

if 'OPBEAT_SECRET_TOKEN' in os.environ:
    from opbeat.contrib.flask import Opbeat
    Opbeat(app, logging=True)

if 'HEROKU_APP_NAME' in os.environ:
    app.wsgi_app = ProxyFix(app.wsgi_app)

app.wsgi_app = WhiteNoise(
    app.wsgi_app,
    root=app.static_folder,
    prefix=app.static_url_path,
)

# setup github-flask
github.init_app(app)

hooks.init_app(app)
Exemple #10
0
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_debugtoolbar import DebugToolbarExtension
from opbeat.contrib.flask import Opbeat

app = Flask(__name__)
from sots.config import BaseConfig as ConfigObject
app.config.from_object(ConfigObject)
if app.debug == True:
    from werkzeug.debug import DebuggedApplication
    app.wsgi_app = DebuggedApplication(app.wsgi_app, True)
    toolbar = DebugToolbarExtension(app)

db = SQLAlchemy(app)

opbeat = Opbeat(
    app,
    organization_id=ConfigObject.OPBEAT_ORG_ID,
    app_id=ConfigObject.OPBEAT_APP_ID,
    secret_token=ConfigObject.OPBEAT_SECRET,
)

from sots.models import *
from sots.views import *
Exemple #11
0
def create_app(config_filename='settings.py', config_dict=None):
    """Creates a Pjuu WSGI application with the passed in config_filename.

    ``config_filename`` should be a Python file as per the default. To
    create one simply copy the settings.py file and change the settings
    to suit yourself

    ``settings_dict`` can be used to override any settings inside
    ``config_filename``. This is useful for testing. See run_tests.py for an
    example
    """
    # Pylint has suggested I don't set config_dict to a empty dict, we now have
    # to check if it is None and then assign an empty dict
    if config_dict is None:  # pragma: no cover
        config_dict = {}

    # Create application
    app = Flask(__name__)

    # Load configuration from the Python file passed as config_filename
    app.config.from_pyfile(config_filename)
    # Override the settings from config_filename, even add new ones :)
    # This is useful for testing, we use it for Travis-CI, see run_tests.py
    app.config.update(config_dict)

    # You can also set an environment variable called PJUU_SETTINGS this will
    # override all other Settings applied to Pjuu so long as you define them
    app.config.from_envvar('PJUU_SETTINGS', silent=True)

    # OpBeat
    # We use OpBeat in production to log errors and performance of the code.
    # We do not need it if debug is True as we expect there could be errors
    # and we get full visibility.
    if not app.debug:  # pragma: no cover
        Opbeat(
            app,
            organization_id=app.config.get('OPBEAT_ORG_ID'),
            app_id=app.config.get('OPBEAT_APP_ID'),
            secret_token=app.config.get('OPBEAT_SECRET_TOKEN')
        )

    # Initialize the PyMongo client
    mongo.init_app(app)

    # This is the _MAIN_ redis client. ONLY STORE DATA HERE
    redis.init_app(app)

    # Create Flask-Mail
    mail.init_app(app)

    # Create the Redis session interface
    redis_sessions.init_app(app, 'SESSION_REDIS')

    # Initialize CSRF protection
    csrf.init_app(app)

    # Set session handler to Redis
    app.session_interface = RedisSessionInterface(redis=redis_sessions)

    # Generic handles
    @app.before_request
    def gather_time():
        """This is used to measure the request time for each page"""
        if app.debug and not app.testing:  # pragma: no cover
            if request.endpoint != 'static':
                g.start_time = timestamp()

    @app.after_request
    def display_time(response):
        """This is will write the time to the console in DEBUG mode"""
        if app.debug and not app.testing:  # pragma: no cover
            if request.endpoint != 'static':
                print request.path, request.endpoint, \
                    str((timestamp() - g.start_time) * 100) + 'ms'
        return response

    @app.url_defaults
    def cache_buster(endpoint, values):
        """Static URLs will have an mtime appended as a query string"""
        if 'static' == endpoint or '.static' == endpoint[-7:]:
            filename = values.get('filename', None)
            if filename:  # pragma: no branch
                static_folder = app.static_folder

                param_name = 'h'
                while param_name in values:
                    # Doesn't need coverage. Simple stops the param_name 'h'
                    # colliding with any future param
                    param_name = '_' + param_name  # pragma: no cover

                # Get the mtime of the file
                values[param_name] = int(os.stat(
                    os.path.join(static_folder, filename)).st_mtime)

    # Register error handlers
    from pjuu.lib.errors import register_errors
    register_errors(app)

    # Import all Pjuu blue prints
    from pjuu.auth.views import auth_bp
    app.register_blueprint(auth_bp)
    from pjuu.posts.views import posts_bp
    app.register_blueprint(posts_bp)
    from pjuu.users.views import users_bp
    app.register_blueprint(users_bp)
    from pjuu.lib.dashboard import dashboard_bp
    app.register_blueprint(dashboard_bp)
    from pjuu.lib.pages import pages_bp
    app.register_blueprint(pages_bp)

    # Return a nice shiny new Pjuu WSGI application :)
    return app
Exemple #12
0
from flask_cors import CORS
from flask_dotenv import DotEnv
from flask_mailgun import Mailgun
from flask_recaptcha import ReCaptcha
from flask_bootstrap import Bootstrap
from opbeat.contrib.flask import Opbeat

__app__ = ['cors', 'mailgun', 'recaptcha', 'bootstrap', 'opbeat']

cors = CORS()
env = DotEnv()
mailgun = Mailgun()
recaptcha = ReCaptcha()
bootstrap = Bootstrap()
opbeat = Opbeat()
Exemple #13
0
def create_app(config_path):
    # setup flask
    app = Flask('jazzband')

    @app.errorhandler(404)
    def page_not_found(e):
        return render_template('error.html'), 404

    @app.errorhandler(403)
    def forbidden(error):
        return render_template('forbidden.html'), 403

    @app.errorhandler(500)
    def error(error):
        return render_template('error.html'), 500

    @app.route('/favicon.ico')
    def favicon():
        filename = 'favicon.ico'
        cache_timeout = app.get_send_file_max_age(filename)
        return send_from_directory(os.path.join(app.static_folder, 'favicons'),
                                   filename,
                                   mimetype='image/vnd.microsoft.icon',
                                   cache_timeout=cache_timeout)

    # load decoupled config variables
    app.config.from_object(config_path)

    from .models import db, User, Project, EmailAddress
    db.init_app(app)

    from flask_migrate import Migrate
    Migrate(app, db)

    from .admin import admin, JazzbandModelView
    admin.init_app(app)
    admin.add_view(JazzbandModelView(User, db.session))
    admin.add_view(JazzbandModelView(Project, db.session))
    admin.add_view(JazzbandModelView(EmailAddress, db.session))

    if 'OPBEAT_SECRET_TOKEN' in os.environ:
        from opbeat.contrib.flask import Opbeat
        Opbeat(app, logging=True)

    if not app.debug:
        from werkzeug.contrib.fixers import ProxyFix
        app.wsgi_app = ProxyFix(app.wsgi_app)

    from whitenoise import WhiteNoise
    app.wsgi_app = WhiteNoise(
        app.wsgi_app,
        root=app.static_folder,
        prefix=app.static_url_path,
    )

    # setup github-flask
    from .github import github
    github.init_app(app)

    from .hooks import hooks
    hooks.init_app(app)

    # setup webassets
    from .assets import assets
    assets.init_app(app)

    # setup session store
    from flask.ext.session import Session
    Session(app)

    from flask.ext.compress import Compress
    Compress(app)

    from .content import about_pages, news_pages, content
    about_pages.init_app(app)
    news_pages.init_app(app)
    app.register_blueprint(content)

    from .account import account, login_manager
    app.register_blueprint(account)
    login_manager.init_app(app)

    from .members import members
    app.register_blueprint(members)

    from .projects import projects
    app.register_blueprint(projects)

    @app.context_processor
    def app_context_processor():
        return {
            'about': about_pages,
            'news': news_pages,
            'User': User,
            'Project': Project,
        }

    @app.after_request
    def add_vary_header(response):
        response.vary.add('Cookie')
        response.headers['Jazzband'] = "We're all part of the band"
        return response

    return app
Exemple #14
0
from os import environ
from redis import StrictRedis
from redis.exceptions import ConnectionError, TimeoutError
from flask import Flask
from flask.ext.redis import FlaskRedis
from mockredis import MockRedis
from opbeat.contrib.flask import Opbeat

from segment.views import views

opbeat = Opbeat(
    organization_id=environ.get('OPBEAT_ORGANIZATION_ID'),
    app_id=environ.get('OPBEAT_APP_ID'),
    secret_token=environ.get('OPBEAT_SECRET_TOKEN'),
)


class OptionalRedis(StrictRedis):
    """ Redis is optional """
    def safe_get(self, name):
        try:
            return self.get(name)
        except (ConnectionError, TimeoutError) as e:
            print 'Warning: Redis connection error during get: {}'.format(e)
            return None

    def safe_set(self, name, value, **kwargs):
        try:
            return self.set(name, value, **kwargs)
        except (ConnectionError, TimeoutError) as e:
            print 'Warning: Redis connection error during set: {}'.format(e)
Exemple #15
0
 def test_framework_version(self):
     opbeat = Opbeat(app=self.app)
     self.assertIn('framework=flask', opbeat.client.get_platform_info())
Exemple #16
0
# add a few extra template filters to jinja
app.jinja_env.globals['momentjs'] = Momentjs
app.jinja_env.globals['format'] = formatter
app.jinja_env.filters['strip_html'] = strip_html
app.jinja_env.filters['split'] = jinja_split
app.jinja_env.filters['format_currency'] = format_currency
app.jinja_env.filters['format_percent'] = format_percent
app.jinja_env.filters['langify'] = langify
app.jinja_env.filters['num_format'] = num_format

# add custom url arg converter
app.url_map.converters['year'] = YearConverter

# Load the modules for each different section of the site
for view in [
        "db_attr", "db_data", "general", "profile", "rankings", "resources",
        "visualize"
]:
    mod = __import__("oec.{}.views".format(view), fromlist=["mod"])
    mod = getattr(mod, "mod")
    app.register_blueprint(mod)

app.wsgi_app = ProxyFix(app.wsgi_app)

# opbeat initialization
opbeat = Opbeat(app)

if __name__ == '__main__':
    app.run()
Exemple #17
0

def cat_optionals(data: Iterator[Optional[T]]) -> Iterator[T]:
    for x in data:
        if x is not None:
            yield x


app = Flask(__name__)
app.secret_key = os.environ['COOKIE_SECRET'].encode('utf-8')
app.config.update(SESSION_COOKIE_SECURE=constants.PRODUCTION,
                  PERMANENT_SESSION_LIFETIME=60 * 60 * 24 * 365 * 20)

if constants.PRODUCTION:
    opbeat = Opbeat(app,
                    organization_id=os.environ['OPBEAT_ORGANIZATION_ID'],
                    app_id=os.environ['OPBEAT_APP_ID'],
                    secret_token=os.environ['OPBEAT_SECRET_TOKEN'])


class ProjectIdConverter(BaseConverter):
    def to_python(self, value: str) -> Optional[ProjectId]:
        return ProjectId.from_string(value)

    def to_url(self, value: ProjectId) -> str:
        return str(value)


app.url_map.converters['project_id'] = ProjectIdConverter


@app.errorhandler(ApiError)
Exemple #18
0
    def setUp(self):
        self.app = create_app()
        self.client = self.app.test_client()

        self.opbeat_client = get_tempstoreclient()
        self.opbeat = Opbeat(self.app, client=self.opbeat_client)
Exemple #19
0
def create_app(debug=True, test=False):
    '''creates app instance, db instance, and apimanager instance'''

    app.config['DEBUG'] = debug

    # Generate DB URI
    if settings.SQL_ADAPTER == 'sqlite':
        db_uri = settings.SQLALCHEMY_SQLITE_URI
    elif settings.SQL_ADAPTER == 'mysql':
        if test:
            db_to_use = settings.MYSQL_TEST_DB
        elif settings.DEBUG:
            db_to_use = settings.MYSQL_DEVELOPMENT_DB
        else:
            db_to_use = settings.MYSQL_PRODUCTION_DB
        db_uri = 'mysql://%s:%s@localhost/%s' % (
            settings.MYSQL_USER, settings.MYSQL_PASSWORD, db_to_use)
    else:
        raise ValueError("Value of SQL_ADAPTER in settings must be either"
                         "'sqlite' or 'mysql'")

    app.config['SQLALCHEMY_DATABASE_URI'] = db_uri
    app.secret_key = "very_sekret"  # move this out of here eventually

    # Add slim support
    Flask.jinja_options['extensions'].append(SlimishExtension)

    # Initialize assets
    init_assets(app)

    # Initialize caching
    db.init_app(app)
    cache.init_app(app)

    # i18n support
    Babel(app)

    # Opbeat error logging
    if settings.OPBEAT_ENABLED:
        app.config['OPBEAT'] = {
            'ORGANIZATION_ID': settings.OPBEAT_ORGANIZATION_ID,
            'APP_ID': settings.OPBEAT_APP_ID,
            'SECRET_TOKEN': settings.OPBEAT_SECRET_TOKEN,
            'INCLUDE_PATHS': ['nsweb'],
            'DEBUG': settings.DEBUG or settings.OPBEAT_DEBUG
        }
        Opbeat(app)

    # API
    marshmallow.init_app(app)

    # Set up mail stuff
    if settings.MAIL_ENABLE:
        params = [
            'MAIL_USERNAME', 'MAIL_PASSWORD', 'MAIL_DEFAULT_SENDER',
            'MAIL_SERVER', 'MAIL_PORT', 'MAIL_USE_SSL'
        ]
        app.config.update({(p, getattr(settings, p)) for p in params})
        mail.init_app(app)

    # Set up user management
    app.config['CSRF_ENABLED'] = True
    app.config['USER_ENABLE_FORGOT_PASSWORD'] = True
    from nsweb.models.users import User
    db_adapter = SQLAlchemyAdapter(db, User)
    UserManager(db_adapter, app, password_validator=password_validator)

    # load blueprints
    register_blueprints()
Exemple #20
0
from crawlers.one337x import One337x
from flask import Flask, request, Response, render_template
from flask_cors import CORS, cross_origin
from jsonpickle import encode
from opbeat.contrib.flask import Opbeat

app = Flask(__name__, template_folder='static', static_folder='static')
app.secret_key = '\xb0\xf6\x86K\x0c d\x15\xfc\xdd\x96\xf5\t\xa5\xba\xfb6\x1am@\xb2r\x82\xc1'

CORS(app)

opbeat = Opbeat(
    app,
    organization_id='02f5f35ab2ff4076a4cb9cf1614e5dc0',
    app_id='00aa53c50c',
    secret_token='44954c630c0a6f6438dc4fb211533fc622ba64b4',
)


@app.route('/')
def index():
    return render_template('index.html')


@app.route('/search/<search_term>', methods=['GET'])
def search(search_term):
    crawler = One337x()
    torrents = list(crawler.fetch_torrents(search_term))
    json_str = encode(torrents, unpicklable=False)
    return Response(json_str, mimetype="application/json")