def export(self, request):
        self.full_clean()
        format = 'yaml'
        selected = []
        include_images = False
        include_categories = False

        for name, value in self.cleaned_data.items():
            if name == 'format':
                format = value
                continue

            if name == 'include_images':
                include_images = value
                continue

            if name == 'include_categories':
                include_categories = value
                continue

            opt, key = name.split('__')

            if opt=='export':
                if value:
                    selected.append(key)

        try:
            serializers.get_serializer(format)
        except __HOLE__:
            raise CommandError("Unknown serialization format: %s" % format)

        objects = []
        images = []
        categories = {}
        for slug in selected:
            product = Product.objects.get(slug=slug)
            objects.append(product)
            for subtype in product.get_subtypes():
                objects.append(getattr(product,subtype.lower()))
            objects.extend(list(product.price_set.all()))
            objects.extend(list(product.productimage_set.all()))
            if include_images:
                for image in product.productimage_set.all():
                    images.append(image.picture)
            if include_categories:
                for category in product.category.all():
                    if category not in categories:
                        categories[category] = 1

        # Export all categories, translations.  Export images,translations if
        # desired.
        if include_categories:
            for category in categories.keys():
                objects.append(category)
                objects.extend(list(category.translations.all()))
                if include_images:
                    for image in category.images.all():
                        objects.append(image)
                        objects.extend(list(image.translations.all()))

        try:
            raw = serializers.serialize(format, objects, indent=False)
        except Exception, e:
            raise CommandError("Unable to serialize database: %s" % e)

        if include_images:
            filedir = settings.MEDIA_ROOT
            buf = StringIO()
            zf = zipfile.ZipFile(buf, 'a', zipfile.ZIP_STORED)

            export_file = 'products.%s' % format
            zf.writestr(str(export_file), raw)

            zinfo = zf.getinfo(str(export_file))
            # Caution, highly magic number, chmods the file to 644
            zinfo.external_attr = 2175008768L

            image_dir = config_value('PRODUCT', 'IMAGE_DIR')
            config = "PRODUCT.IMAGE_DIR=%s\nEXPORT_FILE=%s" % (image_dir, export_file)
            zf.writestr('VARS', config)

            zinfo = zf.getinfo('VARS')
            # Caution, highly magic number, chmods the file to 644
            zinfo.external_attr = 2175008768L

            for image in images:
                f = os.path.join(filedir, image)
                if os.path.exists(f):
                    zf.write(f, str(image))

            zf.close()

            raw = buf.getvalue()
            mimetype = "application/zip"
            format = "zip"
        else:
            mimetype = "text/" + format

        response = HttpResponse(mimetype=mimetype, content=raw)
        response['Content-Disposition'] = 'attachment; filename="products-%s.%s"' % (time.strftime('%Y%m%d-%H%M'), format)

        return response
dataset/ETHPy150Open dokterbob/satchmo/satchmo/apps/product/forms.py/ProductExportForm.export