def modify_org(args): connect_db(get_database_url()) try: with session_scope() as session: try: org = session.query(Organisation).filter_by( name=args.name).one() is_new = True except sqlalchemy.orm.exc.NoResultFound: is_new = False org = Organisation(name=args.name) session.add(org) if args.region is not None: org.region = args.region if args.url is not None: org.url = args.url if args.customers is not None: org.number_of_customers = args.customers session.flush() session.expunge(org) except sqlalchemy.exc.IntegrityError as e: print('Failed to create organisation %s' % args.name) print('\n'.join(e.orig.args)) sys.exit(1) if is_new: print('Added organisation %s' % org.name) else: print('Updated organisation %s' % org.name) print('ID: %s' % org.id)
def setUp(self): super().setUp() self.remove_all_data(type(self)._engine) model.drop_analyst_user() model.create_analyst_user() url = model.get_database_url() model.connect_db_ro(url) self.create_survey_groups() self.create_org_structure() self.create_program_structure() self.assign_access()
def modify_user(args): connect_db(get_database_url()) password = getpass.getpass(prompt='Enter new password: '******'Re-enter password: '******'Failed to add user %s' % args.name) print('Passwords do not match') sys.exit(1) try: with session_scope() as session: if args.organisation is not None: organisation = session.query(Organisation) \ .filter_by(name=args.organisation).one() else: organisation = None try: user = session.query(AppUser).filter_by(email=args.email).one() is_new = False if password != "": user.password = password except sqlalchemy.orm.exc.NoResultFound: is_new = True user = AppUser(email=args.email) session.add(user) if password != "": user.password = password else: print( "Not setting a password. User will not be able to log in." ) user.password = "******" if args.name is not None: user.name = args.name if args.role is not None: user.role = args.role if organisation is not None: user.organisation_id = str(organisation.id) session.flush() session.expunge(user) except sqlalchemy.exc.IntegrityError as e: print('Failed to add user %s' % args.email) print('\n'.join(e.orig.args)) sys.exit(1) if is_new: print('Added user %s' % user.email) else: print('Updated user %s' % user.email) print('ID: %s' % user.id)
def connect_db(): db_url = model.get_database_url() model.connect_db(db_url) try: model.connect_db_ro(db_url) except model.MissingUser: # This shouldn't happen because the user is created as part of the # schema. However if the user is accidentally deleted - or if the # database is restored from backup before the app is started for the # first time - then this is the easiest way to get the user back. model.create_analyst_user() model.connect_db_ro(db_url) except model.WrongPassword: # If the database is restored from a backup, the analyst password might # be wrong. model.reset_analyst_password() model.connect_db_ro(db_url)
def setUpClass(cls): url = model.get_database_url() cls._engine = model.connect_db(url) cls.destroy_schema() model.drop_analyst_user() model.Base.metadata.create_all(cls._engine)
def connect_db(): url = model.get_database_url() model.connect_db(url)