<commit_before>from optparse import make_option
import pprint

import requests

from django.core.management.base import BaseCommand

from mkt.prices.models import Price, PriceCurrency


domains = {
    'prod': 'https://marketplace.firefox.com',
    'stage': 'https://marketplace.allizom.org',
    'dev': 'https://marketplace-dev.allizom.org',
    'altpay': 'https://payments-alt.allizom.org'
}

endpoint = '/api/v1/webpay/prices/'


class Command(BaseCommand):
    help = """
    Load prices and pricecurrencies from the specified marketplace.
    Defaults to prod.
    """
    option_list = BaseCommand.option_list + (
        make_option('--prod',
                    action='store_const',
                    const=domains['prod'],
                    dest='domain',
                    default=domains['prod'],
                    help='Use prod as source of data.'),
        make_option('--stage',
                    action='store_const',
                    const=domains['stage'],
                    dest='domain',
                    help='Use stage as source of data.'),
        make_option('--dev',
                    action='store_const',
                    const=domains['dev'],
                    dest='domain',
                    help='Use use dev as source of data.'),
        make_option('--altpay',
                    action='store_const',
                    const=domains['altpay'],
                    dest='domain',
                    help='Use use payments-alt as source of data.'),
        make_option('--delete',
                    action='store_true',
                    dest='delete',
                    default=False,
                    help='Start by deleting all prices.'),
        make_option('--noop',
                    action='store_true',
                    dest='noop',
                    default=False,
                    help=('Show data that would be added, '
                          'but do not create objects.')),
    )

    def handle(self, *args, **kw):

        data = requests.get(kw['domain'] + endpoint).json()

        if kw['delete']:
            Price.objects.all().delete()
            PriceCurrency.objects.all().delete()

        if kw['noop']:
            pprint.pprint(data['objects'], indent=2)
        else:
            for p in data['objects']:
                pr = Price.objects.create(name=p['name'].split(' ')[-1],
                                          active=p['active'],
                                          method=p['method'],
                                          price=p['price'])
                for pc in p['prices']:
                    pr.pricecurrency_set.create(currency=pc['currency'],
                                                carrier=pc['carrier'],
                                                price=pc['price'],
                                                paid=pc['paid'],
                                                tier=pc['tier'],
                                                dev=pc['dev'],
                                                provider=pc['provider'],<commit_msg>load prices from dev by default, since that uses zippy<commit_after>from optparse import make_option
import pprint

import requests

from django.core.management.base import BaseCommand

from mkt.prices.models import Price, PriceCurrency


domains = {
    'prod': 'https://marketplace.firefox.com',
    'stage': 'https://marketplace.allizom.org',
    'dev': 'https://marketplace-dev.allizom.org',
    'altpay': 'https://payments-alt.allizom.org'
}

endpoint = '/api/v1/webpay/prices/'


class Command(BaseCommand):
    help = """
    Load prices and pricecurrencies from the specified marketplace.
    Defaults to prod.
    """
    option_list = BaseCommand.option_list + (
        make_option('--prod',
                    action='store_const',
                    const=domains['prod'],
                    dest='domain',
                    help='Use prod as source of data.'),
        make_option('--stage',
                    action='store_const',
                    const=domains['stage'],
                    dest='domain',
                    help='Use stage as source of data.'),
        make_option('--dev',
                    action='store_const',
                    const=domains['dev'],
                    dest='domain',
                    default=domains['dev'],
                    help='Use use dev as source of data.'),
        make_option('--altpay',
                    action='store_const',
                    const=domains['altpay'],
                    dest='domain',
                    help='Use use payments-alt as source of data.'),
        make_option('--delete',
                    action='store_true',
                    dest='delete',
                    default=False,
                    help='Start by deleting all prices.'),
        make_option('--noop',
                    action='store_true',
                    dest='noop',
                    default=False,
                    help=('Show data that would be added, '
                          'but do not create objects.')),
    )

    def handle(self, *args, **kw):
        print "Loading prices from: {0}".format(kw['domain'])
        data = requests.get(kw['domain'] + endpoint).json()

        if kw['delete']:
            Price.objects.all().delete()
            PriceCurrency.objects.all().delete()

        if kw['noop']:
            pprint.pprint(data['objects'], indent=2)
        else:
            for p in data['objects']:
                pr = Price.objects.create(name=p['name'].split(' ')[-1],
                                          active=p['active'],
                                          method=p['method'],
                                          price=p['price'])
                for pc in p['prices']:
                    pr.pricecurrency_set.create(currency=pc['currency'],
                                                carrier=pc['carrier'],
                                                price=pc['price'],
                                                paid=pc['paid'],
                                                tier=pc['tier'],
                                                dev=pc['dev'],
                                                provider=pc['provider'],
{'fraction_non_alphanumeric': 0.12474243144713901, 'fraction_numerical': 0.0011095260738627358, 'mean_word_length': 1.2304701307882644, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7311493', 'n_tokens_mistral': 1507, 'n_tokens_neox': 1415, 'n_words': 309}