class Shop(models.Model): """ Holds all shop related information. name The name of the shop. This is used for the the title of the HTML pages shop_owner The shop owner. This is displayed within several places for instance the checkout page from_email This e-mail address is used for the from header of all outgoing e-mails notification_emails This e-mail addresses are used for incoming notification e-mails, e.g. received an order. One e-mail address per line. description A description of the shop image An image which can be used as default image if a category doesn't have one product_cols, product_rows, category_cols Upmost format information, which defines how products and categories are displayed within several views. These may be inherited by categories and sub categories. delivery_time The default delivery time, which is used if no delivery time can be calculated for a product. google_analytics_id Used to generate google analytics tracker code and e-commerce code. the id has the format UA-xxxxxxx-xx and is provided by Google. ga_site_tracking If selected and the google_analytics_id is given google analytics site tracking code is inserted into the HTML source code. ga_ecommerce_tracking If selected and the google_analytics_id is given google analytics e-commerce tracking code is inserted into the HTML source code. countries Selected countries will be offered to the shop customer tho choose for shipping and invoice address. default_country This country will be used to calculate shipping price if the shop customer doesn't have select a country yet. use_international_currency_code If this is True the international currency code from the current locale is used. price_calculator Class that implements lfs.price.PriceCalculator for calculating product price. This is the default price calculator for all products. checkout_type Decides whether the customer has to login, has not to login or has the choice to to login or not to be able to check out. confirm_toc If this is activated the shop customer has to confirm terms and conditions to checkout. meta* This information is used within HTML meta tags of the shop view. """ name = models.CharField(_(u"Name"), max_length=30) shop_owner = models.CharField(_(u"Shop owner"), max_length=100, blank=True) from_email = models.EmailField(_(u"From e-mail address")) notification_emails = models.TextField(_(u"Notification email addresses")) description = models.TextField(_(u"Description"), blank=True) image = ImageWithThumbsField(_(u"Image"), upload_to="images", blank=True, null=True, sizes=((60, 60), (100, 100), (200, 200), (400, 400))) static_block = models.ForeignKey(StaticBlock, verbose_name=_(u"Static block"), blank=True, null=True, related_name="shops") product_cols = models.IntegerField(_(u"Product cols"), default=1) product_rows = models.IntegerField(_(u"Product rows"), default=10) category_cols = models.IntegerField(_(u"Category cols"), default=1) delivery_time = models.ForeignKey(DeliveryTime, verbose_name=_(u"Delivery time"), blank=True, null=True) google_analytics_id = models.CharField(_(u"Google Analytics ID"), blank=True, max_length=20) ga_site_tracking = models.BooleanField(_(u"Google Analytics Site Tracking"), default=False) ga_ecommerce_tracking = models.BooleanField(_(u"Google Analytics E-Commerce Tracking"), default=False) invoice_countries = models.ManyToManyField(Country, verbose_name=_(u"Invoice countries"), related_name="invoice") shipping_countries = models.ManyToManyField(Country, verbose_name=_(u"Shipping countries"), related_name="shipping") default_country = models.ForeignKey(Country, verbose_name=_(u"Default shipping country")) use_international_currency_code = models.BooleanField(_(u"Use international currency codes"), default=False) price_calculator = models.CharField(choices=settings.LFS_PRICE_CALCULATORS, max_length=255, default=settings.LFS_PRICE_CALCULATORS[0][0], verbose_name=_(u"Price calculator")) checkout_type = models.PositiveSmallIntegerField(_(u"Checkout type"), choices=CHECKOUT_TYPES, default=CHECKOUT_TYPE_SELECT) confirm_toc = models.BooleanField(_(u"Confirm TOC"), default=False) meta_title = models.CharField(_(u"Meta title"), blank=True, default="<name>", max_length=80) meta_keywords = models.TextField(_(u"Meta keywords"), blank=True) meta_description = models.TextField(_(u"Meta description"), blank=True) class Meta: permissions = (("manage_shop", "Manage shop"),) def __unicode__(self): return self.name def get_format_info(self): """Returns the global format info. """ return { "product_cols": self.product_cols, "product_rows": self.product_rows, "category_cols": self.category_cols, } def get_default_country(self): """Returns the default country of the shop. """ cache_key = "%s-default-country-%s" % (settings.CACHE_MIDDLEWARE_KEY_PREFIX, self.id) default_country = cache.get(cache_key) if default_country: return default_country default_country = self.default_country cache.set(cache_key, default_country) return default_country def get_notification_emails(self): """Returns the notification e-mail addresses as list """ import re adresses = re.split("[\s,]+", self.notification_emails) return adresses def get_parent_for_portlets(self): """Implements contract for django-portlets. Returns always None as there is no parent for a shop. """ return None def get_meta_title(self): """Returns the meta title of the shop. """ return self.meta_title.replace("<name>", self.name) def get_meta_keywords(self): """Returns the meta keywords of the shop. """ return self.meta_keywords.replace("<name>", self.name) def get_meta_description(self): """Returns the meta description of the shop. """ return self.meta_description.replace("<name>", self.name)
def migrate_to_08(self, application, version): from django.contrib.contenttypes import generic from django.contrib.contenttypes.models import ContentType from lfs.addresses.models import Address from lfs.catalog.models import DeliveryTime from lfs.criteria.models import CartPriceCriterion from lfs.criteria.models import CombinedLengthAndGirthCriterion from lfs.criteria.models import CountryCriterion from lfs.criteria.models import HeightCriterion from lfs.criteria.models import LengthCriterion from lfs.criteria.models import WidthCriterion from lfs.criteria.models import WeightCriterion from lfs.criteria.models import ShippingMethodCriterion from lfs.criteria.models import PaymentMethodCriterion from lfs.customer.models import Customer from lfs.order.models import Order # Addresses db.add_column( "customer_customer", "sa_content_type", models.ForeignKey(ContentType, related_name="sa_content_type", blank=True, null=True)) db.add_column("customer_customer", "sa_object_id", models.PositiveIntegerField(default=0)) db.add_column( "customer_customer", "ia_content_type", models.ForeignKey(ContentType, related_name="ia_content_type", blank=True, null=True)) db.add_column("customer_customer", "ia_object_id", models.PositiveIntegerField(default=0)) db.add_column( "order_order", "sa_content_type", models.ForeignKey(ContentType, related_name="sa_content_type", blank=True, null=True)) db.add_column("order_order", "sa_object_id", models.PositiveIntegerField(default=0)) db.add_column( "order_order", "ia_content_type", models.ForeignKey(ContentType, related_name="ia_content_type", blank=True, null=True)) db.add_column("order_order", "ia_object_id", models.PositiveIntegerField(default=0)) cursor = connection.cursor() cursor.execute("SELECT * FROM customer_address") for address in dictfetchall(cursor): try: # 1st try to get customer by the stored customer_id in addresses customer = Customer.objects.get(pk=address["customer_id"]) except Customer.DoesNotExist: # If there is no customer_id we try the other way around. customer_cursor = connection.cursor() customer_cursor.execute( "SELECT id from customer_customer WHERE selected_invoice_address_id=%s or selected_shipping_address_id=%s" % (address["id"], address["id"])) try: customer_id = customer_cursor.fetchone()[0] except TypeError: continue else: customer = Customer.objects.get(pk=customer_id) # Create new address out of old new_address = Address.objects.create( pk=address["id"], customer=customer, firstname=address["firstname"], lastname=address["lastname"], company_name=address["company_name"], line1=address["line1"], line2=address["line2"], zip_code=address["zip_code"], city=address["city"], state=address["state"], country_id=address["country_id"], phone=address["phone"], email=address["email"], ) # Get current selected shipping and invoice address (these aren't # available through ORM) customer_cursor = connection.cursor() customer_cursor.execute( "SELECT selected_invoice_address_id, selected_shipping_address_id from customer_customer WHERE id=%s" % customer.id) cur_ia, cur_sa = customer_cursor.fetchone() # Assign the new address to the customer if cur_ia == address["id"]: customer.selected_invoice_address = new_address elif cur_sa == address["id"]: customer.selected_shipping_address = new_address customer.save() # Migrate addresses of orders cursor.execute("SELECT * FROM order_order") for order in dictfetchall(cursor): if order["user_id"]: try: customer = Customer.objects.get(user=order["user_id"]) except Customer.DoesNotExist: continue else: customer = None invoice_address = Address.objects.create( order_id=order["id"], customer=customer, firstname=order["invoice_firstname"], lastname=order["invoice_lastname"], company_name=order["invoice_company_name"], line1=order["invoice_line1"], line2=order["invoice_line2"], zip_code=order["invoice_code"], city=order["invoice_city"], state=order["invoice_state"], country_id=order["invoice_country_id"], phone=order["invoice_phone"], email=order["customer_email"], ) shipping_address = Address.objects.create( order_id=order["id"], customer=customer, firstname=order["shipping_firstname"], lastname=order["shipping_lastname"], company_name=order["shipping_company_name"], line1=order["shipping_line1"], line2=order["shipping_line2"], zip_code=order["shipping_code"], city=order["shipping_city"], state=order["shipping_state"], country_id=order["shipping_country_id"], phone=order["shipping_phone"], email=order["customer_email"], ) order_instance = Order.objects.get(pk=order["id"]) order_instance.invoice_address = invoice_address order_instance.shipping_address = shipping_address order_instance.save() fields = [ "invoice_firstname", "invoice_lastname", "invoice_company_name", "invoice_line1", "invoice_line2", "invoice_city", "invoice_state", "invoice_code", "invoice_country", "invoice_phone", "shipping_firstname", "shipping_lastname", "shipping_company_name", "shipping_line1", "shipping_line2", "shipping_city", "shipping_state", "shipping_code", "shipping_country", "shipping_phone", ] for field in fields: db.delete_column("order_order", field) # Delete locale from shop db.delete_column("core_shop", "default_locale") # Migrate Criteria ##################################################### cursor1 = connection.cursor() cursor2 = connection.cursor() cursor3 = connection.cursor() cursor4 = connection.cursor() db.add_column("criteria_cartpricecriterion", "criterion_ptr_id", models.IntegerField(null=True)) db.add_column("criteria_combinedlengthandgirthcriterion", "criterion_ptr_id", models.IntegerField(null=True)) db.add_column("criteria_heightcriterion", "criterion_ptr_id", models.IntegerField(null=True)) db.add_column("criteria_lengthcriterion", "criterion_ptr_id", models.IntegerField(null=True)) db.add_column("criteria_widthcriterion", "criterion_ptr_id", models.IntegerField(null=True)) db.add_column("criteria_weightcriterion", "criterion_ptr_id", models.IntegerField(null=True)) db.add_column("criteria_countrycriterion", "criterion_ptr_id", models.IntegerField(null=True)) db.add_column("criteria_shippingmethodcriterion", "criterion_ptr_id", models.IntegerField(null=True)) db.add_column("criteria_paymentmethodcriterion", "criterion_ptr_id", models.IntegerField(null=True)) # CartPriceCriterion db.add_column("criteria_cartpricecriterion", "value", models.FloatField(default=0.0)) db.alter_column('criteria_cartpricecriterion', 'price', models.FloatField(default=0.0)) cursor1.execute("""SELECT id FROM criteria_cartpricecriterion""") old_criteria = ", ".join([str(row[0]) for row in cursor1.fetchall()]) content_type = ContentType.objects.get_for_model(CartPriceCriterion) cursor2.execute( """SELECT id, operator, price FROM criteria_cartpricecriterion""") for row in cursor2.fetchall(): cursor3.execute( """Select content_type_id, content_id, position FROM criteria_criteriaobjects WHERE criterion_type_id=%s and criterion_id=%s""" % (content_type.id, row[0])) criterion_object = cursor3.fetchone() CartPriceCriterion.objects.create( operator=row[1], value=row[2], content_type_id=criterion_object[0], content_id=criterion_object[1], position=criterion_object[2]) cursor1.execute( """DELETE FROM criteria_cartpricecriterion WHERE id in (%s)""" % old_criteria) transaction.commit_unless_managed() db.delete_column("criteria_cartpricecriterion", "price") # CombinedLengthAndGirthCriterion db.add_column("criteria_combinedlengthandgirthcriterion", "value", models.FloatField(default=0.0)) db.alter_column('criteria_combinedlengthandgirthcriterion', 'clag', models.FloatField(default=0.0)) cursor1.execute( """SELECT id FROM criteria_combinedlengthandgirthcriterion""") old_criteria = ", ".join([str(row[0]) for row in cursor1.fetchall()]) content_type = ContentType.objects.get_for_model( CombinedLengthAndGirthCriterion) cursor2.execute( """SELECT id, operator, clag FROM criteria_combinedlengthandgirthcriterion""" ) for row in cursor2.fetchall(): cursor3.execute( """Select content_type_id, content_id, position FROM criteria_criteriaobjects WHERE criterion_type_id=%s and criterion_id=%s""" % (content_type.id, row[0])) criterion_object = cursor3.fetchone() CombinedLengthAndGirthCriterion.objects.create( operator=row[1], value=row[2], content_type_id=criterion_object[0], content_id=criterion_object[1], position=criterion_object[2]) cursor1.execute( """DELETE FROM criteria_combinedlengthandgirthcriterion WHERE id in (%s)""" % old_criteria) transaction.commit_unless_managed() # HeightCriterion db.add_column("criteria_heightcriterion", "value", models.FloatField(default=0.0)) db.alter_column('criteria_heightcriterion', 'height', models.FloatField(default=0.0)) cursor1.execute("""SELECT id FROM criteria_heightcriterion""") old_criteria = ", ".join([str(row[0]) for row in cursor1.fetchall()]) content_type = ContentType.objects.get_for_model(HeightCriterion) cursor2.execute( """SELECT id, operator, height FROM criteria_heightcriterion""") for row in cursor2.fetchall(): cursor3.execute( """Select content_type_id, content_id, position FROM criteria_criteriaobjects WHERE criterion_type_id=%s and criterion_id=%s""" % (content_type.id, row[0])) criterion_object = cursor3.fetchone() HeightCriterion.objects.create(operator=row[1], value=row[2], content_type_id=criterion_object[0], content_id=criterion_object[1], position=criterion_object[2]) cursor1.execute( """DELETE FROM criteria_heightcriterion WHERE id in (%s)""" % old_criteria) transaction.commit_unless_managed() db.delete_column("criteria_heightcriterion", "height") # LengthCriterion db.add_column("criteria_lengthcriterion", "value", models.FloatField(default=0.0)) db.alter_column('criteria_lengthcriterion', 'length', models.FloatField(default=0.0)) cursor1.execute("""SELECT id FROM criteria_lengthcriterion""") old_criteria = ", ".join([str(row[0]) for row in cursor1.fetchall()]) content_type = ContentType.objects.get_for_model(LengthCriterion) cursor2.execute( """SELECT id, operator, length FROM criteria_lengthcriterion""") for row in cursor2.fetchall(): cursor3.execute( """Select content_type_id, content_id, position FROM criteria_criteriaobjects WHERE criterion_type_id=%s and criterion_id=%s""" % (content_type.id, row[0])) criterion_object = cursor3.fetchone() LengthCriterion.objects.create(operator=row[1], value=row[2], content_type_id=criterion_object[0], content_id=criterion_object[1], position=criterion_object[2]) cursor1.execute( """DELETE FROM criteria_lengthcriterion WHERE id in (%s)""" % old_criteria) transaction.commit_unless_managed() db.delete_column("criteria_lengthcriterion", "length") # WidthCriterion db.add_column("criteria_widthcriterion", "value", models.FloatField(default=0.0)) db.alter_column('criteria_widthcriterion', 'width', models.FloatField(default=0.0)) cursor1.execute("""SELECT id FROM criteria_widthcriterion""") old_criteria = ", ".join([str(row[0]) for row in cursor1.fetchall()]) content_type = ContentType.objects.get_for_model(WidthCriterion) cursor2.execute( """SELECT id, operator, width FROM criteria_widthcriterion""") for row in cursor2.fetchall(): cursor3.execute( """Select content_type_id, content_id, position FROM criteria_criteriaobjects WHERE criterion_type_id=%s and criterion_id=%s""" % (content_type.id, row[0])) criterion_object = cursor3.fetchone() WidthCriterion.objects.create(operator=row[1], value=row[2], content_type_id=criterion_object[0], content_id=criterion_object[1], position=criterion_object[2]) cursor1.execute( """DELETE FROM criteria_widthcriterion WHERE id in (%s)""" % old_criteria) transaction.commit_unless_managed() db.delete_column("criteria_widthcriterion", "width") # WeightCriterion db.add_column("criteria_weightcriterion", "value", models.FloatField(default=0.0)) db.alter_column('criteria_weightcriterion', 'weight', models.FloatField(default=0.0)) cursor1.execute("""SELECT id FROM criteria_weightcriterion""") old_criteria = ", ".join([str(row[0]) for row in cursor1.fetchall()]) content_type = ContentType.objects.get_for_model(WeightCriterion) cursor2.execute( """SELECT id, operator, weight FROM criteria_weightcriterion""") for row in cursor2.fetchall(): cursor3.execute( """Select content_type_id, content_id, position FROM criteria_criteriaobjects WHERE criterion_type_id=%s and criterion_id=%s""" % (content_type.id, row[0])) criterion_object = cursor3.fetchone() WeightCriterion.objects.create(operator=row[1], value=row[2], content_type_id=criterion_object[0], content_id=criterion_object[1], position=criterion_object[2]) cursor1.execute( """DELETE FROM criteria_weightcriterion WHERE id in (%s)""" % old_criteria) transaction.commit_unless_managed() db.delete_column("criteria_weightcriterion", "weight") # CountryCriterion from lfs.core.models import Country db.create_table('criteria_countrycriterion_value', ( ('id', models.AutoField(primary_key=True)), ('countrycriterion', models.ForeignKey(CountryCriterion)), ('country', models.ForeignKey(Country)), )) cursor1.execute("""SELECT id FROM criteria_countrycriterion""") old_criteria = ", ".join([str(row[0]) for row in cursor1.fetchall()]) content_type = ContentType.objects.get_for_model(CountryCriterion) cursor2.execute( """SELECT id, operator FROM criteria_countrycriterion""") for row in cursor2.fetchall(): cursor3.execute( """Select content_type_id, content_id, position FROM criteria_criteriaobjects WHERE criterion_type_id=%s and criterion_id=%s""" % (content_type.id, row[0])) criterion_object = cursor3.fetchone() cc = CountryCriterion.objects.create( operator=row[1], content_type_id=criterion_object[0], content_id=criterion_object[1], position=criterion_object[2]) cursor4.execute( """Select country_id FROM criteria_countrycriterion_countries WHERE id=%s""" % row[0]) for row_2 in cursor4.fetchall(): cc.value.add(row_2[0]) cursor1.execute( """DELETE FROM criteria_countrycriterion WHERE id in (%s)""" % old_criteria) transaction.commit_unless_managed() db.delete_table("criteria_countrycriterion_countries") # PaymentMethodCriterion from lfs.payment.models import PaymentMethod db.create_table('criteria_paymentmethodcriterion_value', ( ('id', models.AutoField(primary_key=True)), ('paymentmethodcriterion', models.ForeignKey(PaymentMethodCriterion)), ('paymentmethod', models.ForeignKey(PaymentMethod)), )) cursor1.execute("""SELECT id FROM criteria_paymentmethodcriterion""") old_criteria = ", ".join([str(row[0]) for row in cursor1.fetchall()]) content_type = ContentType.objects.get_for_model( PaymentMethodCriterion) cursor2.execute( """SELECT id, operator FROM criteria_paymentmethodcriterion""") for row in cursor2.fetchall(): cursor3.execute( """Select content_type_id, content_id, position FROM criteria_criteriaobjects WHERE criterion_type_id=%s and criterion_id=%s""" % (content_type.id, row[0])) criterion_object = cursor3.fetchone() pmc = PaymentMethodCriterion.objects.create( operator=row[1], content_type_id=criterion_object[0], content_id=criterion_object[1], position=criterion_object[2]) cursor4.execute( """Select paymentmethod_id FROM criteria_paymentmethodcriterion_payment_methods WHERE id=%s""" % row[0]) for row_2 in cursor4.fetchall(): pmc.value.add(row_2[0]) cursor1.execute( """DELETE FROM criteria_paymentmethodcriterion WHERE id in (%s)""" % old_criteria) transaction.commit_unless_managed() db.delete_table("criteria_paymentmethodcriterion_payment_methods") # ShippingMethodCriterion from lfs.shipping.models import ShippingMethod db.create_table('criteria_shippingmethodcriterion_value', ( ('id', models.AutoField(primary_key=True)), ('shippingmethodcriterion', models.ForeignKey(PaymentMethodCriterion)), ('shippingmethod', models.ForeignKey(ShippingMethod)), )) old_criteria = ", ".join([str(row[0]) for row in cursor1.fetchall()]) content_type = ContentType.objects.get_for_model( ShippingMethodCriterion) cursor2.execute( """SELECT id, operator FROM criteria_shippingmethodcriterion""") for row in cursor2.fetchall(): cursor3.execute( """Select content_type_id, content_id, position FROM criteria_criteriaobjects WHERE criterion_type_id=%s and criterion_id=%s""" % (content_type.id, row[0])) criterion_object = cursor3.fetchone() smc = ShippingMethodCriterion.objects.create( operator=row[1], content_type_id=criterion_object[0], content_id=criterion_object[1], position=criterion_object[2]) cursor4.execute( """Select shippingmethod_id FROM criteria_shippingmethodcriterion_shipping_methods WHERE id=%s""" % row[0]) for row_2 in cursor4.fetchall(): smc.value.add(row_2[0]) cursor1.execute( """DELETE FROM criteria_shippingmethodcriterion WHERE id in (%s)""" % old_criteria) transaction.commit_unless_managed() db.delete_table("criteria_shippingmethodcriterion_shipping_methods") # Manufacturers # Adding field 'Manufacturer.position' db.add_column('manufacturer_manufacturer', 'position', models.IntegerField(default=1000), keep_default=False) # Adding field 'Manufacturer.short_description' db.add_column('manufacturer_manufacturer', 'short_description', models.TextField(default='', blank=True), keep_default=False) # Adding field 'Manufacturer.description' db.add_column('manufacturer_manufacturer', 'description', models.TextField(default='', blank=True), keep_default=False) # Adding field 'Manufacturer.image' db.add_column('manufacturer_manufacturer', 'image', ImageWithThumbsField(blank=True, max_length=100, null=True, sizes=((60, 60), (100, 100), (200, 200), (400, 400))), keep_default=False) # Adding field 'Manufacturer.meta_title' db.add_column('manufacturer_manufacturer', 'meta_title', models.CharField(default='<name>', max_length=100), keep_default=False) # Adding field 'Manufacturer.meta_keywords' db.add_column('manufacturer_manufacturer', 'meta_keywords', models.TextField(default='', blank=True), keep_default=False) # Adding field 'Manufacturer.meta_description' db.add_column('manufacturer_manufacturer', 'meta_description', models.TextField(default='', blank=True), keep_default=False) # Adding field 'Manufacturer.slug' db.add_column('manufacturer_manufacturer', 'slug', models.SlugField(default='', max_length=50, db_index=True, null=True), keep_default=False) # Adding field 'Manufacturer.active_formats' db.add_column('manufacturer_manufacturer', 'active_formats', models.fields.BooleanField(default=False), keep_default=False) # Adding field 'Manufacturer.product_rows' db.add_column('manufacturer_manufacturer', 'product_rows', models.fields.IntegerField(default=3), keep_default=False) # Adding field 'Manufacturer.product_cols' db.add_column('manufacturer_manufacturer', 'product_cols', models.fields.IntegerField(default=3), keep_default=False) for i, manufacturer in enumerate(Manufacturer.objects.all()): manufacturer.slug = slugify(manufacturer.name) manufacturer.position = (i + 1) * 10 manufacturer.save() # Set field 'Manufacturer.slug' to not null db.alter_column('manufacturer_manufacturer', 'slug', models.SlugField(unique=True, max_length=50)) # Delivery Time db.add_column( 'core_shop', 'delivery_time', models.ForeignKey(DeliveryTime, verbose_name=_(u"Delivery time"), blank=True, null=True)) # PayPal paypal = PaymentMethod.objects.get(pk=3) paypal.module = "lfs_paypal.PayPalProcessor" paypal.save() # Adding model 'LatestPortlet' db.create_table('portlet_latestportlet', ( ('id', models.fields.AutoField(primary_key=True)), ('title', models.fields.CharField(max_length=100, blank=True)), ('limit', models.fields.IntegerField(default=5)), ('current_category', models.fields.BooleanField(default=False)), ('slideshow', models.fields.BooleanField(default=False)), )) application.version = "0.8" application.save()
def migrate_to_08(self, application, version): from django.contrib.contenttypes.models import ContentType from lfs.catalog.models import DeliveryTime from lfs.criteria.models import CartPriceCriterion from lfs.criteria.models import CombinedLengthAndGirthCriterion from lfs.criteria.models import CountryCriterion from lfs.criteria.models import HeightCriterion from lfs.criteria.models import LengthCriterion from lfs.criteria.models import WidthCriterion from lfs.criteria.models import WeightCriterion from lfs.criteria.models import ShippingMethodCriterion from lfs.criteria.models import PaymentMethodCriterion # Delete locale from shop db.delete_column("core_shop", "default_locale") # Migrate Criteria ##################################################### cursor1 = connection.cursor() cursor2 = connection.cursor() cursor3 = connection.cursor() cursor4 = connection.cursor() db.add_column("criteria_cartpricecriterion", "criterion_ptr_id", models.IntegerField(null=True)) db.add_column("criteria_combinedlengthandgirthcriterion", "criterion_ptr_id", models.IntegerField(null=True)) db.add_column("criteria_heightcriterion", "criterion_ptr_id", models.IntegerField(null=True)) db.add_column("criteria_lengthcriterion", "criterion_ptr_id", models.IntegerField(null=True)) db.add_column("criteria_widthcriterion", "criterion_ptr_id", models.IntegerField(null=True)) db.add_column("criteria_weightcriterion", "criterion_ptr_id", models.IntegerField(null=True)) db.add_column("criteria_countrycriterion", "criterion_ptr_id", models.IntegerField(null=True)) db.add_column("criteria_shippingmethodcriterion", "criterion_ptr_id", models.IntegerField(null=True)) db.add_column("criteria_paymentmethodcriterion", "criterion_ptr_id", models.IntegerField(null=True)) # CartPriceCriterion db.add_column("criteria_cartpricecriterion", "value", models.FloatField(default=0.0)) cursor1.execute("""SELECT id FROM criteria_cartpricecriterion""") old_criteria = ", ".join([str(row[0]) for row in cursor1.fetchall()]) content_type = ContentType.objects.get_for_model(CartPriceCriterion) cursor2.execute( """SELECT id, operator, price FROM criteria_cartpricecriterion""") for row in cursor2.fetchall(): cursor3.execute( """Select content_type_id, content_id, position FROM criteria_criteriaobjects WHERE criterion_type_id=%s and criterion_id=%s""" % (content_type.id, row[0])) criterion_object = cursor3.fetchone() CartPriceCriterion.objects.create( operator=row[1], value=row[2], content_type_id=criterion_object[0], content_id=criterion_object[1], position=criterion_object[2]) cursor1.execute( """DELETE FROM criteria_cartpricecriterion WHERE id in (%s)""" % old_criteria) transaction.commit_unless_managed() db.delete_column("criteria_cartpricecriterion", "price") # CombinedLengthAndGirthCriterion db.add_column("criteria_combinedlengthandgirthcriterion", "value", models.FloatField(default=0.0)) cursor1.execute( """SELECT id FROM criteria_combinedlengthandgirthcriterion""") old_criteria = ", ".join([str(row[0]) for row in cursor1.fetchall()]) content_type = ContentType.objects.get_for_model( CombinedLengthAndGirthCriterion) cursor2.execute( """SELECT id, operator, clag FROM criteria_combinedlengthandgirthcriterion""" ) for row in cursor2.fetchall(): cursor3.execute( """Select content_type_id, content_id, position FROM criteria_criteriaobjects WHERE criterion_type_id=%s and criterion_id=%s""" % (content_type.id, row[0])) criterion_object = cursor3.fetchone() CombinedLengthAndGirthCriterion.objects.create( operator=row[1], value=row[2], content_type_id=criterion_object[0], content_id=criterion_object[1], position=criterion_object[2]) cursor1.execute( """DELETE FROM criteria_combinedlengthandgirthcriterion WHERE id in (%s)""" % old_criteria) transaction.commit_unless_managed() # HeightCriterion db.add_column("criteria_heightcriterion", "value", models.FloatField(default=0.0)) cursor1.execute("""SELECT id FROM criteria_heightcriterion""") old_criteria = ", ".join([str(row[0]) for row in cursor1.fetchall()]) content_type = ContentType.objects.get_for_model(HeightCriterion) cursor2.execute( """SELECT id, operator, height FROM criteria_heightcriterion""") for row in cursor2.fetchall(): cursor3.execute( """Select content_type_id, content_id, position FROM criteria_criteriaobjects WHERE criterion_type_id=%s and criterion_id=%s""" % (content_type.id, row[0])) criterion_object = cursor3.fetchone() HeightCriterion.objects.create(operator=row[1], value=row[2], content_type_id=criterion_object[0], content_id=criterion_object[1], position=criterion_object[2]) cursor1.execute( """DELETE FROM criteria_heightcriterion WHERE id in (%s)""" % old_criteria) transaction.commit_unless_managed() db.delete_column("criteria_heightcriterion", "height") # LengthCriterion db.add_column("criteria_lengthcriterion", "value", models.FloatField(default=0.0)) cursor1.execute("""SELECT id FROM criteria_lengthcriterion""") old_criteria = ", ".join([str(row[0]) for row in cursor1.fetchall()]) content_type = ContentType.objects.get_for_model(LengthCriterion) cursor2.execute( """SELECT id, operator, length FROM criteria_lengthcriterion""") for row in cursor2.fetchall(): cursor3.execute( """Select content_type_id, content_id, position FROM criteria_criteriaobjects WHERE criterion_type_id=%s and criterion_id=%s""" % (content_type.id, row[0])) criterion_object = cursor3.fetchone() LengthCriterion.objects.create(operator=row[1], value=row[2], content_type_id=criterion_object[0], content_id=criterion_object[1], position=criterion_object[2]) cursor1.execute( """DELETE FROM criteria_lengthcriterion WHERE id in (%s)""" % old_criteria) transaction.commit_unless_managed() db.delete_column("criteria_lengthcriterion", "length") # WidthCriterion db.add_column("criteria_widthcriterion", "value", models.FloatField(default=0.0)) cursor1.execute("""SELECT id FROM criteria_widthcriterion""") old_criteria = ", ".join([str(row[0]) for row in cursor1.fetchall()]) content_type = ContentType.objects.get_for_model(WidthCriterion) cursor2.execute( """SELECT id, operator, width FROM criteria_widthcriterion""") for row in cursor2.fetchall(): cursor3.execute( """Select content_type_id, content_id, position FROM criteria_criteriaobjects WHERE criterion_type_id=%s and criterion_id=%s""" % (content_type.id, row[0])) criterion_object = cursor3.fetchone() WidthCriterion.objects.create(operator=row[1], value=row[2], content_type_id=criterion_object[0], content_id=criterion_object[1], position=criterion_object[2]) cursor1.execute( """DELETE FROM criteria_widthcriterion WHERE id in (%s)""" % old_criteria) transaction.commit_unless_managed() db.delete_column("criteria_widthcriterion", "width") # WeightCriterion db.add_column("criteria_weightcriterion", "value", models.FloatField(default=0.0)) cursor1.execute("""SELECT id FROM criteria_weightcriterion""") old_criteria = ", ".join([str(row[0]) for row in cursor1.fetchall()]) content_type = ContentType.objects.get_for_model(WeightCriterion) cursor2.execute( """SELECT id, operator, weight FROM criteria_weightcriterion""") for row in cursor2.fetchall(): cursor3.execute( """Select content_type_id, content_id, position FROM criteria_criteriaobjects WHERE criterion_type_id=%s and criterion_id=%s""" % (content_type.id, row[0])) criterion_object = cursor3.fetchone() WeightCriterion.objects.create(operator=row[1], value=row[2], content_type_id=criterion_object[0], content_id=criterion_object[1], position=criterion_object[2]) cursor1.execute( """DELETE FROM criteria_weightcriterion WHERE id in (%s)""" % old_criteria) transaction.commit_unless_managed() db.delete_column("criteria_weightcriterion", "weight") # CountryCriterion from lfs.core.models import Country db.create_table('criteria_countrycriterion_value', ( ('id', models.AutoField(primary_key=True)), ('countrycriterion', models.ForeignKey(CountryCriterion)), ('country', models.ForeignKey(Country)), )) cursor1.execute("""SELECT id FROM criteria_countrycriterion""") old_criteria = ", ".join([str(row[0]) for row in cursor1.fetchall()]) content_type = ContentType.objects.get_for_model(CountryCriterion) cursor2.execute( """SELECT id, operator FROM criteria_countrycriterion""") for row in cursor2.fetchall(): cursor3.execute( """Select content_type_id, content_id, position FROM criteria_criteriaobjects WHERE criterion_type_id=%s and criterion_id=%s""" % (content_type.id, row[0])) criterion_object = cursor3.fetchone() cc = CountryCriterion.objects.create( operator=row[1], content_type_id=criterion_object[0], content_id=criterion_object[1], position=criterion_object[2]) cursor4.execute( """Select country_id FROM criteria_countrycriterion_countries WHERE id=%s""" % row[0]) for row_2 in cursor4.fetchall(): cc.value.add(row_2[0]) cursor1.execute( """DELETE FROM criteria_countrycriterion WHERE id in (%s)""" % old_criteria) transaction.commit_unless_managed() db.delete_table("criteria_countrycriterion_countries") # PaymentMethodCriterion from lfs.payment.models import PaymentMethod db.create_table('criteria_paymentmethodcriterion_value', ( ('id', models.AutoField(primary_key=True)), ('paymentmethodcriterion', models.ForeignKey(PaymentMethodCriterion)), ('paymentmethod', models.ForeignKey(PaymentMethod)), )) cursor1.execute("""SELECT id FROM criteria_paymentmethodcriterion""") old_criteria = ", ".join([str(row[0]) for row in cursor1.fetchall()]) content_type = ContentType.objects.get_for_model( PaymentMethodCriterion) cursor2.execute( """SELECT id, operator FROM criteria_paymentmethodcriterion""") for row in cursor2.fetchall(): cursor3.execute( """Select content_type_id, content_id, position FROM criteria_criteriaobjects WHERE criterion_type_id=%s and criterion_id=%s""" % (content_type.id, row[0])) criterion_object = cursor3.fetchone() pmc = PaymentMethodCriterion.objects.create( operator=row[1], content_type_id=criterion_object[0], content_id=criterion_object[1], position=criterion_object[2]) cursor4.execute( """Select paymentmethod_id FROM criteria_paymentmethodcriterion_payment_methods WHERE id=%s""" % row[0]) for row_2 in cursor4.fetchall(): pmc.value.add(row_2[0]) cursor1.execute( """DELETE FROM criteria_paymentmethodcriterion WHERE id in (%s)""" % old_criteria) transaction.commit_unless_managed() db.delete_table("criteria_paymentmethodcriterion_payment_methods") # ShippingMethodCriterion from lfs.shipping.models import ShippingMethod db.create_table('criteria_shippingmethodcriterion_value', ( ('id', models.AutoField(primary_key=True)), ('shippingmethodcriterion', models.ForeignKey(PaymentMethodCriterion)), ('shippingmethod', models.ForeignKey(ShippingMethod)), )) old_criteria = ", ".join([str(row[0]) for row in cursor1.fetchall()]) content_type = ContentType.objects.get_for_model( ShippingMethodCriterion) cursor2.execute( """SELECT id, operator FROM criteria_shippingmethodcriterion""") for row in cursor2.fetchall(): cursor3.execute( """Select content_type_id, content_id, position FROM criteria_criteriaobjects WHERE criterion_type_id=%s and criterion_id=%s""" % (content_type.id, row[0])) criterion_object = cursor3.fetchone() smc = ShippingMethodCriterion.objects.create( operator=row[1], content_type_id=criterion_object[0], content_id=criterion_object[1], position=criterion_object[2]) cursor4.execute( """Select shippingmethod_id FROM criteria_shippingmethodcriterion_shipping_methods WHERE id=%s""" % row[0]) for row_2 in cursor4.fetchall(): smc.value.add(row_2[0]) cursor1.execute( """DELETE FROM criteria_shippingmethodcriterion WHERE id in (%s)""" % old_criteria) transaction.commit_unless_managed() db.delete_table("criteria_shippingmethodcriterion_shipping_methods") # Manufacturers # Adding field 'Manufacturer.position' db.add_column('manufacturer_manufacturer', 'position', models.IntegerField(default=1000), keep_default=False) # Adding field 'Manufacturer.short_description' db.add_column('manufacturer_manufacturer', 'short_description', models.TextField(default='', blank=True), keep_default=False) # Adding field 'Manufacturer.description' db.add_column('manufacturer_manufacturer', 'description', models.TextField(default='', blank=True), keep_default=False) # Adding field 'Manufacturer.image' db.add_column('manufacturer_manufacturer', 'image', ImageWithThumbsField(blank=True, max_length=100, null=True, sizes=((60, 60), (100, 100), (200, 200), (400, 400))), keep_default=False) # Adding field 'Manufacturer.meta_title' db.add_column('manufacturer_manufacturer', 'meta_title', models.CharField(default='<name>', max_length=100), keep_default=False) # Adding field 'Manufacturer.meta_keywords' db.add_column('manufacturer_manufacturer', 'meta_keywords', models.TextField(default='', blank=True), keep_default=False) # Adding field 'Manufacturer.meta_description' db.add_column('manufacturer_manufacturer', 'meta_description', models.TextField(default='', blank=True), keep_default=False) # Adding field 'Manufacturer.slug' db.add_column('manufacturer_manufacturer', 'slug', models.SlugField(default='', max_length=50, db_index=True, null=True), keep_default=False) # Adding field 'Manufacturer.active_formats' db.add_column('manufacturer_manufacturer', 'active_formats', models.fields.BooleanField(default=False), keep_default=False) # Adding field 'Manufacturer.product_rows' db.add_column('manufacturer_manufacturer', 'product_rows', models.fields.IntegerField(default=3), keep_default=False) # Adding field 'Manufacturer.product_cols' db.add_column('manufacturer_manufacturer', 'product_cols', models.fields.IntegerField(default=3), keep_default=False) for i, manufacturer in enumerate(Manufacturer.objects.all()): manufacturer.slug = slugify(manufacturer.name) manufacturer.position = (i + 1) * 10 manufacturer.save() # Set field 'Manufacturer.slug' to not null db.alter_column('manufacturer_manufacturer', 'slug', models.SlugField(unique=True, max_length=50)) # Delivery Time db.add_column( 'core_shop', 'delivery_time', models.ForeignKey(DeliveryTime, verbose_name=_(u"Delivery time"), blank=True, null=True)) # PayPal paypal = PaymentMethod.objects.get(pk=3) paypal.module = "lfs_paypal.PayPalProcessor" paypal.save() application.version = "0.8" application.save()
class Shop(models.Model): """Holds all shop related information. At the moment there must be exactly one shop with id == 1. (This may be changed in future to provide multi shops.) Instance variables: - name The name of the shop. This is used for the the title of the HTML pages - shop_owner The shop owner. This is displayed within several places for instance the checkout page - from_email This e-mail address is used for the from header of all outgoing e-mails - notification_emails This e-mail addresses are used for incoming notification e-mails, e.g. received an order. One e-mail address per line. - description A description of the shop - image An image which can be used as default image if a category doesn't have one - product_cols, product_rows, category_cols Upmost format information, which defines how products and categories are displayed within several views. These may be inherited by categories and sub categories. - google_analytics_id Used to generate google analytics tracker code and e-commerce code. the id has the format UA-xxxxxxx-xx and is provided by Google. - ga_site_tracking If selected and the google_analytics_id is given google analytics site tracking code is inserted into the HTML source code. - ga_ecommerce_tracking If selected and the google_analytics_id is given google analytics e-commerce tracking code is inserted into the HTML source code. - countries Selected countries will be offered to the shop customer tho choose for shipping and invoice address. - default_country This country will be used to calculate shipping price if the shop customer doesn't have select a country yet. - checkout_type Decides whether the customer has to login, has not to login or has the choice to to login or not to be able to check out. """ name = models.CharField(_(u"Name"), max_length=30) shop_owner = models.CharField(_(u"Shop owner"), max_length=100, blank=True) from_email = models.EmailField(_(u"From e-mail address")) notification_emails = models.TextField(_(u"Notification email addresses")) description = models.TextField(_(u"Description"), blank=True) image = ImageWithThumbsField(_(u"Image"), upload_to="images", blank=True, null=True, sizes=((60, 60), (100, 100), (200, 200), (400, 400))) static_block = models.ForeignKey(StaticBlock, verbose_name=_(u"Static block"), blank=True, null=True, related_name="shops") product_cols = models.IntegerField(_(u"Product cols"), default=3) product_rows = models.IntegerField(_(u"Product rows"), default=3) category_cols = models.IntegerField(_(u"Category cols"), default=3) google_analytics_id = models.CharField(_(u"Google Analytics ID"), blank=True, max_length=20) ga_site_tracking = models.BooleanField( _(u"Google Analytics Site Tracking"), default=False) ga_ecommerce_tracking = models.BooleanField( _(u"Google Analytics E-Commerce Tracking"), default=False) countries = models.ManyToManyField(Country, verbose_name=_(u"Countries"), related_name="shops") default_country = models.ForeignKey(Country, verbose_name=_(u"Default country")) default_currency = models.CharField(_(u"Default Currency"), max_length=30, default="EUR") checkout_type = models.PositiveSmallIntegerField( _(u"Checkout type"), choices=CHECKOUT_TYPES, default=CHECKOUT_TYPE_SELECT) class Meta: permissions = (("manage_shop", "Manage shop"), ) def __unicode__(self): return self.name def get_format_info(self): """ """ return { "product_cols": self.product_cols, "product_rows": self.product_rows, "category_cols": self.category_cols, } def get_notification_emails(self): """Returns the notification e-mail addresses as list """ adresses = re.split("[\s,]+", self.notification_emails) return adresses def get_parent_for_portlets(self): """Implements contract for django-portlets. Returns always None as there is no parent for a shop. """ return None
class Manufacturer(models.Model): """The manufacturer is the unique creator of a product. """ name = models.CharField(_(u"Name"), max_length=50) slug = models.SlugField(_(u"Slug"), unique=True) short_description = models.TextField(_(u"Short description"), blank=True) description = models.TextField(_(u"Description"), blank=True) image = ImageWithThumbsField(_(u"Image"), upload_to="images", blank=True, null=True, sizes=((60, 60), (100, 100), (200, 200), (400, 400))) position = models.IntegerField(_(u"Position"), default=1000) active_formats = models.BooleanField(_(u"Active formats"), default=False) product_rows = models.IntegerField(_(u"Product rows"), default=3) product_cols = models.IntegerField(_(u"Product cols"), default=3) meta_title = models.CharField(_(u"Meta title"), max_length=100, default="<name>") meta_keywords = models.TextField(_(u"Meta keywords"), blank=True) meta_description = models.TextField(_(u"Meta description"), blank=True) class Meta: ordering = ("name", ) app_label = 'manufacturer' def __str__(self): return self.name def get_absolute_url(self): """Returns the absolute url of the manufacturer """ return reverse("lfs_manufacturer", kwargs={"slug": self.slug}) def get_format_info(self): """Returns format information. """ if self.active_formats is True: return { "product_cols": self.product_cols, "product_rows": self.product_rows } else: try: # TODO: Use cache here. Maybe we need a lfs_get_object, # which raise a ObjectDoesNotExist if the object does not # exist from lfs.core.models import Shop shop = Shop.objects.get(pk=1) except ObjectDoesNotExist: return { "product_cols": 3, "product_rows": 3 } else: return { "product_cols": shop.product_cols, "product_rows": shop.product_rows } def get_meta_title(self): """Returns the meta keywords of the catgory. """ mt = self.meta_title.replace("<name>", self.name) return mt def get_meta_keywords(self): """Returns the meta keywords of the catgory. """ mk = self.meta_keywords.replace("<name>", self.name) mk = mk.replace("<short-description>", self.short_description) return mk def get_meta_description(self): """Returns the meta description of the product. """ md = self.meta_description.replace("<name>", self.name) md = md.replace("<short-description>", self.short_description) return md def get_image(self): """Returns the image of the category if it has none it inherits that from the parent category. """ if self.image: return self.image return None def get_all_products(self): """Returns all products for manufacturer """ from lfs.catalog.settings import VARIANT cache_key = "%s-manufacturer-all-products-%s" % (settings.CACHE_MIDDLEWARE_KEY_PREFIX, self.id) products = cache.get(cache_key) if products is not None: return products products = self.products.filter(active=True).exclude(sub_type=VARIANT).distinct() cache.set(cache_key, products) return products def get_filtered_products(self, filters, price_filter, sorting): """Returns products for given categories and current filters sorted by current sorting. """ from lfs.catalog.models import Product from lfs.catalog.settings import PROPERTY_VALUE_TYPE_FILTER products = self.get_all_products() if filters: # Generate ids for collected products product_ids = products.values_list('pk', flat=True) product_ids = ", ".join(product_ids) # Generate filter temp = [] for f in filters: if not isinstance(f[1], (list, tuple)): temp.append("property_id='%s' AND value='%s'" % (f[0], f[1])) else: temp.append("property_id='%s' AND value_as_float BETWEEN '%s' AND '%s'" % (f[0], f[1][0], f[1][1])) fstr = " OR ".join(temp) # TODO: Will this work with every DB? # Get all product ids with matching filters. The idea behind this SQL # query is: If for every filter (property=value) for a product id exists # a "product property value" the product matches. cursor = connection.cursor() cursor.execute(""" SELECT product_id, count(*) FROM catalog_productpropertyvalue WHERE product_id IN (%s) and (%s) and type=%s GROUP BY product_id HAVING count(*)=%s""" % (product_ids, fstr, PROPERTY_VALUE_TYPE_FILTER, len(filters))) matched_product_ids = [row[0] for row in cursor.fetchall()] # All variants of category products all_variants = Product.objects.filter(parent__in=products) if all_variants: all_variant_ids = [str(p.id) for p in all_variants] all_variant_ids = ", ".join(all_variant_ids) # Variants with matching filters cursor.execute(""" SELECT product_id, count(*) FROM catalog_productpropertyvalue WHERE product_id IN (%s) and %s and type=%s GROUP BY product_id HAVING count(*)=%s""" % (all_variant_ids, fstr, PROPERTY_VALUE_TYPE_FILTER, len(filters))) # Get the parent ids of the variants as the "product with variants" # should be displayed and not the variants. variant_ids = [str(row[0]) for row in cursor.fetchall()] if variant_ids: variant_ids = ", ".join(variant_ids) cursor.execute(""" SELECT parent_id FROM catalog_product WHERE id IN (%s)""" % variant_ids) parent_ids = [str(row[0]) for row in cursor.fetchall()] matched_product_ids.extend(parent_ids) # As we factored out the ids of all matching products now, we get the # product instances in the correct order products = Product.objects.filter(pk__in=matched_product_ids).distinct() if price_filter: matched_product_ids = [] # Get all variants of the products variants = Product.objects.filter(parent__in=products) # Filter the variants by price variants = variants.filter(effective_price__range=[price_filter["min"], price_filter["max"]]) # Get the parent ids of the variants as the "product with variants" # should be displayed and not the variants. if variants: variant_ids = [str(r.id) for r in variants] variant_ids = ", ".join(variant_ids) cursor = connection.cursor() cursor.execute(""" SELECT parent_id FROM catalog_product WHERE id IN (%s)""" % variant_ids) parent_ids = [str(row[0]) for row in cursor.fetchall()] matched_product_ids.extend(parent_ids) # Filter the products products = products.filter(effective_price__range=[price_filter["min"], price_filter["max"]]) # Merge the results matched_product_ids.extend(products.values_list('pk', flat=True)) # And get a new query set of all products products = Product.objects.filter(pk__in=matched_product_ids) if sorting: products = products.order_by(sorting) return products
def migrate_to_09(self, application, version): from django.contrib.contenttypes import generic from django.contrib.contenttypes.models import ContentType from lfs.core.models import Country from lfs.addresses.models import Address, BaseAddress from lfs.catalog.models import DeliveryTime from lfs.criteria.models import CartPriceCriterion from lfs.criteria.models import CombinedLengthAndGirthCriterion from lfs.criteria.models import CountryCriterion from lfs.criteria.models import HeightCriterion from lfs.criteria.models import LengthCriterion from lfs.criteria.models import WidthCriterion from lfs.criteria.models import WeightCriterion from lfs.criteria.models import ShippingMethodCriterion from lfs.criteria.models import PaymentMethodCriterion from lfs.customer.models import Customer from lfs.order.models import Order if not 'south' in settings.INSTALLED_APPS: print "You have to add 'south' to settings.INSTALLED_APPS!" return # Addresses # Adding model 'BaseAddress' db.create_table('addresses_baseaddress', ( ('id', models.fields.AutoField(primary_key=True)), ('customer', models.fields.related.ForeignKey( blank=True, related_name='addresses', null=True, to=Customer)), ('order', models.fields.related.ForeignKey( blank=True, related_name='addresses', null=True, to=Order)), ('firstname', models.fields.CharField(max_length=50)), ('lastname', models.fields.CharField(max_length=50)), ('line1', models.fields.CharField(max_length=100, null=True, blank=True)), ('line2', models.fields.CharField(max_length=100, null=True, blank=True)), ('zip_code', models.fields.CharField(max_length=10)), ('city', models.fields.CharField(max_length=50)), ('state', models.fields.CharField(max_length=50, null=True, blank=True)), ('country', models.fields.related.ForeignKey( to=Country, null=True, blank=True)), ('created', models.fields.DateTimeField(auto_now_add=True, blank=True)), ('modified', models.fields.DateTimeField(auto_now=True, blank=True)), )) db.send_create_signal('addresses', ['BaseAddress']) # Adding model 'Address' db.create_table('addresses_address', ( ('baseaddress_ptr', models.fields.related.OneToOneField( to=BaseAddress, unique=True, primary_key=True)), ('company_name', models.fields.CharField(max_length=50, null=True, blank=True)), ('phone', models.fields.CharField(max_length=20, null=True, blank=True)), ('email', models.fields.EmailField(max_length=50, null=True, blank=True)), )) db.send_create_signal('addresses', ['Address']) db.add_column( "customer_customer", "sa_content_type", models.ForeignKey(ContentType, related_name="sa_content_type", blank=True, null=True)) db.add_column("customer_customer", "sa_object_id", models.PositiveIntegerField(default=0)) db.add_column( "customer_customer", "ia_content_type", models.ForeignKey(ContentType, related_name="ia_content_type", blank=True, null=True)) db.add_column("customer_customer", "ia_object_id", models.PositiveIntegerField(default=0)) db.add_column( "order_order", "sa_content_type", models.ForeignKey(ContentType, related_name="sa_content_type", blank=True, null=True)) db.add_column("order_order", "sa_object_id", models.PositiveIntegerField(default=0)) db.add_column( "order_order", "ia_content_type", models.ForeignKey(ContentType, related_name="ia_content_type", blank=True, null=True)) db.add_column("order_order", "ia_object_id", models.PositiveIntegerField(default=0)) cursor = connection.cursor() cursor.execute("SELECT * FROM customer_address") for address in dictfetchall(cursor): try: # 1st try to get customer by the stored customer_id in addresses customer = Customer.objects.get(pk=address["customer_id"]) except Customer.DoesNotExist: # If there is no customer_id we try the other way around. customer_cursor = connection.cursor() customer_cursor.execute( "SELECT id from customer_customer WHERE selected_invoice_address_id=%s or selected_shipping_address_id=%s" % (address["id"], address["id"])) try: customer_id = customer_cursor.fetchone()[0] except TypeError: continue else: customer = Customer.objects.get(pk=customer_id) # Create new address out of old new_address = Address.objects.create( pk=address["id"], customer=customer, firstname=address["firstname"], lastname=address["lastname"], company_name=address["company_name"], line1=address["line1"], line2=address["line2"], zip_code=address["zip_code"], city=address["city"], state=address["state"], country_id=address["country_id"], phone=address["phone"], email=address["email"], ) # Get current selected shipping and invoice address (these aren't # available through ORM) customer_cursor = connection.cursor() customer_cursor.execute( "SELECT selected_invoice_address_id, selected_shipping_address_id from customer_customer WHERE id=%s" % customer.id) cur_ia, cur_sa = customer_cursor.fetchone() # Assign the new address to the customer if cur_ia == address["id"]: customer.selected_invoice_address = new_address elif cur_sa == address["id"]: customer.selected_shipping_address = new_address customer.save() try: # fix: django.db.utils.IntegrityError: duplicate key value violates unique constraint "addresses_baseaddress_pkey" # in try because has been tested only in postgres # please check it in your db engine and then remove this try block cursor.execute( "SELECT setval('addresses_baseaddress_id_seq', (SELECT MAX(id) FROM addresses_baseaddress)+1)" ) except: pass # Migrate addresses of orders cursor.execute("SELECT * FROM order_order") for order in dictfetchall(cursor): if order["user_id"]: try: customer = Customer.objects.get(user=order["user_id"]) except Customer.DoesNotExist: continue else: customer = None invoice_address = Address.objects.create( order_id=order["id"], customer=customer, firstname=order["invoice_firstname"], lastname=order["invoice_lastname"], company_name=order["invoice_company_name"], line1=order["invoice_line1"], line2=order["invoice_line2"], zip_code=order["invoice_code"], city=order["invoice_city"], state=order["invoice_state"], country_id=order["invoice_country_id"], phone=order["invoice_phone"], email=order["customer_email"], ) shipping_address = Address.objects.create( order_id=order["id"], customer=customer, firstname=order["shipping_firstname"], lastname=order["shipping_lastname"], company_name=order["shipping_company_name"], line1=order["shipping_line1"], line2=order["shipping_line2"], zip_code=order["shipping_code"], city=order["shipping_city"], state=order["shipping_state"], country_id=order["shipping_country_id"], phone=order["shipping_phone"], email=order["customer_email"], ) order_instance = Order.objects.get(pk=order["id"]) order_instance.invoice_address = invoice_address order_instance.shipping_address = shipping_address order_instance.save() fields = [ "invoice_firstname", "invoice_lastname", "invoice_company_name", "invoice_line1", "invoice_line2", "invoice_city", "invoice_state", "invoice_code", "invoice_country_id", "invoice_phone", "shipping_firstname", "shipping_lastname", "shipping_company_name", "shipping_line1", "shipping_line2", "shipping_city", "shipping_state", "shipping_code", "shipping_country_id", "shipping_phone", ] for field in fields: db.delete_column("order_order", field) # Delete locale from shop db.delete_column("core_shop", "default_locale") # Customer db.alter_column('customer_creditcard', 'expiration_date_month', models.IntegerField(blank=True, null=True)) db.alter_column('customer_creditcard', 'expiration_date_year', models.IntegerField(blank=True, null=True)) # Migrate Criteria ##################################################### cursor1 = connection.cursor() cursor2 = connection.cursor() cursor3 = connection.cursor() cursor4 = connection.cursor() db.create_table('criteria_criterion', ( ('id', models.fields.AutoField(primary_key=True)), ('content_type', models.fields.related.ForeignKey(related_name='content_type', to=ContentType)), ('content_id', models.fields.PositiveIntegerField()), ('sub_type', models.fields.CharField(max_length=100, blank=True)), ('position', models.fields.PositiveIntegerField(default=999)), ('operator', models.fields.PositiveIntegerField(null=True, blank=True)), )) db.send_create_signal('criteria', ['Criterion']) db.add_column("criteria_cartpricecriterion", "criterion_ptr_id", models.IntegerField(null=True)) db.add_column("criteria_combinedlengthandgirthcriterion", "criterion_ptr_id", models.IntegerField(null=True)) db.add_column("criteria_heightcriterion", "criterion_ptr_id", models.IntegerField(null=True)) db.add_column("criteria_lengthcriterion", "criterion_ptr_id", models.IntegerField(null=True)) db.add_column("criteria_widthcriterion", "criterion_ptr_id", models.IntegerField(null=True)) db.add_column("criteria_weightcriterion", "criterion_ptr_id", models.IntegerField(null=True)) db.add_column("criteria_countrycriterion", "criterion_ptr_id", models.IntegerField(null=True)) db.add_column("criteria_shippingmethodcriterion", "criterion_ptr_id", models.IntegerField(null=True)) db.add_column("criteria_paymentmethodcriterion", "criterion_ptr_id", models.IntegerField(null=True)) # CartPriceCriterion db.add_column("criteria_cartpricecriterion", "value", models.FloatField(default=0.0)) db.alter_column('criteria_cartpricecriterion', 'price', models.FloatField(default=0.0, null=True)) cursor1.execute("""SELECT id FROM criteria_cartpricecriterion""") old_criteria = ", ".join([str(row[0]) for row in cursor1.fetchall()]) content_type = ContentType.objects.get_for_model(CartPriceCriterion) cursor2.execute( """SELECT id, operator, price FROM criteria_cartpricecriterion""") for row in cursor2.fetchall(): cursor3.execute( """Select content_type_id, content_id, position FROM criteria_criteriaobjects WHERE criterion_type_id=%s and criterion_id=%s""" % (content_type.id, row[0])) criterion_object = cursor3.fetchone() if criterion_object: CartPriceCriterion.objects.create( operator=row[1], value=row[2], content_type_id=criterion_object[0], content_id=criterion_object[1], position=criterion_object[2]) if old_criteria: cursor1.execute( """DELETE FROM criteria_cartpricecriterion WHERE id in (%s)""" % old_criteria) transaction.commit_unless_managed() db.delete_column("criteria_cartpricecriterion", "price") # CombinedLengthAndGirthCriterion db.add_column("criteria_combinedlengthandgirthcriterion", "value", models.FloatField(default=0.0)) db.alter_column('criteria_combinedlengthandgirthcriterion', 'clag', models.FloatField(default=0.0)) cursor1.execute( """SELECT id FROM criteria_combinedlengthandgirthcriterion""") old_criteria = ", ".join([str(row[0]) for row in cursor1.fetchall()]) content_type = ContentType.objects.get_for_model( CombinedLengthAndGirthCriterion) cursor2.execute( """SELECT id, operator, clag FROM criteria_combinedlengthandgirthcriterion""" ) for row in cursor2.fetchall(): cursor3.execute( """Select content_type_id, content_id, position FROM criteria_criteriaobjects WHERE criterion_type_id=%s and criterion_id=%s""" % (content_type.id, row[0])) criterion_object = cursor3.fetchone() if criterion_object: CombinedLengthAndGirthCriterion.objects.create( operator=row[1], value=row[2], content_type_id=criterion_object[0], content_id=criterion_object[1], position=criterion_object[2]) if old_criteria: cursor1.execute( """DELETE FROM criteria_combinedlengthandgirthcriterion WHERE id in (%s)""" % old_criteria) transaction.commit_unless_managed() # HeightCriterion db.add_column("criteria_heightcriterion", "value", models.FloatField(default=0.0)) db.alter_column('criteria_heightcriterion', 'height', models.FloatField(default=0.0, null=True)) cursor1.execute("""SELECT id FROM criteria_heightcriterion""") old_criteria = ", ".join([str(row[0]) for row in cursor1.fetchall()]) content_type = ContentType.objects.get_for_model(HeightCriterion) cursor2.execute( """SELECT id, operator, height FROM criteria_heightcriterion""") for row in cursor2.fetchall(): cursor3.execute( """Select content_type_id, content_id, position FROM criteria_criteriaobjects WHERE criterion_type_id=%s and criterion_id=%s""" % (content_type.id, row[0])) criterion_object = cursor3.fetchone() if criterion_object: HeightCriterion.objects.create( operator=row[1], value=row[2], content_type_id=criterion_object[0], content_id=criterion_object[1], position=criterion_object[2]) if old_criteria: cursor1.execute( """DELETE FROM criteria_heightcriterion WHERE id in (%s)""" % old_criteria) transaction.commit_unless_managed() db.delete_column("criteria_heightcriterion", "height") # LengthCriterion db.add_column("criteria_lengthcriterion", "value", models.FloatField(default=0.0)) db.alter_column('criteria_lengthcriterion', 'length', models.FloatField(default=0.0, null=True)) cursor1.execute("""SELECT id FROM criteria_lengthcriterion""") old_criteria = ", ".join([str(row[0]) for row in cursor1.fetchall()]) content_type = ContentType.objects.get_for_model(LengthCriterion) cursor2.execute( """SELECT id, operator, length FROM criteria_lengthcriterion""") for row in cursor2.fetchall(): cursor3.execute( """Select content_type_id, content_id, position FROM criteria_criteriaobjects WHERE criterion_type_id=%s and criterion_id=%s""" % (content_type.id, row[0])) criterion_object = cursor3.fetchone() if criterion_object: LengthCriterion.objects.create( operator=row[1], value=row[2], content_type_id=criterion_object[0], content_id=criterion_object[1], position=criterion_object[2]) if old_criteria: cursor1.execute( """DELETE FROM criteria_lengthcriterion WHERE id in (%s)""" % old_criteria) transaction.commit_unless_managed() db.delete_column("criteria_lengthcriterion", "length") # WidthCriterion db.add_column("criteria_widthcriterion", "value", models.FloatField(default=0.0)) db.alter_column('criteria_widthcriterion', 'width', models.FloatField(default=0.0, null=True)) cursor1.execute("""SELECT id FROM criteria_widthcriterion""") old_criteria = ", ".join([str(row[0]) for row in cursor1.fetchall()]) content_type = ContentType.objects.get_for_model(WidthCriterion) cursor2.execute( """SELECT id, operator, width FROM criteria_widthcriterion""") for row in cursor2.fetchall(): cursor3.execute( """Select content_type_id, content_id, position FROM criteria_criteriaobjects WHERE criterion_type_id=%s and criterion_id=%s""" % (content_type.id, row[0])) criterion_object = cursor3.fetchone() if criterion_object: WidthCriterion.objects.create( operator=row[1], value=row[2], content_type_id=criterion_object[0], content_id=criterion_object[1], position=criterion_object[2]) if old_criteria: cursor1.execute( """DELETE FROM criteria_widthcriterion WHERE id in (%s)""" % old_criteria) transaction.commit_unless_managed() db.delete_column("criteria_widthcriterion", "width") # WeightCriterion db.add_column("criteria_weightcriterion", "value", models.FloatField(default=0.0)) db.alter_column('criteria_weightcriterion', 'weight', models.FloatField(default=0.0, null=True)) cursor1.execute("""SELECT id FROM criteria_weightcriterion""") old_criteria = ", ".join([str(row[0]) for row in cursor1.fetchall()]) content_type = ContentType.objects.get_for_model(WeightCriterion) cursor2.execute( """SELECT id, operator, weight FROM criteria_weightcriterion""") for row in cursor2.fetchall(): cursor3.execute( """Select content_type_id, content_id, position FROM criteria_criteriaobjects WHERE criterion_type_id=%s and criterion_id=%s""" % (content_type.id, row[0])) criterion_object = cursor3.fetchone() if criterion_object: WeightCriterion.objects.create( operator=row[1], value=row[2], content_type_id=criterion_object[0], content_id=criterion_object[1], position=criterion_object[2]) if old_criteria: cursor1.execute( """DELETE FROM criteria_weightcriterion WHERE id in (%s)""" % old_criteria) transaction.commit_unless_managed() db.delete_column("criteria_weightcriterion", "weight") # CountryCriterion from lfs.core.models import Country db.create_table('criteria_countrycriterion_value', ( ('id', models.AutoField(primary_key=True)), ('countrycriterion', models.ForeignKey(CountryCriterion)), ('country', models.ForeignKey(Country)), )) cursor1.execute("""SELECT id FROM criteria_countrycriterion""") old_criteria = ", ".join([str(row[0]) for row in cursor1.fetchall()]) content_type = ContentType.objects.get_for_model(CountryCriterion) cursor2.execute( """SELECT id, operator FROM criteria_countrycriterion""") for row in cursor2.fetchall(): cursor3.execute( """Select content_type_id, content_id, position FROM criteria_criteriaobjects WHERE criterion_type_id=%s and criterion_id=%s""" % (content_type.id, row[0])) criterion_object = cursor3.fetchone() if criterion_object: cc = CountryCriterion.objects.create( operator=row[1], content_type_id=criterion_object[0], content_id=criterion_object[1], position=criterion_object[2]) cursor4.execute( """Select country_id FROM criteria_countrycriterion_countries WHERE id=%s""" % row[0]) for row_2 in cursor4.fetchall(): cc.value.add(row_2[0]) if old_criteria: cursor1.execute( """DELETE FROM criteria_countrycriterion WHERE id in (%s)""" % old_criteria) transaction.commit_unless_managed() db.delete_table("criteria_countrycriterion_countries") # PaymentMethodCriterion from lfs.payment.models import PaymentMethod db.create_table('criteria_paymentmethodcriterion_value', ( ('id', models.AutoField(primary_key=True)), ('paymentmethodcriterion', models.ForeignKey(PaymentMethodCriterion)), ('paymentmethod', models.ForeignKey(PaymentMethod)), )) cursor1.execute("""SELECT id FROM criteria_paymentmethodcriterion""") old_criteria = ", ".join([str(row[0]) for row in cursor1.fetchall()]) content_type = ContentType.objects.get_for_model( PaymentMethodCriterion) cursor2.execute( """SELECT id, operator FROM criteria_paymentmethodcriterion""") for row in cursor2.fetchall(): cursor3.execute( """Select content_type_id, content_id, position FROM criteria_criteriaobjects WHERE criterion_type_id=%s and criterion_id=%s""" % (content_type.id, row[0])) criterion_object = cursor3.fetchone() if criterion_object: pmc = PaymentMethodCriterion.objects.create( operator=row[1], content_type_id=criterion_object[0], content_id=criterion_object[1], position=criterion_object[2]) cursor4.execute( """Select paymentmethod_id FROM criteria_paymentmethodcriterion_payment_methods WHERE id=%s""" % row[0]) for row_2 in cursor4.fetchall(): pmc.value.add(row_2[0]) if old_criteria: cursor1.execute( """DELETE FROM criteria_paymentmethodcriterion WHERE id in (%s)""" % old_criteria) transaction.commit_unless_managed() db.delete_table("criteria_paymentmethodcriterion_payment_methods") # ShippingMethodCriterion from lfs.shipping.models import ShippingMethod db.create_table('criteria_shippingmethodcriterion_value', ( ('id', models.AutoField(primary_key=True)), ('shippingmethodcriterion', models.ForeignKey(PaymentMethodCriterion)), ('shippingmethod', models.ForeignKey(ShippingMethod)), )) old_criteria = ", ".join([str(row[0]) for row in cursor1.fetchall()]) content_type = ContentType.objects.get_for_model( ShippingMethodCriterion) cursor2.execute( """SELECT id, operator FROM criteria_shippingmethodcriterion""") for row in cursor2.fetchall(): cursor3.execute( """Select content_type_id, content_id, position FROM criteria_criteriaobjects WHERE criterion_type_id=%s and criterion_id=%s""" % (content_type.id, row[0])) criterion_object = cursor3.fetchone() if criterion_object: smc = ShippingMethodCriterion.objects.create( operator=row[1], content_type_id=criterion_object[0], content_id=criterion_object[1], position=criterion_object[2]) cursor4.execute( """Select shippingmethod_id FROM criteria_shippingmethodcriterion_shipping_methods WHERE id=%s""" % row[0]) for row_2 in cursor4.fetchall(): smc.value.add(row_2[0]) if old_criteria: cursor1.execute( """DELETE FROM criteria_shippingmethodcriterion WHERE id in (%s)""" % old_criteria) transaction.commit_unless_managed() db.delete_table("criteria_shippingmethodcriterion_shipping_methods") # Manufacturers # Adding field 'Manufacturer.position' db.add_column('manufacturer_manufacturer', 'position', models.IntegerField(default=1000), keep_default=False) # Adding field 'Manufacturer.short_description' db.add_column('manufacturer_manufacturer', 'short_description', models.TextField(default='', blank=True), keep_default=False) # Adding field 'Manufacturer.description' db.add_column('manufacturer_manufacturer', 'description', models.TextField(default='', blank=True), keep_default=False) # Adding field 'Manufacturer.image' db.add_column('manufacturer_manufacturer', 'image', ImageWithThumbsField(blank=True, max_length=100, null=True, sizes=((60, 60), (100, 100), (200, 200), (400, 400))), keep_default=False) # Adding field 'Manufacturer.meta_title' db.add_column('manufacturer_manufacturer', 'meta_title', models.CharField(default='<name>', max_length=100), keep_default=False) # Adding field 'Manufacturer.meta_keywords' db.add_column('manufacturer_manufacturer', 'meta_keywords', models.TextField(default='', blank=True), keep_default=False) # Adding field 'Manufacturer.meta_description' db.add_column('manufacturer_manufacturer', 'meta_description', models.TextField(default='', blank=True), keep_default=False) # Adding field 'Manufacturer.slug' db.add_column('manufacturer_manufacturer', 'slug', models.SlugField(default='', max_length=50, db_index=True, null=True), keep_default=False) # Adding field 'Manufacturer.active_formats' db.add_column('manufacturer_manufacturer', 'active_formats', models.fields.BooleanField(default=False), keep_default=False) # Adding field 'Manufacturer.product_rows' db.add_column('manufacturer_manufacturer', 'product_rows', models.fields.IntegerField(default=3), keep_default=False) # Adding field 'Manufacturer.product_cols' db.add_column('manufacturer_manufacturer', 'product_cols', models.fields.IntegerField(default=3), keep_default=False) for i, manufacturer in enumerate(Manufacturer.objects.all()): manufacturer.slug = slugify(manufacturer.name) manufacturer.position = (i + 1) * 10 manufacturer.save() # Set field 'Manufacturer.slug' to not null db.alter_column('manufacturer_manufacturer', 'slug', models.SlugField(unique=True, max_length=50)) # Delivery Time db.add_column( 'core_shop', 'delivery_time', models.ForeignKey(DeliveryTime, verbose_name=_(u"Delivery time"), blank=True, null=True)) # PayPal paypal = PaymentMethod.objects.get(pk=3) paypal.module = "lfs_paypal.PayPalProcessor" paypal.save() # Adding model 'LatestPortlet' db.create_table('portlet_latestportlet', ( ('id', models.fields.AutoField(primary_key=True)), ('title', models.fields.CharField(max_length=100, blank=True)), ('limit', models.fields.IntegerField(default=5)), ('current_category', models.fields.BooleanField(default=False)), ('slideshow', models.fields.BooleanField(default=False)), )) application.version = "0.9" application.save() # Fake south migrations management.call_command('syncdb', interactive=False) management.call_command('migrate', interactive=False, all=True, fake="0001") management.call_command('migrate', 'order', interactive=False, fake="0002") management.call_command('migrate', interactive=False)