Esempio n. 1
0
def rescanLibrary(request):
    if request.method == 'POST':
        response = json.loads(request.body)
        if 'LIBRARY_ID' in response:
            library = strip_tags(response['LIBRARY_ID'])
            if Library.objects.filter(id=library).count() == 1:
                library = Library.objects.get(id=library)

                # Check if the library is not used somewhere else
                if library.playlist.isScanned:
                    # Delete all the old tracks
                    library.playlist.delete()

                    # Recreating playlist
                    playlist = Playlist()
                    playlist.name = library.name
                    playlist.user = request.user
                    playlist.isLibrary = True
                    playlist.save()
                    library.playlist = playlist
                    library.save()

                    # Scan library
                    data = scanLibrary(library, playlist, library.convertID3)
                else:
                    data = errorCheckMessage(False, "rescanError")
            else:
                data = errorCheckMessage(False, "dbError")
        else:
            data = errorCheckMessage(False, "badFormat")
    else:
        data = errorCheckMessage(False, "badRequest")
    return JsonResponse(data)
Esempio n. 2
0
def newLibrary(request):
    if request.method == 'POST':
        user = request.user
        if checkPermission(["LIBR"], user):
            response = json.loads(request.body)
            if 'URL' in response and 'NAME' in response:
                dirPath = response['URL']
                if os.path.isdir(dirPath):
                    # Removing / at the end of the dir path if present
                    if dirPath.endswith("/"):
                        dirPath = dirPath[:-1]
                    library = Library()
                    library.path = dirPath
                    playlist = Playlist()
                    playlist.user = user
                    playlist.isLibrary = True
                    playlist.name = strip_tags(response['NAME'])
                    playlist.save()
                    library.playlist = playlist
                    library.save()
                    data = {
                        'LIBRARY_ID': library.id,
                        'LIBRARY_NAME': library.playlist.name,
                    }
                    data = {**data, **errorCheckMessage(True, None)}
                else:
                    data = errorCheckMessage(False, "dirNotFound")
            else:
                data = errorCheckMessage(False, "badFormat")
        else:
            data = errorCheckMessage(False, "permissionError")
    else:
        data = errorCheckMessage(False, "badRequest")
    return JsonResponse(data)
Esempio n. 3
0
def newPlaylist(request):
    if request.method == 'POST':
        response = json.loads(request.body)
        if 'NAME' in response:
            playlist = Playlist()
            playlist.name = strip_tags(response['NAME'])
            playlist.user = request.user
            playlist.save()
            data = {
                'PLAYLIST_ID': playlist.id,
                'NAME': playlist.name,
            }
            data = {**data, **errorCheckMessage(True, None)}
        else:
            data = errorCheckMessage(False, "badFormat")
    else:
        data = errorCheckMessage(False, "badRequest")
    return JsonResponse(data)
Esempio n. 4
0
def newLibrary(request):
    if request.method == 'POST':
        user = request.user
        if checkPermission(["LIBR"], user):
            response = json.loads(request.body)
            if 'URL' in response and 'NAME' in response:
                dirPath = response['URL']
                if os.path.isdir(dirPath):
                    # Removing / at the end of the dir path if present
                    if dirPath.endswith("/"):
                        dirPath = dirPath[:-1]
                    library = Library()
                    library.path = dirPath
                    playlist = Playlist()
                    playlist.user = user
                    playlist.isLibrary = True
                    playlist.name = strip_tags(response['NAME'])
                    playlist.save()
                    library.playlist = playlist
                    library.save()
                    data = {
                        'INFO': {
                            'ID': library.id,
                            'NAME': library.playlist.name,
                            'DESCRIPTION': library.playlist.description,
                            'IS_PUBLIC': library.playlist.isPublic,
                            'IS_LIBRARY': library.playlist.isLibrary,
                            'TOTAL_TRACK': "TO BE IMPLEMENTED",
                            'TOTAL_DURATION': "TO BE IMPLEMENTED",
                            'AVERAGE_BITRATE': "TO BE IMPLEMENTED",
                            'OWNER': library.playlist.user.username
                        }
                    }
                    data = {**data, **errorCheckMessage(True, None, newLibrary)}
                else:
                    data = errorCheckMessage(False, ErrorEnum.DIR_NOT_FOUND, newLibrary)
            else:
                data = errorCheckMessage(False, ErrorEnum.BAD_FORMAT, newLibrary, user)
        else:
            data = errorCheckMessage(False, ErrorEnum.PERMISSION_ERROR, newLibrary, user)
    else:
        data = errorCheckMessage(False, ErrorEnum.BAD_REQUEST, newLibrary)
    return JsonResponse(data)
Esempio n. 5
0
def initialScan(request):
    print("Asked for initial scan")
    if request.method == 'POST':
        response = json.loads(request.body)
        if 'LIBRARY_ID' in response:
            library = Library.objects.get(id=response['LIBRARY_ID'])
            if os.path.isdir(library.path):
                playlist = Playlist()
                playlist.name = library.name
                playlist.user = request.user
                playlist.isLibrary = True
                playlist.save()
                data = scanLibrary(library, playlist, library.convertID3)
            else:
                data = errorCheckMessage(False, "dirNotFound")
        else:
            data = errorCheckMessage(False, "badFormat")
    else:
        data = errorCheckMessage(False, "badRequest")
    return JsonResponse(data)
Esempio n. 6
0
def newPlaylist(request):
    if request.method == 'POST':
        response = json.loads(request.body)
        user = request.user
        if checkPermission(["PLST"], user):
            if 'PLAYLIST_NAME' in response:
                playlist = Playlist()
                playlist.name = strip_tags(response['PLAYLIST_NAME'])
                playlist.user = request.user
                playlist.save()
                data = {
                    'PLAYLIST_ID': playlist.id,
                    'PLAYLIST_NAME': playlist.name,
                }
                data = {**data, **errorCheckMessage(True, None, newPlaylist)}
            else:
                data = errorCheckMessage(False, ErrorEnum.BAD_FORMAT, newPlaylist, user)
        else:
            data = errorCheckMessage(False, ErrorEnum.PERMISSION_ERROR, newPlaylist, user)
    else:
        data = errorCheckMessage(False, ErrorEnum.BAD_REQUEST, newPlaylist)
    return JsonResponse(data)