def save(self, set_modified=True, pull_parent_from_old_files=True): if set_modified: self._modified = dateutil.utcnow() if not getattr(self, '_created', None): self._created = self._modified doc = self._get_nonschema_mongo_save_document() old_file_ids = [] new_file_ids = [] gridfs = dbutil.get_gridfs(self.request) if self._id: doc['_id'] = self._id for item in gridfs._GridFS__files.find({'parents':self._id}, fields=[]): old_file_ids.append(item['_id']) schema_values = _prep_schema_values_for_save(self.get_schema_values(), gridfs, new_file_ids) doc.update(schema_values) _id = self._get_collection().save(doc, safe=True) if not self._id: self._id = _id # Update file parents: # FIXME: Reduce to at most 2 updates by using "$in" queries for "_id". if pull_parent_from_old_files: for id in old_file_ids: if id not in new_file_ids: gridfs._GridFS__files.update({'_id':id}, {"$pull":{"parents":self._id}}) for id in new_file_ids: if id not in old_file_ids: gridfs._GridFS__files.update({'_id':id}, {"$addToSet":{"parents":self._id}})
def log_history(self, action, ids, **kwargs): doc = dict( time = utcnow(), user = authenticated_userid(self.request), action = action, ids = ids, ) doc.update(**kwargs) self._get_collection().save(doc, safe=True)
def move_child(self, obj): if obj.__parent__._id == self._id: return orig_parent = obj.__parent__ orig_name = obj.__name__ obj._memento = dict( orig_name = orig_name, orig_parent_id = orig_parent._id, orig_parent_path = orig_parent.resource_path(), trashed_at = utcnow(), trashed_by = authenticated_userid(self.request), ) obj.__parent__ = self obj.__name__ = str(obj._id) obj.save() # FIXME: set_modified=False? unindex_recursively(obj, include_self=True) # Notify old parent that child was moved (gives ordered folders an opportunity to update their ordered name list). orig_parent._child_removed(orig_name)
def populate(folder, request): # Read a file containing 100 paragraphs of lorem ipsum text. f = open('lorem.txt') raw = f.read() f.close() lorem = [x for x in raw.split('\n') if x] for n in range(50): num = n+1 p1 = lorem[n*2] p2 = lorem[(n*2)+1] title = "Article %s" % num body = "<p>%s</p>\n<p>%s</p>" % (p1, p2) name = "article-%s" %num dateline = utcnow(zero_seconds=True) #obj = Article(request, title=title, body=body, dateline=dateline, description='meh', attachments=[], list_attachments=False) #folder.add_child(name, obj) command.create(request, folder, Article, name, dict(title=title, body=body, dateline=dateline, description='meh', attachments=[], list_attachments=False))
def set_last_logged_in(self, timestamp=None): if not timestamp: timestamp = utcnow() self.last_logged_in = timestamp
def get_class_schema(cls, request=None): schema = Content.get_class_schema(request) #schema.add(colander.SchemaNode(colander_types.DateUS(), name='dateline', default=today_for_request_tz(request))) schema.add(colander.SchemaNode(colander_types.DateTimeUS(get_timezone_for_request(request)), name='dateline', default=utcnow())) schema.add(colander.SchemaNode(colander.String(), name='body', widget=widgets.get_html_widget())) # Single file upload: #schema.add(colander.SchemaNode(deform.FileData(), name='attachment', widget=widgets.get_fileupload_widget(request))) # Sequence of file uploads: schema.add(colander.SchemaNode(colander.Sequence(), colander.SchemaNode(deform.FileData(), widget=widgets.get_fileupload_widget(request)), name='attachments', missing=[], default=[])) schema.add(colander.SchemaNode(colander.Boolean(), name='list_attachments', title="List attachments after body?", default=False, missing=False)) return schema