예제 #1
0
def createsuperuser():
    """
    Create a super user of the system, requiring Email and password.
    """

    email = prompt('User E-Mail')
    email_confirm = prompt('Confirm E-Mail')

    if not email == email_confirm:
        sys.exit('\nCould not create user: E-Mail did not match')

    if not EMAIL_REGEX.match(email):
        sys.exit('\nCould not create user: Invalid E-Mail addresss')

    password = prompt_pass('User password')
    password_confirm = prompt_pass('Confirmed password')

    if not password == password_confirm:
        sys.exit('\nCould not create user: Passwords did not match')

    datastore = SQLAlchemyUserDatastore(db, User, Role)
    datastore.create_user(
        email=email,
        password=encrypt_password(password),
        active=True,
        super_user=True)

    db.session.commit()
예제 #2
0
파일: setup.py 프로젝트: vnthang/pgadmin4
def do_setup(app):
    """Create a new settings database from scratch"""
    if config.SERVER_MODE is False:
        print("NOTE: Configuring authentication for DESKTOP mode.")
        email = config.DESKTOP_USER
        p1 = ''.join([
            random.choice(string.ascii_letters + string.digits)
            for n in range(32)
        ])

    else:
        print("NOTE: Configuring authentication for SERVER mode.\n")

        # Prompt the user for their default username and password.
        print("""
Enter the email address and password to use for the initial pgAdmin user \
account:\n""")
        email = ''
        while email == '':
            email = input("Email address: ")

        def pprompt():
            return getpass.getpass(), getpass.getpass('Retype password:'******'Passwords do not match. Try again')
            p1, p2 = pprompt()

    # Setup Flask-Security
    user_datastore = SQLAlchemyUserDatastore(db, User, Role)
    security = Security(app, user_datastore)

    with app.app_context():
        password = encrypt_password(p1)

        db.create_all()
        user_datastore.create_role(name='Administrators',
                                   description='pgAdmin Administrators Role')
        user_datastore.create_user(email=email, password=password)
        db.session.flush()
        user_datastore.add_role_to_user(email, 'Administrators')

        # Get the user's ID and create the default server group
        user = User.query.filter_by(email=email).first()
        server_group = ServerGroup(user_id=user.id, name="Servers")
        db.session.merge(server_group)

        # Set the schema version
        version = Version(name='ConfigDB',
                          value=config.SETTINGS_SCHEMA_VERSION)
        db.session.merge(version)

        db.session.commit()

    # Done!
    print("")
    print("The configuration database has been created at {0}".format(
        config.SQLITE_PATH))
예제 #3
0
def do_setup(app):
    """Create a new settings database from scratch"""
    if config.SERVER_MODE is False:
        print("NOTE: Configuring authentication for DESKTOP mode.")
        email = config.DESKTOP_USER
        p1 = "".join([random.choice(string.ascii_letters + string.digits) for n in xrange(32)])

    else:
        print("NOTE: Configuring authentication for SERVER mode.\n")

        # Prompt the user for their default username and password.
        print("Enter the email address and password to use for the initial pgAdmin user account:\n")
        email = ""
        while email == "":
            email = raw_input("Email address: ")

        pprompt = lambda: (getpass.getpass(), getpass.getpass("Retype password: "******"Passwords do not match. Try again")
            p1, p2 = pprompt()

    # Setup Flask-Security
    user_datastore = SQLAlchemyUserDatastore(db, User, Role)
    security = Security(app, user_datastore)

    with app.app_context():
        password = encrypt_password(p1)

        db.create_all()
        user_datastore.create_role(name="Administrators", description="pgAdmin Administrators Role")
        user_datastore.create_user(email=email, password=password)
        db.session.flush()
        user_datastore.add_role_to_user(email, "Administrators")

        # Get the user's ID and create the default server group
        user = User.query.filter_by(email=email).first()
        server_group = ServerGroup(user_id=user.id, name="Servers")
        db.session.merge(server_group)

        # Set the schema version
        version = Version(name="ConfigDB", value=config.SETTINGS_SCHEMA_VERSION)
        db.session.merge(version)

        db.session.commit()

    # Done!
    print("")
    print("The configuration database has been created at %s" % config.SQLITE_PATH)
예제 #4
0
class UserRepository(object):
    def __init__(self):
        self.ud = SQLAlchemyUserDatastore(db, User, Role)

    def get(self, email):
        return self.ud.get_user(email)

    def create(self, username, email, pwd):
        user = self.ud.create_user(
            email=email,
            username=username,
            password=pwd
        )
        db.session.commit()
        return user

    def login(self, email, pwd):
        user = self.get(email)
        if user:
            if user.password == pwd:
                return login_user(user)
        return False

    def register(self, username, email, pwd):
        user = self.get(email)
        if not user:
            user_obj = self.create(username, email, pwd)
            return login_user(user_obj)
        else:
            return False
예제 #5
0
    # create tabdata queries
    replace_tabdata_query(
        db, name="Tabdata_hexbin",
        description="Hexbin of geographical data", 
        parameters='{ "locationColumn" : 0, "valueColumn" : 0, "textColumn" : 0 }',
        template="tabdata_hexbin_query.html")
    replace_tabdata_query(
        db, name="Tabdata_markercluster",
        description="Markercluster map of geographical data",
        parameters='{ "locationColumn" : 0, "valueColumn" : 0, "textColumn" : 0 }', 
        template="tabdata_markercluster_query.html")

        # create users
    user_datastore = SQLAlchemyUserDatastore(db, User, Role)
    if user_datastore.find_user(nickname="admin") is None:
        admin = user_datastore.create_user(
            nickname="admin", email="*****@*****.**", password="******")
        db.session.add(admin)
        db.session.commit()

    create_gps_cache()


if not os.path.exists(SQLALCHEMY_MIGRATE_REPO):
    api.create(SQLALCHEMY_MIGRATE_REPO, 'database repository')
    api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
else:
    api.version_control(SQLALCHEMY_DATABASE_URI,
                        SQLALCHEMY_MIGRATE_REPO,
                        api.version(SQLALCHEMY_MIGRATE_REPO))
예제 #6
0
파일: tut_tests.py 프로젝트: vputz/marion
class UserTest(MarionTest):
    def setUp(self):
        self.user_datastore = SQLAlchemyUserDatastore(tutorial.db, User, Role)
        tutorial.db.create_all()
        #u = User( nickname='test1', email='*****@*****.**' )
        self.user = self.user_datastore.create_user(nickname='test1',
                                                    email='*****@*****.**',
                                                    password='******')
        tutorial.db.session.add(self.user)
        tutorial.db.session.commit()
        # must commit before ID exists
        self.first_id = self.user.id

    def get_user(self):
        return User.query.get(1)

    def _login(self, email="*****@*****.**", password="******"):
        data = {'email': email, 'password': password, 'remember': 'y'}
        #print( "login: "******""
        with open(filename, 'r') as f:
            result = f.read()
        return result

    def test_upload_form(self):

        self._login()
        rv = self.client.post(
            '/upload_file/',
            buffered=True,
            content_type='multipart/form-data',
            data={'file_1': (BytesIO(b'Hello, World!'), 'test.txt')},
            follow_redirects=True)
        # check to see if the file is there
        filename = os.path.join(self._user_basedir, 'test.txt')
        self.assertTrue(os.path.isfile(filename))
        self.assertEqual(self.fileContents(filename), "Hello, World!")

    def test_upload_api(self):

        self._login()
        rv = self.client.post(
            '/api/1.0/upload',
            buffered=True,
            content_type='multipart/form-data',
            data={'files': (BytesIO(b'Hello, World!'), 'test.txt')},
            follow_redirects=True)
        # check to see if the file is there
        filename = os.path.join(self._user_basedir, 'test.txt')
        self.assertTrue(os.path.isfile(filename))
        self.assertEqual(self.fileContents(filename), "Hello, World!")
        self._logout()

    def clear_test_data_directory(self):
        if os.path.exists(self._user_basedir):
            shutil.rmtree(self._user_basedir)

    dataset_description = "Description"
    dataset_query = "Query"

    def add_basic_dataset(self):
        rv = self.client.post("/add_dataset/",
                              buffered=True,
                              data={
                                  'descriptionField': self.dataset_description,
                                  'queryField': self.dataset_query
                              },
                              follow_redirects=True)

    def test_add_dataset_form(self):
        self._login()
        u = self.get_user()
        self.add_basic_dataset()
        self.assertEqual(len(list(u.datasets)), 1)
        self.assertEqual(u.datasets[0].description, self.dataset_description)
        self.assertEqual(u.datasets[0].query_text, self.dataset_query)

    def add_tabfiles(self, dataset_id, filename):
        with open(os.path.join('test_data', filename), 'rb') as f1:
            rv = self.client.post("/edit_dataset/" + str(dataset_id),
                                  buffered=True,
                                  data={'file_1': (f1, filename)},
                                  follow_redirects=True)
            f1.close()
            print(rv)

    def test_dataset_h5_up_to_date(self):
        self._login()
        u = self.get_user()
        self.add_basic_dataset()
        d = u.datasets[0]
        self.add_tabfiles(d.id, 'metamaterials_1.tab')
        self.assertEqual(len(list(d.csv_files)), 1)
        self.assertEqual(d.h5_file_is_up_to_date(), False)
        self.client.post('/regenerate_h5/' + str(d.id))
        self.assertEqual(d.h5_file_is_up_to_date(), True)
        self.add_tabfiles(d.id, 'metamaterials_2.tab')
        self.assertEqual(len(list(d.csv_files)), 2)
        self.assertEqual(d.h5_file_is_up_to_date(), False)
        self.client.post('/regenerate_h5/' + str(d.id))
        self.assertEqual(d.h5_file_is_up_to_date(), True)
예제 #7
0
class GPSTest(MarionTest):
    """
    Tests both the GPS lookup and some biblio functions that rely on it
    """
    def setUp(self):
        self.user_datastore = SQLAlchemyUserDatastore(tutorial.db, User, Role)
        tutorial.db.create_all()
        # u = User( nickname='test1', email='*****@*****.**' )
        self.remap_from = "University of Oxford"
        self.remap_to = "Oxford, United Kingdom"
        self.cache_loc = "Oxford, United Kingdom"
        self.cache_lat = 51.7519
        self.cache_lon = -1.2578

        self.user = self.user_datastore.create_user(nickname='test1',
                                                    email='*****@*****.**',
                                                    password='******')
        tutorial.db.session.add(self.user)
        tutorial.db.session.commit()

        oxford_remap = Gps_remap(from_location=self.remap_from,
                                 to_location=self.remap_to)
        tutorial.db.session.add(oxford_remap)

        oxford_lookup = Gps_cache(location=self.cache_loc,
                                  latitude=self.cache_lat,
                                  longitude=self.cache_lon)
        tutorial.db.session.add(oxford_lookup)

        tutorial.db.session.commit()

    def _logout(self):
        logout_user()

    def tearDown(self):
        self._logout()
        tutorial.db.session.remove()
        tutorial.db.drop_all()

    def test_retrieval(self):
        remap = Gps_remap.query.get(self.remap_from)
        self.assertEqual(remap.from_location, self.remap_from)

        cache = Gps_cache.query.get(self.cache_loc)
        self.assertAlmostEqual(cache.latitude, self.cache_lat)
        self.assertAlmostEqual(cache.longitude, self.cache_lon)

    def test_has_keys(self):
        self.assertAlmostEqual(remap_has_key(self.remap_from), True)
        self.assertAlmostEqual(cache_has_key(self.cache_loc), True)

    def test_simplified_guess(self):
        self.assertEqual(
            next_guess("Inst Angew Phys, D-76131 Karlsruhe, Germany"),
            "Inst Angew Phys, D-76131 Karlsruhe, Germany")
        self.assertEqual(
            next_guess(
                "Soochow Univ, Sch Phys Sci & Technol, Suzhou 215006, Jiangsu, Peoples R China"
            ),
            "Sch Phys Sci & Technol, Suzhou 215006, Jiangsu, Peoples R China")

    def test_get_location(self):
        self.assertEqual(get_location("blorfing"), None)
        self.assertAlmostEqual(
            get_location(self.cache_loc)['lat'], self.cache_lat)
        self.assertAlmostEqual(get_location("london")['lon'], -0.1277583)

    def test_get_locations_and_unknowns(self):
        locs, unks = get_locations_and_unknowns(
            ["University of Oxford", "blorfing"], False)
        self.assertAlmostEqual(locs["University of Oxford"]['lon'],
                               self.cache_lon)
        self.assertEqual(unks[0], "blorfing")

    def test_paper_lat_lon_query(self):
        """
        """
        with wos_reader.open_wos_h5("test_data/irwin.h5") as w5:
            locresults = wos_reader_query.paperLatLonQuery(
                w5, get_locations_and_unknowns_nocache)
        tmp = self.maxDiff
        self.maxDiff = None
        irwin_network = {
            'not_located': [
                'Kyushu Univ, Dept Phys, Fukuoka 812, Japan',
                'Natl Res Inst Astron & Geophys, Dept Astron, Cairo 11421, Egypt; Natl Res Inst Astron & Geophys, Kottamia Ctr Sci Excellence Astron & Space Sci KC, Cairo 11421, Egypt',
                'Natl Res Inst Astron & Geophys, Dept Astron, Cairo 11421, Egypt'
            ],
            'paper_locations': {
                '10.1051/mmnp/20138208': {
                    'edges': {
                        0: {
                            'to': 1,
                            'text': '10.1051/mmnp/20138208',
                            'from': 0,
                            'val': 0
                        },
                        1: {
                            'to': 2,
                            'text': '10.1051/mmnp/20138208',
                            'from': 0,
                            'val': 0
                        },
                        2: {
                            'to': 3,
                            'text': '10.1051/mmnp/20138208',
                            'from': 0,
                            'val': 0
                        },
                        3: {
                            'to': 4,
                            'text': '10.1051/mmnp/20138208',
                            'from': 0,
                            'val': 0
                        },
                        4: {
                            'to': 2,
                            'text': '10.1051/mmnp/20138208',
                            'from': 1,
                            'val': 0
                        },
                        5: {
                            'to': 3,
                            'text': '10.1051/mmnp/20138208',
                            'from': 1,
                            'val': 0
                        },
                        6: {
                            'to': 4,
                            'text': '10.1051/mmnp/20138208',
                            'from': 1,
                            'val': 0
                        },
                        7: {
                            'to': 3,
                            'text': '10.1051/mmnp/20138208',
                            'from': 2,
                            'val': 0
                        },
                        8: {
                            'to': 4,
                            'text': '10.1051/mmnp/20138208',
                            'from': 2,
                            'val': 0
                        },
                        9: {
                            'to': 4,
                            'text': '10.1051/mmnp/20138208',
                            'from': 3,
                            'val': 0
                        }
                    },
                    'nodes': {
                        0: {
                            'text':
                            '10.1051/mmnp/20138208: Univ Southern Denmark, MEMPHYS Ctr Biomembrane Phys, Dept Phys Chem & Pharm, DK-5230 Odense M, Denmark',
                            'val': 0,
                            'lon': 10.4033399,
                            'lat': 55.37906169999999
                        },
                        1: {
                            'text':
                            '10.1051/mmnp/20138208: Univ Potsdam, Inst Phys & Astron, D-14476 Potsdam, Germany; Tech Univ Tampere, Dept Phys, FI-33101 Tampere, Finland',
                            'val': 0,
                            'lon': 23.7610254,
                            'lat': 61.4981508
                        },
                        2: {
                            'text':
                            '10.1051/mmnp/20138208: Univ Oxford, Rudolf Peierls Ctr Theoret Phys, Oxford OX1 3NP, England',
                            'val': 0,
                            'lon': -1.259116,
                            'lat': 51.7595933
                        },
                        3: {
                            'text':
                            '10.1051/mmnp/20138208: Inst Theoret Phys NSC KIPT, UA-61108 Kharkov, Ukraine; Max Planck Inst Phys Komplexer Syst, D-01187 Dresden, Germany',
                            'val': 0,
                            'lon': 13.7090684,
                            'lat': 51.0266014
                        },
                        4: {
                            'text':
                            '10.1051/mmnp/20138208: Humboldt Univ, Inst Phys, D-12489 Berlin, Germany',
                            'val': 0,
                            'lon': 13.5470509,
                            'lat': 52.4370179
                        }
                    }
                }
            },
            'failed_papers': ['', '10.1016/j.newast.2014.02.011']
        }
        self.assertEqual(locresults['paper_locations'].keys(),
                         irwin_network['paper_locations'].keys())
        self.assertEqual(set(locresults['failed_papers']),
                         set(irwin_network['failed_papers']))
        self.assertEqual(set(locresults['not_located']),
                         set(irwin_network['not_located']))
        self.maxDiff = tmp

    def test_paper_hexbin_query(self):
        with wos_reader.open_wos_h5("test_data/irwin.h5") as w5:
            locresults = wos_reader_query.paperHexbinQuery(
                w5, get_locations_and_unknowns_nocache)
        expected_results = {
            'not_located': [
                'Kyushu Univ, Dept Phys, Fukuoka 812, Japan',
                'Natl Res Inst Astron & Geophys, Dept Astron, Cairo 11421, Egypt',
                'Natl Res Inst Astron & Geophys, Dept Astron, Cairo 11421, Egypt; Natl Res Inst Astron & Geophys, Kottamia Ctr Sci Excellence Astron & Space Sci KC, Cairo 11421, Egypt'
            ],
            'paper_locations': {
                '10.1051/mmnp/20138208': {
                    'nodes': {
                        '3': {
                            'val': 0,
                            'text':
                            '10.1051/mmnp/20138208: Humboldt Univ, Inst Phys, D-12489 Berlin, Germany',
                            'lat': 52.4370179,
                            'lon': 13.5470509
                        },
                        '2': {
                            'val': 0,
                            'text':
                            '10.1051/mmnp/20138208: Univ Potsdam, Inst Phys & Astron, D-14476 Potsdam, Germany; Tech Univ Tampere, Dept Phys, FI-33101 Tampere, Finland',
                            'lat': 61.4981508,
                            'lon': 23.7610254
                        },
                        '1': {
                            'val': 0,
                            'text':
                            '10.1051/mmnp/20138208: Univ Oxford, Rudolf Peierls Ctr Theoret Phys, Oxford OX1 3NP, England',
                            'lat': 51.7595933,
                            'lon': -1.259116
                        },
                        '0': {
                            'val': 0,
                            'text':
                            '10.1051/mmnp/20138208: Univ Southern Denmark, MEMPHYS Ctr Biomembrane Phys, Dept Phys Chem & Pharm, DK-5230 Odense M, Denmark',
                            'lat': 55.37906169999999,
                            'lon': 10.4033399
                        },
                        '4': {
                            'val': 0,
                            'text':
                            '10.1051/mmnp/20138208: Inst Theoret Phys NSC KIPT, UA-61108 Kharkov, Ukraine; Max Planck Inst Phys Komplexer Syst, D-01187 Dresden, Germany',
                            'lat': 51.0266014,
                            'lon': 13.7090684
                        }
                    },
                    'edges': {
                        '3': {
                            'val': 0,
                            'to': 4,
                            'from': 0,
                            'text': '10.1051/mmnp/20138208'
                        },
                        '2': {
                            'val': 0,
                            'to': 3,
                            'from': 0,
                            'text': '10.1051/mmnp/20138208'
                        },
                        '1': {
                            'val': 0,
                            'to': 2,
                            'from': 0,
                            'text': '10.1051/mmnp/20138208'
                        },
                        '0': {
                            'val': 0,
                            'to': 1,
                            'from': 0,
                            'text': '10.1051/mmnp/20138208'
                        },
                        '7': {
                            'val': 0,
                            'to': 3,
                            'from': 2,
                            'text': '10.1051/mmnp/20138208'
                        },
                        '6': {
                            'val': 0,
                            'to': 4,
                            'from': 1,
                            'text': '10.1051/mmnp/20138208'
                        },
                        '5': {
                            'val': 0,
                            'to': 3,
                            'from': 1,
                            'text': '10.1051/mmnp/20138208'
                        },
                        '4': {
                            'val': 0,
                            'to': 2,
                            'from': 1,
                            'text': '10.1051/mmnp/20138208'
                        },
                        '9': {
                            'val': 0,
                            'to': 4,
                            'from': 3,
                            'text': '10.1051/mmnp/20138208'
                        },
                        '8': {
                            'val': 0,
                            'to': 4,
                            'from': 2,
                            'text': '10.1051/mmnp/20138208'
                        }
                    }
                }
            },
            'failed_papers': ['', '10.1016/j.newast.2014.02.011'],
            'hexbin': {
                'nodes': [{
                    'text':
                    '10.1051/mmnp/20138208: Univ Southern Denmark, MEMPHYS Ctr Biomembrane Phys, Dept Phys Chem & Pharm, DK-5230 Odense M, Denmark',
                    'lng': 10.4033399,
                    'lat': 55.37906169999999,
                    'pubcount': 1
                }, {
                    'text':
                    '10.1051/mmnp/20138208: Univ Oxford, Rudolf Peierls Ctr Theoret Phys, Oxford OX1 3NP, England',
                    'lng': -1.259116,
                    'lat': 51.7595933,
                    'pubcount': 1
                }, {
                    'text':
                    '10.1051/mmnp/20138208: Univ Potsdam, Inst Phys & Astron, D-14476 Potsdam, Germany; Tech Univ Tampere, Dept Phys, FI-33101 Tampere, Finland',
                    'lng': 23.7610254,
                    'lat': 61.4981508,
                    'pubcount': 1
                }, {
                    'text':
                    '10.1051/mmnp/20138208: Humboldt Univ, Inst Phys, D-12489 Berlin, Germany',
                    'lng': 13.5470509,
                    'lat': 52.4370179,
                    'pubcount': 1
                }, {
                    'text':
                    '10.1051/mmnp/20138208: Inst Theoret Phys NSC KIPT, UA-61108 Kharkov, Ukraine; Max Planck Inst Phys Komplexer Syst, D-01187 Dresden, Germany',
                    'lng': 13.7090684,
                    'lat': 51.0266014,
                    'pubcount': 1
                }],
                'edges': [{
                    'fromlng': 10.4033399,
                    'fromlat': 55.37906169999999,
                    'tolng': -1.259116,
                    'weight': 1,
                    'tolat': 51.7595933
                }, {
                    'fromlng': 10.4033399,
                    'fromlat': 55.37906169999999,
                    'tolng': 23.7610254,
                    'weight': 1,
                    'tolat': 61.4981508
                }, {
                    'fromlng': 10.4033399,
                    'fromlat': 55.37906169999999,
                    'tolng': 13.5470509,
                    'weight': 1,
                    'tolat': 52.4370179
                }, {
                    'fromlng': 10.4033399,
                    'fromlat': 55.37906169999999,
                    'tolng': 13.7090684,
                    'weight': 1,
                    'tolat': 51.0266014
                }, {
                    'fromlng': -1.259116,
                    'fromlat': 51.7595933,
                    'tolng': 23.7610254,
                    'weight': 1,
                    'tolat': 61.4981508
                }, {
                    'fromlng': -1.259116,
                    'fromlat': 51.7595933,
                    'tolng': 13.5470509,
                    'weight': 1,
                    'tolat': 52.4370179
                }, {
                    'fromlng': -1.259116,
                    'fromlat': 51.7595933,
                    'tolng': 13.7090684,
                    'weight': 1,
                    'tolat': 51.0266014
                }, {
                    'fromlng': 23.7610254,
                    'fromlat': 61.4981508,
                    'tolng': 13.5470509,
                    'weight': 1,
                    'tolat': 52.4370179
                }, {
                    'fromlng': 23.7610254,
                    'fromlat': 61.4981508,
                    'tolng': 13.7090684,
                    'weight': 1,
                    'tolat': 51.0266014
                }, {
                    'fromlng': 13.5470509,
                    'fromlat': 52.4370179,
                    'tolng': 13.7090684,
                    'weight': 1,
                    'tolat': 51.0266014
                }]
            }
        }
        self.assertEqual(len(locresults['hexbin']['nodes']),
                         len(expected_results['hexbin']['nodes']))
        self.assertEqual(len(locresults['hexbin']['edges']),
                         len(expected_results['hexbin']['edges']))
예제 #8
0
def do_setup(app):
    """Create a new settings database from scratch"""
    if config.SERVER_MODE is False:
        print("NOTE: Configuring authentication for DESKTOP mode.")
        email = config.DESKTOP_USER
        p1 = ''.join([
                         random.choice(string.ascii_letters + string.digits)
                         for n in range(32)
                         ])

    else:
        print("NOTE: Configuring authentication for SERVER mode.\n")

        # Prompt the user for their default username and password.
        print("""
Enter the email address and password to use for the initial pgAdmin user \
account:\n""")
        email_filter = re.compile(
            "^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9]"
            "(?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9]"
            "(?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")

        email = ''
        input("Email address: ")
        while email == '' or not email_filter.match(email):
            print('Invalid email address. Please try again.')
            email = input("Email address: ")

        def pprompt():
            return getpass.getpass(), getpass.getpass('Retype password:'******'Passwords do not match. Please try again.')
            p1, p2 = pprompt()

    # Setup Flask-Security
    user_datastore = SQLAlchemyUserDatastore(db, User, Role)
    Security(app, user_datastore)

    with app.app_context():
        password = encrypt_password(p1)

        db.create_all()
        user_datastore.create_role(
            name='Administrator',
            description='pgAdmin Administrator Role'
        )
        user_datastore.create_role(
            name='User',
            description='pgAdmin User Role'
        )

        user_datastore.create_user(email=email, password=password)
        db.session.flush()
        user_datastore.add_role_to_user(email, 'Administrator')

        # Get the user's ID and create the default server group
        user = User.query.filter_by(email=email).first()
        server_group = ServerGroup(user_id=user.id, name="Servers")
        db.session.merge(server_group)

        # Set the schema version
        version = Version(
            name='ConfigDB', value=config.SETTINGS_SCHEMA_VERSION
        )
        db.session.merge(version)

        db.session.commit()

    # Done!
    print("")
    print(
        "The configuration database has been created at {0}".format(
            config.SQLITE_PATH
        )
    )
예제 #9
0
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///videlish.db'

    
    
Base.metadata.bind = engine

DBSession = sessionmaker(bind=engine)
session = DBSession()


# Setup Flask-Security
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
security = Security(app, user_datastore)

newRecipe1 = Recipe(name = 'Spanish Omlet', host = 'YouTube', credit = 'Tasty', hostId = 'ah4YoiIbRpc', photo = "https://img.youtube.com/vi/ah4YoiIbRpc/mqdefault.jpg"
)
newRecipe2 = Recipe(name = 'Almond Butter Oatmeal Muffins', host = 'YouTube', credit = 'Quaker', hostId = 'OqfNwI0Qxww', photo = "https://img.youtube.com/vi/OqfNwI0Qxww/mqdefault.jpg" )

user1 = Account(name = 'Emma Green', username = '******')
user_datastore.create_user(email='*****@*****.**', password='******')

recipeLike1 = RecipeLike(account = user1, recipe=newRecipe1)

session.add(newRecipe1)
session.add(newRecipe2)
session.add(user1)
session.commit()
session.add(recipeLike1)


session.commit()
def create_user():
    user_datastore = SQLAlchemyUserDatastore(db, User, Role)
    user_datastore.create_user(email='admin', password='******')
    db.session.commit()
예제 #11
0
from flask.ext.security import Security, SQLAlchemyUserDatastore
from flask_wtf.csrf import CsrfProtect
from config import FIRST_USER_PASS, FIRST_USER_NAME

# Initialize the app and database, import the config
app = Flask(__name__)
app.config.from_object('config')
db = SQLAlchemy(app)
CsrfProtect(app)
app.killswitch = 0

# setup db, create the db, setup security, and add first user
from app import models
userstore = SQLAlchemyUserDatastore(db, models.User, None)
sec = Security(app, userstore)
db.create_all()
try:
    userstore.create_user(email=FIRST_USER_NAME, password=FIRST_USER_PASS)
    db.session.commit()
except: db.session.rollback()

#this loads all views
from app.views import main, admin

#admin setup
_admin = Admin(app, 'NYX Admin', template_mode='bootstrap3',
              index_view=admin.ProtectedIndexView())
_admin.add_link(MenuLink(name='Back to Site', url='/'))
_admin.add_view(admin.UserModelView(models.User, db.session))
_admin.add_view(admin.BotModelView(models.Bot, db.session))
예제 #12
0
파일: app.py 프로젝트: LACMTA/places
def create_app(config_name):
	from models import (
		User, 
		Role, 
		)
	from places.models import (
		Category, 
		Feature, 
		Place,
		)

	app = Flask(__name__)
	app.config.from_object(config[config_name])
	config[config_name].init_app(app)
	user_datastore = SQLAlchemyUserDatastore(db, User, Role)

	security.init_app(app,user_datastore)
	principal.init_app(app)
	# jwt.init_app(app)
	api.init_app(app)
	admin.init_app(app)
	assets.init_app(app)
	assets.register('css_all', css_all)
	assets.register('js_vendor', js_vendor)

	with app.app_context():
		db.init_app(app)
		db.create_all()

		if not Role.query.first():
			admin_role = Role(name='admin',description='Administrator')
			db.session.add(admin_role)
			db.session.commit()
		else:
			admin_role = Role.query.filter(Role.name=='admin').first()

		if not User.query.first():
			user_datastore.create_user(
				email='*****@*****.**',
				password=encrypt_password('Haukola'),
				roles=[admin_role]
				)
			db.session.commit()

	# attach routes and custom error pages here
	admin.add_view(UserModelView(User, db.session, category='Auth'))
	admin.add_view(AdminModelView(Role, db.session, category='Auth'))
	admin.add_view(PlaceAdmin(Place, db.session))
	admin.add_view(CategoryAdmin(Category, db.session))
	admin.add_view(FeatureAdmin(Feature, db.session))
	admin.add_view(LogoutView(name='Logout', endpoint='logout'))
	admin.add_view(LoginView(name='Login', endpoint='login'))

	sitemap_view = Sitemap.as_view('sitemap')
	app.add_url_rule('/sitemap', view_func=sitemap_view)
	
	tapvendors_csv_view = TAPVendorsCSV.as_view('tapvendors_csv')
	app.add_url_rule('/csv/tapvendors', view_func=tapvendors_csv_view)
	railstations_csv_view = RailStationsCSV.as_view('railstations_csv')
	app.add_url_rule('/csv/railstations', view_func=railstations_csv_view)

	placemap_view = Placemap.as_view('placemap')
	app.add_url_rule('/placemap/', 
		view_func=placemap_view,
		methods=['GET'],
		)
	placemaptapvendors_view = Placemap.as_view('placemaptapvendors')
	app.add_url_rule('/placemap/tapvendors', 
		view_func=placemaptapvendors_view,
		methods=['GET'],
		)
	placemaprailstations_view = Railmap.as_view('placemaprailstations')
	app.add_url_rule('/placemap/railstations', 
		view_func=placemaprailstations_view,
		methods=['GET'],
		)

	return app
예제 #13
0
파일: dhbox.py 프로젝트: sabo/dhbox
                    duration = 86400
                elif user['duration'] == 'week':
                    duration = 604800
                else:
<<<<<<< HEAD
                    duration = 13148730
=======
                    duration = 2592000
                # if user['duration'] == 'week':
                #     duration = 604800
                # elif user['duration'] == 'month':
                #     duration = 2592000
                # else:
                #     duration = 13148730 
>>>>>>> master
                admin_user_object = user_datastore.create_user(email=user['email'], name=user['name'], password=user['pass'], dhbox_duration=duration)
                db.session.commit()
                login_user(admin_user_object)
                the_new_dhbox = DockerBackend.setup_new_dhbox(admin_user, admin_pass, admin_email)
    return str('Successfully created a new DH Box.')


@app.route('/kill_dhbox', methods=['POST'])
@login_required
def kill_dhbox():
    the_next = request.form.get('next')
    user = request.form.get('user')
    print(user)
    if current_user.has_role("admin"):
        pass
    elif user != current_user.name:
예제 #14
0
파일: sandbox.py 프로젝트: napplebee/culog
# -*- coding: utf-8 -*-

from flask.ext.security import SQLAlchemyUserDatastore

from app import create_app
from app.core import db
from app.data.users import User, Role

app = create_app("dev")
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
# security = Security(app, user_datastore)

# Create a user to test with
# db.drop_all()
# db.create_all()
role = user_datastore.create_role(name="root", description="Site administrator")
user_datastore.create_user(email='*****@*****.**', password='******', roles=[role])
user_datastore.create_user(email='*****@*****.**', password='******')

db.session.commit()

class Test(object):
    id = 1
    en_title = "title"
    ru_title = u"тайтл"
예제 #15
0
class GPSTest(MarionTest):

    """
    Tests both the GPS lookup and some biblio functions that rely on it
    """
    def setUp(self):
        self.user_datastore = SQLAlchemyUserDatastore(tutorial.db, User, Role)
        tutorial.db.create_all()
        # u = User( nickname='test1', email='*****@*****.**' )
        self.remap_from = "University of Oxford"
        self.remap_to = "Oxford, United Kingdom"
        self.cache_loc = "Oxford, United Kingdom"
        self.cache_lat = 51.7519
        self.cache_lon = -1.2578

        self.user = self.user_datastore.create_user(nickname='test1',
                                                    email='*****@*****.**',
                                                    password='******')
        tutorial.db.session.add(self.user)
        tutorial.db.session.commit()

        oxford_remap = Gps_remap(from_location=self.remap_from,
                                 to_location=self.remap_to)
        tutorial.db.session.add(oxford_remap)

        oxford_lookup = Gps_cache(location=self.cache_loc,
                                  latitude=self.cache_lat,
                                  longitude=self.cache_lon)
        tutorial.db.session.add(oxford_lookup)

        tutorial.db.session.commit()

    def _logout(self):
        logout_user()

    def tearDown(self):
        self._logout()
        tutorial.db.session.remove()
        tutorial.db.drop_all()

    def test_retrieval(self):
        remap = Gps_remap.query.get(self.remap_from)
        self.assertEqual(remap.from_location, self.remap_from)

        cache = Gps_cache.query.get(self.cache_loc)
        self.assertAlmostEqual(cache.latitude, self.cache_lat)
        self.assertAlmostEqual(cache.longitude, self.cache_lon)

    def test_has_keys(self):
        self.assertAlmostEqual(remap_has_key(self.remap_from), True)
        self.assertAlmostEqual(cache_has_key(self.cache_loc), True)

    def test_simplified_guess(self):
        self.assertEqual(next_guess("Inst Angew Phys, D-76131 Karlsruhe, Germany"),
                         "Inst Angew Phys, D-76131 Karlsruhe, Germany")
        self.assertEqual(next_guess("Soochow Univ, Sch Phys Sci & Technol, Suzhou 215006, Jiangsu, Peoples R China"),
                         "Sch Phys Sci & Technol, Suzhou 215006, Jiangsu, Peoples R China")

    def test_get_location(self):
        self.assertEqual(get_location("blorfing"), None)
        self.assertAlmostEqual(get_location(self.cache_loc)['lat'],
                               self.cache_lat)
        self.assertAlmostEqual(get_location("london")['lon'], -0.1277583)

    def test_get_locations_and_unknowns(self):
        locs, unks = get_locations_and_unknowns(
            ["University of Oxford", "blorfing"],
            False)
        self.assertAlmostEqual(locs["University of Oxford"]['lon'],
                               self.cache_lon)
        self.assertEqual(unks[0], "blorfing")

    def test_paper_lat_lon_query(self):
        """
        """
        with wos_reader.open_wos_h5("test_data/irwin.h5") as w5:
            locresults = wos_reader_query.paperLatLonQuery(
                w5, get_locations_and_unknowns_nocache)
        tmp = self.maxDiff
        self.maxDiff = None
        irwin_network = {'not_located': ['Kyushu Univ, Dept Phys, Fukuoka 812, Japan', 'Natl Res Inst Astron & Geophys, Dept Astron, Cairo 11421, Egypt; Natl Res Inst Astron & Geophys, Kottamia Ctr Sci Excellence Astron & Space Sci KC, Cairo 11421, Egypt', 'Natl Res Inst Astron & Geophys, Dept Astron, Cairo 11421, Egypt'], 'paper_locations': {'10.1051/mmnp/20138208': {'edges': {0: {'to': 1, 'text': '10.1051/mmnp/20138208', 'from': 0, 'val': 0}, 1: {'to': 2, 'text': '10.1051/mmnp/20138208', 'from': 0, 'val': 0}, 2: {'to': 3, 'text': '10.1051/mmnp/20138208', 'from': 0, 'val': 0}, 3: {'to': 4, 'text': '10.1051/mmnp/20138208', 'from': 0, 'val': 0}, 4: {'to': 2, 'text': '10.1051/mmnp/20138208', 'from': 1, 'val': 0}, 5: {'to': 3, 'text': '10.1051/mmnp/20138208', 'from': 1, 'val': 0}, 6: {'to': 4, 'text': '10.1051/mmnp/20138208', 'from': 1, 'val': 0}, 7: {'to': 3, 'text': '10.1051/mmnp/20138208', 'from': 2, 'val': 0}, 8: {'to': 4, 'text': '10.1051/mmnp/20138208', 'from': 2, 'val': 0}, 9: {'to': 4, 'text': '10.1051/mmnp/20138208', 'from': 3, 'val': 0}}, 'nodes': {0: {'text': '10.1051/mmnp/20138208: Univ Southern Denmark, MEMPHYS Ctr Biomembrane Phys, Dept Phys Chem & Pharm, DK-5230 Odense M, Denmark', 'val': 0, 'lon': 10.4033399, 'lat': 55.37906169999999}, 1: {'text': '10.1051/mmnp/20138208: Univ Potsdam, Inst Phys & Astron, D-14476 Potsdam, Germany; Tech Univ Tampere, Dept Phys, FI-33101 Tampere, Finland', 'val': 0, 'lon': 23.7610254, 'lat': 61.4981508}, 2: {'text': '10.1051/mmnp/20138208: Univ Oxford, Rudolf Peierls Ctr Theoret Phys, Oxford OX1 3NP, England', 'val': 0, 'lon': -1.259116, 'lat': 51.7595933}, 3: {'text': '10.1051/mmnp/20138208: Inst Theoret Phys NSC KIPT, UA-61108 Kharkov, Ukraine; Max Planck Inst Phys Komplexer Syst, D-01187 Dresden, Germany', 'val': 0, 'lon': 13.7090684, 'lat': 51.0266014}, 4: {'text': '10.1051/mmnp/20138208: Humboldt Univ, Inst Phys, D-12489 Berlin, Germany', 'val': 0, 'lon': 13.5470509, 'lat': 52.4370179}}}}, 'failed_papers': ['', '10.1016/j.newast.2014.02.011']}
        self.assertEqual(locresults['paper_locations'].keys(),
                         irwin_network['paper_locations'].keys())
        self.assertEqual(set(locresults['failed_papers']),
                         set(irwin_network['failed_papers']))
        self.assertEqual(set(locresults['not_located']),
                         set(irwin_network['not_located']))
        self.maxDiff = tmp

    def test_paper_hexbin_query(self):
        with wos_reader.open_wos_h5("test_data/irwin.h5") as w5:
            locresults = wos_reader_query.paperHexbinQuery(
                w5,
                get_locations_and_unknowns_nocache)
        expected_results = {'not_located': ['Kyushu Univ, Dept Phys, Fukuoka 812, Japan', 'Natl Res Inst Astron & Geophys, Dept Astron, Cairo 11421, Egypt', 'Natl Res Inst Astron & Geophys, Dept Astron, Cairo 11421, Egypt; Natl Res Inst Astron & Geophys, Kottamia Ctr Sci Excellence Astron & Space Sci KC, Cairo 11421, Egypt'], 'paper_locations': {'10.1051/mmnp/20138208': {'nodes': {'3': {'val': 0, 'text': '10.1051/mmnp/20138208: Humboldt Univ, Inst Phys, D-12489 Berlin, Germany', 'lat': 52.4370179, 'lon': 13.5470509}, '2': {'val': 0, 'text': '10.1051/mmnp/20138208: Univ Potsdam, Inst Phys & Astron, D-14476 Potsdam, Germany; Tech Univ Tampere, Dept Phys, FI-33101 Tampere, Finland', 'lat': 61.4981508, 'lon': 23.7610254}, '1': {'val': 0, 'text': '10.1051/mmnp/20138208: Univ Oxford, Rudolf Peierls Ctr Theoret Phys, Oxford OX1 3NP, England', 'lat': 51.7595933, 'lon': -1.259116}, '0': {'val': 0, 'text': '10.1051/mmnp/20138208: Univ Southern Denmark, MEMPHYS Ctr Biomembrane Phys, Dept Phys Chem & Pharm, DK-5230 Odense M, Denmark', 'lat': 55.37906169999999, 'lon': 10.4033399}, '4': {'val': 0, 'text': '10.1051/mmnp/20138208: Inst Theoret Phys NSC KIPT, UA-61108 Kharkov, Ukraine; Max Planck Inst Phys Komplexer Syst, D-01187 Dresden, Germany', 'lat': 51.0266014, 'lon': 13.7090684}}, 'edges': {'3': {'val': 0, 'to': 4, 'from': 0, 'text': '10.1051/mmnp/20138208'}, '2': {'val': 0, 'to': 3, 'from': 0, 'text': '10.1051/mmnp/20138208'}, '1': {'val': 0, 'to': 2, 'from': 0, 'text': '10.1051/mmnp/20138208'}, '0': {'val': 0, 'to': 1, 'from': 0, 'text': '10.1051/mmnp/20138208'}, '7': {'val': 0, 'to': 3, 'from': 2, 'text': '10.1051/mmnp/20138208'}, '6': {'val': 0, 'to': 4, 'from': 1, 'text': '10.1051/mmnp/20138208'}, '5': {'val': 0, 'to': 3, 'from': 1, 'text': '10.1051/mmnp/20138208'}, '4': {'val': 0, 'to': 2, 'from': 1, 'text': '10.1051/mmnp/20138208'}, '9': {'val': 0, 'to': 4, 'from': 3, 'text': '10.1051/mmnp/20138208'}, '8': {'val': 0, 'to': 4, 'from': 2, 'text': '10.1051/mmnp/20138208'}}}}, 'failed_papers': ['', '10.1016/j.newast.2014.02.011'], 'hexbin': {'nodes': [{'text': '10.1051/mmnp/20138208: Univ Southern Denmark, MEMPHYS Ctr Biomembrane Phys, Dept Phys Chem & Pharm, DK-5230 Odense M, Denmark', 'lng': 10.4033399, 'lat': 55.37906169999999, 'pubcount': 1}, {'text': '10.1051/mmnp/20138208: Univ Oxford, Rudolf Peierls Ctr Theoret Phys, Oxford OX1 3NP, England', 'lng': -1.259116, 'lat': 51.7595933, 'pubcount': 1}, {'text': '10.1051/mmnp/20138208: Univ Potsdam, Inst Phys & Astron, D-14476 Potsdam, Germany; Tech Univ Tampere, Dept Phys, FI-33101 Tampere, Finland', 'lng': 23.7610254, 'lat': 61.4981508, 'pubcount': 1}, {'text': '10.1051/mmnp/20138208: Humboldt Univ, Inst Phys, D-12489 Berlin, Germany', 'lng': 13.5470509, 'lat': 52.4370179, 'pubcount': 1}, {'text': '10.1051/mmnp/20138208: Inst Theoret Phys NSC KIPT, UA-61108 Kharkov, Ukraine; Max Planck Inst Phys Komplexer Syst, D-01187 Dresden, Germany', 'lng': 13.7090684, 'lat': 51.0266014, 'pubcount': 1}], 'edges': [{'fromlng': 10.4033399, 'fromlat': 55.37906169999999, 'tolng': -1.259116, 'weight': 1, 'tolat': 51.7595933}, {'fromlng': 10.4033399, 'fromlat': 55.37906169999999, 'tolng': 23.7610254, 'weight': 1, 'tolat': 61.4981508}, {'fromlng': 10.4033399, 'fromlat': 55.37906169999999, 'tolng': 13.5470509, 'weight': 1, 'tolat': 52.4370179}, {'fromlng': 10.4033399, 'fromlat': 55.37906169999999, 'tolng': 13.7090684, 'weight': 1, 'tolat': 51.0266014}, {'fromlng': -1.259116, 'fromlat': 51.7595933, 'tolng': 23.7610254, 'weight': 1, 'tolat': 61.4981508}, {'fromlng': -1.259116, 'fromlat': 51.7595933, 'tolng': 13.5470509, 'weight': 1, 'tolat': 52.4370179}, {'fromlng': -1.259116, 'fromlat': 51.7595933, 'tolng': 13.7090684, 'weight': 1, 'tolat': 51.0266014}, {'fromlng': 23.7610254, 'fromlat': 61.4981508, 'tolng': 13.5470509, 'weight': 1, 'tolat': 52.4370179}, {'fromlng': 23.7610254, 'fromlat': 61.4981508, 'tolng': 13.7090684, 'weight': 1, 'tolat': 51.0266014}, {'fromlng': 13.5470509, 'fromlat': 52.4370179, 'tolng': 13.7090684, 'weight': 1, 'tolat': 51.0266014}]}}
        self.assertEqual(len(locresults['hexbin']['nodes']),
                         len(expected_results['hexbin']['nodes']))
        self.assertEqual(len(locresults['hexbin']['edges']),
                         len(expected_results['hexbin']['edges']))
def init_db():

    fake_app = create_app()

    with fake_app.test_request_context():

        db.create_all()

        datastore = SQLAlchemyUserDatastore(db, User, Role)

        print("\n** ADDING ROLES **")
        for role in ['admin', 'manager']:
            try:
                print("adding role '%s'" % role)
                datastore.create_role(name=role, description=role)
                datastore.commit()
                print("   ...OK")
            except IntegrityError as r:
                print("   '%s' role already exists" % role)
                db.session.rollback()

        print("\n** ADDING USERS **")
        users = [
            fake_app.config['ADMIN_USER'], fake_app.config['MANAGER_USER'],
            fake_app.config['CLERK_USER']
        ]
        for user in users:
            if user:
                name = user['name']
                email = user['email']
                role = user['role']
                password = user['password']
                try:
                    print("adding user '%s'" % name)
                    password = encrypt_password(password)
                    datastore.create_user(email=email,
                                          password=password,
                                          name=name,
                                          active=True)
                    datastore.commit()
                    print("   ...OK")
                except IntegrityError as e:
                    print("   user '%s' already exists" % name)
                    db.session.rollback()

                if role:
                    try:
                        print("adding '%s' role to user '%s'" % (role, name))
                        this_user = db.session.query(User)\
                            .filter(User.name == name).first()
                        this_role = db.session.query(Role).filter(
                            Role.name == role).first()
                        datastore.add_role_to_user(this_user, this_role)
                        datastore.commit()
                        print("   ...OK")
                    except IntegrityError as e:
                        print("   unable to add role '%s' to user '%s'" %
                              (role, name))

    from alembic.config import Config
    from alembic import command
    path = os.path.join(os.path.dirname(__file__), 'alembic.ini')
    alembic_cfg = Config(path)
    command.stamp(alembic_cfg, 'head')