def testSelectExistingContentCreateReference(self):
     self.field.widget = UploadReferenceWidget()
     form = {
         'relatedItems': [self.image.UID()],
         'relatedItems_option': 'select',
     }
     self.request.form.update(form)
     self.doc.processForm(REQUEST=self.request)
     expected = [self.image]
     result = self.doc.getRefs('relatesTo')
     self.assertEqual(expected, result)
 def testSelectExistingContentProcessWidget(self):
     self.field.widget = UploadReferenceWidget()
     form = {
         'relatedItems': [self.image.UID()],
         'relatedItems_option': 'select',
     }
     expected = [self.image.UID()], {}
     result = self.field.widget.process_form(self.doc,
                                             self.field,
                                             form,
                                             validating=False)
     self.assertEqual(expected, result)
 def testSelectMultipleExistingContentCreateReferences(self):
     video = _createObjectByType('File', self.portal, id='video')
     self.field.widget = UploadReferenceWidget()
     form = {
         'relatedItems': [self.image.UID(), video.UID()],
         'relatedItems_option': 'select',
     }
     self.request.form.update(form)
     self.doc.processForm(REQUEST=self.request)
     expected = [self.image, video]
     result = self.doc.getRefs('relatesTo')
     self.assertEqual(expected, result)
 def uploadContentToPath(self, obj, path, destination):
     field = obj.getField('relatedItems')
     fileupload = FakeFileUpload(filename='enfold.pdf', file=self.PDF)
     field.widget = UploadReferenceWidget(startup_directory=path)
     form = {
         'relatedItems_option': 'upload',
         'relatedItems_file': [fileupload],
     }
     self.request.form.update(form)
     obj.processForm(REQUEST=self.request)
     expected = destination.objectValues('ATFile')
     result = obj.getRefs('relatesTo')
     self.assertEqual(expected, result)
 def testSelectMultipleExistingContentProcessWidget(self):
     video = _createObjectByType('File', self.portal, id='video')
     self.field.widget = UploadReferenceWidget()
     form = {
         'relatedItems': [self.image.UID(), video.UID()],
         'relatedItems_option': 'select',
     }
     expected = [self.image.UID(), video.UID()], {}
     result = self.field.widget.process_form(self.doc,
                                             self.field,
                                             form,
                                             validating=False)
     self.assertEqual(expected, result)
 def testUploadReplaceCurrentReferencesForSingleValuedField(self):
     fileupload = FakeFileUpload(filename='enfold.pdf', file=self.PDF)
     self.field.multiValued = False
     self.field.widget = UploadReferenceWidget(startup_directory='test')
     self.portal.invokeFactory('Folder', id='test')
     form = {
         'relatedItems': self.image.UID(),
         'relatedItems_option': 'upload',
         'relatedItems_file': [fileupload],
     }
     self.request.form.update(form)
     self.doc.processForm(REQUEST=self.request)
     expected = self.portal.test.objectValues()
     result = self.doc.getRefs('relatesTo')
     self.assertEqual(expected, result)
 def testUploadFileCreateObjectAndReference(self):
     fileupload = FakeFileUpload(filename='enfold.pdf', file=self.PDF)
     self.field.widget = UploadReferenceWidget(startup_directory='test')
     self.portal.invokeFactory('Folder', id='test')
     form = {
         'relatedItems_option': 'upload',
         'relatedItems_file': [fileupload],
     }
     self.request.form.update(form)
     self.doc.processForm(REQUEST=self.request)
     expected = self.portal.test.objectValues('ATFile')
     result = self.doc.getRefs('relatesTo')
     self.assertEqual(expected, result)
     self.assertEqual(len(result), 1)
     self.assertEqual(result[0].getContentType(), 'application/pdf')
 def testUploadKeepReferenceWhenUploadingWithoutAValueForMultiValuedField(
         self):
     fileupload = FakeFileUpload(filename='', file=StringIO())
     self.field.widget = UploadReferenceWidget(startup_directory='test')
     self.portal.invokeFactory('Folder', id='test')
     form = {
         'relatedItems': [self.image.UID()],
         'relatedItems_option': 'upload',
         'relatedItems_file': [fileupload],
     }
     self.request.form.update(form)
     self.doc.processForm(REQUEST=self.request)
     self.assertEqual(self.portal.test.objectValues(), [])
     expected = [self.image]
     result = self.doc.getRefs('relatesTo')
     self.assertEqual(expected, result)
 def testUploadKeepCurrentReferencesForMultiValuedFieldWithMultipleUploads(
         self):
     fileupload1 = FakeFileUpload(filename='enfold.pdf', file=self.PDF)
     fileupload2 = FakeFileUpload(filename='enfold.gif', file=self.IMG)
     self.field.widget = UploadReferenceWidget(startup_directory='test')
     self.portal.invokeFactory('Folder', id='test')
     form = {
         'relatedItems': [self.image.UID()],
         'relatedItems_option': 'upload',
         'relatedItems_file': [fileupload1, fileupload2],
     }
     self.request.form.update(form)
     self.doc.processForm(REQUEST=self.request)
     expected = self.portal.test.objectValues() + [self.image]
     result = self.doc.getRefs('relatesTo')
     self.assertEqual(len(expected), 3)
     self.assertEqual(expected, result)
    def testValidateUploadWithoutSelectAFileInARequiredField(self):
        fileupload = FakeFileUpload(filename='', file=StringIO())
        self.field.widget = UploadReferenceWidget()
        self.field.required = True
        self.field.multiValued = False

        # First force a validation error, as we don't pass anything:
        form = {
            'relatedItems': '',
            'relatedItems_option': 'upload',
            'relatedItems_file': [fileupload],
        }
        value, errors = self.field.widget.process_form(self.doc,
                                                       self.field,
                                                       form,
                                                       validating=False)
        result = self.field.validate(value, self.doc, errors)
        self.failUnless('required' in result)

        # Then pass something as the field previous value, no error this time:
        form = {
            'relatedItems': 'something',
            'relatedItems_option': 'upload',
            'relatedItems_file': [fileupload],
        }
        value, errors = self.field.widget.process_form(self.doc,
                                                       self.field,
                                                       form,
                                                       validating=False)
        result = self.field.validate(value, self.doc, errors)
        self.assertEqual(result, None)

        # Finally, upload a valid file. Again, no error:
        fileupload = FakeFileUpload(filename='foo.txt', file=StringIO('foo'))
        form = {
            'relatedItems': '',
            'relatedItems_option': 'upload',
            'relatedItems_file': [fileupload],
        }
        value, errors = self.field.widget.process_form(self.doc,
                                                       self.field,
                                                       form,
                                                       validating=False)
        result = self.field.validate(value, self.doc, errors)
        self.assertEqual(result, None)
示例#11
0
from Products.Archetypes.public import BaseSchema
from Products.Archetypes.public import BaseContent
from Products.Archetypes.public import ReferenceField
from archetypes.uploadreferencewidget.widget import UploadReferenceWidget
from archetypes.uploadreferencewidget import config

URWSchema = BaseSchema.copy() + Schema((
    ReferenceField(
        name='ref1',
        allowed_types=('File', 'Image'),
        relationship='rel1',
        required=True,
        widget=UploadReferenceWidget(
            label='Single Reference',
            description='This is the first field. It can reference a '
            'single item. This field uses the site root as '
            'the the startup directory.',
            startup_directory='/',
        ),
    ),
    ReferenceField(
        name='ref2',
        multiValued=True,
        allowed_types=('File', 'Image'),
        relationship='rel2',
        widget=UploadReferenceWidget(
            label='Multiple References',
            description='This is the second field. It can reference '
            'multiple items. This field uses the current '
            'folder as the startup directory.',
            startup_directory='',