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
Example #2
0
def upload_create_resource_form(request, project, prefix='create_form'):
    """
    Render a form that uses StorageFile field to upload files. It creates a
    resource after the form ve validated, extract the file strings as
    sourceentities on the fly.

    The parameter 'prefix' can be used to add a prefix to the form name and
    its sub-fields.
    """
    resource = None
    if request.method == 'POST' and request.POST.get('create_resource', None):
        create_resource_form = CreateResourceForm(request.POST, prefix=prefix)
        if create_resource_form.is_valid():
            resource = create_resource_form.save(commit=False)
        display_form=True

        # If we have a resource in the creation form, check if a we already
        # have a resource with this slug in the db.
        if resource:
            try:
                Resource.objects.get(
                    slug = slugify(resource.source_file.name),
                    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(resource.source_file.name)
                identifier = Resource.objects.filter(project=project,
                    slug__icontains = "%s_" % slug ).count() + 1
                resource.slug = "%s_%s" % (slug, identifier)

    else:
        create_resource_form = CreateResourceForm(prefix=prefix,
            initial={'source_file':['en', ""]})
        display_form=False


    api_project_files = get_url_pattern('api_project_files')
    return {
          'project' : project,
          'resource': resource,
          'create_resource_form': create_resource_form,
          'display_form': display_form,
          'api_project_files': api_project_files,
    }
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
                              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
    else:
        cr_form = CreateResourceForm(prefix=prefix)
        display_form = False

    return {
        'project': project,
        'resource': resource,
        'create_resource_form': cr_form,
        'display_form': display_form,
    }


@register.inclusion_tag("resources/upload_resource_translation_button.html",
                        takes_context=True)
def upload_resource_translation_button(context,
                                       request,
                                       resource,