def isAdmin(request): if request.method == 'GET': data = {'IS_ADMIN': request.user.is_superuser} data = {**data, **errorCheckMessage(True, None, isAdmin)} else: data = errorCheckMessage(False, ErrorEnum.BAD_REQUEST, isAdmin) return JsonResponse(data)
def isInviteEnabled(request): if request.method == 'GET': data = {'INVITE': getAdminOptions().inviteCodeEnabled} data = {**data, **errorCheckMessage(True, None, isInviteEnabled)} else: data = errorCheckMessage(False, ErrorEnum.BAD_REQUEST, isInviteEnabled) return JsonResponse(data)
def getTracksDetailedInfo(request): if request.method == 'POST': response = json.loads(request.body) user = request.user if checkPermission(["PLAY"], user): if 'TRACK_ID' in response: trackIds = response['TRACK_ID'] trackInfo = [] for trackId in trackIds: if Track.objects.filter(id=trackId).count() == 1: trackInfo.append( exportTrackInfo(Track.objects.get(id=trackId))) else: data = errorCheckMessage(False, ErrorEnum.DB_ERROR, getTracksDetailedInfo) return JsonResponse(data) data = { **dict({'RESULT': trackInfo}), **errorCheckMessage(True, None, getTracksDetailedInfo) } else: data = errorCheckMessage(False, ErrorEnum.BAD_FORMAT, getTracksDetailedInfo, user) else: data = errorCheckMessage(False, ErrorEnum.PERMISSION_ERROR, getTracksDetailedInfo, user) else: data = errorCheckMessage(False, ErrorEnum.BAD_REQUEST, getTracksDetailedInfo) return JsonResponse(data)
def toggleRandom(request): if request.method == 'POST': user = request.user response = json.loads(request.body) if 'RANDOM_MODE' in response and 'PLAYLIST_ID' in response: randomMode = strip_tags(response['RANDOM_MODE']) randomMode = int(randomMode) if randomMode in range(0, 3): playlistId = strip_tags(response['PLAYLIST_ID']) if Playlist.objects.filter(id=playlistId).count() == 1: playlist = Playlist.objects.get(id=playlistId) if PlaylistSettings.objects.filter(playlist=playlist, user=user).count() == 1: settings = PlaylistSettings.objects.get(playlist=playlist, user=user) else: settings = PlaylistSettings() settings.user = user # TODO : change to right view mode settings.viewMode = 0 settings.playlist = playlist settings.randomMode = randomMode settings.save() data = errorCheckMessage(True, None, toggleRandom) else: data = errorCheckMessage(False, ErrorEnum.DB_ERROR, toggleRandom) else: data = errorCheckMessage(False, ErrorEnum.BAD_FORMAT, toggleRandom, user) else: data = errorCheckMessage(False, ErrorEnum.BAD_FORMAT, toggleRandom, user) else: data = errorCheckMessage(False, ErrorEnum.BAD_REQUEST, toggleRandom) return JsonResponse(data)
def randomNextTrack(request): if request.method == 'POST': response = json.loads(request.body) user = request.user if checkPermission(["PLAY"], user): if 'PLAYLIST_ID' in response: playlistID = strip_tags(response['PLAYLIST_ID']) if Playlist.objects.filter(id=playlistID).count() == 1: playlist = Playlist.objects.get(id=playlistID) rangeRand = playlist.track.count() selectedTrack = randint(0, rangeRand) count = 0 for track in playlist.track.all(): if count == selectedTrack: data = { 'TRACK_ID': track.id, } data = {**data, **errorCheckMessage(True, None, randomNextTrack)} return JsonResponse(data) else: count += 1 data = errorCheckMessage(False, ErrorEnum.DB_ERROR, randomNextTrack) else: data = errorCheckMessage(False, ErrorEnum.BAD_FORMAT, randomNextTrack, user) else: data = errorCheckMessage(False, ErrorEnum.PERMISSION_ERROR, randomNextTrack, user) else: data = errorCheckMessage(False, ErrorEnum.BAD_REQUEST, randomNextTrack) return JsonResponse(data)
def addTracksToPlaylist(request): if request.method == 'POST': response = json.loads(request.body) user = request.user if 'PLAYLIST_ID' in response and 'TRACKS_ID' in response: tracksId = response['TRACKS_ID'] playlistId = strip_tags(response['PLAYLIST_ID']) if Playlist.objects.filter(id=playlistId).count() == 1: playlist = Playlist.objects.get(id=playlistId) # Checking permissions if not playlist.isLibrary and checkPermission(["PLST"], user) and playlist.user == user: tracks = Track.objects.filter(id__in=tracksId) for track in tracks: playlist.track.add(track) # Set rescan flag for view generation playlist.refreshView = True playlist.save() data = errorCheckMessage(True, None, addTracksToPlaylist) else: data = errorCheckMessage(False, ErrorEnum.PERMISSION_ERROR, addTracksToPlaylist, user) else: data = errorCheckMessage(False, ErrorEnum.DB_ERROR, addTracksToPlaylist) else: data = errorCheckMessage(False, ErrorEnum.BAD_FORMAT, addTracksToPlaylist, user) else: data = errorCheckMessage(False, ErrorEnum.BAD_REQUEST, addTracksToPlaylist) return JsonResponse(data)
def getTracksFromSameAlbum(request): if request.method == 'POST': response = json.loads(request.body) user = request.user if 'TRACK_ID' in response: trackId = strip_tags(response['TRACK_ID']) if Track.objects.filter(id=trackId).count() == 1: originalTrack = Track.objects.get(id=trackId) sameTracks = Track.objects.filter(album=originalTrack.album).exclude(id=originalTrack.id) if sameTracks.count() > 0: trackIds = [] for track in sameTracks: trackIds.append(track.id) data = { 'TRACKS': trackIds } data = {**data, **errorCheckMessage(True, None, getTracksFromSameAlbum)} else: data = errorCheckMessage(False, ErrorEnum.NO_SAME_ALBUM, getTracksFromSameAlbum) else: data = errorCheckMessage(False, ErrorEnum.DB_ERROR, getTracksFromSameAlbum) else: data = errorCheckMessage(False, ErrorEnum.BAD_FORMAT, getTracksFromSameAlbum, user) else: data = errorCheckMessage(False, ErrorEnum.BAD_REQUEST, getTracksFromSameAlbum) return JsonResponse(data)
def renamePlaylist(request): if request.method == 'POST': user = request.user response = json.loads(request.body) if 'PLAYLIST_ID' in response and 'PLAYLIST_NAME' in response: playlistId = strip_tags(response['PLAYLIST_ID']) if Playlist.objects.filter(id=playlistId, user=user).count() == 1: playlist = Playlist.objects.get(id=playlistId, user=user) if (checkPermission(["PLST"], user) and not playlist.isLibrary) \ or (playlist.isLibrary and checkPermission(["LIBR"], user)): deleteView(playlist) playlist.name = strip_tags(response['PLAYLIST_NAME']) playlist.refreshView = True playlist.save() data = { 'PLAYLIST_ID': playlist.id, 'PLAYLIST_NAME': playlist.name, } data = {**data, **errorCheckMessage(True, None, renamePlaylist)} else: data = errorCheckMessage(False, ErrorEnum.PERMISSION_ERROR, renamePlaylist, user) else: data = errorCheckMessage(False, ErrorEnum.PERMISSION_ERROR, renamePlaylist, user) else: data = errorCheckMessage(False, ErrorEnum.BAD_FORMAT, renamePlaylist, user) else: data = errorCheckMessage(False, ErrorEnum.BAD_REQUEST, renamePlaylist) return JsonResponse(data)
def getDownloadLocation(request): if request.method == 'POST': response = json.loads(request.body) user = request.user if checkPermission(["DOWN"], user): if 'TRACK_ID' in response: trackId = strip_tags(response['TRACK_ID']) if Track.objects.filter(id=trackId).count() == 1: track = Track.objects.get(id=trackId) track.downloadCounter += 1 track.save() data = { 'DOWNLOAD_PATH': track.location, } data = { **data, **errorCheckMessage(True, None, getDownloadLocation) } else: data = errorCheckMessage(False, ErrorEnum.DB_ERROR, getDownloadLocation) else: data = errorCheckMessage(False, ErrorEnum.BAD_FORMAT, getDownloadLocation, user) else: data = errorCheckMessage(False, ErrorEnum.PERMISSION_ERROR, getDownloadLocation, user) else: data = errorCheckMessage(False, ErrorEnum.BAD_REQUEST, getDownloadLocation) return JsonResponse(data)
def changeAvatar(request): if request.method == 'POST': response = json.loads(request.body) user = request.user if str(response['AVATAR'].split(",")[0]) == "image/png": extension = ".png" else: extension = ".jpg" username_hash = hashlib.md5(user.username.encode("utf-8")).hexdigest() avatar_path = "static/img/avatars/" + username_hash + extension # if only one user with that username is found if UserPreferences.objects.filter(user=user).count() == 1: userPref = UserPreferences.objects.get(user=user) userPref.avatar = avatar_path userPref.save() with open(avatar_path, 'wb') as destination: img_b64 = str(response['AVATAR'].split(",")[1]) destination.write(base64.b64decode(img_b64)) data = errorCheckMessage(True, None, changeAvatar) else: data = errorCheckMessage(False, ErrorEnum.DB_ERROR, changeAvatar) else: data = errorCheckMessage(False, ErrorEnum.BAD_REQUEST, changeAvatar) return JsonResponse(data)
def loadLanguage(request): if request.method == 'POST': response = json.loads(request.body) user = request.user pathToLang = "/ManaZeak/languages/" if 'LANG' in response: language = response['LANG'] # If the user tweaked the request if "/" not in language or "\\" not in language: jsonFile = os.path.join(pathToLang, language + ".json") # If the local is not found using a default one if not os.path.isfile(jsonFile): jsonFile = os.path.join(pathToLang, "en.json") with open(jsonFile) as file: data = json.load(file) data = {**data, **errorCheckMessage(True, None, loadLanguage)} else: data = errorCheckMessage(False, ErrorEnum.SUSPICIOUS_OPERATION, loadLanguage, user) else: data = errorCheckMessage(False, ErrorEnum.BAD_FORMAT, loadLanguage, user) else: data = errorCheckMessage(False, ErrorEnum.BAD_REQUEST, loadLanguage) return JsonResponse(data)
def getMoodbar(request): if request.method == 'POST': response = json.loads(request.body) user = request.user if checkPermission(["PLAY"], user): if 'TRACK_ID' in response: trackID = response['TRACK_ID'] if Track.objects.filter(id=trackID).count() == 1: track = Track.objects.get(id=trackID) data = { 'TRACK_MOOD': track.moodbar, } data = { **data, **errorCheckMessage(True, None, getMoodbar) } else: data = errorCheckMessage(False, ErrorEnum.DB_ERROR, getMoodbar) else: data = errorCheckMessage(False, ErrorEnum.BAD_FORMAT, getMoodbar, user) else: data = errorCheckMessage(False, ErrorEnum.PERMISSION_ERROR, getMoodbar, user) else: data = errorCheckMessage(False, ErrorEnum.BAD_REQUEST, getMoodbar) return JsonResponse(data)
def shuffleNextTrack(request): if request.method == 'POST': response = json.loads(request.body) user = request.user if checkPermission(["PLAY"], user): if 'PLAYLIST_ID' in response: if Shuffle.objects.filter(playlist=Playlist.objects.get(id=response['PLAYLIST_ID']), user=request.user).count() == 1: shuffle = Shuffle.objects.get(playlist=Playlist.objects.get(id=response['PLAYLIST_ID']), user=request.user) else: shuffle = Shuffle(playlist=Playlist.objects.get(id=response['PLAYLIST_ID']), user=request.user) shuffle.save() track, playlistEnd = shuffleSoundSelector(shuffle) shuffle.tracksPlayed.add(track) shuffle.save() if Track.objects.filter(id=track.id).count() == 1: data = { 'TRACK_ID': track.id, 'IS_LAST': playlistEnd, } data = {**data, **errorCheckMessage(True, None, shuffleNextTrack)} else: data = errorCheckMessage(False, ErrorEnum.DB_ERROR, shuffleNextTrack) else: data = errorCheckMessage(False, ErrorEnum.BAD_FORMAT, shuffleNextTrack, user) else: data = errorCheckMessage(False, ErrorEnum.PERMISSION_ERROR, shuffleNextTrack, user) else: data = errorCheckMessage(False, ErrorEnum.BAD_REQUEST, shuffleNextTrack) return JsonResponse(data)
def getLastSongPlayed(request): if request.method == 'GET': user = request.user if checkPermission(["PLAY"], user): if UserHistory.objects.filter(user=user).count() != 0: userHistory = UserHistory.objects.get(user=user) if userHistory.histories.count() != 0: trackId = 0 for history in userHistory.histories.order_by('-date'): trackId = history.track.id history.delete() break data = { 'TRACK_ID': trackId, } data = { **data, **errorCheckMessage(True, None, getLastSongPlayed) } else: data = errorCheckMessage(False, ErrorEnum.NO_HISTORY, getLastSongPlayed, user) else: data = errorCheckMessage(False, ErrorEnum.NO_HISTORY, getLastSongPlayed, user) else: data = errorCheckMessage(False, ErrorEnum.PERMISSION_ERROR, getLastSongPlayed, user) else: data = errorCheckMessage(False, ErrorEnum.BAD_REQUEST, getLastSongPlayed) return JsonResponse(data)
def changeBufferPath(request): if request.method == 'POST': user = request.user if checkPermission(["ADMV"], user): response = json.loads(request.body) if 'BUFFER_PATH' in response: adminOptions = getAdminOptions() bufferPath = strip_tags(response['BUFFER_PATH']) if os.path.isdir(bufferPath): adminOptions.bufferPath = bufferPath adminOptions.save() data = errorCheckMessage(True, None, changeBufferPath) else: data = errorCheckMessage(False, ErrorEnum.DIR_NOT_FOUND, changeBufferPath) else: data = errorCheckMessage(False, ErrorEnum.BAD_FORMAT, changeBufferPath, user) else: data = errorCheckMessage(False, ErrorEnum.PERMISSION_ERROR, changeBufferPath, user) else: data = errorCheckMessage(False, ErrorEnum.BAD_REQUEST, changeBufferPath) return JsonResponse(data)
def getUserInformation(request): if request.method == 'GET': user = request.user userPref = UserPreferences.objects.get(user=user) inviteCode = InviteCode.objects.get(user=user).code data = { 'USER_ID': user.id, 'USERNAME': user.username, 'IS_ADMIN': user.is_superuser, 'GROUP_NAME': userPref.group.name, 'GROUP_ID': userPref.group.id, 'INVITE_CODE': inviteCode, 'AVATAR_PATH': userPref.avatar, } if userPref.inviteCode is not None: data = {**data, **{ 'GODFATHER_CODE': userPref.inviteCode.code, 'GODFATHER_NAME': userPref.inviteCode.user.username, }} else: data = {**data, **{ 'GODFATHER_CODE': "Christ", 'GODFATHER_NAME': "Jesus", }} permissions = [] for permission in userPref.group.permissions.all(): permissions.append(permission.code) data = {**data, **{'PERMISSIONS': permissions}} data = {**data, **errorCheckMessage(True, None, getUserInformation)} else: data = errorCheckMessage(False, ErrorEnum.BAD_REQUEST, getUserInformation) return JsonResponse(data)
def deleteUser(request): if request.method == 'GET': user = request.user deleteLinkedEntities(user) user.delete() data = errorCheckMessage(True, None, deleteUser) else: data = errorCheckMessage(False, ErrorEnum.BAD_REQUEST, deleteUser) return JsonResponse(data)
def checkNamingConventionArtistsOnPlaylist(request): data = {} if request.method == 'POST': user = request.user if user.is_superuser: response = json.loads(request.body) if 'PLAYLIST_ID' in response: playlistId = response['PLAYLIST_ID'] data = set() if Playlist.objects.filter(id=playlistId).count() == 1: tracks = Playlist.objects.get(id=playlistId).track.all() for track in tracks: path, fileName = os.path.split(track.location) splicedName = fileName.split(" - ") # Extracting artists artists = splicedName[0] splicedArtists = artists.split(",") # Checking if the artists are in a good order for i in range(len(splicedArtists) - 1): artist1 = splicedArtists[i].rstrip().lstrip() artist2 = splicedArtists[i + 1].rstrip().lstrip() if artist1[0] > artist2[0]: data.add(track) break # Checking if the title contains caps at the beginning of each word fileName = splicedName[1] words = fileName.split(" ") for word in words: if not word[0].isupper(): data.add(track) break # The tracks contains a featuring if "(feat." in fileName: feats = fileName.split("feat.") for i in range(len(feats) - 1): artist1 = feats[i].rstrip().lstrip() artist2 = feats[i + 1].rstrip().lstrip() if artist1[0] > artist2[0]: data.add(track) break data = dict({'RESULT': data}) data = {**data, **errorCheckMessage(True, None)} else: data = errorCheckMessage( False, ErrorEnum.DB_ERROR, checkNamingConventionArtistsOnPlaylist) else: data = errorCheckMessage(False, ErrorEnum.PERMISSION_ERROR, checkNamingConventionArtistsOnPlaylist, user) else: data = errorCheckMessage(False, ErrorEnum.BAD_REQUEST, checkNamingConventionArtistsOnPlaylist) return JsonResponse(data)
def getTrackPath(request): if request.method == 'POST': response = json.loads(request.body) user = request.user if checkPermission(["PLAY"], user): # Checking JSON keys if 'TRACK_ID' in response and 'PREVIOUS' in response and 'LAST_TRACK_PATH' in response \ and 'TRACK_PERCENTAGE' in response: trackId = strip_tags(response['TRACK_ID']) # Getting the track asked if Track.objects.filter(id=trackId).count() == 1: track = Track.objects.get(id=trackId) # If we don't ask a previous track if not bool(response['PREVIOUS']): # Adding the current track to the history # Removing the first 2 chars previousTrackPath = strip_tags( response['LAST_TRACK_PATH'])[2:] # If the previous track exists if Track.objects.filter( location=previousTrackPath).count() == 1: listeningPercentage = float( strip_tags(response['TRACK_PERCENTAGE'])) previousTrack = Track.objects.get( location=previousTrackPath) checkListeningGain(previousTrack, user) # Adding to stats if the user has listened more than 15% of the song if listeningPercentage > 5: previousTrack.playCounter += 1 previousTrack.save() addToStats(previousTrack, listeningPercentage, user) addToHistory(track, user) # Returning the asked song data = { 'TRACK_PATH': track.location, } data = { **data, **errorCheckMessage(True, None, getTrackPath) } else: data = errorCheckMessage(False, ErrorEnum.DB_ERROR, getTrackPath) else: data = errorCheckMessage(False, ErrorEnum.BAD_FORMAT, getTrackPath, user) else: data = errorCheckMessage(False, ErrorEnum.PERMISSION_ERROR, getTrackPath, user) else: data = errorCheckMessage(False, ErrorEnum.BAD_REQUEST, getTrackPath) return JsonResponse(data)
def rescanAllLibraries(request): if request.method == 'GET': user = request.user if checkPermission(["LIBR"], user): scanThread = Process(target=rescanLibraryProcess, args=[None]) db.connections.close_all() scanThread.start() data = errorCheckMessage(True, None, rescanAllLibraries) else: data = errorCheckMessage(False, ErrorEnum.PERMISSION_ERROR, rescanAllLibraries, user) else: data = errorCheckMessage(False, ErrorEnum.BAD_REQUEST, rescanAllLibraries) return JsonResponse(data)
def removeAllMoods(request): if request.method == 'GET': user = request.user if checkPermission(["ADMV"], user): moodbars = "/ManaZeak/static/mood/" for mood in os.listdir(moodbars): os.remove(os.path.join(moodbars, mood)) data = errorCheckMessage(True, None, removeAllMoods) else: data = errorCheckMessage(False, ErrorEnum.PERMISSION_ERROR, removeAllMoods, user) else: data = errorCheckMessage(False, ErrorEnum.BAD_REQUEST, removeAllMoods) return JsonResponse(data)
def toggleInvite(request): if request.method == 'GET': user = request.user if checkPermission(["ADMV"], user): adminOptions = getAdminOptions() adminOptions.inviteCodeEnabled = not adminOptions.inviteCodeEnabled adminOptions.save() data = {'INVITE': adminOptions.inviteCodeEnabled} data = {**data, **errorCheckMessage(True, None, toggleInvite)} else: data = errorCheckMessage(False, ErrorEnum.PERMISSION_ERROR, toggleInvite) else: data = errorCheckMessage(False, ErrorEnum.BAD_REQUEST, toggleInvite) return JsonResponse(data)
def refreshCrontab(request): if request.method == 'GET': user = request.user if checkPermission(['LIBR'], user): cron = CronTab("root") for job in cron: cron.remove(job) setCronJobs() data = errorCheckMessage(True, None, refreshCrontab) else: data = errorCheckMessage(False, ErrorEnum.PERMISSION_ERROR, refreshCrontab, user) else: data = errorCheckMessage(False, ErrorEnum.BAD_REQUEST, refreshCrontab) return JsonResponse(data)
def deleteAllLibrary(request): if request.method == 'GET': user = request.user if checkPermission(["LIBR"], user): libraries = Library.objects.all() for library in libraries: library.playlist.track.all().delete() library.playlist.delete() library.delete() for playlist in Playlist.objects.all(): playlist.delete() data = errorCheckMessage(True, None, deleteAllLibrary) else: data = errorCheckMessage(False, ErrorEnum.PERMISSION_ERROR, deleteAllLibrary, user) else: data = errorCheckMessage(False, ErrorEnum.BAD_REQUEST, deleteAllLibrary) return JsonResponse(data)
def getUserPrefTracks(request): if request.method == 'GET': user = request.user if checkPermission(["STAT"], user): statsPref = Stats.objects.filter(user=user).order_by( '-playCounter', '-listeningPercentage') statsLeast = Stats.objects.filter(user=user).order_by( 'playCounter', 'listeningPercentage') trackTuplePref = [] trackTupleLeast = [] for stat in statsPref: if stat.listeningPercentage is not None: trackTuplePref.append( (stat.track.title, stat.playCounter, stat.listeningPercentage / stat.playCounter)) else: trackTuplePref.append( (stat.track.title, stat.playCounter, 0)) for stat in statsLeast: if stat.listeningPercentage is not None: trackTupleLeast.append( (stat.track.title, stat.playCounter, stat.listeningPercentage / stat.playCounter)) else: trackTupleLeast.append( (stat.track.title, stat.playCounter, 0)) if len(trackTuplePref) == 0: data = errorCheckMessage(True, ErrorEnum.NO_STATS, getUserPrefTracks) else: data = { 'PREF_TRACKS': trackTuplePref[:100], 'LEAST_TRACKS': trackTupleLeast[:100], } data = { **data, **errorCheckMessage(True, None, getUserPrefTracks) } else: data = errorCheckMessage(False, ErrorEnum.PERMISSION_ERROR, getUserPrefTracks, user) else: data = errorCheckMessage(False, ErrorEnum.BAD_REQUEST, getUserPrefTracks) return JsonResponse(data)
def removeUser(request): if request.method == 'POST': user = request.user if checkPermission(["ADMV"], user): response = json.loads(request.body) if 'USER_ID' in response: try: userId = int(strip_tags(response['USER_ID'])) if userId != user.id: if User.objects.filter(id=userId).count() == 1: user = User.objects.get(id=userId) deleteLinkedEntities(user) user.delete() data = errorCheckMessage(True, None, removeUser) else: data = errorCheckMessage(False, ErrorEnum.DB_ERROR, removeUser) else: data = errorCheckMessage(False, ErrorEnum.USER_DELETE_ERROR, removeUser, user) except ValueError: data = errorCheckMessage(False, ErrorEnum.VALUE_ERROR, removeUser, user) else: data = errorCheckMessage(False, ErrorEnum.BAD_FORMAT, removeUser, user) else: data = errorCheckMessage(False, ErrorEnum.PERMISSION_ERROR, removeUser, user) else: data = errorCheckMessage(False, ErrorEnum.BAD_REQUEST, removeUser) return JsonResponse(data)
def rescanLibraryRequest(request): if request.method == 'POST': response = json.loads(request.body) user = request.user if checkPermission(["LIBR"], user): if 'LIBRARY_ID' in response: libraryId = strip_tags(response['LIBRARY_ID']) scanThread = Process(target=rescanLibraryProcess, args=libraryId) db.connections.close_all() scanThread.start() data = errorCheckMessage(True, None, rescanLibraryRequest) else: data = errorCheckMessage(False, ErrorEnum.BAD_FORMAT, rescanLibraryRequest, user) else: data = errorCheckMessage(False, ErrorEnum.PERMISSION_ERROR, rescanLibraryRequest, user) else: data = errorCheckMessage(False, ErrorEnum.BAD_REQUEST, rescanLibraryRequest) return JsonResponse(data)
def checkLibraryScanStatus(request): if request.method == 'POST': response = json.loads(request.body) user = request.user if checkPermission(["LIBR"], user): if 'PLAYLIST_ID' in response: if Playlist.objects.filter(id=response['PLAYLIST_ID']).count() == 1: playlist = Playlist.objects.get(id=response['PLAYLIST_ID']) data = errorCheckMessage(playlist.isScanned, None, newLibrary) else: data = errorCheckMessage(False, ErrorEnum.DB_ERROR, checkLibraryScanStatus) else: data = errorCheckMessage(False, ErrorEnum.BAD_FORMAT, checkLibraryScanStatus, user) else: data = errorCheckMessage(False, ErrorEnum.PERMISSION_ERROR, checkLibraryScanStatus, user) else: data = errorCheckMessage(False, ErrorEnum.BAD_REQUEST, checkLibraryScanStatus) return JsonResponse(data)
def syncthingRescan(request): if request.method == 'GET': user = request.user if checkPermission(["ADMV"], user): headers = { 'X-API-Key': AdminOptions.objects.all().first().syncthingKey } req = requests.post('http://st:8384/rest/db/scan', headers=headers) if req.status_code == 200: data = errorCheckMessage(True, None, syncthingRescan) else: data = errorCheckMessage(False, ErrorEnum.SYNCTHING_ERROR, syncthingRescan) else: data = errorCheckMessage(False, ErrorEnum.PERMISSION_ERROR, syncthingRescan, user) else: data = errorCheckMessage(False, ErrorEnum.BAD_REQUEST, syncthingRescan) return JsonResponse(data)
def getPlaylistDescription(request): if request.method == 'POST': user = request.user response = json.loads(request.body) if 'PLAYLIST_ID' in response: playlistId = response['PLAYLIST_ID'] if Playlist.objects.filter(id=playlistId).count() == 1: playlist = Playlist.objects.get(id=playlistId) data = { 'PLAYLIST_DESC': playlist.description } data = {{**data, **errorCheckMessage(True, None, getPlaylistDescription)}} else: data = errorCheckMessage(False, ErrorEnum.DB_ERROR, getPlaylistDescription) else: data = errorCheckMessage(False, ErrorEnum.BAD_FORMAT, getPlaylistDescription, user) else: data = errorCheckMessage(False, ErrorEnum.BAD_REQUEST, getPlaylistDescription) return JsonResponse(data)