Exemple #1
0
 def bulk(self, upload):
   f = File()
   
   f.title, _, _ = upload.filename.rpartition('.')
   f.content = upload.file
   f.content.content_type = f.mimetype = upload.type
   f.content.filename = f.filename = upload.filename
   f.size = upload.length
   
   
   siblings = [i.name for i in Asset.objects(parent=self.controller.asset).only('name')]
   f.name = normalize(f.title.lower(), siblings)
   
   f.save()
   f.attach(self.controller.asset)
   
   return 'json:', dict()
Exemple #2
0
 def _save(self, action, asset, data):
     from web.extras.contentment.components.asset.model import Asset
     
     form = self._form(asset, web.core.request.referrer if action == 'create' else None, action.title())
     
     if not data:
         return action, dict(kind=asset.__class__.__name__, form=form, data=asset.prepare())
     
     data.pop('submit', None)
     
     try:
         result, remaining = form.native(data)
     
     except:
         if web.core.config.get('debug', False): raise
         
         log.exception("Error processing form.")
         web.core.session['flash'] = dict(cls="error", title="Server Error", message="Unable to create asset; see system log for details." % (asset.__class__.__name__, asset.title, asset.path))
         
         return action, dict(kind=asset.__class__.__name__, form=form, data=asset.prepare())
     
     # Root node must not be renamed.
     if asset.path == '/': del result['name']
     
     dirty = []
     
     siblings = [i.name for i in Asset.objects(parent=self.asset if action=="create" else self.asset.parent).only('name')]
     if asset.path != '/' and ( 'name' not in result or not result['name'] ):
         result['name'] = normalize(result['title'].lower(), siblings)
     
     elif asset.path != '/' and action != "create":
         result['name'] = normalize(result['name'].lower(), [i for i in siblings if i != asset.name])
     
     elif asset.path != '/' and action == "create":
         result['name'] = normalize(result['name'].lower(), siblings)
     
     del siblings
     
     if result.get('tags', None) is None:
         result['tags'] = []
     
     # Handle explicit setting of the modification time.
     if action == 'modify' and ( not result['modified'] or result['modified'] == asset.modified.replace(microsecond=0) ):
         result['modified'] = datetime.now(UTC)
     
     result = asset.process(result)
     
     for name, value in result.iteritems():
         if action == 'modify' and getattr(asset, name) == value: continue
         dirty.append(name)
         setattr(asset, name, value)
     
     try:
         asset.save(dirty=dirty)
     
     except:
         if web.core.config.get('debug', False):
             raise
         
         log.exception('Error saving record.')
         web.core.session['flash'] = dict(cls="error", title="Server Error", message="Unable to save asset; see system log for details." % (asset.__class__.__name__, asset.title, asset.path))
         return action, dict(kind=asset.__class__.__name__, form=form, data=asset.prepare())
     
     if action == 'create':
         asset.attach(self.asset)