Commit 1f27cdb4 authored by Arthur Diniz's avatar Arthur Diniz 🇧🇷

Creates database seeds

Signed-off-by: Arthur Diniz's avatarArthur Diniz <arthurbdiniz@gmail.com>
parent a939f2f1
......@@ -54,7 +54,7 @@ docker exec image-finder python3 manage.py db upgrade
* **Seed Database**
```bash
docker exec image-finder flask seed run
docker exec image-finder python3 seed.py
```
#### Running tests
......
......@@ -3,6 +3,7 @@ from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_script import Manager, Server
from flask_migrate import Migrate, MigrateCommand
from flask_seeder import FlaskSeeder
app = Flask(__name__)
......@@ -26,5 +27,8 @@ manager = Manager(app)
manager.add_command('db', MigrateCommand)
manager.add_command('runserver', server)
seeder = FlaskSeeder()
seeder.init_app(app, db)
from app.controllers import default
from app.models import Provider, Image
DEVELOPMENT = True
DEBUG = True
DEBUG = False
TESTING = False
CSRF_ENABLED = True
SECRET_KEY = 'this-really-needs-to-be-changed'
SQLALCHEMY_TRACK_MODIFICATIONS = True
\ No newline at end of file
......@@ -3,16 +3,25 @@ Click==7.0
alembic==1.0.10
atomicwrites==1.3.0
attrs==19.1.0
certifi==2019.3.9
chardet==3.0.4
Click==7.0
Flask==1.0.3
Flask-Fixtures==0.3.8
flask-marshmallow==0.10.1
Flask-Migrate==2.5.2
Flask-Script==2.0.6
Flask-Seeder==0.1a2
Flask-SQLAlchemy==2.4.0
idna==2.8
importlib-metadata==0.17
itsdangerous==1.1.0
Jinja2==2.10.1
jsonify==0.5
Mako==1.0.11
MarkupSafe==1.1.1
marshmallow==2.19.2
marshmallow-sqlalchemy==0.16.3
more-itertools==7.0.0
packaging==19.0
pluggy==0.12.0
......@@ -23,8 +32,10 @@ pytest==4.6.2
python-dateutil==2.8.0
python-editor==1.0.4
PyYAML==5.1
requests==2.22.0
six==1.12.0
SQLAlchemy==1.3.4
urllib3==1.25.3
wcwidth==0.1.7
Werkzeug==0.15.4
zipp==0.5.1
{
"providers": [
{
"name": "EC2",
"body": "Description for provider"
},
{
"name": "Google Cloud",
"body": "Description for provider"
},
{
"name": "OpenStack",
"body": "Description for provider"
}
],
"images": [
{
"name": "Image name",
"arch": "amd64",
"release": "sid",
"img_type": "dev",
"vendor": "gce",
"version": "365",
"job_url": "",
"package": "adduser: 3.118, apparmor: 2.13.2-10",
"created_at": "2017-08-09",
"provider_id": "2"
}
]
}
\ No newline at end of file
from app.models import Provider, Image
from app import db
import json
json_file = open('seed.json')
data = json.load(json_file)
for provider in data['providers']:
print('Name: ' + provider['name'])
print('Body: ' + provider['body'])
print('')
db.session.add(Provider(name=provider['name'], body=provider['body']))
for image in data['images']:
print('Name: ' + image['name'])
print('arch: ' + image['arch'])
print('release: ' + image['release'])
print('img_type: ' + image['img_type'])
print('vendor: ' + image['vendor'])
print('version: ' + image['version'])
print('job_url: ' + image['job_url'])
print('package: ' + image['package'])
print('created_at: ' + image['created_at'])
print('provider_id: ' + image['provider_id'])
print('')
db.session.add(Image(name=image['name'],
arch=image['arch'],
release=image['release'],
img_type=image['img_type'],
vendor=image['vendor'],
version=image['version'],
job_url=image['job_url'],
package=image['package'],
created_at=image['created_at'],
provider_id=image['provider_id']))
db.session.commit()
\ No newline at end of file
from flask_seeder import Seeder, Faker, generator
from app.models import Provider, Image
class ProviderSeeder(Seeder):
def run(self):
providers = []
providers.append(Provider(name='EC2', body='In posuere dolor non dui sodales rutrum.'))
providers.append(Provider(name='Google Cloud', body='Phasellus feugiat nulla quis iaculis facilisis.'))
providers.append(Provider(name='OpenStack', body='In interdum odio orci, a fermentum lectus fermentum id.'))
for provider in providers:
print("Adding provider: %s" % provider)
self.db.session.add(provider)
images = []
images.append(Image(name='Nam fringilla pulvinar tellus',
arch='Eu vehicula augue faucibus sed',
release='Etiam quis sollicitudin dolor',
img_type='Aliquam suscipit porttitor justo',
vendor='Vestibulum augue nisi',
version='Faucibus eget dolor sit amet',
job_url='Dapibus eleifend justo',
package='Sed ac quam ipsum',
created_at='2017-08-09',
provider_id=1))
for image in images:
print("Adding images: %s" % image)
self.db.session.add(image)
\ No newline at end of file
......@@ -3,4 +3,4 @@ POSTGRES_PASSWORD=password
POSTGRES_DB=testdb
POSTGRES_HOST=db
DB_PORT=5432
APP_SETTINGS=config.default
APP_SETTINGS=config
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment