def post(self, request, *args, **kwargs): db = get_mongo_client() event_code = request.POST.get('id', '') res = db.menu_event.find_one({'event_code': event_code}) if not res: return self.render_json_response(dict( state=False, error="not found" )) # 1 确保没有被名单管理使用 if db.menus.find_one({"event": event_code}): return self.render_json_response(dict( state=False, error=u"已生成名单,无法删除" )) # 2 确保没有被名单策略使用 is_using = self._check_event(event_code) if is_using: return self.render_json_response(dict( state=False, error=u"已生成名单策略原子,无法删除" )) db.menu_event.delete_one({'event_code': event_code}) return self.render_json_response(dict( state=True, msg=u"ok" ))
def add_element_to_menu(event_code, menu_type, menu_kind, element, end_time=None): """ 为名单中增加元素 :param event_code: 名单项目code :param menu_type: 名单类型 black white gray :param menu_kind: 名单维度 user_id / ip / ... :param element: 放入名单的元素 :param end_time: 失效时间 :return: """ end_time = (end_time or datetime.now() + timedelta(hours=1)) menu_desc = get_sample_str(15) payload = dict(end_time=end_time, menu_desc=menu_desc, menu_status=u"有效", create_time=datetime.now(), creator='test', value=element, event=event_code, menu_kind=menu_kind, menu_type=menu_type) db = get_mongo_client() insert_result = db['menus'].insert_one(payload) redis_client = get_redis_client() redis_key = build_redis_key(event_code, menu_type, menu_kind) if redis_key: redis_client.sadd(redis_key, element) return str(insert_result.inserted_id)
def get_queryset(self): db = get_mongo_client() qs = db[self.collection_name].find( self.build_filter_query(), sort=[("create_time", pymongo.DESCENDING)] ) return qs
def clean_event_name(self): db = get_mongo_client() event_name = self.cleaned_data['event_name'].strip() res = db.menu_event.find_one({'event_name': event_name}) if res: raise forms.ValidationError(u"该项目名称已存在") return event_name
def _build_event_choices(cls): db = get_mongo_client() choices = [(x["event_code"], x["event_name"]) for x in db['menu_event'].find({}, projection={'_id': False, 'event_code': True, 'event_name': True})] return choices
def save(self, *args, **kwargs): db = get_mongo_client() cd = self.cleaned_data event_code = str(uuid.uuid4()) pay_load = dict(event_code=event_code, event_name=cd['event_name']) db.menu_event.insert_one(pay_load) return event_code
def save(self): db = get_mongo_client() if self.entity_id: entity_id = ObjectId(self.entity_id) return db[self.COLLECTION].update_one({'_id': entity_id}, {'$set': self.values}) else: return db[self.COLLECTION].insert_one(self.values)
def post(self, request, *args, **kwargs): db = get_mongo_client() ids = request.POST.get('ids') try: assert ids obj_ids = [ObjectId(id_) for id_ in ids.split(',')] except Exception: return self.render_json_response(dict( state=False, error=u"id不合法" )) redis_values_should_remove = defaultdict(list) menus_records = list( db['menus'].find({'_id': {"$in": obj_ids}, 'menu_status': u'有效'}, {'event_code': True, '_id': False, 'dimension': True, 'menu_type': True, 'value': True, })) if not menus_records: return self.render_json_response(dict( state=False, error=u"记录均不存在" )) for d in menus_records: redis_key = build_redis_key(d['event_code'], d['dimension'], d['menu_type']) if redis_key: redis_values_should_remove[redis_key].append(d['value']) update_payload = { 'menu_status': u'无效', 'creator': request.user.username, 'create_time': datetime.now(), } try: db.menus.update_many({'_id': {"$in": obj_ids}}, {"$set": update_payload}) # 同时删除redis中数据 redis_client = get_redis_client() pipeline = redis_client.pipeline(transaction=False) for key, values in redis_values_should_remove.items(): pipeline.srem(key, *values) pipeline.execute() except Exception: return self.render_json_response(dict( state=False, error=u"操作失败,请稍后重试" )) return self.render_json_response(dict( state=True, msg="ok" ))
def tearDownClass(cls): super(BaseTestCase, cls).tearDownClass() # 清理测试库 db = get_mongo_client() db.client.drop_database(db.name) client = get_redis_client() client.flushdb()
def raw_query(self, query): db = get_mongo_client() docs = db[self.collection].find(query).sort('_id', pymongo.DESCENDING) result = [] for doc in docs: try: result.append(self.klass(**doc)) except TypeError: logger.error('Permission data corruption! class: {}, doc: {}' .format(self.klass, doc)) return result
def save(self, *args, **kwargs): """新增时,若存在维度值+项目+名单类型相同情况,则update为新增值""" cd = self.cleaned_data value_list = cd['value'] chinese_name = self.request.user.username error_datas = [] end_time = cd['end_time'].replace(tzinfo=None) db = get_mongo_client() redis_client = get_redis_client() for value in value_list: payload = dict( end_time=end_time, menu_desc=cd['menu_desc'], menu_status=u"有效", create_time=datetime.datetime.now(), creator=chinese_name ) value = value.strip() dimension = cd['dimension'] menu_type = cd['menu_type'] event_code = cd.get('event_code') condition = dict(value=value, event_code=event_code, menu_type=menu_type, dimension=dimension ) res = db.menus.find_one(condition) try: if not res: payload.update(condition) db.menus.insert_one(payload) else: db.menus.update_one({"_id": res.get("_id", '')}, {"$set": payload}) # 同时写redis redis_key = build_redis_key(event_code, dimension, menu_type) if redis_key: redis_client.sadd(redis_key, value) except Exception: error_datas.append(value) return error_datas
def multi_get(self, items): db = get_mongo_client() docs = db[self.collection].find({self.primary_key: {'$in': items}}) \ .sort('_id', pymongo.DESCENDING) return [self.klass(**doc) for doc in docs]
def get_by_id(self, entity_id): db = get_mongo_client() doc = db[self.collection].find_one({'_id': ObjectId(entity_id)}) if not doc: return None return self.klass(**doc)
def get(self, item): db = get_mongo_client() doc = db[self.collection].find_one({self.primary_key: item}) if not doc: return None return self.klass(**doc)
def delete_by_element(self, container, element): db = get_mongo_client() db[self.collection].update({}, {'$pull': {container: element}})
def create_menu_event(event_code=None, event_name=None): db = get_mongo_client() payload = dict(event_code=event_code or str(uuid.uuid4()), event_name=event_name or get_sample_str(length=8)) db['menu_event'].insert_one(payload) return payload
def _build_event_choices(cls): db = get_mongo_client() choices = [(x["event_code"], x["event_name"]) for x in db['menu_event'].find()] return choices
def delete(self): db = get_mongo_client() if not self.entity_id: return entity_id = ObjectId(self.entity_id) db[self.COLLECTION].delete_one({'_id': entity_id})
def get_queryset(self): db = get_mongo_client() qs = db[self.collection_name].find() return qs
def get_qs_count(self): db = get_mongo_client() count = db[self.collection_name].count() return count
def _get_test_mongo_client(db_name='test_risk_control'): return get_mongo_client(db_name)
def render_event(self, value): db = get_mongo_client() res = db['menu_event'].find_one({'event_code': value}) or {} return res.get('event_name', value)
def _build_event_choices(cls): db = get_mongo_client() choices = [(x["event_code"], x["event_name"]) for x in db['menu_event'].find()] choices.insert(0, ('', u"全部项目")) return choices
def get_qs_count(self): db = get_mongo_client() count = db[self.collection_name].find( self.build_filter_query()).count() return count
def all(self, meta_only=False): db = get_mongo_client() projection = {f: True for f in self.projection_fields} if meta_only else {} return db[self.collection].find(projection=projection)
def render_event_code(self, value): db = get_mongo_client() res = db.menu_event.find_one({'event_code': value}) if not res: return value return res.get('event_name', value)
def all_fields(self): db = get_mongo_client() return db[self.collection].find({})