def getIpsFitting(self): """Returns a list of ip mongo dict fitting this scope Returns: A list ip IP dictionnary from mongo db """ mongoInstance = MongoCalendar.getInstance() ips = mongoInstance.find("ips", ) ips_fitting = [] isdomain = self.isDomain() for ip in ips: if isdomain: my_ip = Utils.performLookUp(self.scope) my_domain = self.scope ip_isdomain = not Utils.isIp(ip["ip"]) if ip_isdomain: if my_domain == ip["ip"]: ips_fitting.append(ip) if Scope.isSubDomain(my_domain, ip["ip"]): ips_fitting.append(ip) else: if my_ip == ip["ip"]: ips_fitting.append(ip) else: if Ip.checkIpScope(self.scope, ip["ip"]): ips_fitting.append(ip) return ips_fitting
def doInsert(self, values): """ Insert the Scope represented by this model in the database with the given values. Args: values: A dictionary crafted by MultipleScopeView or ScopeView containg all form fields values needed. Returns: { '_id': The mongo ObjectId _id of the inserted command document. 'nbErrors': The number of objects that has not been inserted in database due to errors. } """ # Only multi insert exists at the moment for Scope # Get form values wave = values["wave"] ret = [] total = 0 accepted = 0 insert_setting = values["Settings"] split_range_setting = values.get("Split", False) for line in values["Scopes"].split("\n"): if line.strip() != "": # Insert in database scopeToAdd = line.strip() if Utils.isIp(scopeToAdd): scopeToAdd += "/32" if Utils.isNetworkIp(scopeToAdd): if split_range_setting: network_ips = Utils.splitRange(scopeToAdd) if len(network_ips) == 0: model = Scope().initialize(wave, scopeToAdd, "") inserted_res, iid = model.addInDb() else: for network_ip in network_ips: model = Scope().initialize(wave, str(network_ip), "") inserted_res, iid = model.addInDb() if inserted_res: accepted += 1 ret.append(iid) total += 1 else: model = Scope().initialize(wave, scopeToAdd, "") inserted_res, iid = model.addInDb() else: model = Scope().initialize(wave, scopeToAdd, "") inserted_res, iid = model.addDomainInDb(insert_setting) if inserted_res == 1: accepted += 1 ret.append(iid) total += 1 return ret, total-accepted # nb errors = total - accepted