Esempio n. 1
0
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():
            urlinfo = url_form.save(commit=False)
            resource_new = resource_form.save(user=request.user)
            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()

            post_resource_save.send(sender=None, instance=resource_new,
                created=False, user=request.user)

            return HttpResponseRedirect(reverse('resource_detail',
                args=[resource.project.slug, resource.slug]))
Esempio n. 2
0
def send_notices_for_resource_edited(resource, user):
    """
    Send notifications, when a resource has been edited.

    Args:
        resource: The resource that has been edited.
        user: The user that did the update.
    """
    post_resource_save.send(
        sender=None, instance=resource, created=False, user=user
    )
Esempio n. 3
0
def send_notices_for_resource_edited(resource, user):
    """
    Send notifications, when a resource has been edited.

    Args:
        resource: The resource that has been edited.
        user: The user that did the update.
    """
    post_resource_save.send(sender=None,
                            instance=resource,
                            created=False,
                            user=user)
Esempio n. 4
0
    def _createv1(self, request, project_slug, resource_slug, data):
        try:
            project = Project.objects.get(slug=project_slug)
        except Project.DoesNotExist:
            return rc.NOT_FOUND
        slang = data.pop('source_language', None)
        source_language = None

        if not source_language:
            return BAD_REQUEST("No or wrong source language was specified.")

        try:
            r, created = Resource.objects.get_or_create(
                project=project, **data
            )

            if created:
                post_resource_save.send(sender=None, instance=r,
                        created=created, user=request.user)

        except:
            return BAD_REQUEST("The json you provided is misformatted.")
        return rc.CREATED
Esempio n. 5
0
            )

        try:
            content = self._get_content(request, data)
            filename = self._get_filename(request, data)
        except NoContentError, e:
            raise BadRequestError(unicode(e))

        try:
            rb = ResourceBackend()
            rb_create =  rb.create(
                project, slug, name, method, project.source_language, content,
                extra_data={'filename': filename}
            )
            post_resource_save.send(sender=None, instance=Resource.objects.get(
                slug=slug, project=project),
                    created=True, user=request.user)
            return rb_create
        except ResourceBackendError, e:
            raise BadRequestError(unicode(e))

    def _update(self, request, project_slug, resource_slug):
        data = getattr(request, 'data', None)
        if not data:            # Check for {} as well
            return BAD_REQUEST("Empty request")
        try:
            self._check_fields(data.iterkeys(), self.written_fields)
        except AttributeError, e:
            return BAD_REQUEST("Field '%s' is not allowed." % e)

        try:
Esempio n. 6
0
                else:
                    messages = []
                    if strings_added > 0:
                        messages.append(_("%i strings added") % strings_added)
                    if strings_updated > 0:
                        messages.append(_("%i strings updated") % strings_updated)
                retval= {
                    'strings_added': strings_added,
                    'strings_updated': strings_updated,
                    'redirect': reverse('resource_detail',args=[project_slug,
                        resource.slug])
                    }
                logger.debug("Extraction successful, returning: %s" % retval)

                if created:
                    post_resource_save.send(sender=None, instance=resource,
                            created=created, user=request.user)

                # transaction has been commited by save2db
                # but logger message above marks it dirty again
                transaction.commit()

                return HttpResponse(simplejson.dumps(retval),
                    mimetype='text/plain')

            else:
                transaction.rollback()
                return BAD_REQUEST("Request data missing.")
        else:
            transaction.rollback()
            return BAD_REQUEST("Unsupported request")
Esempio n. 7
0
            filename = self._get_filename(request, data)
        except NoContentError, e:
            raise BadRequestError(unicode(e))

        try:
            rb = ResourceBackend()
            rb_create = rb.create(project,
                                  slug,
                                  name,
                                  method,
                                  project.source_language,
                                  content,
                                  extra_data={'filename': filename})
            post_resource_save.send(sender=None,
                                    instance=Resource.objects.get(
                                        slug=slug, project=project),
                                    created=True,
                                    user=request.user)
            return rb_create
        except ResourceBackendError, e:
            raise BadRequestError(unicode(e))

    def _update(self, request, project_slug, resource_slug):
        data = getattr(request, 'data', None)
        if not data:  # Check for {} as well
            return BAD_REQUEST("Empty request")
        try:
            self._check_fields(data.iterkeys(), self.written_fields)
        except AttributeError, e:
            return BAD_REQUEST("Field '%s' is not allowed." % e)