init_db(**cfg['database'])

    with status("Dropping all tables"):
        drop_tables()

    with status("Creating tables"):
        create_tables()

    for model in Base.metadata.tables:
        print('    -', model)

    with status(f"Creating permissions"):
        setup_permissions()

    with status(f"Creating dummy users"):
        g = Group(name='Stream A')
        super_admin_user = User(username='******',
                                role_ids=['Super admin'])
        group_admin_user = User(username='******',
                                role_ids=['Super admin'])
        DBSession().add_all(
            [GroupUser(group=g, user=super_admin_user, admin=True),
             GroupUser(group=g, user=group_admin_user, admin=True)]
        )
        full_user = User(username='******',
                         role_ids=['Full user'], groups=[g])
        DBSession().add_all([super_admin_user, group_admin_user,
                             full_user])

        for u in [super_admin_user, group_admin_user, full_user]:
            DBSession().add(TornadoStorage.user.create_social_auth(u, u.username,
Beispiel #2
0
            pTelescope.nickname.like(f'{nickname}%')).first()
        instruments = psession.query(pInstrument).filter(
            pInstrument.telid == telescope.id).all()
        if len(instruments) > 1:
            instruments = [i for i in instruments if i.type != 'phot']
        try:
            spectrum = Spectrum.from_ascii(
                f, obj_id, instruments[0].id,
                datetime.strptime(obs_date, '%Y%m%d'))
            DBSession().add(spectrum)
            DBSession().commit()
        except ValueError:
            print(f"Skipped {f}")

    # TODO can't serve from outside static/
    cutout_files = glob(f'{args.data_dir}/cutouts/*')
    phot_info = DBSession().query(sa.sql.functions.min(
        Photometry.id), Photometry.obj_id).group_by(Photometry.obj_id).all()
    phot_map = {obj_id: phot_id for phot_id, obj_id in phot_info}
    for f in cutout_files:
        obj_id, thumb_type = re.split('[\/_\.]', f)[-3:-1]
        DBSession().add(
            Thumbnail(file_uri=f,
                      type=thumb_type,
                      photometry_id=phot_map[obj_id]))
        DBSession().commit()

    g = Group(name="Public group", public=True, sources=list(Source.query))
    DBSession().add(g)
    DBSession().commit()
Beispiel #3
0
    def __init__(self,
                 username,
                 groupname="Public ZTF",
                 create_user=True,
                 create_group=True,
                 create_instrument=True,
                 create_telescope=True):
        """
        username
        create_user=True,
        create_group=True,
        create_instrument=True,
        create_telescope=True
        """

        self._connect()
        self.username = username

        self.g = Group.query.filter(Group.name == groupname).first()
        if not self.g:
            self.g = Group(name=groupname)

        super_admin_user = User.query.filter(
            User.username == self.username).first()
        if not super_admin_user:
            super_admin_user = User(username=self.username,
                                    role_ids=['Super admin'])
            DBSession().add(
                GroupUser(group=self.g, user=super_admin_user, admin=True))
            uu = super_admin_user
            DBSession().add(
                TornadoStorage.user.create_social_auth(uu, uu.username,
                                                       'google-oauth2'))
            DBSession().add(super_admin_user)

        group_admin_user = User.query.filter(
            User.username == '*****@*****.**').first()
        if not group_admin_user:
            group_admin_user = User(username='******',
                                    role_ids=['Group admin'])

            DBSession().add(
                GroupUser(group=self.g, user=group_admin_user, admin=True))
            uu = group_admin_user
            DBSession().add(
                TornadoStorage.user.create_social_auth(uu, uu.username,
                                                       'google-oauth2'))
            DBSession().add(group_admin_user)

        full_user = User.query.filter(
            User.username == '*****@*****.**').first()
        if not full_user:
            full_user = User(username='******',
                             role_ids=['Full user'],
                             groups=[self.g])
            uu = full_user
            DBSession().add(
                TornadoStorage.user.create_social_auth(uu, uu.username,
                                                       'google-oauth2'))
            DBSession().add_all([full_user])

        DBSession().commit()

        self.t1 = Telescope.query.filter(
            Telescope.name == 'Palomar 48inch').first()
        if not self.t1:
            self.t1 = Telescope(name='Palomar 48inch',
                                nickname='P48',
                                lat=33.3633675,
                                lon=-116.8361345,
                                elevation=1870,
                                diameter=1.2)
            if create_telescope:
                DBSession().add(self.t1)
        self.i1 = Instrument.query.filter(
            Instrument.name == 'ZTF Camera').first()
        if not self.i1:
            self.i1 = Instrument(telescope=self.t1,
                                 name='ZTF Camera',
                                 type='phot',
                                 band='optical')
            if create_instrument:
                DBSession().add(self.i1)

        self.super_admin_user = super_admin_user
        self.group_admin_user = group_admin_user

        DBSession().commit()