Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix subtype bugs in Django 1.5, add migrations
Michal Ociepkaff6c76f62837
diff --git a/product/migrations/0003_auto__add_productvariant__del_field_product_subtype_attr.py b/product/migrations/0003_auto__add_productvariant__del_field_product_subtype_attr.pynew file mode 100644index 0000000000..3cf12eccee--- /dev/null+++ b/product/migrations/0003_auto__add_productvariant__del_field_product_subtype_attr.py@@ -0,0 +1,58 @@+# -*- coding: utf-8 -*-+import datetime+from south.db import db+from south.v2 import SchemaMigration+from django.db import models+++class Migration(SchemaMigration):++ def forwards(self, orm):+ # Adding model 'ProductVariant'+ db.create_table(u'product_productvariant', (+ (u'product_ptr', self.gf('django.db.models.fields.related.OneToOneField')(to=orm['product.Product'], unique=True, primary_key=True)),+ ('color', self.gf('django.db.models.fields.CharField')(max_length=9)),+ ))+ db.send_create_signal(u'product', ['ProductVariant'])++ # Deleting field 'Product.subtype_attr'+ db.delete_column(u'product_product', 'subtype_attr')+++ def backwards(self, orm):+ # Deleting model 'ProductVariant'+ db.delete_table(u'product_productvariant')+++ # User chose to not deal with backwards NULL issues for 'Product.subtype_attr'+ raise RuntimeError("Cannot reverse this migration. 'Product.subtype_attr' and its values cannot be restored.")++ models = {+ u'product.category': {+ 'Meta': {'object_name': 'Category'},+ 'description': ('django.db.models.fields.TextField', [], {'blank': 'True'}),+ u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),+ 'level': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),+ 'lft': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '128'}),+ 'parent': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'children'", 'null': 'True', 'to': u"orm['product.Category']"}),+ 'rght': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),+ 'slug': ('django.db.models.fields.SlugField', [], {'unique': 'True', 'max_length': '50'}),+ 'tree_id': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'})+ },+ u'product.product': {+ 'Meta': {'object_name': 'Product'},+ 'category': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'products'", 'to': u"orm['product.Category']"}),+ u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '128'}),+ 'price': ('django_prices.models.PriceField', [], {'currency': "'USD'", 'max_digits': '12', 'decimal_places': '4'}),+ 'stock': ('django.db.models.fields.DecimalField', [], {'default': "'1'", 'max_digits': '10', 'decimal_places': '4'})+ },+ u'product.productvariant': {+ 'Meta': {'object_name': 'ProductVariant', '_ormbases': [u'product.Product']},+ 'color': ('django.db.models.fields.CharField', [], {'max_length': '9'}),+ u'product_ptr': ('django.db.models.fields.related.OneToOneField', [], {'to': u"orm['product.Product']", 'unique': 'True', 'primary_key': 'True'})+ }+ }++ complete_apps = ['product']\ No newline at end of filediff --git a/product/models.py b/product/models.pyindex 0e9f1e133e..81b966c9de 100644--- a/product/models.py+++ b/product/models.py@@ -15,7 +15,10 @@ class SubtypedQuerySet(QuerySet):def find_subclasses(self, root):for a in dir(root):- attr = getattr(root, a)+ try:+ attr = getattr(root, a)+ except AttributeError:+ continueif isinstance(attr, SingleRelatedObjectDescriptor):child = attr.related.modelif (issubclass(child, root) and@@ -30,24 +33,31 @@ class SubtypedQuerySet(QuerySet):root = type(subtype)last_root = rootfor a in dir(root):- attr = getattr(root, a)+ try:+ attr = getattr(root, a)+ except AttributeError:+ continueif isinstance(attr, SingleRelatedObjectDescriptor):child = attr.related.modelif (issubclass(child, root) andchild is not root):- next = getattr(subtype, a)- if next:- subtype = next+ try:+ next_type = getattr(subtype, a)+ except models.ObjectDoesNotExist:+ pass+ else:+ subtype = next_typebreakif root == last_root:breakreturn subtype- def iterator(self, filter=True):+ def iterator(self, subclass=True):subclasses = list(self.find_subclasses(self.model))- if subclasses and filter:+ if subclasses and subclass:# https://code.djangoproject.com/ticket/16572- for obj in self.select_related(*subclasses).iterator(filter=False):+ related = self.select_related(*subclasses)+ for obj in related.iterator(subclass=False):yield objelse:objs = super(SubtypedQuerySet, self).iterator()@@ -110,3 +120,4 @@ class Product(Subtyped, Item):value = re.sub(r'[^\w\s-]', '', value).strip().lower()return mark_safe(re.sub(r'[-\s]+', '-', value))+