def purge_orphaned_gridfs_files(self, cutoff_hours=24): cutoff = dateutil.utcnow() - datetime.timedelta(hours=cutoff_hours) gridfs = self.get_gridfs() old_ids = [item['_id'] for item in gridfs._GridFS__files.find( {'parents':[], 'lastmodDate':{'$lt':cutoff}}, fields=[])] for id in old_ids: gridfs.delete(id) return len(old_ids)
def test_dateutil(self): import datetime, pytz from audrey import dateutil utc_dt_aware = datetime.datetime(2012, 7, 4, 16, 20, tzinfo=pytz.utc) utc_dt_naive = datetime.datetime(2012, 7, 4, 16, 20) est_dt_aware = datetime.datetime(2012, 7, 4, 11, 20, tzinfo=pytz.timezone('US/Eastern')) est_dt_naive = datetime.datetime(2012, 7, 4, 11, 20) self.assertEqual(dateutil.convert_aware_datetime(utc_dt_aware, 'US/Eastern'), est_dt_aware) self.assertEqual(dateutil.convert_aware_datetime(est_dt_aware, 'utc'), utc_dt_aware) self.assertEqual(dateutil.convert_naive_datetime(utc_dt_naive, 'utc', 'US/Eastern'), est_dt_aware) self.assertEqual(dateutil.convert_naive_datetime(est_dt_naive, 'US/Eastern', 'utc'), utc_dt_aware) self.assertEqual(dateutil.make_naive(est_dt_aware), est_dt_naive) now_zero = dateutil.utcnow(zero_seconds=True) now_later = dateutil.utcnow(zero_seconds=False) self.assertEqual(now_zero.second, 0) self.assertTrue(now_later > now_zero) self.assertEqual(now_zero.tzinfo, pytz.utc) self.assertEqual(now_later.tzinfo, pytz.utc)
def save(self, validate_schema=True, index=True, set_modified=True, set_etag=True): """ Save this object in MongoDB (and optionally ElasticSearch). :param validate_schema: Should the colander schema be validated first? If ``True``, may raise :class:`colander.Invalid`. :type validate_schema: boolean :param index: Should the object be (re-)indexed in ElasticSearch? :type index: boolean :param set_modified: Should the object's last modified timestamp (``_modified``) be updated? :type set_modified: boolean :param set_etag: Should the object's Etag be updated? :type set_etag: boolean """ if validate_schema: self.validate_schema() # May raise a colander.Invalid exception if set_modified: self._modified = dateutil.utcnow() if not getattr(self, '_created', None): self._created = self._modified if set_etag: self._etag = self.generate_etag() # Determine all the GridFS file ids that this object # now refers to and used to refer to. root = find_root(self) fs_files_coll = root.get_gridfs()._GridFS__files new_file_ids = set([x._id for x in self.get_all_files()]) old_file_ids = set() if self._id: dbref = self.get_dbref() for item in fs_files_coll.find({'parents':dbref}, fields=[]): old_file_ids.add(item['_id']) # Persist the object in Mongo. doc = self.get_mongo_save_doc() id = self.get_mongo_collection().save(doc, safe=True) if not self._id: self._id = id dbref = self.get_dbref() # Update GridFS file "parents". ids_to_remove = old_file_ids - new_file_ids ids_to_add = new_file_ids - old_file_ids if ids_to_remove: fs_files_coll.update({'_id':{'$in':list(ids_to_remove)}}, {"$pull":{"parents":dbref}, "$set":{"lastmodDate": dateutil.utcnow()}}, multi=True) if ids_to_add: fs_files_coll.update({'_id':{'$in':list(ids_to_add)}}, {"$addToSet":{"parents":dbref}, "$set":{"lastmodDate": dateutil.utcnow()}}, multi=True) if index: self.index()
def create_gridfs_file(self, file, filename, mimetype, parents=None): """ Create a new GridFS file. :param file: file content/data :type file: a file-like object (providing a read() method) or a string :param filename: a filename :type filename: string :param mimetype: a mime-type :type mimetype: string :param parents: list of references to the Objects that "own" the file :type parents: list of :class:`bson.dbref.DBRef` instances, or ``None`` :rtype: :class:`audrey.resources.file.File` """ if parents is None: parents = [] # FIXME: if image, get dimensions (via PIL?) and store as two custom attributes (width and height) return File(self.get_gridfs().put(file, filename=filename, contentType=mimetype, parents=parents, lastmodDate=dateutil.utcnow()))
def _pre_delete(self): # Remove from ElasticSearch self.unindex() # Update parents attribute of related GridFS files dbref = self.get_dbref() root = find_root(self) fs_files_coll = root.get_gridfs()._GridFS__files fs_files_coll.update({'parents':dbref}, {"$pull":{"parents":dbref}, "$set":{"lastmodDate": dateutil.utcnow()}}, multi=True)