Commits

Seraphim Mellos committed f0c9321

Added API view to download translation files.

Comments (0)

Files changed (3)

transifex/api/urls.py

 
 from languages.api import LanguageHandler
 from projects.api import ProjectHandler, ProjectResourceHandler
-from resources.api import (ResourceHandler, StringHandler)
+from resources.api import (ResourceHandler, StringHandler, FileHandler)
 from storage.api import StorageHandler
 
 auth = CustomHttpBasicAuthentication(realm='Transifex API')
 storage_handler = Resource(StorageHandler, authentication=auth)
 project_handler = Resource(ProjectHandler, authentication=auth)
 projectresource_handler = Resource(ProjectResourceHandler, authentication=auth)
+translationfile_handler = Resource(FileHandler, authentication=auth)
 string_handler = Resource(StringHandler, authentication=auth)
 #projectstring_handler = Resource(ProjectStringHandler)
 
         r'^project/(?P<project_slug>[-\w]+)/resource/(?P<resource_slug>[-\w]+)/(?P<language_code>[\-_@\w]+)/$',
         projectresource_handler,
         name='api_resource_storage'
+    ), url(
+        r'^project/(?P<project_slug>[-\w]+)/resource/(?P<resource_slug>[-\w]+)/(?P<language_code>[\-_@\w]+)/file/$',
+        translationfile_handler,
+        name='api_translation_file'
 #    ), url(
 #        r'^project/(?P<project_slug>[-\w]+)/resource/(?P<resource_slug>[-\w]+)/(?P<target_lang_code>[-\w]+)/(?P<source_lang_code>[-\w]+)/$',
 #        strings_handler,

transifex/resources/api.py

 # -*- coding: utf-8 -*-
 from uuid import uuid4
 from django.db import transaction
+from django.conf import settings
+from django.http import HttpResponse
 from django.core.urlresolvers import reverse
 from django.template.defaultfilters import slugify
 from django.contrib.auth.models import User
+from django.utils.encoding import smart_unicode
 
 from piston.handler import BaseHandler, AnonymousBaseHandler
 from piston.utils import rc
 
 from resources.decorators import method_decorator
 from resources.models import Resource, SourceEntity, Translation
-
+from resources.views import _compile_translation_template
 
 class ResourceHandler(BaseHandler):
     """
         return _create_stringset(request, project_slug, resource_slug, target_lang_code)
 
 
+class FileHandler(BaseHandler):
+    allowed_methods = ('GET')
+
+    
+    @method_decorator(one_perm_required_or_403(pr_project_private_perm,
+        (Project, 'slug__exact', 'project_slug')))
+    def read(self, request, project_slug, resource_slug=None, language_code=None):
+        """
+        
+        """
+        try:
+            resource = Resource.objects.get( project__slug = project_slug, slug = resource_slug)
+            language = Language.objects.get( code=language_code)
+        except (Resource.DoesNotExist, Language.DoesNotExist), e:
+            BAD_REQUEST("%s" % e )
+
+        try:
+            template = _compile_translation_template(resource, language)
+        except Exception, e:
+            BAD_REQUEST("Error compiling the translation file: %s" %e )
+
+        i18n_method = settings.I18N_METHODS[resource.i18n_type]
+        response = HttpResponse(template,
+        mimetype=i18n_method['mimetype'])
+        response['Content-Disposition'] = ('attachment; filename="%s_%s%s"' % (
+        smart_unicode(resource.name), language.code,
+        i18n_method['file-extensions'].split(', ')[0]))
+
+        return response
+
 class StringHandler(BaseHandler):
     allowed_methods = ('GET', 'POST','PUT')
     anonymous = AnonymousStringHandler

transifex/resources/views.py

             context_instance=RequestContext(request))
 
 
+def _compile_translation_template(resource=None, language=None):
+    """
+    Given a resource and a language we create the translation file
+    """
+    parser = get_i18n_handler_from_type(resource.i18n_type)
+    handler = parser(resource = resource, language = language)
+    handler.compile()
+
+    return handler.compiled_template
+
+
 # Restrict access only for private projects 
 # DONT allow anonymous access
 @login_required
         slug = resource_slug)
 
     language = get_object_or_404(Language, code=lang_code)
-    parser = get_i18n_handler_from_type(resource.i18n_type)
-    handler = parser(resource = resource, language = language)
+
     try:
-        handler.compile()
+        template = _compile_translation_template(resource, language)
     except:
         request.user.message_set.create(message=_("Error compiling translation file."))
         return HttpResponseRedirect(reverse('resource_detail',
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.