def ajax_upload_samples_as_version(request): """ Upload some samples and then add each uploaded sample to a new project version. """ project = get_obj_from_request(request.POST, 'project', Project) comments = request.POST.get('comments', '') if project is None: return json_failure(design.bad_project_id) band = project.band if not band.permission_to_work(request.user): return json_failure( design.you_dont_have_permission_to_work_on_this_band) if band.is_read_only(): return json_failure(design.band_in_readonly_mode) node = SongCommentNode() node.owner = request.user node.content = comments node.save() version = ProjectVersion() version.project = project version.owner = request.user version.comment_node = node version.version = project.latest_version.version # no +1, only adding samples. version.save() # so we can add provided_samples node.version = version node.save() def add_sample_to_version(sample): version.provided_samples.add(sample) files = request.FILES.getlist('file') for item in files: handle_sample_upload(item, request.user, band, callback=add_sample_to_version) band.save() version.save() version.makeLogEntry() return json_success()
def ajax_rename_project(request): """ Make a new version that renames the project. """ form = RenameProjectForm(request.POST) if form.is_valid(): new_title = form.cleaned_data.get('title') comments = form.cleaned_data.get('comments', '') project_id = form.cleaned_data.get('project') try: project = Project.objects.get(pk=project_id) except Project.DoesNotExist: return json_failure(design.bad_project_id) if not project.band.permission_to_work(request.user): return json_failure( design.you_dont_have_permission_to_work_on_this_band) if project.band.is_read_only(): return json_failure(design.band_in_readonly_mode) node = SongCommentNode() node.owner = request.user node.content = comments node.save() version = ProjectVersion() version.project = project version.owner = request.user version.comment_node = node version.version = project.latest_version.version # no +1 because only renaming version.new_title = new_title version.old_title = project.title version.save() node.version = version node.save() project.title = new_title project.save() return json_success() return json_failure(form_errors(form))
def ajax_upload_samples_as_version(request): """ Upload some samples and then add each uploaded sample to a new project version. """ project = get_obj_from_request(request.POST, 'project', Project) comments = request.POST.get('comments', '') if project is None: return json_failure(design.bad_project_id) band = project.band if not band.permission_to_work(request.user): return json_failure(design.you_dont_have_permission_to_work_on_this_band) if band.is_read_only(): return json_failure(design.band_in_readonly_mode) node = SongCommentNode() node.owner = request.user node.content = comments node.save() version = ProjectVersion() version.project = project version.owner = request.user version.comment_node = node version.version = project.latest_version.version # no +1, only adding samples. version.save() # so we can add provided_samples node.version = version node.save() def add_sample_to_version(sample): version.provided_samples.add(sample) files = request.FILES.getlist('file') for item in files: handle_sample_upload(item, request.user, band, callback=add_sample_to_version) band.save() version.save() version.makeLogEntry() return json_success()
def ajax_rename_project(request): """ Make a new version that renames the project. """ form = RenameProjectForm(request.POST) if form.is_valid(): new_title = form.cleaned_data.get('title') comments = form.cleaned_data.get('comments', '') project_id = form.cleaned_data.get('project') try: project = Project.objects.get(pk=project_id) except Project.DoesNotExist: return json_failure(design.bad_project_id) if not project.band.permission_to_work(request.user): return json_failure(design.you_dont_have_permission_to_work_on_this_band) if project.band.is_read_only(): return json_failure(design.band_in_readonly_mode) node = SongCommentNode() node.owner = request.user node.content = comments node.save() version = ProjectVersion() version.project = project version.owner = request.user version.comment_node = node version.version = project.latest_version.version # no +1 because only renaming version.new_title = new_title version.old_title = project.title version.save() node.version = version node.save() project.title = new_title project.save() return json_success() return json_failure(form_errors(form))
def ajax_comment(request): parent = get_obj_from_request(request.POST, 'parent', SongCommentNode) if parent is None: return json_failure(design.bad_song_comment_node_id) # make sure the user has permission to critique if parent.song is not None: if not parent.song.permission_to_critique(request.user): return json_failure(design.you_dont_have_permission_to_comment) else: if not parent.version.project.band.permission_to_critique(request.user): return json_failure(design.you_dont_have_permission_to_comment) # make sure the parent has enabled replies if parent.reply_disabled: return json_failure(design.comments_disabled_for_this_version) position = request.POST.get('position') if position is not None: try: position = float(position) except ValueError: position = None if position < 0 or position > parent.song.length: return json_failure(design.invalid_position) content = request.POST.get('content') if len(content) == 0 or len(content) > 2000: return json_failure(design.content_wrong_length) node = SongCommentNode() node.song = parent.song node.version = parent.version node.parent = parent node.owner = request.user node.content = content node.position = position node.reply_disabled = False node.save() return json_success(node.to_dict())
def upload_song(user, file_mp3_handle=None, file_source_handle=None, max_song_len=None, band=None, song_title=None, song_album="", song_comments="", filename_appendix=""): """ inputs: user: the person uploading stuff file_mp3_handle: the handle from the form file_source_handle: the handle from the form. need to also pass user if you want this to work. max_song_len: how many seconds long a song can be. None means unlimited. band: band model - who to attribute the song to. song_title: id3 title to enforce upon the mp3 song_album: id3 album to enforce upon the mp3 song_comments: the author's own comments for the song. filename_appendix: use this if you want to add something to the filename, before the extension. * uploads the mp3 and source file if applicable * creates the proper files and id3 tags * generates the waveform image outputs a dict: success: boolean - whether it was successful or not reason: string - what went wrong if success is False. song: new song model. it will have been saved, but then more things added to it so it needs to be saved again. it will have the following fields properly populated: owner source_file waveform_img mp3_file band title album length studio samples effects generators """ data = { 'success': False, 'song': None, } assert band != None, "Band parameter isn't optional." assert song_title != None, "Song title parameter isn't optional." song = Song() song.band = band song.owner = user song.title = song_title song.length = 0 song.album = song_album # save so we can use relational fields song.save() # create the root node for song comments node = SongCommentNode() node.song = song node.owner = user node.content = song_comments node.save() song.comment_node = node if file_mp3_handle != None: result = handle_mp3_upload(file_mp3_handle, song, max_song_len, filename_appendix=filename_appendix) if not result['success']: song.delete() return result # upload the source file if file_source_handle != None: handle_project_upload(file_source_handle, user, song, filename_appendix=filename_appendix) # we incremented bytes_used in band, so save it now band.save() data['song'] = song data['success'] = True return data
def ajax_comment(request): parent = get_obj_from_request(request.POST, 'parent', SongCommentNode) if parent is None: return json_failure(design.bad_song_comment_node_id) # make sure the user has permission to critique if parent.song is not None: if not parent.song.permission_to_critique(request.user): return json_failure(design.you_dont_have_permission_to_comment) else: if not parent.version.project.band.permission_to_critique( request.user): return json_failure(design.you_dont_have_permission_to_comment) # make sure the parent has enabled replies if parent.reply_disabled: return json_failure(design.comments_disabled_for_this_version) position = request.POST.get('position') if position is not None: try: position = float(position) except ValueError: position = None if position < 0 or position > parent.song.length: return json_failure(design.invalid_position) content = request.POST.get('content') if len(content) == 0 or len(content) > 2000: return json_failure(design.content_wrong_length) node = SongCommentNode() node.song = parent.song node.version = parent.version node.parent = parent node.owner = request.user node.content = content node.position = position node.reply_disabled = False node.save() return json_success(node.to_dict())