def describe_pack(request): packs = Pack.objects.filter(user=request.user) if request.method == 'POST': form = PackForm(packs, request.POST, prefix="pack") if form.is_valid(): data = form.cleaned_data if data['new_pack']: pack, created = Pack.objects.get_or_create(user=request.user, name=data['new_pack']) request.session['describe_pack'] = pack elif data['pack']: request.session['describe_pack'] = data['pack'] else: request.session['describe_pack'] = False return HttpResponseRedirect(reverse('accounts-describe-sounds')) else: form = PackForm(packs, prefix="pack") return render_to_response('accounts/describe_pack.html', locals(), context_instance=RequestContext(request))
def describe_pack(request): packs = Pack.objects.filter(user=request.user) if request.method == 'POST': form = PackForm(packs, request.POST, prefix="pack") if form.is_valid(): data = form.cleaned_data if data['new_pack']: pack, created = Pack.objects.get_or_create(user=request.user, name=data['new_pack']) request.session['describe_pack'] = pack elif data['pack']: request.session['describe_pack'] = data['pack'] else: request.session['describe_pack'] = False return HttpResponseRedirect(reverse('accounts-describe-sounds')) else: form = PackForm(packs, prefix="pack") tvars = {'form': form} return render(request, 'accounts/describe_pack.html', tvars)
def describe_pack(request): packs = Pack.objects.filter(user=request.user).exclude(is_deleted=True) if request.method == 'POST': form = PackForm(packs, request.POST, prefix="pack") if form.is_valid(): data = form.cleaned_data if data['new_pack']: pack, created = Pack.objects.get_or_create(user=request.user, name=data['new_pack']) request.session['describe_pack'] = pack elif data['pack']: request.session['describe_pack'] = data['pack'] else: request.session['describe_pack'] = False return HttpResponseRedirect(reverse('accounts-describe-sounds')) else: form = PackForm(packs, prefix="pack") tvars = {'form': form} return render(request, 'accounts/describe_pack.html', tvars)
def describe_pack(request): packs = Pack.objects.filter(user=request.user) if request.method == "POST": form = PackForm(packs, request.POST, prefix="pack") if form.is_valid(): data = form.cleaned_data if data["new_pack"]: pack, created = Pack.objects.get_or_create(user=request.user, name=data["new_pack"]) request.session["describe_pack"] = pack elif data["pack"]: request.session["describe_pack"] = data["pack"] else: request.session["describe_pack"] = False return HttpResponseRedirect(reverse("accounts-describe-sounds")) else: form = PackForm(packs, prefix="pack") tvars = {"form": form} return render(request, "accounts/describe_pack.html", tvars)
def describe_sounds(request): forms = [] sounds_to_process = [] sounds = request.session.get('describe_sounds', False) if not sounds: msg = 'Please pick at least one sound.' messages.add_message(request, messages.WARNING, msg) return HttpResponseRedirect(reverse('accounts-describe')) sounds_to_describe = sounds[0:settings.SOUNDS_PER_DESCRIBE_ROUND] request.session['describe_sounds_number'] = len(request.session.get('describe_sounds')) selected_license = request.session.get('describe_license', False) selected_pack = request.session.get('describe_pack', False) # If there are no files in the session redirect to the first describe page if len(sounds_to_describe) <= 0: msg = 'You have finished describing your sounds.' messages.add_message(request, messages.WARNING, msg) return HttpResponseRedirect(reverse('accounts-describe')) tvars = { 'sounds_per_round': settings.SOUNDS_PER_DESCRIBE_ROUND, 'forms': forms, } if request.method == 'POST': # First get all the data n_sounds_already_part_of_freesound = 0 for i in range(len(sounds_to_describe)): prefix = str(i) forms.append({}) forms[i]['sound'] = sounds_to_describe[i] forms[i]['description'] = SoundDescriptionForm(request.POST, prefix=prefix) forms[i]['geotag'] = GeotaggingForm(request.POST, prefix=prefix) forms[i]['pack'] = PackForm(Pack.objects.filter(user=request.user), request.POST, prefix=prefix) forms[i]['license'] = NewLicenseForm(request.POST, prefix=prefix) # Validate each form for i in range(len(sounds_to_describe)): for f in ['license', 'geotag', 'pack', 'description']: if not forms[i][f].is_valid(): # If at least one form is not valid, render template with form errors return render(request, 'accounts/describe_sounds.html', tvars) # All valid, then create sounds and moderation tickets dirty_packs = [] for i in range(len(sounds_to_describe)): # Create sound object and set basic properties sound = Sound() sound.user = request.user sound.original_filename = forms[i]['description'].cleaned_data['name'] sound.original_path = forms[i]['sound'].full_path try: sound.filesize = os.path.getsize(sound.original_path) except OSError: # If for some reason audio file does not exist, skip creating this sound messages.add_message(request, messages.ERROR, 'Something went wrong with accessing the file %s.' % sound.original_path) continue sound.md5 = md5file(forms[i]['sound'].full_path) sound.type = get_sound_type(sound.original_path) sound.license = forms[i]['license'].cleaned_data['license'] try: # Check if another file with same md5 already exists, if it does, delete the sound and post a message existing_sound = Sound.objects.get(md5=sound.md5) n_sounds_already_part_of_freesound += 1 msg = 'The file %s is already part of freesound and has been discarded, see <a href="%s">here</a>' % \ (forms[i]['sound'].name, reverse('sound', args=[existing_sound.user.username, existing_sound.id])) messages.add_message(request, messages.WARNING, msg) os.remove(forms[i]['sound'].full_path) continue except Sound.DoesNotExist: # Reaching here means that no other sound already exists with the same md5 pass sound.save() # Move the original audio file orig = os.path.splitext(os.path.basename(sound.original_filename))[0] sound.base_filename_slug = "%d__%s__%s" % (sound.id, slugify(sound.user.username), slugify(orig)) new_original_path = sound.locations("path") if sound.original_path != new_original_path: try: os.makedirs(os.path.dirname(new_original_path)) except OSError: pass try: shutil.move(sound.original_path, new_original_path) except IOError, e: logger.info("Failed to move file from %s to %s" % (sound.original_path, new_original_path), e) logger.info("Moved original file from %s to %s" % (sound.original_path, new_original_path)) sound.original_path = new_original_path sound.save() # Set pack (optional) pack = forms[i]['pack'].cleaned_data.get('pack', False) new_pack = forms[i]['pack'].cleaned_data.get('new_pack', False) if not pack and new_pack: pack, created = Pack.objects.get_or_create(user=request.user, name=new_pack) if pack: sound.pack = pack dirty_packs.append(sound.pack) # Set geotag data = forms[i]['geotag'].cleaned_data if not data.get('remove_geotag') and data.get('lat'): # if 'lat' is in data, we assume other fields are too geotag = GeoTag(user=request.user, lat=data.get('lat'), lon=data.get('lon'), zoom=data.get('zoom')) geotag.save() sound.geotag = geotag # Set the tags and description data = forms[i]['description'].cleaned_data sound.description = remove_control_chars(data.get('description', '')) sound.set_tags(data.get('tags')) sound.save() # Remember to process the file and create moderation ticket if user is not whitelisted sounds_to_process.append(sound) if request.user.profile.is_whitelisted: sound.change_moderation_state('OK', do_not_update_related_stuff=True) messages.add_message(request, messages.INFO, 'File <a href="%s">%s</a> has been described and has been added to freesound.' % \ (sound.get_absolute_url(), forms[i]['sound'].name)) else: sound.create_moderation_ticket() messages.add_message(request, messages.INFO, 'File <a href="%s">%s</a> has been described and is now awaiting processing ' 'and moderation.' % (sound.get_absolute_url(), forms[i]['sound'].name)) # Invalidate affected caches in user header invalidate_template_cache("user_header", request.user.id) for moderator in Group.objects.get(name='moderators').user_set.all(): invalidate_template_cache("user_header", moderator.id) # Compute sound crc try: sound.compute_crc() except: pass # Remove the files we just described from the session and redirect to this page request.session['describe_sounds'] = request.session['describe_sounds'][len(sounds_to_describe):] # Process sounds and packs # N.B. we do this at the end to avoid conflicts between django-web and django-workers # If we're not careful django's save() functions will overwrite any processing we # do on the workers. # In the future if django-workers do not write to the db this might be changed try: for s in sounds_to_process: s.process() except Exception as e: audio_logger.error('Sound with id %s could not be scheduled. (%s)' % (s.id, str(e))) for p in dirty_packs: p.process() # Check if all sounds have been described after that round and redirect accordingly if len(request.session['describe_sounds']) <= 0: if len(sounds_to_describe) != n_sounds_already_part_of_freesound: msg = 'You have described all the selected files and are now awaiting processing and moderation. ' \ 'You can check the status of your uploaded sounds in your <a href="%s">home page</a>. ' \ 'Once your sounds have been processed, you can also get information about the moderation ' \ 'status in the <a href="%s">uploaded sounds awaiting moderation' \ '</a> page.' % (reverse('accounts-home'), reverse('accounts-pending')) messages.add_message(request, messages.WARNING, msg) return HttpResponseRedirect(reverse('accounts-describe')) else: return HttpResponseRedirect(reverse('accounts-describe-sounds'))
'status in the <a href="%s">uploaded sounds awaiting moderation' \ '</a> page.' % (reverse('accounts-home'), reverse('accounts-pending')) messages.add_message(request, messages.WARNING, msg) return HttpResponseRedirect(reverse('accounts-describe')) else: return HttpResponseRedirect(reverse('accounts-describe-sounds')) else: for i in range(len(sounds_to_describe)): prefix = str(i) forms.append({}) forms[i]['sound'] = sounds_to_describe[i] forms[i]['description'] = SoundDescriptionForm(initial={'name': forms[i]['sound'].name}, prefix=prefix) forms[i]['geotag'] = GeotaggingForm(prefix=prefix) if selected_pack: forms[i]['pack'] = PackForm(Pack.objects.filter(user=request.user), prefix=prefix, initial={'pack': selected_pack.id}) else: forms[i]['pack'] = PackForm(Pack.objects.filter(user=request.user), prefix=prefix) if selected_license: forms[i]['license'] = NewLicenseForm(initial={'license': selected_license}, prefix=prefix) else: forms[i]['license'] = NewLicenseForm(prefix=prefix) return render(request, 'accounts/describe_sounds.html', tvars) @login_required def attribution(request):
def sound_edit(request, username, sound_id): sound = get_object_or_404(Sound, user__username__iexact=username, id=sound_id, processing_state='OK') if not (request.user.has_perm('sound.can_change') or sound.user == request.user): raise PermissionDenied def invalidate_sound_cache(sound): invalidate_template_cache("sound_header", sound.id, True) invalidate_template_cache("sound_header", sound.id, False) invalidate_template_cache("sound_footer_top", sound.id) invalidate_template_cache("sound_footer_bottom", sound.id) invalidate_template_cache("display_sound", sound.id, True, sound.processing_state, sound.moderation_state) invalidate_template_cache("display_sound", sound.id, False, sound.processing_state, sound.moderation_state) def is_selected(prefix): if request.method == "POST": for name in request.POST.keys(): if name.startswith(prefix + '-'): return True return False if is_selected("description"): description_form = SoundDescriptionForm(request.POST, prefix="description") if description_form.is_valid(): data = description_form.cleaned_data sound.set_tags(data["tags"]) sound.description = data["description"] sound.original_filename = data["name"] sound.mark_index_dirty() invalidate_sound_cache(sound) # also update any possible related sound ticket tickets = Ticket.objects.filter(content__object_id=sound.id, source=TICKET_SOURCE_NEW_SOUND) \ .exclude(status=TICKET_STATUS_CLOSED) for ticket in tickets: tc = TicketComment(sender=request.user, ticket=ticket, moderator_only=False, text='%s updated the sound description and/or tags.' % request.user.username) tc.save() ticket.send_notification_emails(ticket.NOTIFICATION_UPDATED, ticket.MODERATOR_ONLY) return HttpResponseRedirect(sound.get_absolute_url()) else: tags = " ".join([tagged_item.tag.name for tagged_item in sound.tags.all().order_by('tag__name')]) description_form = SoundDescriptionForm(prefix="description", initial=dict(tags=tags, description=sound.description, name=sound.original_filename)) packs = Pack.objects.filter(user=request.user) if is_selected("pack"): pack_form = PackForm(packs, request.POST, prefix="pack") if pack_form.is_valid(): data = pack_form.cleaned_data dirty_packs = [] if data['new_pack']: (pack, created) = Pack.objects.get_or_create(user=sound.user, name=data['new_pack']) sound.pack = pack else: new_pack = data["pack"] old_pack = sound.pack if new_pack != old_pack: sound.pack = new_pack if new_pack: dirty_packs.append(new_pack) if old_pack: dirty_packs.append(old_pack) for p in dirty_packs: p.process() sound.mark_index_dirty() invalidate_sound_cache(sound) return HttpResponseRedirect(sound.get_absolute_url()) else: pack_form = PackForm(packs, prefix="pack", initial=dict(pack=sound.pack.id) if sound.pack else None) if is_selected("geotag"): geotag_form = GeotaggingForm(request.POST, prefix="geotag") if geotag_form.is_valid(): data = geotag_form.cleaned_data if data["remove_geotag"]: if sound.geotag: geotag = sound.geotag.delete() sound.geotag = None sound.mark_index_dirty() else: if sound.geotag: sound.geotag.lat = data["lat"] sound.geotag.lon = data["lon"] sound.geotag.zoom = data["zoom"] sound.geotag.save() else: sound.geotag = GeoTag.objects.create(lat=data["lat"], lon=data["lon"], zoom=data["zoom"], user=request.user) sound.mark_index_dirty() invalidate_sound_cache(sound) return HttpResponseRedirect(sound.get_absolute_url()) else: if sound.geotag: geotag_form = GeotaggingForm(prefix="geotag", initial=dict(lat=sound.geotag.lat, lon=sound.geotag.lon, zoom=sound.geotag.zoom)) else: geotag_form = GeotaggingForm(prefix="geotag") license_form = NewLicenseForm(request.POST) if request.POST and license_form.is_valid(): sound.license = license_form.cleaned_data["license"] sound.mark_index_dirty() invalidate_sound_cache(sound) return HttpResponseRedirect(sound.get_absolute_url()) else: license_form = NewLicenseForm(initial={'license': sound.license}) google_api_key = settings.GOOGLE_API_KEY return render_to_response('sounds/sound_edit.html', locals(), context_instance=RequestContext(request))
def sound_edit(request, username, sound_id): sound = get_object_or_404(Sound, id=sound_id, processing_state='OK') if sound.user.username.lower() != username.lower(): raise Http404 if not (request.user.is_superuser or sound.user == request.user): raise PermissionDenied def is_selected(prefix): if request.method == "POST": for name in request.POST.keys(): if name.startswith(prefix + '-'): return True return False def update_sound_tickets(sound, text): tickets = Ticket.objects.filter(sound_id=sound.id)\ .exclude(status=TICKET_STATUS_CLOSED) for ticket in tickets: tc = TicketComment(sender=request.user, ticket=ticket, moderator_only=False, text=text) tc.save() ticket.send_notification_emails(ticket.NOTIFICATION_UPDATED, ticket.MODERATOR_ONLY) if is_selected("description"): description_form = SoundDescriptionForm( request.POST, prefix="description", explicit_disable=sound.is_explicit) if description_form.is_valid(): data = description_form.cleaned_data sound.is_explicit = data["is_explicit"] sound.set_tags(data["tags"]) sound.description = remove_control_chars(data["description"]) sound.original_filename = data["name"] sound.mark_index_dirty() sound.invalidate_template_caches() update_sound_tickets(sound, '%s updated the sound description and/or tags.' % request.user.username) return HttpResponseRedirect(sound.get_absolute_url()) else: tags = " ".join([tagged_item.tag.name for tagged_item in sound.tags.all().order_by('tag__name')]) description_form = SoundDescriptionForm(prefix="description", explicit_disable=sound.is_explicit, initial=dict(tags=tags, description=sound.description, name=sound.original_filename)) packs = Pack.objects.filter(user=request.user).exclude(is_deleted=True) if is_selected("pack"): pack_form = PackForm(packs, request.POST, prefix="pack") if pack_form.is_valid(): data = pack_form.cleaned_data affected_packs = [] if data['new_pack']: (pack, created) = Pack.objects.get_or_create(user=sound.user, name=data['new_pack']) if sound.pack: affected_packs.append(sound.pack) # Append previous sound pack if exists sound.pack = pack affected_packs.append(pack) else: new_pack = data["pack"] old_pack = sound.pack if new_pack != old_pack: sound.pack = new_pack if new_pack: affected_packs.append(new_pack) if old_pack: affected_packs.append(old_pack) sound.mark_index_dirty() # Marks as dirty and saves sound.invalidate_template_caches() update_sound_tickets(sound, '%s updated the sound pack.' % request.user.username) for affected_pack in affected_packs: # Process affected packs affected_pack.process() return HttpResponseRedirect(sound.get_absolute_url()) else: pack_form = PackForm(packs, prefix="pack", initial=dict(pack=sound.pack.id) if sound.pack else None) if is_selected("geotag"): geotag_form = GeotaggingForm(request.POST, prefix="geotag") if geotag_form.is_valid(): data = geotag_form.cleaned_data if data["remove_geotag"]: if sound.geotag: sound.geotag.delete() sound.geotag = None sound.mark_index_dirty() else: if sound.geotag: sound.geotag.lat = data["lat"] sound.geotag.lon = data["lon"] sound.geotag.zoom = data["zoom"] sound.geotag.save() else: sound.geotag = GeoTag.objects.create(lat=data["lat"], lon=data["lon"], zoom=data["zoom"], user=request.user) sound.mark_index_dirty() sound.mark_index_dirty() sound.invalidate_template_caches() update_sound_tickets(sound, '%s updated the sound geotag.' % request.user.username) return HttpResponseRedirect(sound.get_absolute_url()) else: if sound.geotag: geotag_form = GeotaggingForm(prefix="geotag", initial=dict(lat=sound.geotag.lat, lon=sound.geotag.lon, zoom=sound.geotag.zoom)) else: geotag_form = GeotaggingForm(prefix="geotag") license_form = NewLicenseForm(request.POST) if request.POST and license_form.is_valid(): new_license = license_form.cleaned_data["license"] if new_license != sound.license: sound.set_license(new_license) sound.mark_index_dirty() # Sound is saved here if sound.pack: sound.pack.process() # Sound license changed, process pack (if sound has pack) sound.invalidate_template_caches() update_sound_tickets(sound, '%s updated the sound license.' % request.user.username) return HttpResponseRedirect(sound.get_absolute_url()) else: license_form = NewLicenseForm(initial={'license': sound.license}) tvars = { 'sound': sound, 'description_form': description_form, 'pack_form': pack_form, 'geotag_form': geotag_form, 'license_form': license_form } return render(request, 'sounds/sound_edit.html', tvars)
def sound_edit(request, username, sound_id): sound = get_object_or_404(Sound, id=sound_id, processing_state='OK') if sound.user.username.lower() != username.lower(): raise Http404 if not (request.user.has_perm('sound.can_change') or sound.user == request.user): raise PermissionDenied def is_selected(prefix): if request.method == "POST": for name in request.POST.keys(): if name.startswith(prefix + '-'): return True return False def update_sound_tickets(sound, text): tickets = Ticket.objects.filter(sound_id=sound.id)\ .exclude(status=TICKET_STATUS_CLOSED) for ticket in tickets: tc = TicketComment(sender=request.user, ticket=ticket, moderator_only=False, text=text) tc.save() ticket.send_notification_emails(ticket.NOTIFICATION_UPDATED, ticket.MODERATOR_ONLY) if is_selected("description"): description_form = SoundDescriptionForm( request.POST, prefix="description", explicit_disable=sound.is_explicit) if description_form.is_valid(): data = description_form.cleaned_data sound.is_explicit = data["is_explicit"] sound.set_tags(data["tags"]) sound.description = remove_control_chars(data["description"]) sound.original_filename = data["name"] sound.mark_index_dirty() sound.invalidate_template_caches() update_sound_tickets( sound, '%s updated the sound description and/or tags.' % request.user.username) return HttpResponseRedirect(sound.get_absolute_url()) else: tags = " ".join([ tagged_item.tag.name for tagged_item in sound.tags.all().order_by('tag__name') ]) description_form = SoundDescriptionForm( prefix="description", explicit_disable=sound.is_explicit, initial=dict(tags=tags, description=sound.description, name=sound.original_filename)) packs = Pack.objects.filter(user=request.user).exclude(is_deleted=True) if is_selected("pack"): pack_form = PackForm(packs, request.POST, prefix="pack") if pack_form.is_valid(): data = pack_form.cleaned_data affected_packs = [] if data['new_pack']: (pack, created) = Pack.objects.get_or_create(user=sound.user, name=data['new_pack']) if sound.pack: affected_packs.append( sound.pack) # Append previous sound pack if exists sound.pack = pack affected_packs.append(pack) else: new_pack = data["pack"] old_pack = sound.pack if new_pack != old_pack: sound.pack = new_pack if new_pack: affected_packs.append(new_pack) if old_pack: affected_packs.append(old_pack) sound.mark_index_dirty() # Marks as dirty and saves sound.invalidate_template_caches() update_sound_tickets( sound, '%s updated the sound pack.' % request.user.username) for affected_pack in affected_packs: # Process affected packs affected_pack.process() return HttpResponseRedirect(sound.get_absolute_url()) else: pack_form = PackForm( packs, prefix="pack", initial=dict(pack=sound.pack.id) if sound.pack else None) if is_selected("geotag"): geotag_form = GeotaggingForm(request.POST, prefix="geotag") if geotag_form.is_valid(): data = geotag_form.cleaned_data if data["remove_geotag"]: if sound.geotag: sound.geotag.delete() sound.geotag = None sound.mark_index_dirty() else: if sound.geotag: sound.geotag.lat = data["lat"] sound.geotag.lon = data["lon"] sound.geotag.zoom = data["zoom"] sound.geotag.save() else: sound.geotag = GeoTag.objects.create(lat=data["lat"], lon=data["lon"], zoom=data["zoom"], user=request.user) sound.mark_index_dirty() sound.mark_index_dirty() sound.invalidate_template_caches() update_sound_tickets( sound, '%s updated the sound geotag.' % request.user.username) return HttpResponseRedirect(sound.get_absolute_url()) else: if sound.geotag: geotag_form = GeotaggingForm(prefix="geotag", initial=dict(lat=sound.geotag.lat, lon=sound.geotag.lon, zoom=sound.geotag.zoom)) else: geotag_form = GeotaggingForm(prefix="geotag") license_form = NewLicenseForm(request.POST) if request.POST and license_form.is_valid(): new_license = license_form.cleaned_data["license"] if new_license != sound.license: sound.set_license(new_license) sound.mark_index_dirty() # Sound is saved here if sound.pack: sound.pack.process( ) # Sound license changed, process pack (if sound has pack) sound.invalidate_template_caches() update_sound_tickets( sound, '%s updated the sound license.' % request.user.username) return HttpResponseRedirect(sound.get_absolute_url()) else: license_form = NewLicenseForm(initial={'license': sound.license}) tvars = { 'sound': sound, 'description_form': description_form, 'pack_form': pack_form, 'geotag_form': geotag_form, 'license_form': license_form } return render(request, 'sounds/sound_edit.html', tvars)
'</a> page.' % (reverse('accounts-home'), reverse('accounts-pending')) messages.add_message(request, messages.WARNING, msg) return HttpResponseRedirect(reverse('accounts-describe')) else: return HttpResponseRedirect(reverse('accounts-describe-sounds')) else: for i in range(len(sounds_to_describe)): prefix = str(i) forms.append({}) forms[i]['sound'] = sounds_to_describe[i] forms[i]['description'] = SoundDescriptionForm( initial={'name': forms[i]['sound'].name}, prefix=prefix) forms[i]['geotag'] = GeotaggingForm(prefix=prefix) if selected_pack: forms[i]['pack'] = PackForm(Pack.objects.filter( user=request.user).exclude(is_deleted=True), prefix=prefix, initial={'pack': selected_pack.id}) else: forms[i]['pack'] = PackForm(Pack.objects.filter( user=request.user).exclude(is_deleted=True), prefix=prefix) if selected_license: forms[i]['license'] = NewLicenseForm( initial={'license': selected_license}, prefix=prefix) else: forms[i]['license'] = NewLicenseForm(prefix=prefix) return render(request, 'accounts/describe_sounds.html', tvars) @login_required
def sound_edit(request, username, sound_id): ONLY_RECOMMEND_TAGS_TO_HALF_OF_UPLOADS = settings.ONLY_RECOMMEND_TAGS_TO_HALF_OF_UPLOADS sound = get_object_or_404(Sound, id=sound_id, processing_state='OK') if sound.user.username.lower() != username.lower(): raise Http404 if not (request.user.has_perm('sound.can_change') or sound.user == request.user): raise PermissionDenied def invalidate_sound_cache(sound): invalidate_template_cache("sound_header", sound.id, True) invalidate_template_cache("sound_header", sound.id, False) invalidate_template_cache("sound_footer_top", sound.id) invalidate_template_cache("sound_footer_bottom", sound.id) invalidate_template_cache("display_sound", sound.id, True, sound.processing_state, sound.moderation_state) invalidate_template_cache("display_sound", sound.id, False, sound.processing_state, sound.moderation_state) def is_selected(prefix): if request.method == "POST": for name in request.POST.keys(): if name.startswith(prefix + '-'): return True return False if is_selected("description"): description_form = SoundDescriptionForm(request.POST, prefix="description") if description_form.is_valid(): data = description_form.cleaned_data sound.set_tags(data["tags"]) sound.description = remove_control_chars(data["description"]) sound.original_filename = data["name"] sound.mark_index_dirty() invalidate_sound_cache(sound) # also update any possible related sound ticket tickets = Ticket.objects.filter(content__object_id=sound.id, source=TICKET_SOURCE_NEW_SOUND) \ .exclude(status=TICKET_STATUS_CLOSED) for ticket in tickets: tc = TicketComment(sender=request.user, ticket=ticket, moderator_only=False, text='%s updated the sound description and/or tags.' % request.user.username) tc.save() ticket.send_notification_emails(ticket.NOTIFICATION_UPDATED, ticket.MODERATOR_ONLY) return HttpResponseRedirect(sound.get_absolute_url()) else: tags = " ".join([tagged_item.tag.name for tagged_item in sound.tags.all().order_by('tag__name')]) description_form = SoundDescriptionForm(prefix="description", initial=dict(tags=tags, description=sound.description, name=sound.original_filename)) packs = Pack.objects.filter(user=request.user) if is_selected("pack"): pack_form = PackForm(packs, request.POST, prefix="pack") if pack_form.is_valid(): data = pack_form.cleaned_data dirty_packs = [] if data['new_pack']: (pack, created) = Pack.objects.get_or_create(user=sound.user, name=data['new_pack']) sound.pack = pack else: new_pack = data["pack"] old_pack = sound.pack if new_pack != old_pack: sound.pack = new_pack if new_pack: dirty_packs.append(new_pack) if old_pack: dirty_packs.append(old_pack) for p in dirty_packs: p.process() sound.mark_index_dirty() invalidate_sound_cache(sound) return HttpResponseRedirect(sound.get_absolute_url()) else: pack_form = PackForm(packs, prefix="pack", initial=dict(pack=sound.pack.id) if sound.pack else None) if is_selected("geotag"): geotag_form = GeotaggingForm(request.POST, prefix="geotag") if geotag_form.is_valid(): data = geotag_form.cleaned_data if data["remove_geotag"]: if sound.geotag: geotag = sound.geotag.delete() sound.geotag = None sound.mark_index_dirty() else: if sound.geotag: sound.geotag.lat = data["lat"] sound.geotag.lon = data["lon"] sound.geotag.zoom = data["zoom"] sound.geotag.save() else: sound.geotag = GeoTag.objects.create(lat=data["lat"], lon=data["lon"], zoom=data["zoom"], user=request.user) sound.mark_index_dirty() invalidate_sound_cache(sound) return HttpResponseRedirect(sound.get_absolute_url()) else: if sound.geotag: geotag_form = GeotaggingForm(prefix="geotag", initial=dict(lat=sound.geotag.lat, lon=sound.geotag.lon, zoom=sound.geotag.zoom)) else: geotag_form = GeotaggingForm(prefix="geotag") license_form = NewLicenseForm(request.POST) if request.POST and license_form.is_valid(): sound.license = license_form.cleaned_data["license"] sound.mark_index_dirty() invalidate_sound_cache(sound) return HttpResponseRedirect(sound.get_absolute_url()) else: license_form = NewLicenseForm(initial={'license': sound.license}) google_api_key = settings.GOOGLE_API_KEY return render_to_response('sounds/sound_edit.html', locals(), context_instance=RequestContext(request))