def handle(self, *args, **options):
        for domain in Domain.get_all():
            if domain['commtrack_enabled']:
                fields_definition = CustomDataFieldsDefinition.get_or_create(
                    domain['name'],
                    'ProductFields'
                )

                product_ids = Product.ids_by_domain(domain['name'])

                existing_field_slugs = set(
                    [field.slug for field in fields_definition.fields]
                )
                for product in iter_docs(Product.get_db(), product_ids):
                    product_data = product.get('product_data', {})
                    for key in product_data.keys():
                        if key and key not in existing_field_slugs:
                            existing_field_slugs.add(key)
                            fields_definition.fields.append(CustomDataField(
                                slug=key,
                                label=key,
                                is_required=False
                            ))

                # Only save a definition for domains which use custom product data
                if fields_definition.fields:
                    fields_definition.save()
    def forwards(self, orm):
        product_ids = [r['id'] for r in Product.get_db().view(
            'commtrack/products',
            reduce=False,
        ).all()]

        for product in iter_docs(Product.get_db(), product_ids):
            try:
                sql_product = SQLProduct.objects.get(product_id=product['_id'])
            except SQLProduct.DoesNotExist:
                # weird - something failed syncing products. force creation now by resaving it.
                Product.wrap(product).save()
                sql_product = SQLProduct.objects.get(product_id=product['_id'])

            if 'last_modified' in product.keys() and product['last_modified']:
                sql_product.created_at = product['last_modified']
                sql_product.last_modified = product['last_modified']
                sql_product.save()
예제 #3
0
    def forwards(self, orm):
        # sync products first

        properties_to_sync = [
            ('product_id', '_id'),
            'domain',
            'name',
            'is_archived',
            ('code', 'code_'),
            'description',
            'category',
            'program_id',
            'cost',
            ('units', 'unit'),
            'product_data',
        ]

        product_ids = [
            r['id'] for r in Product.get_db().view(
                'commtrack/products',
                reduce=False,
            ).all()
        ]

        for product in iter_docs(Product.get_db(), product_ids):
            sql_product = SQLProduct()

            for prop in properties_to_sync:
                if isinstance(prop, tuple):
                    sql_prop, couch_prop = prop
                else:
                    sql_prop = couch_prop = prop

                if couch_prop in product:
                    setattr(sql_product, sql_prop, product[couch_prop])

            sql_product.save()

        # now update stock states

        for ss in StockState.include_archived.all():
            ss.sql_product = SQLProduct.objects.get(product_id=ss.product_id)
            ss.save()
    def forwards(self, orm):
        # sync products first

        properties_to_sync = [
            ('product_id', '_id'),
            'domain',
            'name',
            'is_archived',
            ('code', 'code_'),
            'description',
            'category',
            'program_id',
            'cost',
            ('units', 'unit'),
            'product_data',
        ]

        product_ids = [r['id'] for r in Product.get_db().view(
            'commtrack/products',
            reduce=False,
        ).all()]

        for product in iter_docs(Product.get_db(), product_ids):
            sql_product = orm.SQLProduct()

            for prop in properties_to_sync:
                if isinstance(prop, tuple):
                    sql_prop, couch_prop = prop
                else:
                    sql_prop = couch_prop = prop

                if couch_prop in product:
                    setattr(sql_product, sql_prop, product[couch_prop])

            sql_product.save()

        # now update stock states

        for ss in orm.StockState.objects.all():
            ss.sql_product = orm.SQLProduct.objects.get(product_id=ss.product_id)
            ss.save()
예제 #5
0
    def handle(self, *args, **options):
        self.stdout.write("Processing products...\n")

        relevant_ids = set([
            r['id'] for r in Product.get_db().view(
                'commtrack/products',
                reduce=False,
            ).all()
        ])

        to_save = []

        for product in iter_docs(Product.get_db(), relevant_ids):
            if 'last_modified' not in product or not product['last_modified']:
                product['last_modified'] = datetime.now().isoformat()
                to_save.append(product)

                if len(to_save) > 500:
                    Product.get_db().bulk_save(to_save)
                    to_save = []

        if to_save:
            Product.get_db().bulk_save(to_save)

        self.stdout.write("Processing programs...\n")

        relevant_ids = set([
            r['id'] for r in Program.get_db().view(
                'commtrack/programs',
                reduce=False,
            ).all()
        ])

        to_save = []

        for program in iter_docs(Program.get_db(), relevant_ids):
            if 'last_modified' not in program or not program['last_modified']:
                program['last_modified'] = datetime.now().isoformat()
                to_save.append(program)

                if len(to_save) > 500:
                    Program.get_db().bulk_save(to_save)
                    to_save = []

        if to_save:
            Program.get_db().bulk_save(to_save)
    def handle(self, *args, **options):
        self.stdout.write("Processing products...\n")

        relevant_ids = set([r['id'] for r in Product.get_db().view(
            'commtrack/products',
            reduce=False,
        ).all()])

        to_save = []

        for product in iter_docs(Product.get_db(), relevant_ids):
            if 'last_modified' not in product or not product['last_modified']:
                product['last_modified'] = datetime.now().isoformat()
                to_save.append(product)

                if len(to_save) > 500:
                    Product.get_db().bulk_save(to_save)
                    to_save = []

        if to_save:
            Product.get_db().bulk_save(to_save)

        self.stdout.write("Processing programs...\n")

        relevant_ids = set([r['id'] for r in Program.get_db().view(
            'commtrack/programs',
            reduce=False,
        ).all()])

        to_save = []

        for program in iter_docs(Program.get_db(), relevant_ids):
            if 'last_modified' not in program or not program['last_modified']:
                program['last_modified'] = datetime.now().isoformat()
                to_save.append(program)

                if len(to_save) > 500:
                    Program.get_db().bulk_save(to_save)
                    to_save = []

        if to_save:
            Program.get_db().bulk_save(to_save)
예제 #7
0
 def _get_products(domain):
     for p_doc in iter_docs(Product.get_db(), Product.ids_by_domain(domain)):
         yield Product.wrap(p_doc)
예제 #8
0
 def _iter_product_rows(domain):
     for p_doc in iter_docs(Product.get_db(), Product.ids_by_domain(domain)):
         p = Product.wrap(p_doc)
         yield p.to_csv()
예제 #9
0
 def _iter_product_rows(domain):
     for p_doc in iter_docs(Product.get_db(),
                            Product.ids_by_domain(domain)):
         p = Product.wrap(p_doc)
         yield p.to_csv()
예제 #10
0
 def _get_products(domain):
     for p_doc in iter_docs(Product.get_db(), Product.ids_by_domain(domain)):
         # filter out archived products from export
         if not ('is_archived' in p_doc and p_doc['is_archived']):
             yield Product.wrap(p_doc)
예제 #11
0
 def _get_products(domain):
     for p_doc in iter_docs(Product.get_db(), Product.ids_by_domain(domain)):
         # filter out archived products from export
         if not ('is_archived' in p_doc and p_doc['is_archived']):
             yield Product.wrap(p_doc)