def export(*args): """ Export a catalog to the corresponding file name """ filename = args[1] try: args[2] except: proofread = False else: proofread = True fn = path.basename(filename) fn, ext = fn.rsplit('.', 1) if ext == 'xlf': ext = 'xliff' try: project, catalog, language = fn.split('-') except ValueError: project, catalog = fn.split('-') language = 'en_US' from translator.apps.impex.models import Exporter exporter = Exporter(language_code = language, project_code = project, catalog_code = catalog, user = None, proofread = proofread) content = exporter.export(ext) out = open(filename, 'wb') out.write(content.read()) out.close()
def exporter(request, language_code = None, project_code = None, catalog_code = None, filename = None, filter = None, untranslated = None, proofread = None, format = 'xliff', testpacktrans = False, **kwargs): """ Wrapper for the exporter """ # Check the request if request.POST: try: format = request.POST['export-catalog-format'] except: request.user.alerts.create(type = 'error', message = 'Unknown format.') format = None try: language_code = request.POST['export-catalog-language'] except: request.user.alerts.create(type = 'error', message = 'Unknown language.') language_code = None try: project_code = request.POST['export-catalog-project'] except: request.user.alerts.create(type = 'error', message = 'Unknown project.') project_code = None try: catalog_code = request.POST['export-catalog-catalog'] except: request.user.alerts.create(type = 'error', message = 'Unknown catalog.') catalog_code = None try: modifier = request.POST['export-catalog-modifier'] except: untranslated = None proofread = None else: if modifier == 'proofread': proofread = modifier elif modifier == 'untranslated': untranslated = modifier try: filename = request.POST['export-catalog-filename'] except: filename = None try: filter = request.POST['export-catalog-filter'] except: filter = None testpacktrans = request.POST.get('export-catalog-testpacktrans', 'yes') == 'yes' try: # The exporter object exporter = Exporter( language_code = language_code, project_code = project_code, catalog_code = catalog_code, user = request.user, filter = filter, untranslated = untranslated, proofread = proofread, testpacktrans = testpacktrans) except ExporterSourceIterationError: # Cannot find the iteration of the source catalog response = page_not_found(request, _('Cannot find the iteration of the reviewed catalog.')) else: try: # Get the content content = exporter.export(format, **kwargs) except ExporterUnsupportedFormat: # The format is not supported response = page_not_found(request, _('Unsupported format: %s' % format)) except ExporterForbidden: # The user is not allowed to export response = page_forbidden(request, _('You are not allowed to export to the %s format.' % format)) except ExporterSourceIterationError: # Cannot find the iteration of the source catalog response = page_not_found(request, _('The catalog needs to be transfered again. Please contact the system administrator.')) else: # Create the HttpResponse object with the appropriate header if filename is None: filename = exporter.create_file_name(CFT.get_extension(format)) response = HttpResponse(content.getvalue(), mimetype = CFT.get_mime(format)) response['Content-Disposition'] = 'attachment; filename="%s"' % filename # Return the response return response