def test_entity_index(self): author = testing.create_user() jack_white = Artist.create_for_testing(author) jack_white.featured = True jack_white.save() seven_nations_army = Song.create_for_testing(author) self.add_contribution(seven_nations_army, jack_white, True) jolene = Song.create_for_testing(author) self.add_contribution(jolene, jack_white, True) # Approve only Jolene. seven_nations_army.reviewed = False seven_nations_army.save() jolene.reviewed = True jolene.save() # General public should see only Jolene. response = testing.get_public_client().get( jack_white.get_absolute_url()) self.assertEqual(200, response.status_code) self.assertEqual(1, len(response.context['songs'])) # The author should see both. response = testing.get_user_client(author).get( jack_white.get_absolute_url()) self.assertEqual(200, response.status_code) self.assertEqual(2, len(response.context['songs'])) # Any logged-in user should see both, too. response = testing.get_user_client().get(jack_white.get_absolute_url()) self.assertEqual(200, response.status_code) self.assertEqual(2, len(response.context['songs']))
def get_add_url(self): author = testing.create_user() artist = Artist.create_for_testing(author) artist.reviewed = True artist.save() song = Song.create_for_testing(author) song.reviewed = True song.full_clean() song.save() return song.get_add_note_url()
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
def test_add_song_note(self): user = testing.create_user() artist = Artist.create_for_testing(user) artist.reviewed = True artist.full_clean() artist.save() song = Song.create_for_testing(user) song.reviewed = True song.full_clean() song.save() data = { 'title': 'dalsze losy kotka', 'text_trevor': put_text_in_trevor('Abc') } self.assertEqual(len(SongNote.objects.all()), 0) response = testing.get_user_client(user=user).post( song.get_add_note_url(), data=data) self.assertEqual(302, response.status_code) self.assertRedirects(response, song.get_absolute_url()) self.assertEqual(len(SongNote.objects.all()), 1)
def test_add_song(self): user = testing.create_user() artist = Artist.create_for_testing(user) artist.reviewed = True artist.save() data = { 'title': 'wlazlkotek', 'capo_fret': 0, 'lyrics': 'wlazl kotek na plotek', 'entitycontribution_set-TOTAL_FORMS': 1, 'entitycontribution_set-INITIAL_FORMS': 0, 'entitycontribution_set-MIN_NUM_FORMS': 1, 'entitycontribution_set-MAX_NUM_FORMS': 1000, 'entitycontribution_set-0-artist': artist.pk, 'entitycontribution_set-0-texted': True, } response = testing.get_user_client(user=user).post( reverse('add_song'), data=data) self.assertRedirects(response, '/opracowanie/' + artist.name + '-wlazlkotek/') song = Song.objects.get(title='wlazlkotek') self.assertEqual('wlazl kotek na plotek', song.lyrics)
def test_cannot_add_song_note_if_song_not_reviewed(self): user = testing.create_user() artist = Artist.create_for_testing(user) artist.reviewed = True artist.full_clean() artist.save() song = Song.create_for_testing(user) song.reviewed = True song.full_clean() song.save() url = song.get_add_note_url() song.reviewed = False song.full_clean() song.save() data = { 'title': 'dalsze losy kotka', 'text_trevor': put_text_in_trevor('Abc') } response = testing.get_user_client(user=user).post(url, data=data) self.assertEqual(404, response.status_code) self.assertEqual(len(SongNote.objects.all()), 0)
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()
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()
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)))