def upload_create_resource_form(request, project, prefix='create_form'): """Form for creating a new resource.""" resource = None display_form = False if request.method == 'POST' and request.POST.get('create_resource', None): cr_form = CreateResourceForm(request.POST, request.FILES, prefix=prefix) if cr_form.is_valid(): name = cr_form.cleaned_data['name'] slug = slugify(name) # Check if we already have a resource with this slug in the db. try: Resource.objects.get(slug=slug, project=project) except Resource.DoesNotExist: pass else: # if the resource exists, modify slug in order to force the # creation of a new resource. slug = slugify(name) identifier = Resource.objects.filter( project=project, slug__icontains="%s_" % slug).count() + 1 slug = "%s_%s" % (slug, identifier) method = cr_form.cleaned_data['i18n_method'] content = content_from_uploaded_file(request.FILES) filename = filename_of_uploaded_file(request.FILES) rb = ResourceBackend() try: with transaction.commit_on_success(): rb.create(project, slug, name, method, project.source_language, content, user=request.user, extra_data={'filename': filename}) except ResourceBackendError, e: cr_form._errors['source_file'] = ErrorList([ e.message, ]) display_form = True else: display_form = False resource = Resource.objects.get(slug=slug, project=project) else: display_form = True
def upload_create_resource_form(request, project, prefix='create_form'): """Form for creating a new resource.""" resource = None display_form = False if request.method == 'POST' and request.POST.get('create_resource', None): cr_form = CreateResourceForm( request.POST, request.FILES, prefix=prefix ) if cr_form.is_valid(): name = cr_form.cleaned_data['name'] slug = slugify(name) # Check if we already have a resource with this slug in the db. try: Resource.objects.get(slug=slug, project=project) except Resource.DoesNotExist: pass else: # if the resource exists, modify slug in order to force the # creation of a new resource. slug = slugify(name) identifier = Resource.objects.filter( project=project, slug__icontains="%s_" % slug ).count() + 1 slug = "%s_%s" % (slug, identifier) method = cr_form.cleaned_data['i18n_method'] content = content_from_uploaded_file(request.FILES) filename = filename_of_uploaded_file(request.FILES) rb = ResourceBackend() try: rb.create( project, slug, name, method, project.source_language, content, user=request.user, extra_data={'filename': filename} ) except ResourceBackendError, e: transaction.rollback() cr_form._errors['source_file'] = ErrorList([e.message, ]) display_form=True else: transaction.commit() display_form = False resource = Resource.objects.get(slug=slug, project=project) else: display_form=True
def _get_filename(self, request, data): """Get the filename of the uploaded file. Returns: The filename or None, if the request used json. """ if 'application/json' in request.content_type: return None elif 'multipart/form-data' in request.content_type: if not request.FILES: msg = "No file has been uploaded." logger.warning(msg) raise NoContentError(msg) return filename_of_uploaded_file(request.FILES) else: msg = "No content or file found" logger.warning(msg) raise NoContentError(msg)