def render(self):
     relation_id = self.relation.id
     if self.relation.linkage.relation.cardinality.id == 'ONE_TO_MANY':
         multi_selection = 'true'
     else:
         multi_selection = 'false'
     i = ScopedMarkup()
     i.script('var objectPicker = new p2.ObjectPicker(\'%s\', \'%s\', %s, \'%s\');' % (
         self.relation_source.id,
         relation_id,
         multi_selection,
         self.collection_id
     ))
     i.html('<div style="position: relative">')
     a = 0
     query = super(ObjectPicker, self).query()
     for (setobject, linked) in query:
         alternation = (a % 2 == 0) and 'even' or 'odd'
         a += 1
         id = generate_random_identifier()
         i.html("""
                <div style="position:absolute; z-index:10000;">
                     <input id="%s" value="%s" type="checkbox" />
                </div>
                """ % (id, setobject.id))
         subform = self.call_subform(self.collection_id ,setobject.id, linked, alternation)
         subform = subform.replace(r'\"', r'\\\"')
         i.html(subform)
         i.html('</div>')
         i.script('objectPicker.registerCheckbox($(\'#%s\'), %s)' % (id, linked)) 
     html = i.render()
     return html
 def generate_random_identifier(self):
     return generate_random_identifier()
    def update(self):
        self.error = False
        self.response.setHeader('Content-Type', 'application/json')
        
        upload_file = self.request.form['file']
        size = self.request.getHeader('X-File-Size')
        thumbnail_width = int(self.request.getHeader('X-thumbnail-width'))
        thumbnail_height = int(self.request.getHeader('X-thumbnail-height'))
        self.filename = self.request.getHeader('X-File-Name')

        self.identifier = generate_random_identifier()
        media = Media()
        media.id = self.identifier
        media.size = size
        media.filename = self.filename
        
        # Check maximum file size first:
        db_util = getUtility(IDbUtility)
        if int(media.size) > db_util.max_allowed_packet:
            # Problem: Database does not allow BLOBs of this size.
            self.error = True
            self.message = 'The selected file is too big. Please contact your system administrator.'
            self.title = 'Upload failed.'
            upload_file.close()
            return    
        
        # Read data:
        chunk = upload_file.read()
        while chunk != '':
            # Initialise our data array
            if media.data == None:
                media.data = bytearray()
            # Add data
            media.data += chunk
            # Check maximum length
            if len(media.data) > int(media.size):
                # Cancel upload here so uploader cannot throw an endless stream of bytes at us
                self.error = True
                self.message = 'The selected file is bigger than specified.'
                self.title = 'Upload failed.'
                upload_file.close()
                return
            chunk = upload_file.read()
        
        mime_type = getUtility(IMimeTypeGetter)(name=self.filename, data=media.data)
        if not mime_type:
            mime_type = upload_file.headers('Content-Type')
        media.mime_type = mime_type
        
        thumbnail_file = None
        self.has_thumbnail = False
        if mime_type in ('image/jpeg', 'image/png', 'image/gif'):
            thumbnail_file = self.create_thumbnail(upload_file, (thumbnail_width, thumbnail_height))
            self.has_thumbnail = True
            chunk = thumbnail_file.read()
            while chunk != '':
                if media.thumbnail == None:
                    media.thumbnail = bytearray()
                media.thumbnail += chunk
                chunk = thumbnail_file.read()
            thumbnail_file.close()       
        
        upload_file.close()
        
        session = db_util.Session()    
        session.add(media)
        session.commit()
 def __init__(self):
     self.script_id = generate_random_identifier()
     self._snippets = list()
Exemplo n.º 5
0
 def link(self, klass):
     klass.fetch_linkages()
     for linkage in klass.linkages.itervalues():
         self.collections[linkage.attr_name] = {
             'collection_id': generate_random_identifier(),
             'linkage': linkage}
Exemplo n.º 6
0
 def __init__(self):
     if not self.id:
         self.id = generate_random_identifier()
     self.action = 'new'
     self.common_init()
Exemplo n.º 7
0
 def generate_identifier(self):
     """Generates a identifier for the setobject. It is at the moment a random identifier, but probably there should later
     be a more deterministic way of doing that.
     """
     return generate_random_identifier()