예제 #1
0
def album_create(request):
	if request.method == 'POST':
		form = AlbumForm(request.POST, request.FILES)
		if form.is_valid():
			# save album
			album = form.save()
			# save tags
			tags = request.POST['tags'].split(", ")
			for tag_name in tags:
				tag = Tag.objects.filter(name__iexact=tag_name)
				if tag:
					tag = tag[0]
				else:
					tag = Tag(name=tag_name)
					tag.save()
				tag.albums.add(album)
				tag.save()
			# save songs
			index = 0
			while 'song_set-'+str(index)+'-title' in request.POST:
				title = request.POST['song_set-'+str(index)+'-title']
				if title!="":
					song = Song(title=title, album=album)
					song.save()
				index += 1
			return redirect('albums:artists');
	else:
		form = AlbumForm()
	return render_to_response('albums/album_form.html', {'form':form}, context_instance=RequestContext(request))
예제 #2
0
def album_edit(request, pk):
	if request.method == 'POST':
		album = Album.objects.get(pk=pk)
		if album:
			form = AlbumForm(request.POST, request.FILES, instance=album)
		else:
			form = AlbumForm(request.POST, request.FILES)
		if form.is_valid():
			# save album
			album = form.save()
			# save tags
			tags = request.POST['tags'].split(", ")
			for tag_name in tags:
				tag = Tag.objects.filter(name__iexact=tag_name)
				if tag:
					tag = tag[0]
				else:
					tag = Tag(name=tag_name)
					tag.save()
				if album not in tag.albums.all():
					tag.albums.add(album)
					tag.save()
			# save songs
			for song in album.song_set.all():
				song.delete()
			index = 0
			while 'song_set-'+str(index)+'-title' in request.POST:
				title = request.POST['song_set-'+str(index)+'-title']
				print title
				if title!="":
					song = Song(title=title, album=album)
					song.save()
				index += 1
			return redirect('albums:artists');
	else:
		album = Album.objects.get(pk=pk)
		if album:
			form = AlbumForm(instance=album)
			song_set = album.song_set.all()
			tag_set = album.tag_set.all()
			tags = []
			for tag in tag_set:
				tags.append(tag.name)
			tags = ", ".join(tags)
			return render_to_response('albums/album_form.html', {'form':form,'tags':tags, 'song_set':song_set}, context_instance=RequestContext(request))
		else:
			form = AlbumForm()
			return render_to_response('albums/album_form.html', {'form':form}, context_instance=RequestContext(request))
	return render_to_response('albums/album_form.html', {'form':form}, context_instance=RequestContext(request))