示例#1
0
def add_to_list(request, type, ids):
	
	id_array = ids.split("_")
	cur_user = get_user(request)
	
	rec_library = RecordLibrary.objects.filter(user=cur_user.id, library_type=type)
	if not(rec_library):
		rec_library = RecordLibrary(user=cur_user, library_type=type)
		rec_library.save()
	else:
		rec_library = rec_library[0]
	
	duplicate_ids = []
	inserted_recs = []
	for rec_id in id_array:
		try:
			item = RecordLibraryItem(library=rec_library, user=cur_user, record=Record(id=rec_id))
			item.save()
		except IntegrityError:
			duplicate_ids.append(item.record.id)
			transaction.rollback()
		else:
			inserted_recs.append(item.record.id)

	msg = ''
	if len(inserted_recs) > 0:
		id_inclause = 'id in (' + ",".join(inserted_recs) + ')'
		inserted_recs = Record.objects.extra(where=[id_inclause]).values('title')
		instd_rec_title = []
		for instd_rec in inserted_recs:
			instd_rec_title.append(instd_rec.get('title'))
		have = pluralize("have", len(inserted_recs))
		msg = ("'{0}' {1} been added. \n\n ".format("', '".join(instd_rec_title), have))
	if len(duplicate_ids) > 0:
		
		id_inclause = 'id in (' + ",".join(duplicate_ids) + ')'
		duplicate_recs = Record.objects.extra(where=[id_inclause]).values('title')
		dup_rec_title = []
		for dup_rec in duplicate_recs:
			dup_rec_title.append(dup_rec.get('title'))
		
		be = pluralize("be", len(duplicate_ids))
		msg += "'{0}' {1} already in the list.".format("', '".join(dup_rec_title), be)
		
	return HttpResponse(msg)
示例#2
0
def save_to_playlist(cur_user, playlist, ids):
	id_array = ids.split("__")
	
	for id in id_array:
		trid = id.split("_")
		item = PlaylistItem(playlist=playlist, created_by=cur_user,
						track=Soundtrack(id=trid[0]), record=Record(id=trid[1]),
						)
		duplicate_ids = []
		inserted_ids = []
		try:
			item.save()
		except IntegrityError:
			duplicate_ids.append(trid[0])
			transaction.rollback()
		else: 
			inserted_ids.append(trid[0])
	
	msg = ""
	if len(inserted_ids) > 0:
		id_inclause = 'id in (' + ",".join(inserted_ids) + ')'
		inserted_tracks = Soundtrack.objects.extra(where=[id_inclause]).values('title')
		instd_rec_title = []
		for instd_rec in inserted_tracks:
			instd_rec_title.append(instd_rec.get('title'))
			
		have = pluralize("have", len(inserted_tracks))
		msg = ("'{0}' {1} been added. \n\n ".format("', '".join(instd_rec_title), have))
	if len(duplicate_ids) > 0:
		
		id_inclause = 'id in (' + ",".join(duplicate_ids) + ')'
		duplicate_recs = Soundtrack.objects.extra(where=[id_inclause]).values('title')
		dup_rec_title = []
		for dup_rec in duplicate_recs:
			dup_rec_title.append(dup_rec.get('title'))
		
		be = pluralize("be", len(duplicate_ids) == 1)
		msg += "'{0}' {1} already in the list.".format("', '".join(dup_rec_title), be)
	return msg