From: Thomas Fillon Date: Wed, 2 Sep 2015 20:44:47 +0000 (+0200) Subject: Add management command to populate DB (work in progress) X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=da4fd9f4201eddffefe36d65bb2c098755c9564f;p=diggersdigest.git Add management command to populate DB (work in progress) --- diff --git a/diggersdigest/records/management/__init__.py b/diggersdigest/records/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/diggersdigest/records/management/commands/__init__.py b/diggersdigest/records/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/diggersdigest/records/management/commands/populate_db.py b/diggersdigest/records/management/commands/populate_db.py new file mode 100644 index 0000000..9f543ad --- /dev/null +++ b/diggersdigest/records/management/commands/populate_db.py @@ -0,0 +1,288 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from django.core.management.base import BaseCommand, CommandError + +# MYSQL command to restore the old SQL DB +# chown -R mysql:mysql /var/lib/mysql/ +# mysqld_safe & +# mysql -u digger -p diggersdigest < /var/lib/mysql/diggersdigest.sql + + +import os +from records import models as rec_models +from cartridge.shop.models import ProductOption + +from diggersdigest import settings + + +import HTMLParser +parser = HTMLParser.HTMLParser() + +import mezzanine.blog.models as blog_models + + +class Command(BaseCommand): + help = 'Populate the new database from the original backup' + + ## def add_arguments(self, parser): + ## parser.add_argument('poll_id', nargs='+', type=int) + + # Label + def populate_label(self): + old_labels = set([shop.label for shop in rec_models.Shop.objects.all()]) + count = 0 + for lab in old_labels: + obj, created = rec_models.Label.objects.get_or_create(name = lab) + if created: + count += 1 + + self.stdout.write('Labels\n------\n') + self.stdout.write("\t%d new label objects" % count) + self.stdout.write("\t%d labels in DB ( %d in old DB)" % (len(rec_models.Label.objects.all()), len(old_labels))) + + # Artist + def populate_artist(self): + Artist = rec_models.Artist + old_artist = set([shop.artiste for shop in rec_models.Shop.objects.all()]) + count = 0 + for artist in old_artist: + obj, created = Artist.objects.get_or_create(name = artist) + if created: + count += 1 + self.stdout.write('Artist\n------\n') + self.stdout.write("\t%d new artist objects" % count) + self.stdout.write("\t%d artists in DB ( %d in old DB)" % (len(Artist.objects.all()), len(old_artist))) + + # Country + def populate_country(self): + Country = rec_models.Country + old_country = set([shop.pays for shop in rec_models.Shop.objects.all()]) + count = 0 + for country in old_country: + obj, created = Country.objects.get_or_create(name = country) + if created: + count += 1 + + self.stdout.write('Country\n------\n') + self.stdout.write("\t%d new country objects" % count) + self.stdout.write("\t%d countries in DB ( %d in old DB)" % (len(Country.objects.all()), len(old_country))) + + + def handle(self, *args, **options): + ## for poll_id in options['poll_id']: + ## try: + ## poll = Poll.objects.get(pk=poll_id) + ## except Poll.DoesNotExist: + ## raise CommandError('Poll "%s" does not exist' % poll_id) + + ## poll.opened = False + ## poll.save() + + ## self.stdout.write('Successfully closed poll "%s"' % poll_id) + self.populate_label() + self.populate_artist() + self.populate_country() + + + + +# Theme +[theme for theme in rec_models.Theme.objects.all()] + + +# MP3 +mp3_shop_list = [shop.mp3 for shop in rec_models.Shop.objects.all()] + +# MP3 for mix +mp3_mix_list = [mix.mp3 for mix in rec_models.Mix.objects.all()] + +AUDIO_PATH = os.path.join(settings.MEDIA_ROOT, 'uploads/audio/') +MIX_PATH = os.path.join(AUDIO_PATH, 'mixes') +RECORDS_PATH = os.path.join(AUDIO_PATH, "records") + + +# Check mp3 in media upload path +def check_mp3(file_list, file_path): + file_exists = [os.path.exists(os.path.join(file_path, file_name)) + for file_name in file_list] + + missing_files = [file_name for (file_name, ok) in zip(file_list, file_exists) if not ok] + if missing_files: + print "Missing files in path : %s\n" % file_path + print '\n'.join(missing_files) + else: + print "No missing MP3 Mix files" + return missing_files + + +# check_mp3(mp3_mix_list, MIX_PATH) + + +# PODCAST FROM MIX +def populate_podcast(self): + MIX_CATEGORY = 'Podcast' + blog_models.BlogCategory.objects.get_or_create(title=MIX_CATEGORY) + + PODCAST_IMG_PATH = os.path.join('uploads/blog/mixes') + PODCAST_AUDIO_PATH = os.path.join('uploads/audio/mixes') + from django.contrib.auth import models as auth_models + digger, created = auth_models.User.objects.get_or_create(username=u'digger', + password=u'admin', + is_staff=True, + email=u'') + + + + + for mix in rec_models.Mix.objects.all(): + if mix.visu1: + img_file = str(mix.id) + '.jpg' + img_path = os.path.join(PODCAST_IMG_PATH, img_file) + else: + img_path = '' + + audio_path = os.path.join(PODCAST_AUDIO_PATH, mix.mp3) + print '---------------------------' + print 'pk :\t %d' % mix.pk + print 'audio :\t %s' % audio_path + print 'genre :\t %s' % mix.genre + print "old_date :\t %s" % mix.date + print "title :\t %s" % parser.unescape(mix.titre) + #status=mix.published+1, + #featured_image=img_path, + #user=digger) + obj, created = rec_models.Podcast.objects.get_or_create(pk=mix.pk, + title=parser.unescape(mix.titre), + user=digger) + if created: + obj.audio=audio_path + obj.genre=mix.genre + obj.old_date=mix.date + obj.description=parser.unescape(mix.desc) + obj.status=mix.published+1 + obj.featured_image=img_path + obj.save() + + for podcast in rec_models.Podcast.objects.filter(title=''): + podcast.delete() + ## audio = FileField(verbose_name=_("Audio File"), max_length=200, format="audio", + ## upload_to=upload_to("records.Podcast.audio", "audio/mixes")) + ## genre = models.CharField(max_length=128) + ## # titre --> title + ## date = models.CharField(max_length=64) + ## # desc --> description + ## # mp3 --> audio + ## #visu1 = models.IntegerField() si 1 --> featured_image + ## # ordre : on laisse tombé ? + ## # published --> status / 0 --> CONTENT_STATUS_DRAFT = 1 / 1 CONTENT_STATUS_PUBLISHED = 2 + + + + +# check_mp3(mp3_shop_list, RECORDS_PATH) + +# NEWS +def populate_news(self): + news_list = [news for news in rec_models.News.objects.all()] + blog_models.BlogCategory.objects.get_or_create(title='News') + +# GRADING + +Grading_Dict = { + 'SS': { + 'name': "Still Sealed", + 'description': "in perfect condition, no wear"}, + 'M' : { + 'name': "Mint" , + 'description': "Still in new condition, no imperfections, a Perfect Copy !"}, + 'NM' : { + 'name': "Near mint" , + 'description': "Imperceptible wear, full vinyl gloss"}, + 'EX' : { + 'name': "Excellent", + 'description': "Only very slight wear and paper scuffs, vinyl still glossy with very rare noise"}, + 'VG++' : { + 'name': "Near Excellent", + 'description': "Light surface noise and wear but fairly minor, vinyl still plays nicely"}, + 'VG+' : { + 'name': "Very Very Good", + 'description': "Evident groove wear or minor scuff marks, surface noise noticeable but does not really affect the playing"}, + 'VG' : { + 'name': "Very Good", + 'description': "Wear and scuffs are more evident and many surface noises are noticeable"}, + 'VG-' : { + 'name': "Good to Very Good", + 'description': "Heavy groove wear or scuffing, plays noisily"} + } + + +count = 0 +for grade in rec_models.Grading.objects.all(): + name = Grading_Dict[grade.nom]['name'] + desc = Grading_Dict[grade.nom]['description'] + obj, created = rec_models.ConditionGrading.objects.get_or_create(id=grade.id, + abbr=grade.nom, + name=name, + description=desc) + + if created: + print "Create new GradingCondition : %s" % obj.name + count += 1 + + for option in settings.SHOP_OPTION_TYPE_CHOICES: + option_obj, option_created = ProductOption.objects.get_or_create(type=option[0], + name=grade.nom) + +print "%d fiches GradingCondition crées" % count + +def populate__record(): + # SHOPS TO RECORDS + shop_list = rec_models.Shop.objects.all() + for shop in shop_list: + print "-----------" + print shop.titre + s_titre = shop.titre + if not (shop.titre == parser.unescape(shop.titre)): + s_titre = parser.unescape(shop.titre) + print "\t-->\t%s" % s_titre + + s_theme = rec_models.Theme.objects.filter(id=shop.theme)[0] + print "\tTheme : %s" % s_theme.nom + + s_artist = rec_models.Artist.objects.filter(name=shop.artiste)[0] + print "\tArtist : %s" % s_artist.name + + s_label = rec_models.Label.objects.filter(name=shop.label)[0] + print "\tLabel : %s" % s_label.name + s_country = rec_models.Country.objects.filter(name=shop.pays)[0] + print "\tCountry : %s" % s_country.name + + record_from_shop = rec_models.Record(title=s_titre, + artist=s_artist, + label=s_label, + country=s_country) + print record_from_shop.__dict__ + + ## class Shop(models.Model): + ## theme = models.IntegerField() + ## artiste = models.CharField(max_length=128) + ## new = models.IntegerField() + ## titre = models.CharField(max_length=128) + ## label = models.CharField(max_length=128) + ## date = models.CharField(max_length=8) + ## pays = models.CharField(max_length=128) + ## desc = models.TextField() + ## cover = models.IntegerField() + ## vinyl = models.IntegerField() + ## prix = models.IntegerField() + ## devise = models.IntegerField() + ## mp3 = models.CharField(max_length=128) + ## visu1 = models.IntegerField() + ## ordre = models.IntegerField() + ## published = models.IntegerField() + + ## class Record(Product): + ## """ + ## Model for Record + ## """ diff --git a/diggersdigest/records/script_data_recover.py b/diggersdigest/records/script_data_recover.py deleted file mode 100644 index da0cc91..0000000 --- a/diggersdigest/records/script_data_recover.py +++ /dev/null @@ -1,271 +0,0 @@ -# chown -R mysql:mysql /var/lib/mysql/ -# mysqld_safe & -# mysql -u digger -p diggersdigest < /var/lib/mysql/diggersdigest.sql - - -import os -from records import models as rec_models -from cartridge.shop.models import ProductOption - -from diggersdigest import settings - - -import HTMLParser -parser = HTMLParser.HTMLParser() - -import mezzanine.blog.models as blog_models - - - -def populate_label(): - # Label - label_set = set([shop.label for shop in rec_models.Shop.objects.all()]) - count = 0 - for lab in label_set: - obj, created = rec_models.Label.objects.get_or_create(name = lab) - if created: - print "Create new Label : %s" % obj.name - count += 1 - - print '--------------------------------------' - print "%d created label objects" % count - print "%d label in new db / %d in old db" % (len(rec_models.Label.objects.all()), len(label_set)) - print '--------------------------------------' - - return - -# Artist -def populate_artist(): - artist_set = set([shop.artiste for shop in rec_models.Shop.objects.all()]) - count = 0 - for artist in artist_set: - obj, created = rec_models.Artist.objects.get_or_create(name = artist) - if created: - print "Create new Artist : %s" % obj.name - count += 1 - - print '--------------------------------------' - print "%d created artist object" % count - print "%d artist in new db / %d in old db" % (len(rec_models.Artist.objects.all()), len(artist_set)) - print '--------------------------------------' - - return - - -# Country -def populate_country( - country_set = set([shop.pays for shop in rec_models.Shop.objects.all()]) - count = 0 - for country in country_set: - obj, created = rec_models.Country.objects.get_or_create(name = country) - if created: - print "Create new Country : %s" % obj.name - count += 1 - - print '--------------------------------------' - print "%d created country object" % count - print "%d country in new db / %d in old db" % (len(rec_models.Country.objects.all()), len(country_set)) - print '--------------------------------------' - - return - -# Theme -[theme for theme in rec_models.Theme.objects.all()] - - -# MP3 -mp3_shop_list = [shop.mp3 for shop in rec_models.Shop.objects.all()] - -# MP3 for mix -mp3_mix_list = [mix.mp3 for mix in rec_models.Mix.objects.all()] - -AUDIO_PATH = os.path.join(settings.MEDIA_ROOT, 'uploads/audio/') -MIX_PATH = os.path.join(AUDIO_PATH, 'mixes') -RECORDS_PATH = os.path.join(AUDIO_PATH, "records") - - -# Check mp3 in media upload path -def check_mp3(file_list, file_path): - file_exists = [os.path.exists(os.path.join(file_path, file_name)) - for file_name in file_list] - - missing_files = [file_name for (file_name, ok) in zip(file_list, file_exists) if not ok] - if missing_files: - print "Missing files in path : %s\n" % file_path - print '\n'.join(missing_files) - else: - print "No missing MP3 Mix files" - return missing_files - - -check_mp3(mp3_mix_list, MIX_PATH) - - -# PODCAST FROM MIX -MIX_CATEGORY = 'Podcast' -blog_models.BlogCategory.objects.get_or_create(title=MIX_CATEGORY) - -PODCAST_IMG_PATH = os.path.join('uploads/blog/mixes') -PODCAST_AUDIO_PATH = os.path.join('uploads/audio/mixes') -from django.contrib.auth import models as auth_models -digger, created = auth_models.User.objects.get_or_create(username=u'digger', - password=u'admin', - is_staff=True, - email=u'') - - - - -for mix in rec_models.Mix.objects.all(): - if mix.visu1: - img_file = str(mix.id) + '.jpg' - img_path = os.path.join(PODCAST_IMG_PATH, img_file) - else: - img_path = '' - - audio_path = os.path.join(PODCAST_AUDIO_PATH, mix.mp3) - print '---------------------------' - print 'pk :\t %d' % mix.pk - print 'audio :\t %s' % audio_path - print 'genre :\t %s' % mix.genre - print "old_date :\t %s" % mix.date - print "title :\t %s" % parser.unescape(mix.titre) - #status=mix.published+1, - #featured_image=img_path, - #user=digger) - obj, created = rec_models.Podcast.objects.get_or_create(pk=mix.pk, - title=parser.unescape(mix.titre), - user=digger) - if created: - obj.audio=audio_path - obj.genre=mix.genre - obj.old_date=mix.date - obj.description=parser.unescape(mix.desc) - obj.status=mix.published+1 - obj.featured_image=img_path - obj.save() - - for podcast in rec_models.Podcast.objects.filter(title=''): - podcast.delete() - ## audio = FileField(verbose_name=_("Audio File"), max_length=200, format="audio", - ## upload_to=upload_to("records.Podcast.audio", "audio/mixes")) - ## genre = models.CharField(max_length=128) - ## # titre --> title - ## date = models.CharField(max_length=64) - ## # desc --> description - ## # mp3 --> audio - ## #visu1 = models.IntegerField() si 1 --> featured_image - ## # ordre : on laisse tombé ? - ## # published --> status / 0 --> CONTENT_STATUS_DRAFT = 1 / 1 CONTENT_STATUS_PUBLISHED = 2 - - - - -check_mp3(mp3_shop_list, RECORDS_PATH) - -# NEWS -news_list = [news for news in rec_models.News.objects.all()] -blog_models.BlogCategory.objects.get_or_create(title='News') - -# GRADING - -Grading_Dict = { - 'SS': { - 'name': "Still Sealed", - 'description': "in perfect condition, no wear"}, - 'M' : { - 'name': "Mint" , - 'description': "Still in new condition, no imperfections, a Perfect Copy !"}, - 'NM' : { - 'name': "Near mint" , - 'description': "Imperceptible wear, full vinyl gloss"}, - 'EX' : { - 'name': "Excellent", - 'description': "Only very slight wear and paper scuffs, vinyl still glossy with very rare noise"}, - 'VG++' : { - 'name': "Near Excellent", - 'description': "Light surface noise and wear but fairly minor, vinyl still plays nicely"}, - 'VG+' : { - 'name': "Very Very Good", - 'description': "Evident groove wear or minor scuff marks, surface noise noticeable but does not really affect the playing"}, - 'VG' : { - 'name': "Very Good", - 'description': "Wear and scuffs are more evident and many surface noises are noticeable"}, - 'VG-' : { - 'name': "Good to Very Good", - 'description': "Heavy groove wear or scuffing, plays noisily"} - } - - -count = 0 -for grade in rec_models.Grading.objects.all(): - name = Grading_Dict[grade.nom]['name'] - desc = Grading_Dict[grade.nom]['description'] - obj, created = rec_models.ConditionGrading.objects.get_or_create(id=grade.id, - abbr=grade.nom, - name=name, - description=desc) - - if created: - print "Create new GradingCondition : %s" % obj.name - count += 1 - - for option in settings.SHOP_OPTION_TYPE_CHOICES: - option_obj, option_created = ProductOption.objects.get_or_create(type=option[0], - name=grade.nom) - -print "%d fiches GradingCondition crées" % count - - -# SHOPS TO RECORDS -shop_list = rec_models.Shop.objects.all() -for shop in shop_list: - print "-----------" - print shop.titre - s_titre = shop.titre - if not (shop.titre == parser.unescape(shop.titre)): - s_titre = parser.unescape(shop.titre) - print "\t-->\t%s" % s_titre - - s_theme = rec_models.Theme.objects.filter(id=shop.theme)[0] - print "\tTheme : %s" % s_theme.nom - - s_artist = rec_models.Artist.objects.filter(name=shop.artiste)[0] - print "\tArtist : %s" % s_artist.name - - s_label = rec_models.Label.objects.filter(name=shop.label)[0] - print "\tLabel : %s" % s_label.name - s_country = rec_models.Country.objects.filter(name=shop.pays)[0] - print "\tCountry : %s" % s_country.name - - record_from_shop = rec_models.Record(title=s_titre, - artist=s_artist, - label=s_label, - country=s_country) - print record_from_shop.__dict__ - -## class Shop(models.Model): -## theme = models.IntegerField() -## artiste = models.CharField(max_length=128) -## new = models.IntegerField() -## titre = models.CharField(max_length=128) -## label = models.CharField(max_length=128) -## date = models.CharField(max_length=8) -## pays = models.CharField(max_length=128) -## desc = models.TextField() -## cover = models.IntegerField() -## vinyl = models.IntegerField() -## prix = models.IntegerField() -## devise = models.IntegerField() -## mp3 = models.CharField(max_length=128) -## visu1 = models.IntegerField() -## ordre = models.IntegerField() -## published = models.IntegerField() - -## class Record(Product): -## """ -## Model for Record -## """ - -if __name__ == '__main__': - pass