Beispiel #1
0
def addTrack (request):
    if request.method == "POST":
        trackTitle = request.POST.get('title')
        if trackTitle!="":
            try:
                track = Track(title=trackTitle, rating=request.POST.get('rating'), )
                track.save()
                genres = request.POST.getlist('genres')
                for genreId in genres:
                    genre = Genre.objects.get(pk=genreId)
                    track.genres.add(genre)
            except ():
                return render(request, 'music/track.add.html', {
                    'error_message': "Something aweful happened. Try again"
                })
            else:
                return HttpResponseRedirect(reverse('music:showAllTracks'))
        else:
            return render(request, 'music/track.add.html', {
                'error_message': "Can't save empty Track"
            })
    else:
        genreList = Genre.objects.all()
        context = dict()
        context['genreList'] = genreList
        return render(request, 'music/track.add.html', context)
Beispiel #2
0
def addTrack(request):
    if request.method == "POST":
        trackTitle = request.POST.get('title')
        if trackTitle != "":
            try:
                track = Track(
                    title=trackTitle,
                    rating=request.POST.get('rating'),
                )
                track.save()
                genres = request.POST.getlist('genres')
                for genreId in genres:
                    genre = Genre.objects.get(pk=genreId)
                    track.genres.add(genre)
            except ():
                return render(
                    request, 'music/track.add.html',
                    {'error_message': "Something aweful happened. Try again"})
            else:
                return HttpResponseRedirect(reverse('music:showAllTracks'))
        else:
            return render(request, 'music/track.add.html',
                          {'error_message': "Can't save empty Track"})
    else:
        genreList = Genre.objects.all()
        context = dict()
        context['genreList'] = genreList
        return render(request, 'music/track.add.html', context)
 def writeModel(self, root, title, album, artist, track, cover, source):
     obj = Track()
     obj.title = title
     obj.album = album
     obj.artist = artist
     obj.track = track
     obj.source = source
     obj.cover = cover
     obj.save()
Beispiel #4
0
def create_track(sp_track, client, username=None):
    
    client = client
    client_unique = sp_track['id'] 
    name = sp_track['name']
    uri = sp_track['uri'] 
    artist_name = sp_track['artists'][0]['name']
    code=create_code(name, artist_name)
    new_track = Track(
                    client=client,
                    client_unique=client_unique,
                    name=name,
                    uri=uri,
                    artist_name=artist_name,
                    tb_code=code
                    )
    album_id = sp_track['album']['id']
    
    if album_id is not None:
        # Check if album exists
        try:
            album = Album.objects.get(client_unique=album_id)
            new_track.album = album
        except Album.DoesNotExist:
            if username is not None:

                token = util.prompt_for_user_token(username)
                sp = spotipy.Spotify(auth=token)
                sp_album = sp.album(album_id)
                album = create_album(sp_album, client)
                new_track.album = album
    
    new_track.save()
    for artist in sp_track['artists']:
        artist_id = artist['id']
        if artist_id is not None:
            try:
                artist = Artist.objects.get(client_unique=artist_id)
                new_track.artists.add(artist)
            except Artist.DoesNotExist:
                if username is not None:
                    sp_artist = sp.artist(artist_id)
                    artist = create_artist(sp_artist, client)
                    new_track.artists.add(artist)
        
    new_track.save()
    return new_track
    def setUp(self):
        album = Album(album_name='Anti', artist='Rihanna')
        album.save()

        self.album_id = album.id

        track1 = Track(title='Desperado', order=1, album=album, duration=4)
        track1.save()

        self.track1_id = track1.id

        track2 = Track(title='Love on the brain',
                       order=2,
                       album=album,
                       duration=3)
        track2.save()

        self.track2_id = track2.id
Beispiel #6
0
 def test_get_primary_contributors(self):
     # create website site item and set as current site
     web_site = Site(domain="web.address.com")
     web_site.save()
     settings.SITE_ID = web_site.id
     
     # create a track with some credits
     track = Track(title="title")
     track.save()
     contributor1 = TrackContributor(title="title", state="published")
     contributor1.save()
     contributor1.sites.add(web_site)
     contributor2 = TrackContributor(title="title", state="published")
     contributor2.save()
     contributor2.sites.add(web_site)
     contributor3 = TrackContributor(title="title", state="published")
     contributor3.save()
     contributor3.sites.add(web_site)
     contributor4 = TrackContributor(title="title", state="published")
     contributor4.save()
     contributor4.sites.add(web_site)
     unpublished_contributor = TrackContributor(title="title")
     unpublished_contributor.save()
     Credit(track=track, contributor=contributor1, role=2).save()
     Credit(track=track, contributor=contributor2, role=10).save()
     Credit(track=track, contributor=contributor3, role=2).save()
     Credit(track=track, contributor=unpublished_contributor, role=2).save()
     Credit(track=track, contributor=contributor4).save()
     
     # result should only contain contributors with highest role. 
     # can contain multiples.
     # highest role not neccessarily 1. 
     # result should not include non permitted contributors.
     # result should not include contributors with None credit role
     primary_contributors = track.get_primary_contributors()
     self.failUnless(contributor1 in primary_contributors)
     self.failUnless(contributor3 in primary_contributors)
     self.failIf(contributor2 in primary_contributors)
     self.failIf(unpublished_contributor in primary_contributors)
     self.failIf(contributor4 in primary_contributors)
Beispiel #7
0
def upload(request):
    form = TrackForm(request.POST, request.FILES)
    if form.is_valid():
        # get the mp3 data from the temp path
        try:
            mp3 = eyed3.load(request.FILES['file_path'].file.name)
        except Exception as e:
            messages.error(request, 'This is not a mp3 file')
            return redirect('home')

        try:
            # set the track data
            track = Track(artist=mp3.tag.artist,
                          title=mp3.tag.title,
                          album=mp3.tag.album,
                          year=mp3.tag.release_date.year,
                          file_path=request.FILES['file_path'])

            # get the playlist
            playlist = get_object_or_404(Playlist,
                                         pk=request.POST['playlist_id'])
        except Exception as error:

            messages.error(request, 'Error uploading mp3 file: ' + str(error))
            return redirect('home')

        # make sure it will process
        try:
            track.full_clean()
        except ValidationError as error:
            messages.error(request, 'Error validating: ' + str(error))
            return redirect('home')

        messages.success(request, 'MP3 Track uploaded')
        track.save()

        playlist.tracks.add(track)

        return redirect('home')
Beispiel #8
0
def upload_handler(request):
    if request.method == "POST":
        f = request.FILES["upload"]
        filename = str(f)
        f_ext = os.path.splitext(str(f))[1]

        # If file is a compatible music file, build a filesystem structure for
        # it (media/artist-name/album-name), then write it to that location and
        # add details of the file to the database.
        if f_ext in [".mp3", ".MP3"]:
            with open(MEDIA_ROOT + str(f), "wb") as destination:
                for chunk in f.chunks():
                    destination.write(chunk)

                filename = eyed3.load(MEDIA_ROOT + filename)
                dir_struct = MEDIA_ROOT + "music" + "/" + filename.tag.artist + "/" + filename.tag.album + "/"

                try:
                    if not os.path.isdir(dir_struct):
                        os.makedirs(dir_struct)
                    shutil.move(MEDIA_ROOT + str(f), dir_struct)
                except OSError as ose:
                    if ose.errno != 17:
                        raise
                    # Need to pass on Errno 17, due to race conditions caused
                    # by making directories that already exist.
                    pass
                except Exception as e:
                    # File already exists. Remove the duplicate copy.
                    os.remove(MEDIA_ROOT + str(f))
                    return render_to_response(
                        "errors/file_already_exists.html", locals(), context_instance=RequestContext(request)
                    )

            # If the artist or album doesn't exist in the database, create
            # table(s) for them. If they already exists, perform a query to
            # obtain an Artist or Album object for use as a foreign key.
            if not Artist.objects.filter(name=filename.tag.artist).exists():
                artist = Artist(name=filename.tag.artist)
                artist.save()
            else:
                artist_set = Artist.objects.filter(name=filename.tag.artist)
                artist = [a for a in artist_set][0]

            if not Album.objects.filter(title=filename.tag.album).exists():
                album = Album(title=filename.tag.album, artist=artist)
                album.save()
            else:
                album_set = Album.objects.filter(title=filename.tag.album)
                album = [a for a in album_set][0]

            if not Track.objects.filter(title=filename.tag.title).exists():
                track = Track(
                    title=filename.tag.title,
                    album=album,
                    artist=artist,
                    fspath=dir_struct + str(f),
                    media_url=MEDIA_URL + (dir_struct + str(f)).split(MEDIA_ROOT)[1],
                )
                track.save()
                print "Added to DB: " + filename.tag.title
                return render_to_response(
                    "errors/upload_success.html", locals(), context_instance=RequestContext(request)
                )
            else:
                return render_to_response(
                    "errors/file_already_exists.html", locals(), context_instance=RequestContext(request)
                )

        elif f_ext in [".jpg", ".JPG"]:
            if not os.path.exists(MEDIA_ROOT + "pictures/" + str(f)):
                with open(MEDIA_ROOT + "pictures/" + str(f), "wb") as destination:
                    for chunk in f.chunks():
                        destination.write(chunk)
            else:  # File already exists. Remove the duplicate copy.
                return render_to_response(
                    "errors/file_already_exists.html", locals(), context_instance=RequestContext(request)
                )

            # If the picture doesn't exist in the database, add it.
            if not Picture.objects.filter(name=str(f)).exists():
                picture = Picture(
                    name=os.path.splitext(str(f))[0],
                    filename=str(f),
                    fspath=MEDIA_ROOT + "pictures/" + str(f),
                    media_url=MEDIA_URL + (MEDIA_ROOT + "pictures/" + str(f)).split(MEDIA_ROOT)[1],
                )
                picture.save()
                print "Added to DB: " + str(f)
                return render_to_response(
                    "errors/upload_success.html", locals(), context_instance=RequestContext(request)
                )
            else:
                return render_to_response(
                    "errors/file_already_exists.html", locals(), context_instance=RequestContext(request)
                )

        elif f_ext in [".mp4", ".MP4", ".m4v", ".M4V"]:
            if not os.path.exists(MEDIA_ROOT + "videos/" + str(f)):
                with open(MEDIA_ROOT + "videos/" + str(f), "wb") as destination:
                    for chunk in f.chunks():
                        destination.write(chunk)
            else:  # File already exists. Remove the duplicate copy.
                return render_to_response(
                    "errors/file_already_exists.html", locals(), context_instance=RequestContext(request)
                )

            # If the video doesn't exist in the database, add it.
            if not Video.objects.filter(title=str(f)).exists():
                video = Video(
                    title=os.path.splitext(str(f))[0],
                    filename=str(f),
                    fspath=MEDIA_ROOT + "videos/" + str(f),
                    media_url=MEDIA_URL + (MEDIA_ROOT + "videos/" + str(f)).split(MEDIA_ROOT)[1],
                )
                video.save()
                print "Added to DB: " + str(f)
                return render_to_response(
                    "errors/upload_success.html", locals(), context_instance=RequestContext(request)
                )
            else:
                return render_to_response(
                    "errors/file_already_exists.html", locals(), context_instance=RequestContext(request)
                )

    return render_to_response("errors/upload_failure.html", locals(), context_instance=RequestContext(request))