Created
May 8, 2017 18:50
-
-
Save adam-stokes/ba1457ba8db0517c0d4d1ee701162aff to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ management command for adding vendor seed data | |
| """ | |
| # pylint: disable=no-member | |
| import re | |
| import yaml | |
| from pathlib import Path | |
| from django.core.files import File | |
| from django.contrib.auth.models import User | |
| # from django.core.exceptions import ValidationError | |
| from django.core.management.base import BaseCommand | |
| from farmserver.models.address import Address | |
| from farmserver.models.vendor import Vendor | |
| from farmserver.models.vendor_product import VendorProduct, VendorProductPhoto | |
| from ...testing.faker import fake | |
| from . import running_postgres | |
| class Command(BaseCommand): | |
| """ command class for seed data """ | |
| help = 'Load Vendor data' | |
| def add_arguments(self, parser): | |
| """Add extra '--run-postgres' argument.""" | |
| super(Command, self).add_arguments(parser) | |
| parser.add_argument( | |
| '--run-postgres', action='store_true', help=( | |
| 'Spawn postgres before performing the action. Only ' | |
| 'used in development.')) | |
| def _create_vendor(self, images): | |
| product = fake.vendor_product() | |
| for img in images: | |
| print("Adding image: {} to vendor: {}".format( | |
| img, | |
| product.vendor.company_name)) | |
| product.photos.create(image=File(img.read_bytes())) | |
| product.featured_photo = product.photos.first() | |
| product.save() | |
| def _load_data(self): | |
| images = Path('assets/placeholder-imgs') | |
| self._create_vendor(images.glob("produce*")) | |
| self._create_vendor(images.glob("meat*")) | |
| self._create_vendor(images.glob("baked*")) | |
| self.stdout.write(self.style.SUCCESS('Added vendor seed data')) | |
| def handle(self, *args, **options): | |
| if options['run_postgres']: | |
| with running_postgres(): | |
| self._load_data() | |
| else: | |
| self._load_data() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment