Esempio n. 1
0
def make_app(cfg, baselayer_handlers, baselayer_settings):
    """Create and return a `tornado.web.Application` object with specified
    handlers and settings.

    Parameters
    ----------
    cfg : Config
        Loaded configuration.  Can be specified with '--config'
        (multiple uses allowed).
    baselayer_handlers : list
        Tornado handlers needed for baselayer to function.
    baselayer_settings : cfg
        Settings needed for baselayer to function.

    """
    if cfg['cookie_secret'] == 'abc01234':
        print('!' * 80)
        print('  Your server is insecure. Please update the secret string ')
        print('  in the configuration file!')
        print('!' * 80)

    handlers = baselayer_handlers + [
        # API endpoints
        (r'/api/sources/filter', FilterSourcesHandler),
        (r'/api/sources(/.*)?', SourceHandler),
        (r'/api/groups/(.*)/users/(.*)?', GroupUserHandler),
        (r'/api/groups(/.*)?', GroupHandler),
        (r'/api/comment(/[0-9]+)?', CommentHandler),
        (r'/api/comment(/[0-9]+)/(download_attachment)', CommentHandler),
        (r'/api/photometry(/.*)?', PhotometryHandler),
        (r'/api/user(/.*)?', UserInfoHandler),
        (r'/api/sysinfo', SysInfoHandler),

        (r'/api/internal/tokens(/.*)?', TokenHandler),
        (r'/api/internal/profile', ProfileHandler),
        (r'/api/internal/plot/photometry/(.*)', PlotPhotometryHandler),
        (r'/api/internal/plot/spectroscopy/(.*)', PlotSpectroscopyHandler),

        (r'/become_user(/.*)?', BecomeUserHandler),
        (r'/logout', LogoutHandler),

        # User-facing pages
        (r'/.*', MainPageHandler)  # Route all frontend pages, such as
                                   # `/source/g647ba`, through the main page.
                                   #
                                   # Refer to Main.jsx for routing info.
    ]

    settings = baselayer_settings
    settings.update({})  # Specify any additional settings here

    app = tornado.web.Application(handlers, **settings)
    models.init_db(**cfg['database'])
    model_util.create_tables()
    model_util.setup_permissions()
    app.cfg = cfg

    app.openapi_spec = openapi.spec_from_handlers(handlers)

    return app
Esempio n. 2
0
    def _connect(self):
        env, cfg = load_env()
        self.basedir = Path(os.path.dirname(__file__)) / '..'
        (self.basedir / 'static/thumbnails').mkdir(parents=True, exist_ok=True)

        with status(f"Connecting to database {cfg['database']['database']}"):
            init_db(**cfg['database'])
Esempio n. 3
0
    Invitation,
    SourceNotification,
    UserNotification,
)

import tdtax

TMP_DIR = mkdtemp()
env, cfg = load_env()

print("Loading test configuration from _test_config.yaml")
basedir = pathlib.Path(os.path.dirname(__file__))
cfg = load_config([(basedir / "../../test_config.yaml").absolute()])
set_server_url(f'http://localhost:{cfg["ports.app"]}')
print("Setting test database to:", cfg["database"])
init_db(**cfg["database"])


def is_already_deleted(instance, table):
    """
    Helper function to check if a given ORM instance has already been deleted previously,
    either by earlier teardown functions or by a test itself through the API.
    """
    # If the instance is marked detached, that means it was deleted earlier in the
    # current transaction.
    if instance in DBSession() and inspect(instance).detached:
        return True

    if instance not in DBSession():
        return DBSession().query(table).filter(table.id == instance.id).first() is None
Esempio n. 4
0
        raise NotImplementedError("Cannot yet handle multiple data files")

    fname = env.data_files[0]
    src = yaml.load(open(fname, "r"), Loader=Loader)
    src_path = os.path.dirname(fname)

    if env.create_tables:
        from baselayer.app.model_util import create_tables
        from skyportal.models import init_db

        RETRIES = 6
        timeout = 3
        for i in range(RETRIES):
            try:
                print(f"Connecting to database {cfg['database']['database']}")
                init_db(**cfg['database'])
            except TimeoutError:
                if i == RETRIES - 1:
                    print('FAIL')
                    print()
                    print(
                        f'Error: Could not connect to SkyPortal database; trying again in {timeout}s'
                    )
                    sys.exit(-1)
                else:
                    time.sleep(timeout)
                    timeout = max(timeout * 2, 30)
                    print('Retrying connection...')

        print("Creating tables")
        create_tables()
Esempio n. 5
0
                              Source, Spectrum, User)
from skyportal.model_util import create_tables

pBase = automap_base()
pengine = create_engine("postgresql://skyportal:@localhost:5432/ptf")
pBase.prepare(pengine, reflect=True)
pBase.metadata.bind = pengine
pBase.metadata.create_all()

pSource = pBase.classes.sources
pPhotometry = pBase.classes.phot
pTelescope = pBase.classes.telescopes
pInstrument = pBase.classes.instruments

psession = Session(pengine)
init_db(**load_config()['database'])
create_tables()


def import_table(ptf_table, skyportal_table, columns=None, column_map={},
                 condition=None, dedupe=[], sql_statement=None):
    df = pd.read_sql(sql_statement if sql_statement is not None else ptf_table,
                     pengine, columns=columns)
    df = df[columns]

    df.rename(columns=column_map, inplace=True)
    if condition:
        df = df[df.apply(condition, axis=1)]
    if 'created_at' not in df:
        df['created_at'] = datetime.now()
    for col in dedupe: