def delete(blobId, release='draft'): try: try: thumbnail = None dbGrid = Db().getGridFs('blobs') filename = "%s.%s" % (blobId, release) thumbnail = dbGrid.get_last_version(filename) dbGrid.delete(thumbnail._id) thumbnail = thumbnail.__dict__['_file'] except CorruptGridFile as e: pass except NoFile: pass finally: if not thumbnail or not 'activity' in thumbnail: return True a = ActivityFactory.get(thumbnail['activity']) node = a.__dict__[release] if not 'blobs' in node: return True if not thumbnail['type'] in node['blobs']: return True node['blobs'][thumbnail['type']].remove(str(blobId)) ActivityFactory.update(a) except DbError: output.error('cannot access db', 503)
def insert(blobType, release, data, content_type, **args): try: blobId = ObjectId() dbGrid = Db().getGridFs('blobs') dbGrid.put( data, content_type=content_type, filename="%s.%s" % (blobId, release), type=blobType, blobId=str(blobId), release=release, **args ) if 'activity' in args: a = ActivityFactory.get(args['activity']) if not 'blobs' in a.__dict__[release]: a.__dict__[release]['blobs'] = {} if not blobType in a.__dict__[release]['blobs']: a.__dict__[release]['blobs'][blobType] = [] a.__dict__[release]['blobs'][blobType].append(str(blobId)) ActivityFactory.update(a) except DbError: output.error('cannot access db', 503) return blobId
def getAllUsers(): data = Db().get('users').find({'activate': 1, 'seq': { '$gt': 0}}, {'friends': {'$slice': 0}}) result = [] for friend in data: result.append(User(**friend)) total = data.count() return (result, total)
def update(activity): if not isinstance(activity, Activity): raise Exception('invalid activity object', activity) try: c = Db().get('activities') c.update( {'_id': ObjectId(activity.id)}, activity.toDatabase(), True ) except DbError: output.error('cannot access db', 503)
def new(activity): if not isinstance(activity, Activity): raise Exception('invalid activity object', activity) try: c = Db().get('activities') tmp = activity.toDatabase() if '_id' in tmp: del tmp['_id'] id = c.insert(tmp, True) activity.id = id except DbError: output.error('cannot access db', 503)
def incSeen(activity): if not isinstance(activity, Activity): raise Exception('invalid activity object', activity) try: c = Db().get('activities') c.update( {'_id': ObjectId(activity.id)}, {'$inc': {'seenCount': 1}}, True ) activity.seenCount += 1 except DbError: output.error('cannot access db', 503)
def get(blobId, release='draft'): try: try: blob = None dbGrid = Db().getGridFs('blobs') blob = dbGrid.get_last_version("%s.%s" % (blobId, release)) except NoFile: pass except CorruptGridFile: pass except DbError: output.error('cannot access db', 503) return blob
def getBlobIds(**args): try: collection = Db().get('blobs.files') for field in args.keys(): collection.ensure_index( [(field, DESCENDING)], {'background': False} ) ids = Db().get('blobs.files').find(args, {'blobId': True}) result = [] for i in ids: result.append(str(i['blobId'])) return result except DbError: output.error('cannot access db', 503)
def new(obj): #Db().get('users').ensure_index([('username', storage.DESCENDING)], { 'unique' : True, 'background' : False, 'dropDups' : True }) Db().get('users').ensure_index([('uid', DESCENDING) ], {'unique': True, 'background': False, 'dropDups': True}) Db().get('users').ensure_index([('email', DESCENDING) ], {'unique': True, 'background': False, 'dropDups': True}) Db().get('users').ensure_index([('connect.facebook', DESCENDING)], {'unique': True, 'background': False, 'sparse': True, 'dropDups': True}) c = Db().get('users') try: id = c.insert(obj.__dict__, True) obj._id = id except Exception as e: e = '%s' % e err = e if 'username' in e: err = 'username' elif 'email' in e: err = 'email' raise Duplicate(err) return obj
def update(blobId, blobType, release, data, content_type, **args): try: dbGrid = Db().getGridFs('blobs') dbGrid.put( data, content_type=content_type, filename="%s.%s" % (blobId, release), type=blobType, blobId=str(blobId), release=release, **args ) try: oldFile = dbGrid.get_version("%s.%s" % (blobId, release), -2) dbGrid.delete(oldFile._id) except NoFile: pass except CorruptGridFile: pass except DbError: output.error('cannot access db', 503)