def handle(self, *args, **options): for entity in Entity.objects.all(): if Artist.objects.filter(entity=entity): print("No need to create artist for: " + str(entity)) continue artist = Artist() artist.name = str(entity) artist.slug = entity.slug artist.website = entity.website artist.entity = entity if entity.kind == Entity.TYPE_TEXTER: artist.category = Artist.CAT_TEXTER elif entity.kind == Entity.TYPE_COMPOSER: artist.category = Artist.CAT_COMPOSER elif entity.kind == Entity.TYPE_FOREIGN: artist.category = Artist.CAT_FOREIGN elif entity.kind == Entity.TYPE_BAND: artist.category = Artist.CAT_BAND artist.save() print("Created artist: %s, slug: %s" % (artist.name, artist.slug)) for contribution in EntityContribution.objects.all(): if contribution.artist: print("No need to set artist on contribution.") continue contribution.artist = Artist.objects.get(entity=contribution.entity) contribution.save() print("%s set on %s" % (str(contribution.artist), str(contribution)))
def save(self,request): if request.FILES.get('id_headshot', None): if request.POST.get('id_artist_name'): artists = Artist.objects.filter(artist_name__iexact=request.POST.get('id_artist_name'), create_user=request.user) if not artists: artist = Artist() else: artist = artists[0] artist.artist_name = request.POST.get('id_artist_name') #删除掉原头像 import os if artist.headshot and os.path.exists(artist.headshot.path): os.remove(artist.headshot.path) artist.headshot = request.FILES.get('id_headshot', None) artist.create_user = request.user artist.save()
def upload_songs(): path = r"/Users/daniel/emelody/mysite/songs.txt" f = open(path, 'r') songs = [] for line in f: # Lines are in the following format: # Artist - Title.mp3 line = line.replace('.mp3', '') line = line.split('-') artist = line[0].strip() song = line[1].strip() songs.append((artist, song)) f.close() # Populate the Artist table for song in songs: artist = song[0] try: db_artist = Artist.objects.get(name=artist) except Artist.DoesNotExist: db_artist = Artist(name=artist) db_artist.save() # Populate the Song table for song in songs: artist_name = song[0] title = song[1] try: artist = Artist.objects.get(name=artist_name) try: db_song = Song.objects.get(title=title, artist=artist) except Song.DoesNotExist: db_song = Song(title=title, artist=artist) db_song.save() except Artist.DoesNotExist: continue
for line in f: # Lines are in the following format: # Artist - Title.mp3 line = line.replace('.mp3', '') line = line.split(' - ') artist = line[0].strip() song = line[1].strip() songs.append((artist, song)) f.close() # Populate the Artist table for song in songs: artist = song[0] try: db_artist = Artist.objects.get(name=artist) except Artist.DoesNotExist: db_artist = Artist(name=artist) db_artist.save() # Populate the Song table for song in songs: artist = song[0] title = song[1] try: db_song = Song.objects.get(title=title, artist=artist) except Song.DoesNotExist: db_song = Song(title=title, artist=artist) db_song.save()