def resource_edit(request, project_slug, resource_slug): """ Edit the metadata of a Translation Resource in a specific project. """ resource = get_object_or_404(Resource, project__slug=project_slug, slug=resource_slug) try: urlinfo = URLInfo.objects.get(resource=resource) except URLInfo.DoesNotExist: urlinfo = None if request.method == 'POST': resource_form = ResourceForm(request.POST, request.FILES, instance=resource) if urlinfo: url_form = URLInfoForm( request.POST, instance=urlinfo, ) else: url_form = URLInfoForm(request.POST, ) if resource_form.is_valid() and url_form.is_valid(): try: resource = resource_form.save(commit=False) if resource_form.cleaned_data['sourcefile'] is not None: method = resource.i18n_method content = content_from_uploaded_file({ 0: resource_form.cleaned_data['sourcefile'], }) filename = resource_form.cleaned_data['sourcefile'].name save_source_file(resource, request.user, content, method, filename) urlinfo = url_form.save(commit=False) resource_new = resource_form.save() resource_new.save() urlinfo.resource = resource_new invalidate_object_templates(resource_new, resource_new.source_language) if urlinfo.source_file_url: try: urlinfo.update_source_file(fake=True) except Exception, e: url_form._errors['source_file_url'] = _( "The URL you provided" " doesn't link to a valid file.") return render_to_response( 'resources/resource_form.html', { 'resource_form': resource_form, 'url_form': url_form, 'resource': resource, }, context_instance=RequestContext(request)) # If we got a URL, save the model instance urlinfo.save() else: if urlinfo.auto_update: url_form._errors['source_file_url'] = _( "You have checked" " the auto update checkbox but you haven't provided a" " valid url.") return render_to_response( 'resources/resource_form.html', { 'resource_form': resource_form, 'url_form': url_form, 'resource': resource, }, context_instance=RequestContext(request)) else: if urlinfo.id: urlinfo.delete() send_notices_for_resource_edited.delay(resource_new, request.user) return HttpResponseRedirect( reverse('resource_detail', args=[resource.project.slug, resource.slug])) except FormatsBackendError, e: resource_form._errors['sourcefile'] = ErrorList([ unicode(e), ])
return HttpResponseRedirect( reverse('resource_detail', args=[resource.project.slug, resource.slug])) except FormatsBackendError, e: resource_form._errors['sourcefile'] = ErrorList([ unicode(e), ]) else: if resource: initial_data = {} if urlinfo: url_form = URLInfoForm(instance=urlinfo, ) else: url_form = URLInfoForm() resource_form = ResourceForm(instance=resource) return render_to_response('resources/resource_form.html', { 'resource_form': resource_form, 'url_form': url_form, 'resource': resource, }, context_instance=RequestContext(request)) @transaction.commit_on_success def save_source_file(resource, user, content, method, filename=None): """Save new source file. Called by the "edit resource" action. """