]> git.parisson.com Git - diggersdigest.git/commitdiff
Manage all records in populate_db management command
authorThomas Fillon <thomas@parisson.com>
Thu, 10 Sep 2015 16:21:35 +0000 (18:21 +0200)
committerThomas Fillon <thomas@parisson.com>
Thu, 10 Sep 2015 16:21:35 +0000 (18:21 +0200)
diggersdigest/records/management/commands/populate_db.py

index 6c26557d55d4681ec318273be6f6fdf1c6c90bb1..f063718d1b65b851330bc43321b3dc79c2d8313d 100644 (file)
@@ -23,6 +23,7 @@ import string
 import mezzanine.blog.models as blog_models
 from mezzanine.pages.models import Page
 
+
 # Clean Up DB
 try:
     obj = rec_models.Shop.objects.get(titre='nkvbhbjh')
@@ -32,6 +33,12 @@ except rec_models.Shop.DoesNotExist:
 
 DOLLAR_TO_EURO = 0.898642152 # 04/09/2015
 
+RECORDS_TO_SKIP = [2734, 1006, 1393] # Doublons
+RECORDS_TO_SKIP.append(3323)  # old Fake records = "We buy records" page
+
+OLD_SHOP_LIST = rec_models.Shop.objects.exclude(pk__in = RECORDS_TO_SKIP)
+
+
 # Check mp3 in media upload path
 def check_mp3(file_list, file_path):
     abs_file_path = os.path.join(settings.MEDIA_ROOT, file_path)
@@ -46,6 +53,7 @@ def check_mp3(file_list, file_path):
         print "No missing MP3 Mix files"
     return missing_files
 
+# Get year and decade for record metadata
 def get_year(date):
     if date == '197':
         date = date + '?'
@@ -78,7 +86,7 @@ class Command(BaseCommand):
 
     # Label
     def populate_label(self):
-        old_labels = set([shop.label for shop in rec_models.Shop.objects.all()])
+        old_labels = set([shop.label for shop in OLD_SHOP_LIST])
         count = 0
         for lab in old_labels:
             obj, created = rec_models.Label.objects.get_or_create(name = lab)
@@ -92,7 +100,7 @@ class Command(BaseCommand):
     # Artist
     def populate_artist(self):
         Artist = rec_models.Artist
-        old_artist = set([shop.artiste for shop in rec_models.Shop.objects.all()])
+        old_artist = set([shop.artiste for shop in OLD_SHOP_LIST])
         count = 0
         for artist in old_artist:
             obj, created = Artist.objects.get_or_create(name = artist)
@@ -105,7 +113,7 @@ class Command(BaseCommand):
     # Country
     def populate_country(self):
         Country = rec_models.Country
-        old_country = set([shop.pays for shop in rec_models.Shop.objects.all()])
+        old_country = set([shop.pays for shop in OLD_SHOP_LIST])
         count = 0
         for country in old_country:
             obj, created = Country.objects.get_or_create(name = country)
@@ -348,17 +356,33 @@ class Command(BaseCommand):
 
     # Theme -> Category
     def populate_category(self):
-        #
-        shop_cat, created = Category.objects.get_or_create(title="Shop")
+        # This has to be run once as category are mezzanine Pages
+        # they would then be handle by a fixture for 'mezzanine pages' app
+
 
 
-        theme_list = [theme for theme in rec_models.Theme.objects.all() if theme.published==1]
+        shop_cat, created = Category.objects.get_or_create(title="Shop")
+        unclassified_cat, created = Category.objects.get_or_create(
+                title="Unclassified",
+                content="Unclassified records",
+                parent = shop_cat,
+                in_menus = [1,2])
+        showcase, created = Category.objects.get_or_create(
+                title="Showcase",
+                content="Category for product to be placed on the HomePage",
+                parent = shop_cat,
+                in_menus = [])
+
+
+        theme_list = [theme for theme in rec_models.Theme.objects.all()
+                      if (theme.published==1) and not(theme.id==39)]
         count = 0
         for theme in theme_list:
-            obj, create = Category.objects.get_or_create(pk=theme.id)
-            obj.title = theme.nom
-            obj.parent = shop_cat
-            obj.save()
+            category, create = Category.objects.get_or_create(pk=theme.id)
+            category.title = theme.nom
+            category.parent = shop_cat
+            category.in_menus = [1, 2]  # Show only in header and left panel
+            category.save()
             if create:
                 count +=1
 
@@ -373,72 +397,124 @@ class Command(BaseCommand):
         AUDIO_PATH = 'uploads/audio/'
         RECORDS_PATH = os.path.join(AUDIO_PATH, "records")
         IMG_PATH = os.path.join('uploads/product')
-        shop_list = rec_models.Shop.objects.all()
 
-        DOUBLONS_TO_SKIP = [2734, 1006, 1393]
+        unclassified_cat, created = Category.objects.get(title="Unclassified")
+        UNCLASS_ID = unclassified_cat.pk
+
+        THEME_TO_CATEGORY  = {
+            # Published
+            1: 1, # Jazz
+            2: 2, # Prog / Psych / Rock
+            37: 37,  #  Antilles / West Indies
+            6: 6,  # Experimental / Avant
+            7: 7,  # Soundtracks
+            10: 10,  # Electronic / Cosmic
+            13: 13,  # Middle East & Oriental
+            15: 15,  # Afro / Latin
+            19: 19,  # Library / Euro Grooves
+            29: 29,  # Prog / Psych / Pop 7
+            22: 22,  # Jazz Funk / Fusion
+            24: 24,  # French Sounds
+            28: 28,  # Breaks & Samples
+            30: 30,  # Selected Reissues
+            33: 33,  # Soul / Funk
+            38: 38,  # OUR PRODUCTION
+            # Unpublished categories classified above
+            42: 37,  # SOLD OUT ANTILLES WEST INDIES
+            16: 19,  # SOLD OUT LIBRARY 0
+            41:1,  # SOLD OUT JAZZ 0
+            25: 10,  # SOLD OUT ELECTRO COSMIC
+            # New category for old sold-out and unclass records
+            UNCLASS_ID: UNCLASS_ID,
+            # Unpublished and unclassified categories
+            17: UNCLASS_ID,  # IN STOCK ->> UNCLASSIFIED
+            18: UNCLASS_ID,  # SOLD OUT ->> UNCLASSIFIED
+            9: UNCLASS_ID,  # Stuffs->> UNCLASSIFIED
+            27: UNCLASS_ID,  # SOLD OUT DEPOT->> UNCLASSIFIED
+            40: UNCLASS_ID,  # Brazilian Music->> UNCLASSIFIED
+            31: UNCLASS_ID,  # NEW ! 20 € & UNDER->> UNCLASSIFIED
+            # Fake records category --> turn into a Page in fixture
+            # 39 WE BUY RECORDS
+            }
 
-        for shop in shop_list:
-            if shop.id in DOUBLONS_TO_SKIP:
-                continue
-            obj, created = rec_models.Record.objects.get_or_create(pk=shop.id)
+        for shop in OLD_SHOP_LIST:
 
-            obj.title = parser.unescape(shop.titre)
-            obj.country = rec_models.Country.objects.get(name = shop.pays)
-            obj.audio_file = os.path.join(RECORDS_PATH, shop.mp3)
+            # Get or Create record
+            record, created = rec_models.Record.objects.get_or_create(pk=shop.id)
+            # Set record metadata
+            record.title = parser.unescape(shop.titre)
+            record.country = rec_models.Country.objects.get(name = shop.pays)
+            record.audio_file = os.path.join(RECORDS_PATH, shop.mp3)
             # product_id --> FK
 
-            obj.release_year , obj.release_decade = get_year(shop.date)
-            obj.date_text = shop.date # To be deleted later ?
-            obj.record_status = shop.new
-            obj.cover_condition_id = shop.cover
-            obj.vinyl_condition_id = shop.vinyl
-            obj.label = rec_models.Label.objects.get(name = shop.label)
-            obj.artist = rec_models.Artist.objects.get(name = shop.artiste)
-            obj.save()
+            record.release_year , record.release_decade = get_year(shop.date)
+            record.date_text = shop.date # To be deleted later ?
+            record.record_status = shop.new
+            record.cover_condition_id = shop.cover
+            record.vinyl_condition_id = shop.vinyl
+            record.label = rec_models.Label.objects.get(name = shop.label)
+            record.artist = rec_models.Artist.objects.get(name = shop.artiste)
+            record.save()
 
             self.stdout.write('------')
-            self.stdout.write('Record %d -  %d' % (obj.pk,len(shop_list)))
+            self.stdout.write('Record %d -  %d' % (record.pk,len(OLD_SHOP_LIST)))
+
+            # --------------------------
+            # Create associated Product
+            # --------------------------
+            if isinstance(record.product, Product):
+                product = record.product
+            else:
+                product = Product.objects.create()
+                # Fill records ForeignKey to Product
+                record.product = product
+                record.save()
+
 
+            product.variations.manage_empty()
+
+            # Metadata
+            product.title = record.title
+            product.content = shop.desc
+
+            # Category
             theme = rec_models.Theme.objects.get(id=shop.theme)
-            if (theme.published == 1) and (shop.published==1):
-                prod_obj, prod_created = Product.objects.get_or_create(
-                                                title=obj.title,
-                                                content = shop.desc)
-                if shop.devise == 3: #Euros
-                    prod_obj.unit_price = shop.prix
-                elif shop.devise == 1: # Dollar
-                    prod_obj.price = round(shop.prix * DOLLAR_TO_EURO)
-                prod_obj.title = obj.title
-                prod_obj.sale_price = prod_obj.unit_price
-                prod_obj.available = bool(shop.published)
-                prod_obj.num_in_stock = 1
-                category = Category.objects.get(pk=shop.theme)
-                prod_obj.categories.add(category)
-
-                prodvar, created_var = ProductVariation.objects.get_or_create(product=prod_obj,
-                                                                 default=True)
-
-                prod_obj.save()
-                obj.product = prod_obj
-                obj.save()
-
-                img_file = os.path.join(IMG_PATH, str(shop.pk)+'.jpg')
-                abs_img_file = os.path.join(settings.MEDIA_ROOT, img_file)
-                if os.path.exists(abs_img_file):
-                    image, created = ProductImage.objects.get_or_create(
-                        file=img_file,
-                        product=prod_obj)
-                    prodvar.image = image
-                    prodvar.save()
-
-                prod_obj.copy_default_variation()
-                prod_obj.save()
-
-                print 'Product Image %s' % prod_obj.image
-                self.stdout.write('Product %d / %d --> %s' % (prod_obj.pk,len(shop_list), prod_obj.available))
-
-            #self.stdout.write('\b\b\b\b%.4d' % obj.pk)
-            #print obj.__dict__
+            category_id = THEME_TO_CATEGORY[theme.pk]
+            category = Category.objects.get(pk=category_id)
+            product.categories.add(category)
+
+            # Manage price / availability
+            product.available = (theme.published == 1) & (shop.published==1)
+            product.num_in_stock = int(product.available)
+
+            if shop.devise == 3: #Euros
+                product.unit_price = shop.prix
+            elif shop.devise == 1: # Dollar
+                product.price = round(shop.prix * DOLLAR_TO_EURO)
+            product.sale_price = None # No sale / pas de soldes
+
+            product.save() # Needed to copy the price fields to the default variation
+
+            # Cover image
+            img_file = os.path.join(IMG_PATH, str(shop.pk)+'.jpg')
+            abs_img_file = os.path.join(settings.MEDIA_ROOT, img_file)
+            if os.path.exists(abs_img_file):
+                image, created = ProductImage.objects.get_or_create(
+                    file=img_file,
+                    product=product)
+                product.variations.set_default_images([])
+
+            product.copy_default_variation()
+            product.save()
+
+            default_variation = product.variations.get(default=True)
+            default_variation.sku = "DD_REC_" + str(record.pk)
+            default_variation.save()
+
+            self.stdout.write('Product %d / %d --> %s' % (product.pk,len(OLD_SHOP_LIST), product.available))
+
+            #self.stdout.write('\b\b\b\b%.4d' % record.pk)
+            #print record.__dict__
 
         ## class Shop(models.Model):
         ##     theme = models.IntegerField()
@@ -485,7 +561,7 @@ class Command(BaseCommand):
         self.populate_label()
         self.populate_artist()
         self.populate_country()
-        self.populate_podcast(user=digger)
+        #self.populate_podcast(user=digger)
         #self.populate_pages()
         #self.populate_news()
 
@@ -494,7 +570,7 @@ class Command(BaseCommand):
 
 
 # MP3
-mp3_shop_list = [shop.mp3 for shop in rec_models.Shop.objects.all()]
+mp3_shop_list = [shop.mp3 for shop in OLD_SHOP_LIST]
 
 
 AUDIO_PATH = os.path.join(settings.MEDIA_ROOT, 'uploads/audio/')