Esempio n. 1
0
def usage_example():

    # Create a new Config object to ease reading the Platform.sh environment variables.
    # You can alternatively use os.environ yourself.
    config = Config()

    # The 'database' relationship is generally the name of primary SQL database of an application.
    # It could be anything, though, as in the case here here where it's called "database".
    credentials = config.credentials('database')

    try:
        formatted = config.formatted_credentials('database', 'pymongo')

        server = '{0}://{1}:{2}@{3}'.format(credentials['scheme'],
                                            credentials['username'],
                                            credentials['password'], formatted)

        client = MongoClient(server)

        collection = client.main.starwars

        post = {"name": "Rey", "occupation": "Jedi"}

        post_id = collection.insert_one(post).inserted_id

        document = collection.find_one({"_id": post_id})

        # Clean up after ourselves.
        collection.drop()

        return 'Found {0} ({1})<br />'.format(document['name'],
                                              document['occupation'])

    except Exception as e:
        return e
    def test_pymongo_formatter(self):

        config = Config(self.mockEnvironmentDeploy)

        formatted = config.formatted_credentials('mongodb', 'pymongo')

        self.assertEqual('mongodb.internal:27017/main',
                         formatted)  # include formatted string
    def test_formatted_credentials_calls_a_formatter(self):

        config = Config(self.mockEnvironmentDeploy)

        config.register_formatter('test', lambda credentials: 'called')
        formatted = config.formatted_credentials('database', 'test')

        self.assertEqual('called', formatted)
Esempio n. 4
0
def usage_example():

    # Create a new Config object to ease reading the Platform.sh environment variables.
    # You can alternatively use os.environ yourself.
    config = Config()

    # Get the credentials to connect to the Solr service.
    credentials = config.credentials('solr')

    try:
        formatted_url = config.formatted_credentials('solr', 'pysolr')

        # Create a new Solr Client using config variables
        client = pysolr.Solr(formatted_url)

        # Add a document
        message = ''
        doc_1 = {"id": 123, "name": "Valentina Tereshkova"}

        result0 = client.add([doc_1])
        client.commit()
        message += 'Adding one document. Status (0 is success): {0} <br />'.format(
            et.fromstring(result0)[0][0].text)

        # Select one document
        query = client.search('*:*')
        message += '\nSelecting documents (1 expected): {0} <br />'.format(
            str(query.hits))

        # Delete one document
        result1 = client.delete(doc_1['id'])
        client.commit()
        message += '\nDeleting one document. Status (0 is success): {0}'.format(
            et.fromstring(result1)[0][0].text)

        return message

    except Exception as e:
        return e
    def test_formatted_credentials_throws_when_no_formatter_defined(self):

        config = Config(self.mockEnvironmentDeploy)

        with self.assertRaises(NoCredentialFormatterFoundException):
            config.formatted_credentials('database', 'not-defined')
Esempio n. 6
0
config = Config()

# Web server bind address and port, automatically
# mapped by the platform.sh router to the outside world.
# worker instances don't have a port attribute, so we
# need to fake it.
try:
    port = int(config.port)
except AttributeError:
    port = 0

HOST, PORT = "127.0.0.1", port

# PostgreSQL database connection string (data source name)
# including both the credentials and the server address.
SQLALCHEMY_DATABASE_URI = config.formatted_credentials("database",
                                                       "postgresql_dsn")

# Redis cache configuration without Sentinel support.
# This requires the no-sentinel branch of the PyBossa
# repository in order to work.
REDIS_SENTINEL = []
REDIS_CACHE_ENABLED = True
REDIS_HOST = config.credentials("cache")["host"]
REDIS_PORT = config.credentials("cache")["port"]
REDIS_KEYPREFIX = "pybossa_cache"
REDIS_MASTER = "mymaster"
REDIS_DB = 0

# Session secrets, automatically derived from the default
# platform.sh entropy, which is created during the
# first deployment and doesn't change over time.