Commits

Chris Moffitt committed aeb2ebe

Improved the product export code. Closes [262]

  • Participants
  • Parent commits 9af23eb

Comments (0)

Files changed (3)

  * Updated swedish translation
  * Improve checks to make sure dupe emails are not used by accounts
  * Fixed a bug with PDF generation in windows
+ * Improved product export capability to support categories
 
 Version 0.6, October 30, 2007
 ----------------

satchmo/product/forms.py

         
         self.fields['format'] = forms.ChoiceField(label=_('export format'), choices=export_choices(), required=True)
         self.fields['include_images'] = forms.BooleanField(label=_('Include Images'), initial=True, required=False)
+        self.fields['include_categories'] = forms.BooleanField(label=_('Include Categories'), initial=True, required=False)
         
         if not products:
             products = Product.objects.all().order_by('slug')
         format = 'yaml'
         selected = []
         include_images = False
+        include_categories = False
         
         for name, value in self.cleaned_data.items():
             if name == 'format':
             if name == 'include_images':
                 include_images = value
                 continue
+            
+            if name == 'include_categories':
+                include_categories = value
+                continue
                 
             opt, key = name.split('__')
             
 
         objects = []
         images = []
+        categories = {}
         for slug in selected:
             product = Product.objects.get(slug=slug)
             objects.append(product)
             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)
                             cursor.execute(line)
                     
                 results.append(_('Added %(count)i objects from %(filename)s') % {'count': ct, 'filename': filename})
+                transaction.commit()
                 #label_found = True
             except Exception, e:
                 #fixture.close()

satchmo/templates/admin/product_export_form.html

         		</tr>
     </table>
     <p>{% trans 'Include images?' %} {{ form.include_images }}</p>
+    <p>{% trans 'Include categories?' %} {{ form.include_categories }}</p>
     <p>{% trans 'Format?' %}{{ form.format }}</p>
 <input type="submit" value="{% trans 'Export' %}" class="default" />
 <input type="reset" value="{% trans 'Reset' %}" class="default" />