From b33bb798ba3542bed9302666c41c70a00cdca8f5 Mon Sep 17 00:00:00 2001 From: Thomas Fillon Date: Tue, 25 Aug 2015 09:21:11 +0200 Subject: [PATCH] Link new models with ForeignKey --- .../migrations/0004_auto_20150824_1537.py | 29 ++++ .../migrations/0005_auto_20150824_1547.py | 24 +++ diggersdigest/records/models.py | 6 +- diggersdigest/records/script_data_recover.py | 147 ++++++++++++++++++ 4 files changed, 203 insertions(+), 3 deletions(-) create mode 100644 diggersdigest/records/migrations/0004_auto_20150824_1537.py create mode 100644 diggersdigest/records/migrations/0005_auto_20150824_1547.py create mode 100644 diggersdigest/records/script_data_recover.py diff --git a/diggersdigest/records/migrations/0004_auto_20150824_1537.py b/diggersdigest/records/migrations/0004_auto_20150824_1537.py new file mode 100644 index 0000000..b5f9a63 --- /dev/null +++ b/diggersdigest/records/migrations/0004_auto_20150824_1537.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('records', '0003_podcast'), + ] + + operations = [ + migrations.AlterField( + model_name='record', + name='artiste', + field=models.ForeignKey(to='records.Artist'), + ), + migrations.AlterField( + model_name='record', + name='label', + field=models.ForeignKey(to='records.Label'), + ), + migrations.AlterField( + model_name='record', + name='pays', + field=models.ForeignKey(to='records.Country'), + ), + ] diff --git a/diggersdigest/records/migrations/0005_auto_20150824_1547.py b/diggersdigest/records/migrations/0005_auto_20150824_1547.py new file mode 100644 index 0000000..f7dd0ca --- /dev/null +++ b/diggersdigest/records/migrations/0005_auto_20150824_1547.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('records', '0004_auto_20150824_1537'), + ] + + operations = [ + migrations.RenameField( + model_name='record', + old_name='artiste', + new_name='artist', + ), + migrations.RenameField( + model_name='record', + old_name='pays', + new_name='country', + ), + ] diff --git a/diggersdigest/records/models.py b/diggersdigest/records/models.py index f0f5cfc..13bd507 100644 --- a/diggersdigest/records/models.py +++ b/diggersdigest/records/models.py @@ -185,11 +185,11 @@ class Record(Product): # price --> shop.prix # status --> shop.published - artiste = models.CharField(max_length=128) + artist = models.ForeignKey(Artist) new = models.IntegerField() - label = models.CharField(max_length=128) + label = models.ForeignKey(Label) date = models.CharField(max_length=8) - pays = models.CharField(max_length=128) + country = models.ForeignKey(Country) desc = models.TextField() cover = models.IntegerField() # TODO : choices=GRADINGS) vinyl = models.IntegerField() # TODO : choices=GRADING) diff --git a/diggersdigest/records/script_data_recover.py b/diggersdigest/records/script_data_recover.py new file mode 100644 index 0000000..6f0d9e5 --- /dev/null +++ b/diggersdigest/records/script_data_recover.py @@ -0,0 +1,147 @@ +# 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 diggersdigest import settings + + + +# 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 "%d fiches label créées" % count + +# 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 "%d fiches artiste créées" % count + +# Pays +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 "%d fiches pays créées" % count + +# 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) + +check_mp3(mp3_shop_list, RECORDS_PATH) + +# NEWS +news_list = [news for news in rec_models.News.objects.all()] + + +# SHOPS TO RECORDS +import HTMLParser +parser = HTMLParser.HTMLParser() +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 +## """ +## # herited fields (from Product): +## # title --> shop.titre +## # categories --> shop.theme +## # price --> shop.prix +## # status --> shop.published + +## artiste = models.CharField(max_length=128) +## new = models.IntegerField() +## label = models.CharField(max_length=128) +## date = models.CharField(max_length=8) +## pays = models.CharField(max_length=128) +## desc = models.TextField() +## cover = models.IntegerField() # TODO : choices=GRADINGS) +## vinyl = models.IntegerField() # TODO : choices=GRADING) +## audio = FileField(_("Audio File"), max_length=200, format="media", +## upload_to=upload_to("records.Record.audio", "audio/records")) -- 2.39.5