def validate_content(fileName, contentMetaData): # make sure the uploaded version is valid xml exitMessages = DaisyPipeline.validate(fileName) if exitMessages: return exitMessages # make sure the meta data of the uploaded version corresponds # to the meta data in the document xmlContent = XMLContent() try: errorList = xmlContent.validateContentMetaData(fileName , **contentMetaData) except etree.XMLSyntaxError as e: return "The uploaded file is not a valid DTBook XML document:" + e.message if errorList: return "; ".join( ("The meta data '%s' in the uploaded file does not correspond to the value in the document: '%s' instead of '%s'" % errorTuple for errorTuple in errorList))
def validate_content(fileName, contentMetaData, removeFile=False): # make sure the uploaded version is valid xml exitMessages = DaisyPipeline.validate(fileName) if exitMessages: if removeFile: os.remove(fileName) raise forms.ValidationError( ["The uploaded file is not a valid DTBook XML document: "] + exitMessages) # make sure the meta data of the uploaded version corresponds # to the meta data in the document xmlContent = XMLContent() errorList = xmlContent.validateContentMetaData(fileName , **contentMetaData) if removeFile: os.remove(fileName) if errorList: raise forms.ValidationError( map(lambda errorTuple : "The meta data '%s' in the uploaded file does not correspond to the value in the document: '%s' instead of '%s'" % errorTuple, errorList))
def update_xml_with_metadata(document, **params): user = get_abacus_user() if document.version_set.count() == 0: # create an initial version contentString = XMLContent.getInitialContent(document) content = ContentFile(contentString) version = Version.objects.create( comment = "Initial version created from meta data", document = document, created_by = user) version.content.save("initial_version.xml", content) else: # create a new version with the new content xmlContent = XMLContent(document.latest_version()) contentString = xmlContent.getUpdatedContent(**params) content = ContentFile(contentString) version = Version.objects.create( comment = "Updated version due to meta data change", document = document, created_by = user) version.content.save("updated_version.xml", content)
def save(self): instance = super(PartialDocumentForm, self).save() if instance.version_set.count() == 0: # create an initial version contentString = XMLContent.getInitialContent(instance) content = ContentFile(contentString) version = Version.objects.create( comment = "Initial version created from meta data", document = instance, created_by = self.user) version.content.save("initial_version.xml", content) else: # create a new version with the new content xmlContent = XMLContent(instance.latest_version()) contentString = xmlContent.getUpdatedContent(**self.cleaned_data) content = ContentFile(contentString) version = Version.objects.create( comment = "Updated version due to meta data change", document = instance, created_by = self.user) version.content.save("updated_version.xml", content) return instance
def setUp(self): # clean the archive os.rename(settings.MEDIA_ROOT, settings.MEDIA_ROOT + '.old') os.mkdir(settings.MEDIA_ROOT) user = User.objects.get(pk=1) self.document = Document(title="A Title", author="An Author", language="de") self.document.save() contentString = XMLContent.getInitialContent(self.document) content = ContentFile(contentString) v = Version.objects.create( comment = "Initial version created from meta data", document = self.document, created_by=user) v.content.save("initial_version.xml", content)
def import_metadata_csv(request): if request.method != 'POST': return HttpResponseRedirect(reverse('upload_metadata_csv')) DocumentFormSet = modelformset_factory(Document, fields=('author', 'title', 'identifier', 'source', 'source_edition', 'source_publisher', 'language', 'production_series', 'production_series_number', 'production_source'), can_delete=True) ProductFormset = formset_factory(PartialProductForm) document_formset = DocumentFormSet(request.POST, prefix='documents') product_formset = ProductFormset(request.POST, prefix='products') if not document_formset.is_valid() or not product_formset.is_valid(): return render_to_response('documents/manage_import_metadata_csv.html', locals(), context_instance=RequestContext(request)) instances = document_formset.save() for instance in instances: if instance.version_set.count() == 0: # create an initial version contentString = XMLContent.getInitialContent(instance) content = ContentFile(contentString) version = Version.objects.create( comment = "Initial version created from meta data", document = instance, created_by = request.user) version.content.save("initial_version.xml", content) products = {} for form in product_formset.forms: isbn, product_number = form.cleaned_data['isbn'], form.cleaned_data['productNumber'] products.setdefault(isbn, set()).add(product_number) for document in Document.objects.filter(source__in=products.keys()): for product_number in products[document.source]: if product_number.startswith('PS'): type = 0 elif product_number.startswith('GD'): type = 1 elif product_number.startswith('EB'): type = 2 elif product_number.startswith('ET'): type = 3 else: continue Product.objects.get_or_create(document=document, type=type, identifier=product_number) return HttpResponseRedirect(reverse('manage_index'))
def import_metadata_csv(request): if request.method != 'POST': return HttpResponseRedirect(reverse('upload_metadata_csv')) DocumentFormSet = modelformset_factory(Document, fields=('author', 'title', 'identifier', 'source', 'source_edition', 'source_publisher', 'language', 'production_series', 'production_series_number'), can_delete=True) formset = DocumentFormSet(request.POST) if not formset.is_valid(): return render_to_response('documents/manage_import_metadata_csv.html', locals(), context_instance=RequestContext(request)) instances = formset.save() for instance in instances: if instance.version_set.count() == 0: # create an initial version contentString = XMLContent.getInitialContent(instance) content = ContentFile(contentString) version = Version.objects.create( comment = "Initial version created from meta data", document = instance, created_by = request.user) version.content.save("initial_version.xml", content) return HttpResponseRedirect(reverse('manage_index'))