def save(self, commit = True, user = None): """ Store the new catalog file """ if self.catalogfile is None: # New Catalog file, create it self.catalogfile = CatalogFile() # Set the properties if 'language' in self.clean_data: self.catalogfile.language = self.clean_data['language'] if 'project' in self.clean_data: self.catalogfile.project = self.clean_data['project'] if 'catalog' in self.clean_data: self.catalogfile.catalog = self.clean_data['catalog'] if 'status' in self.clean_data: self.catalogfile.status = self.clean_data['status'] # Check if there is any file uploaded if self.clean_data['catfile'] is not None: # Save the catalog file content and original file name self.catalogfile.origfile = self.clean_data['catfile']['filename'] (_nothing, extension) = self.catalogfile.origfile.rsplit('.', 1) self.catalogfile.filetype = CFT.get_type(extension) # Save the file self.catalogfile.save_file(self.clean_data['catfile']['content']) # Set the user if user is not None: self.catalogfile.user = user # Save the catalogfile model if requested if commit == True: self.catalogfile.save() # Return the model return self.catalogfile
def clean_catfile(self): """ This field keeps the file content, not the file name """ if 'catfile' in self.clean_data: if self.clean_data['catfile'] is not None: # Get the file extension (_nothing, extension) = self.clean_data['catfile']['filename'].rsplit('.', 1) # Check the extension if CFT.check_extension(extension): return self.clean_data['catfile'] else: raise forms.ValidationError(_('Unsupported file type')) return None
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