def download_translation_file(translation, fmt=None, units=None): if fmt is not None: try: exporter = get_exporter(fmt)(translation=translation) except KeyError: raise Http404('File format not supported') if units is None: units = translation.unit_set.all() exporter.add_units(units) return exporter.get_response( '{{project}}-{0}-{{language}}.{{extension}}'.format( translation.component.slug)) # Force flushing pending units translation.commit_pending('download', None) srcfilename = translation.get_filename() # Construct file name (do not use real filename as it is usually not # that useful) filename = '{0}-{1}-{2}.{3}'.format(translation.component.project.slug, translation.component.slug, translation.language.code, translation.store.extension) # Create response with open(srcfilename) as handle: response = HttpResponse(handle.read(), content_type=translation.store.mimetype) # Fill in response headers response['Content-Disposition'] = 'attachment; filename={0}'.format( filename) return response
def download_dictionary(request, project, lang): """Export dictionary into various formats.""" prj = get_project(request, project) lang = get_object_or_404(Language, code=lang) # Parse parameters export_format = None if 'format' in request.GET: export_format = request.GET['format'] if export_format not in ('csv', 'po', 'tbx', 'xliff'): export_format = 'csv' # Grab all words words = Dictionary.objects.filter(project=prj, language=lang).order_by(Lower('source')) # Translate toolkit based export exporter = get_exporter(export_format)( prj, lang, get_site_url( reverse('show_dictionary', kwargs={ 'project': prj.slug, 'lang': lang.code })), fieldnames=('source', 'target'), ) # Add words for word in words.iterator(): exporter.add_dictionary(word) # Save to response return exporter.get_response('glossary-{project}-{language}.{extension}')
def download_glossary(request, project, lang): """Export glossary into various formats.""" prj = get_project(request, project) lang = get_object_or_404(Language, code=lang) # Parse parameters export_format = None if "format" in request.GET: export_format = request.GET["format"] if export_format not in ("csv", "po", "tbx", "xliff"): export_format = "csv" # Grab all terms terms = Term.objects.for_project(prj).filter(language=lang).order() # Translate toolkit based export exporter = get_exporter(export_format)( prj, lang, get_site_url( reverse("show_glossary", kwargs={ "project": prj.slug, "lang": lang.code })), fieldnames=("source", "target"), ) # Add terms for term in terms.iterator(): exporter.add_glossary_term(term) # Save to response return exporter.get_response("glossary-{project}-{language}.{extension}")
def download_translation_file(translation, fmt=None, units=None): if fmt is not None: try: exporter = get_exporter(fmt)(translation=translation) except KeyError: raise Http404('File format not supported') if units is None: units = translation.unit_set.all() exporter.add_units(units) response = exporter.get_response( '{{project}}-{0}-{{language}}.{{extension}}'.format( translation.component.slug ) ) else: # Force flushing pending units translation.commit_pending('download', None) filenames = translation.store.get_filenames() if len(filenames) == 1: extension = translation.store.extension # Create response with open(filenames[0], 'rb') as handle: response = HttpResponse( handle.read(), content_type=translation.store.mimetype ) else: extension = 'zip' response = HttpResponse(content_type='application/zip') root = translation.get_filename() with ZipFile(response, 'w') as zipfile: for filename in filenames: with open(filename, 'rb') as handle: zipfile.writestr( os.path.relpath(filename, root), handle.read() ) # Construct filename (do not use real filename as it is usually not # that useful) filename = '{0}-{1}-{2}.{3}'.format( translation.component.project.slug, translation.component.slug, translation.language.code, extension ) # Fill in response headers response['Content-Disposition'] = 'attachment; filename={0}'.format( filename ) if translation.stats.last_changed: response['Last-Modified'] = http_date( mktime(translation.stats.last_changed.timetuple()) ) return response
def download_translation_file(translation, fmt=None, units=None): if fmt is not None: try: exporter_cls = get_exporter(fmt) except KeyError: raise Http404('File format not supported') if not exporter_cls.supports(translation): raise Http404('File format not supported') exporter = exporter_cls(translation=translation) if units is None: units = translation.unit_set.all() exporter.add_units(units) response = exporter.get_response( '{{project}}-{0}-{{language}}.{{extension}}'.format( translation.component.slug ) ) else: # Force flushing pending units translation.commit_pending('download', None) filenames = translation.filenames if len(filenames) == 1: extension = translation.store.extension # Create response with open(filenames[0], 'rb') as handle: response = HttpResponse( handle.read(), content_type=translation.store.mimetype ) else: extension = 'zip' response = zip_download(translation.get_filename(), filenames) # Construct filename (do not use real filename as it is usually not # that useful) filename = '{0}-{1}-{2}.{3}'.format( translation.component.project.slug, translation.component.slug, translation.language.code, extension ) # Fill in response headers response['Content-Disposition'] = 'attachment; filename={0}'.format( filename ) if translation.stats.last_changed: response['Last-Modified'] = http_date( mktime(translation.stats.last_changed.timetuple()) ) return response
def download_translation_file(translation, fmt=None, units=None): if fmt is not None: try: exporter_cls = get_exporter(fmt) except KeyError: raise Http404("File format not supported") if not exporter_cls.supports(translation): raise Http404("File format not supported") exporter = exporter_cls(translation=translation) if units is None: units = translation.unit_set.all() exporter.add_units(units) response = exporter.get_response( "{{project}}-{0}-{{language}}.{{extension}}".format( translation.component.slug ) ) else: # Force flushing pending units translation.commit_pending("download", None) filenames = translation.filenames if len(filenames) == 1: extension = translation.store.extension() # Create response with open(filenames[0], "rb") as handle: response = HttpResponse( handle.read(), content_type=translation.store.mimetype() ) else: extension = "zip" response = zip_download(translation.get_filename(), filenames) # Construct filename (do not use real filename as it is usually not # that useful) filename = "{0}-{1}-{2}.{3}".format( translation.component.project.slug, translation.component.slug, translation.language.code, extension, ) # Fill in response headers response["Content-Disposition"] = "attachment; filename={0}".format(filename) if translation.stats.last_changed: response["Last-Modified"] = http_date( mktime(translation.stats.last_changed.timetuple()) ) return response
def download_translation_file(translation, fmt=None, units=None): if fmt is not None: try: exporter = get_exporter(fmt)(translation=translation) except KeyError: raise Http404('File format not supported') if units is None: units = translation.unit_set.all() exporter.add_units(units) return exporter.get_response( '{{project}}-{0}-{{language}}.{{extension}}'.format( translation.component.slug ) ) # Force flushing pending units translation.commit_pending('download', None) srcfilename = translation.get_filename() # Construct file name (do not use real filename as it is usually not # that useful) filename = '{0}-{1}-{2}.{3}'.format( translation.component.project.slug, translation.component.slug, translation.language.code, translation.store.extension ) # Create response with open(srcfilename, 'rb') as handle: response = HttpResponse( handle.read(), content_type=translation.store.mimetype ) # Fill in response headers response['Content-Disposition'] = 'attachment; filename={0}'.format( filename ) return response
def download_dictionary(request, project, lang): """Export dictionary into various formats.""" prj = get_project(request, project) lang = get_object_or_404(Language, code=lang) # Parse parameters export_format = None if 'format' in request.GET: export_format = request.GET['format'] if export_format not in ('csv', 'po', 'tbx', 'xliff'): export_format = 'csv' # Grab all words words = Dictionary.objects.filter( project=prj, language=lang ).order_by(Lower('source')) # Translate toolkit based export exporter = get_exporter(export_format)( prj, lang, get_site_url(reverse( 'show_dictionary', kwargs={'project': prj.slug, 'lang': lang.code} )), fieldnames=('source', 'target'), ) # Add words for word in words.iterator(): exporter.add_dictionary(word) # Save to response return exporter.get_response( 'glossary-{project}-{language}.{extension}' )