def _zip_file_response(self): uploaded_file = self.convert_translation_form.cleaned_data.get( 'upload_file') uploaded_zipfile = ZipFile(uploaded_file) mem_file = BytesIO() with ZipFile(mem_file, 'w') as zipfile: for file_info in uploaded_zipfile.filelist: filename = file_info.filename if filename.endswith('.po'): po_file = BytesIO(uploaded_zipfile.read(filename)) wb = self._generate_excel_file(po_file) result_filename = filename.split('.po')[0] zipfile.writestr(result_filename + '.xlsx', get_file_content_from_workbook(wb)) elif filename.endswith('.xls') or filename.endswith('.xlsx'): worksheet = openpyxl.load_workbook( BytesIO(uploaded_zipfile.read(filename))).worksheets[0] po_file_content = self._generate_po_content(worksheet) result_filename = filename.split('.xls')[0] zipfile.writestr(result_filename + '.po', po_file_content) else: assert False, "unexpected filename: {}".format(filename) mem_file.seek(0) response = HttpResponse(mem_file, content_type="text/html") zip_filename = 'Converted-' + uploaded_zipfile.filename.split( '.zip')[0] response['Content-Disposition'] = safe_filename_header( zip_filename, "zip") return response
def _excel_file_response(self): wb = self._generate_excel_file( self.convert_translation_form.cleaned_data.get('upload_file')) content = get_file_content_from_workbook(wb) response = HttpResponse(content, content_type="text/html; charset=utf-8") response['Content-Disposition'] = safe_filename_header( self._uploaded_file_name.split('.po')[0], 'xlsx') return response
def _pull_resource(self, request): resource_slug = self.pull_resource_form.cleaned_data['resource_slug'] project_slug = self.pull_resource_form.cleaned_data['transifex_project_slug'] file_response = self._generate_response_file(request.domain, project_slug, resource_slug) if isinstance(file_response, Workbook): content = get_file_content_from_workbook(file_response) response = HttpResponse(content, content_type="text/html; charset=utf-8") response['Content-Disposition'] = safe_filename_header(resource_slug, "xlsx") else: response = HttpResponse(file_response, content_type="text/html; charset=utf-8") response['Content-Disposition'] = safe_filename_header(project_slug, "zip") return response
def _pull_resource(self, request): resource_slug = self.pull_resource_form.cleaned_data['resource_slug'] project_slug = self.pull_resource_form.cleaned_data['transifex_project_slug'] file_response = self._generate_response_file(request.domain, project_slug, resource_slug) if isinstance(file_response, Workbook): content = get_file_content_from_workbook(file_response) response = HttpResponse(content, content_type="text/html; charset=utf-8") response['Content-Disposition'] = safe_filename_header(resource_slug, "xlsx") else: response = HttpResponse(file_response, content_type="text/html; charset=utf-8") response['Content-Disposition'] = safe_filename_header(project_slug, "zip") return response
def _generate_zip_file(transifex, target_lang): mem_file = BytesIO() with ZipFile(mem_file, 'w') as zipfile: for resource_slug in transifex.resource_slugs: wb = Workbook(write_only=True) ws = wb.create_sheet(title='translations') ws.append(['context', 'source', 'translation', 'occurrence']) for po_entry in transifex.client.get_translation(resource_slug, target_lang, False): ws.append([po_entry.msgctxt, po_entry.msgid, po_entry.msgstr, po_entry.occurrences[0][0] if po_entry.occurrences else '']) zipfile.writestr(resource_slug + '.xlsx', get_file_content_from_workbook(wb)) mem_file.seek(0) return mem_file
def _generate_zip_file(transifex, target_lang): mem_file = BytesIO() with ZipFile(mem_file, 'w') as zipfile: for resource_slug in transifex.resource_slugs: wb = Workbook(write_only=True) ws = wb.create_sheet(title='translations') ws.append(['context', 'source', 'translation', 'occurrence']) for po_entry in transifex.client.get_translation(resource_slug, target_lang, False): ws.append([po_entry.msgctxt, po_entry.msgid, po_entry.msgstr, po_entry.occurrences[0][0] if po_entry.occurrences else '']) zipfile.writestr(resource_slug + '.xlsx', get_file_content_from_workbook(wb)) mem_file.seek(0) return mem_file
def get(self, request, *args, **kwargs): lang = request.GET.get('lang') if lang: from corehq.apps.hqmedia.view_helpers import download_audio_translator_files eligible_for_transifex_only = request.GET.get('skip_blacklisted', "false") == "true" files = download_audio_translator_files(self.domain, self.app, lang, eligible_for_transifex_only) zip_in_memory = io.BytesIO() with zipfile.ZipFile(zip_in_memory, "w", zipfile.ZIP_DEFLATED) as zip_content: for filename, workbook in files.items(): zip_content.writestr(filename, get_file_content_from_workbook(workbook)) today = datetime.strftime(datetime.utcnow(), "%Y-%m-%d") filename = "Audio Translator Files {} {}.zip".format(lang, today) zip_in_memory.seek(0) response = HttpResponse(zip_in_memory.read(), content_type='application/zip') response['Content-Disposition'] = 'attachment; filename="{}"'.format(filename) return response return super().get(request, *args, **kwargs)
def _zip_file_response(self): uploaded_file = self.convert_translation_form.cleaned_data.get('upload_file') uploaded_zipfile = ZipFile(uploaded_file) mem_file = BytesIO() with ZipFile(mem_file, 'w') as zipfile: for file_info in uploaded_zipfile.filelist: filename = file_info.filename if filename.endswith('.po'): po_file = BytesIO(uploaded_zipfile.read(filename)) wb = self._generate_excel_file(po_file) result_filename = filename.split('.po')[0] zipfile.writestr(result_filename + '.xlsx', get_file_content_from_workbook(wb)) elif filename.endswith('.xls') or filename.endswith('.xlsx'): worksheet = openpyxl.load_workbook(BytesIO(uploaded_zipfile.read(filename))).worksheets[0] po_file_content = self._generate_po_content(worksheet) result_filename = filename.split('.xls')[0] zipfile.writestr(result_filename + '.po', po_file_content) else: assert False, "unexpected filename: {}".format(filename) mem_file.seek(0) response = HttpResponse(mem_file, content_type="text/html") zip_filename = 'Converted-' + uploaded_zipfile.filename.split('.zip')[0] response['Content-Disposition'] = safe_filename_header(zip_filename, "zip") return response
def _excel_file_response(self): wb = self._generate_excel_file(self.convert_translation_form.cleaned_data.get('upload_file')) content = get_file_content_from_workbook(wb) response = HttpResponse(content, content_type="text/html; charset=utf-8") response['Content-Disposition'] = safe_filename_header(self._uploaded_file_name.split('.po')[0], 'xlsx') return response