class ChecklistResource(resources.ModelResource):
    questions = fields.Field(
        column_name='questions',
        widget=widgets.JSONWidget(),
    )
    category_data = fields.Field(column_name='category_data',
                                 widget=widgets.JSONWidget())

    def before_import_row(self, row, **kwargs):
        if row.get('category_data'):
            dataset = tablib.Dataset().load(row.get('category_data'), 'json')
            CategoryResource().import_data(dataset)

    def dehydrate_category_data(self, checklist):
        if checklist.category:
            dataset = CategoryResource().export(
                queryset=models.Category.objects.filter(
                    pk=checklist.category.pk))
            return dataset.json

    def dehydrate_questions(self, checklist):
        dataset = QuestionResource().export(queryset=checklist.questions.all())
        return dataset.json

    def save_m2m(self, instance, row, using_transactions, dry_run):
        super().save_m2m(instance, row, using_transactions, dry_run)

        if row.get('questions'):
            dataset = tablib.Dataset().load(row.get('questions'), 'json')
            QuestionResource().import_data(dataset)

    class Meta:
        exclude = ('created', 'modified', 'uuid', 'customers')
 def setUp(self):
     self.value = {"value": 23}
     self.widget = widgets.JSONWidget()
Exemple #3
0
class ProductResource(VerboseNameModelResource):
    brand = Field(attribute='brand',
                  column_name='Бренд',
                  widget=widgets.ForeignKeyWidget(Brand, field='code'))

    enable = Field(attribute='enable',
                   column_name='Показывать',
                   widget=MyBooleanWidget())

    categories = Field(attribute='categories',
                       column_name='Категории',
                       widget=widgets.ManyToManyWidget(Category,
                                                       field='full_name',
                                                       separator='\n'))

    description = TranslatableField(attribute='description',
                                    column_name="Описание",
                                    widget=widgets.JSONWidget(),
                                    saves_null_values=False)

    content = TranslatableField(attribute='content',
                                column_name="Состав",
                                widget=widgets.JSONWidget(),
                                saves_null_values=False)

    thumbnail = Field(
        attribute='thumbnail',
        column_name='Превью',
        # widget=widgets.ForeignKeyWidget(MediaFile, field='link')
        widget=MyGetForeignKeyWidget(MediaFile, field='link'))

    media_files = Field(
        attribute='media_files',
        column_name='Медиа файлы',
        # widget=widgets.ManyToManyWidget(MediaFile, field='link', separator='\n')
        widget=MyGetManyToManyWidget(MediaFile, field='link', separator='\n'))

    video_files = Field(
        attribute='video_files',
        column_name='Видео',
        # widget=widgets.ManyToManyWidget(MediaFile, field='link', separator='\n')
        widget=MyGetManyToManyWidget(MediaFile, field='link', separator='\n'))

    def get_locales(self, product, order=("ru", "en")):
        available_locales = product.get_available_languages(
            include_unsaved=True)
        export_order = order + tuple(
            l for l in available_locales if l not in order)
        return [l for l in export_order if l in available_locales]

    def dehydrate_description(self, product):
        return {
            loc: product.safe_translation_getter('description',
                                                 language_code=loc)
            for loc in self.get_locales(product)
        }

    def dehydrate_content(self, product):
        return {
            loc: product.safe_translation_getter('content', language_code=loc)
            for loc in self.get_locales(product)
        }

    class Meta:
        model = Product
        skip_unchanged = False
        report_skipped = True
        import_id_fields = ('sku', )
        exclude = ('id', )
        widgets = {
            'pub_date': {
                'format': '%d.%m.%y %H:%M'
            },
            'mod_date': {
                'format': '%d.%m.%y %H:%M'
            },
        }

        fields = (
            'sku',
            'model',
            'brand',
            'enable',
            'categories',
            'description',
            'content',
            'thumbnail',
            'media_files',
            'video_files',
            'price',
        )

        export_order = fields