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.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)
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): 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))