def find_by_id(cls, object_id): assert object_id, 'the id of the lookup object must be provided' if isinstance(object_id, str) and object_id.startswith(OBJ_PREFIX): object_id = ObjectId(object_id.split(OBJ_PREFIX)[1]) document_dict = cls.get_collection().find_one({'_id': object_id}) return Model.from_dict(document_dict, cls, convert_ids=True, converter_func=mongo_type_converter_from_dict ) if document_dict else None
def bulk_delete(pentest, body): data = body if isinstance(data, str): data = json.loads(data, cls=JSONDecoder) if not isinstance(data, dict): return "body was not a valid dictionnary", 400 if pentest == "pollenisator": return "Impossible to bulk delete in this database", 403 elif pentest not in mongoInstance.listCalendarNames(): return "Pentest argument is not a valid pollenisator pentest", 403 deleted = 0 for obj_type in data: for obj_id in data[obj_type]: if not isinstance(obj_id, ObjectId): if obj_id.startswith("ObjectId|"): obj_id = ObjectId(obj_id.split("ObjectId|")[1]) res = mongoInstance.deleteFromDb(pentest, obj_type, {"_id": ObjectId(obj_id)}, False, True) if res is not None: deleted += res.deleted_count return deleted
def data_and_condition_handler(obj, survey_id=None): if isinstance(obj, dict): for (k, v) in obj.items(): obj[k] = data_and_condition_handler(v, survey_id) elif isinstance(obj, list): for k, v in enumerate(obj): obj[k] = data_and_condition_handler(v, survey_id) elif isinstance(obj, ObjectId): obj = ObjectId(obj) elif isinstance(obj, str): if not obj.startswith(('date_', 'regex_')): return obj s = obj.split('_') if s[0] == 'date': d = s[1].split('-') obj = datetime(int(d[0]), int(d[1]), int(d[2])) elif s[0] == 'regex': obj = compile(s[1]) elif isinstance(obj, FileStorage): if not survey_id or not ObjectId.is_valid(survey_id): raise ValueError('invalid survey_id!') filename = '{}_{}_{}'.format(uuid1(), obj.name, obj.filename) file_path = join(FILE_FOLDER, survey_id, filename) obj.save(file_path) if obj.mimetype.startswith('image/') and obj.mimetype != 'image/gif': img = Image.open(file_path) if getsize(file_path) > 102400: img.thumbnail((img.width / 4, img.height / 4)) out = BytesIO() img.save(out, 'jpeg', quality=25) out.seek(0) data_url = b64encode(out.read()).decode() obj = 'img|{}|{}'.format(filename, data_url) else: obj = 'file|{}'.format(filename) return obj
def list(skip, count, keyword='', filter='', sortby='_id', sortdir='desc', showdelete=False, listdata=False): # ## Standard Query # try: # skip = int(skip) if isinstance(skip, int) else skip # count = int(count) # except ValueError: # skip = 0 # count = 10 # direction = '+' # if sortdir == 'desc': # direction = "-" # sorter = "{}{}".format(direction, sortby) ## Aggregation Query try: skip = int(skip) if isinstance(skip, int) else skip count = int(count) except ValueError: skip = 0 count = 10 if isinstance(skip, int): limit = {"$limit": count} skip = {"$skip": skip} sortdir = -1 if sortdir == 'desc' else +1 sorter = {"$sort": {sortby: sortdir}} count = {"$count": "count"} aggsCompany = { '$lookup': { 'from': 'companies', 'localField': 'companyId', 'foreignField': '_id', 'as': 'company' } } aggsBranch = { '$lookup': { 'from': 'branchs', 'localField': 'branchs', 'foreignField': '_id', 'as': 'branchs' } } try: query = {} if keyword: query = { "$and": [{ "$or": [{ "name": { "$regex": "{}.*".format(keyword), "$options": "i" } }, { "address": { "$regex": "{}.*".format(keyword), "$options": "i" } }, { "phone": { "$regex": "{}.*".format(keyword), "$options": "i" } }, { "email": { "$regex": "{}.*".format(keyword), "$options": "i" } }, { "username": { "$regex": "{}.*".format(keyword), "$options": "i" } }] }] } if listdata: query['$and'].append(listdata) else: if listdata: query.update(listdata) if keyword: query['$and'].append({'isDeleted': showdelete}) else: query.update({'isDeleted': showdelete}) if filter: regex = True if 'regex' in filter else False for f in filter: fx = ObjectId(filter[f]) if ObjectId.is_valid( filter[f]) else filter[f] active = True if f == 'active' and fx == '1' else False fx = active if f == 'active' else fx delete = True if f == 'delete' and fx == '1' else False fx = delete if f == 'delete' else fx if f in ['users', 'branchs' ] and not ObjectId.is_valid(filter[f]): if len(fx.split(',')) > 0: dxx = [] for x in fx.split(','): dxx.append(ObjectId(x)) fx = {'$in': dxx} if regex and not ObjectId.is_valid(filter[f]): fill = { UserMod.fields(f): { "$regex": "{}.*".format(fx), "$options": "i" } } else: fill = {UserMod.fields(f): fx} if f not in ['regex']: if keyword: query['$and'].append(fill) else: query.update(fill) # ## Standard Query # model = UserMod.objects(__raw__=query) # # model = UserMod.objects(Q(isDeleted=False)) # numrows = model.count() # data = model.order_by(sorter) # if isinstance(skip, int): # data = data.skip(skip).limit(count) # if data: # return { # 'data': list(data), # json.loads(data.to_json()) # 'count': numrows # } ## Aggregation Query query = {"$match": query} if query else {} aggs = [aggsCompany, aggsBranch] nums = [aggsCompany, aggsBranch] if query: aggs.append(query) nums.append(query) aggs.extend([sorter]) nums.extend([count]) if '$skip' in skip: aggs.extend([skip, limit]) data = UserMod.objects.aggregate(*aggs) numrows = UserMod.objects.aggregate(*nums) if data: return { 'data': list(data), # json.loads(data.to_json()) 'count': list(numrows)[0]['count'] } return data except Exception: pass