def edit_item(request, item_id, cat_id): message = "" item = Item.objects.get(ref=item_id, category__id=cat_id) FieldSet = inlineformset_factory(Item, Field, form=FieldForm, fields=('key','value','modified'), can_delete=False, extra=0) if request.method == 'POST': item_form = ItemForm(request.POST, instance=item) form = FieldSet(request.POST, instance = item) if item_form.is_valid() and form.is_valid(): item_form.save() form.save() # Re-make the form with the item from the database so # that `modified` is set if it was changed item = Item.objects.get(ref=item_id, category__id=cat_id) form = FieldSet(instance=item) item_form = ItemForm(instance=item) message = "Item successfully saved." if item.verified and item.category.source_file_type and item.category.collection: if item.fields.filter(key="name").exists(): name = item.fields.get(key="name").value else: name = item.ref document = docserver_create_document(item.category.collection.collectionid, item.ref, name) docserver_upload_and_save_file(document.id, item.category.source_file_type.id, ContentFile(item.to_json())) message += " SourceFile uploaded." else: item_form = ItemForm(instance=item) form = FieldSet(instance = item) return render(request, 'kvedit/edit.html', {'item_form':item_form, 'form': form, 'item': item, "message": message})
def test_upload_and_save_file_audio(self, add_sourcefile, write, makedirs): thefile = StringIO() util.docserver_upload_and_save_file(self.doc.id, self.sft.id, thefile) final_filename = "/root/directory/audio/11/1122-3333-4444/mp3/1122-3333-4444-mp3.mp3" write.assert_called_with(thefile, final_filename) makedirs.assert_called_with("/root/directory/audio/11/1122-3333-4444/mp3") add_sourcefile.assert_called_with(self.doc.id, self.sft.id, final_filename)
def test_upload_and_save_file_data(self, add_sourcefile, write, makedirs): thefile = StringIO() othersft = models.SourceFileType.objects.get_by_slug("csv") util.docserver_upload_and_save_file(self.doc.id, othersft.id, thefile) final_filename = "/root/directory/data/11/1122-3333-4444/csv/1122-3333-4444-csv.csv" write.assert_called_with(thefile, final_filename) makedirs.assert_called_with("/root/directory/data/11/1122-3333-4444/csv") add_sourcefile.assert_called_with(self.doc.id, othersft.id, final_filename)
def test_upload_and_save_file_audio(self, add_sourcefile, write, makedirs): thefile = six.StringIO() util.docserver_upload_and_save_file(self.doc.id, self.sft.id, thefile) final_filename = "/root/directory/audio/11/1122-3333-4444/mp3/1122-3333-4444-mp3.mp3" write.assert_called_with(thefile, final_filename) makedirs.assert_called_with( "/root/directory/audio/11/1122-3333-4444/mp3") add_sourcefile.assert_called_with(self.doc.id, self.sft.id, final_filename)
def test_upload_and_save_file_data(self, add_sourcefile, write, makedirs): thefile = six.StringIO() othersft = models.SourceFileType.objects.get_by_slug("csv") util.docserver_upload_and_save_file(self.doc.id, othersft.id, thefile) final_filename = "/root/directory/data/11/1122-3333-4444/csv/1122-3333-4444-csv.csv" write.assert_called_with(thefile, final_filename) makedirs.assert_called_with( "/root/directory/data/11/1122-3333-4444/csv") add_sourcefile.assert_called_with(self.doc.id, othersft.id, final_filename)
def _save_file(self, external_identifier, file_type, file): try: document = models.Document.objects.get(external_identifier=external_identifier) except models.Document.DoesNotExist: data = {'detail': 'No document with this id'} return response.Response(data, status=status.HTTP_404_NOT_FOUND) try: sft = models.SourceFileType.objects.get(slug=file_type) except models.SourceFileType.DoesNotExist: data = {'detail': 'No filetype with this slug'} return response.Response(data, status=status.HTTP_404_NOT_FOUND) if not file: data = {'detail': 'Need exactly one file called "file"'} return response.Response(data, status=status.HTTP_400_BAD_REQUEST) try: sf, created = util.docserver_upload_and_save_file(document.id, sft.id, file) except IOError as e: data = {'detail': 'Cannot write file'} return response.Response(data, status=status.HTTP_500_INTERNAL_SERVER_ERROR) if created: retstatus = status.HTTP_201_CREATED data = {'detail': 'created'} else: retstatus = status.HTTP_200_OK data = {'detail': 'updated'} return response.Response(data, status=retstatus)
def _save_file(self, external_identifier, file_type, file): try: document = models.Document.objects.get(external_identifier=external_identifier) except models.Document.DoesNotExist: data = {'detail': 'No document with this id'} return response.Response(data, status=status.HTTP_404_NOT_FOUND) try: sft = models.SourceFileType.objects.get(slug=file_type) except models.SourceFileType.DoesNotExist: data = {'detail': 'No filetype with this slug'} return response.Response(data, status=status.HTTP_404_NOT_FOUND) if not file: data = {'detail': 'Need exactly one file called "file"'} return response.Response(data, status=status.HTTP_400_BAD_REQUEST) try: sf, created = util.docserver_upload_and_save_file(document.id, sft.id, file) except IOError as e: data = {'detail': 'Cannot write file'} return response.Response(data, status=status.HTTP_500_INTERNAL_SERVER_ERROR) if created: retstatus = status.HTTP_201_CREATED data = {'detail': 'created'} else: retstatus = status.HTTP_200_OK data = {'detail': 'updated'} return response.Response(data, status=retstatus)
def edit_item(request, item_id, cat_id): message = "" item = Item.objects.get(ref=item_id, category__id=cat_id) FieldSet = inlineformset_factory(Item, Field, form=FieldForm, fields=('key', 'value', 'modified'), can_delete=False, extra=0) if request.method == 'POST': item_form = ItemForm(request.POST, instance=item) form = FieldSet(request.POST, instance=item) if item_form.is_valid() and form.is_valid(): item_form.save() form.save() # Re-make the form with the item from the database so # that `modified` is set if it was changed item = Item.objects.get(ref=item_id, category__id=cat_id) form = FieldSet(instance=item) item_form = ItemForm(instance=item) message = "Item successfully saved." if item.verified and item.category.source_file_type and item.category.collection: if item.fields.filter(key="name").exists(): name = item.fields.get(key="name").value else: name = item.ref document = docserver_create_document( item.category.collection.collectionid, item.ref, name) docserver_upload_and_save_file( document.id, item.category.source_file_type.id, ContentFile(item.to_json())) message += " SourceFile uploaded." else: item_form = ItemForm(instance=item) form = FieldSet(instance=item) return render(request, 'kvedit/edit.html', { 'item_form': item_form, 'form': form, 'item': item, "message": message })