Ejemplo n.º 1
0
 def test_get_settings(self):
     registry = getUtility(IRegistry)
     fields = deepcopy(registry['castle.file_upload_fields'])
     for field in fields:
         field['required'] = unicode(field['required']).lower()
     self.assertEquals(len(get_upload_fields(registry)), 5)
     fields.append({
         u'name': u'foobar',
         u'label': u'Foobar',
         u'widget': u'text',
         u'required': u'false',
         u'for-file-types': u'*'
     })
     registry['castle.file_upload_fields'] = fields
     self.assertEquals(len(get_upload_fields(registry)), 6)
Ejemplo n.º 2
0
 def test_fallback_to_default_settings_until_upgrade_done(self):
     registry = getUtility(IRegistry)
     del registry.records['castle.file_upload_fields']
     record = Record(List(), [u'title', u'description'])
     registry.records['castle.required_file_upload_fields'] = record
     fields = get_upload_fields(registry)
     self.assertTrue(fields[0]['required'])
     self.assertTrue(fields[1]['required'])
     self.assertEquals(len(fields), 4)
Ejemplo n.º 3
0
    def test_handle_bad_field_values(self):
        # requires fields defined
        registry = getUtility(IRegistry)
        fields = deepcopy(registry['castle.file_upload_fields'])
        fields.append({})
        registry['castle.file_upload_fields'] = deepcopy(fields)
        self.assertEquals(len(get_upload_fields(registry)), 5)

        # auto fill in for missing
        fields.append({
            u'name': u'foobar'
        })
        registry['castle.file_upload_fields'] = fields

        fields = get_upload_fields(registry)
        self.assertEquals(len(fields), 6)
        self.assertEquals(fields[-1]['label'], 'Foobar')
        self.assertEquals(fields[-1]['required'], False)
        self.assertEquals(fields[-1]['widget'], 'text')
        self.assertEquals(fields[-1]['for-file-types'], '*')
Ejemplo n.º 4
0
    def create_object(self, folder, type_, info):
        filename = info['name']
        name = filename.decode("utf8")
        chooser = INameChooser(folder)
        chooser_name = name.lower().replace('aq_', '')
        newid = chooser.chooseName(chooser_name, folder.aq_parent)

        # strip metadata from file
        if (type_ in ('Image', 'File', 'Video', 'Audio')
                and exiftool is not None and 'tmp_file' in info):
            try:
                exiftool(info['tmp_file'])
            except Exception:
                logger.warn('Could not strip metadata from file: %s' %
                            info['tmp_file'])

        fi = open(info['tmp_file'], 'r')
        try:
            # Try to determine which kind of NamedBlob we need
            # This will suffice for standard p.a.contenttypes File/Image
            # and any other custom type that would have 'File' or 'Image' in
            # its type name
            filename = ploneutils.safe_unicode(filename)
            create_opts = dict(type=type_, id=newid, container=folder)
            if 'Image' in type_:
                image = NamedBlobImage(data=fi, filename=filename)
                try:
                    image.focal_point = [
                        float(self.request.form.get('focalX')),
                        float(self.request.form.get('focalY'))
                    ]
                except Exception:
                    pass
                create_opts['image'] = image
            else:
                create_opts['file'] = NamedBlobFile(data=fi, filename=filename)

            for field in get_upload_fields():
                if not field.get('name'):
                    continue
                name = field['name']
                if not self.request.form.get(name):
                    continue
                if name in ('tags', 'subject'):
                    # tags needs to be converted
                    create_opts['subject'] = self.request.form.get(name).split(
                        ';')
                else:
                    create_opts[name] = self.request.form.get(name, '')
            return api.content.create(**create_opts)
        finally:
            fi.close()
Ejemplo n.º 5
0
    def __call__(self):
        data = super(CastleSettingsAdapter, self).__call__()

        if api.user.is_anonymous():
            return data

        folder = self.context
        if not IDexterityContainer.providedBy(folder):
            folder = aq_parent(folder)

        upload_fields = get_upload_fields(self.registry)
        required_fields = [f['name'] for f in upload_fields
                           if f.get('required')]
        data.update({
            'data-file-upload-fields': json.dumps(upload_fields),
            'data-google-maps-api-key': self.registry.get(
                'castle.google_maps_api_key', '') or '',
            'data-folder-url': folder.absolute_url(),

            # b/w compat until resources updated
            'data-required-file-upload-fields': json.dumps(required_fields),
        })

        data.update(self.get_cachable_config_data())

        show_tour = False
        user = api.user.get_current()
        viewed = user.getProperty('tours_viewed', [])
        if ('all' not in viewed and
                set(viewed) != set(['welcome', 'dashboard', 'foldercontents',
                                    'addcontentinitial', 'addcontentadd', 'editpage'])):
            show_tour = True

        if show_tour and not api.env.test_mode():
            data['data-show-tour'] = json.dumps({
                'viewed': viewed
            })

        folder = self.context
        if not ISiteRoot.providedBy(folder) and not IDexterityContainer.providedBy(folder):
            folder = aq_parent(folder)
        site_path = self.site.getPhysicalPath()
        folder_path = folder.getPhysicalPath()
        data['data-base-path'] = '/' + '/'.join(folder_path[len(site_path):])

        real_context = self.context
        if ISiteRoot.providedBy(real_context):
            # we're at site root but actually kind of want context front page
            try:
                real_context = real_context[getDefaultPage(real_context)]
            except (AttributeError, KeyError):
                pass
        if IDashboard.providedBy(real_context):
            real_context = self.site

        transform = theming.getTransform(real_context, self.request)
        if transform is not None:
            data['data-site-layout'] = transform.get_layout_name(real_context)

        data['data-site-default'] = getDefaultPage(self.site) or 'front-page'
        data['data-uid'] = IUUID(real_context, '')

        return data