def update_hitcount(self, domain=None, protocol=None, whitelistobj=None): """Look for matching domains and update all matching entries regardless of whether they are enabled or not. If a queryset is specified, then just update the hitcount on that. We are allowing general rules to co-exist with specific ones, e.g: images.slashdot.org and *.slashdot.org could both be in the database. Hitcount will updated on both entries for the images.slashdot.org domain.""" if whitelistobj: whitelistobj.hitcount = F('hitcount') + 1 whitelistobj.save() else: lall = Whitelist.get_wildcard_choice("ALL") lone = Whitelist.get_wildcard_choice("ONE") return Whitelist.objects.filter( Q(domain=domain, protocol=protocol) | Q(domain=self.all_wildcard(domain), protocol=protocol, wildcard=lall) | Q(domain=self.one_label_wildcard(domain), protocol=protocol, wildcard=lone)).update(hitcount=F('hitcount') + 1)
def is_whitelisted(self, domain, protocol): """Return true (the queryset) if the domain is whitelisted""" lall = Whitelist.get_wildcard_choice("ALL") lone = Whitelist.get_wildcard_choice("ONE") return Whitelist.objects.filter( Q(enabled=True, domain=domain, protocol=protocol) | Q(enabled=True, domain=self.all_wildcard(domain), protocol=protocol, wildcard=lall) | Q(enabled=True, domain=self.one_label_wildcard(domain), protocol=protocol, wildcard=lone))
def get_or_create_disabled(self, domain, protocol, url, src_ip): """Return the disabled domain entry, creating one if necessary. Note we don't guarantee the whitelist entry returned will be disabled. We assume you checked is_whitelisted first. I'd rather have a race condition where there is the chance we will return an enabled domain rather than create a second entry for the domain, one enabled and one disabled. Returning an enabled object is not a big problem since we only use this for updating the hit count. If an entry is created, it is created disabled.""" user = User.objects.filter(username="******").get() w, created = Whitelist.objects.get_or_create( domain=domain, protocol=protocol, defaults={ 'user': user, 'url': url, 'comment': "", 'enabled': False, 'client_ip': src_ip, 'wildcard': Whitelist.get_wildcard_choice("NONE"), 'hitcount': 0 }) return w
def add_domain(self, domain, protocol, url, comment, src_ip, user): #settings.LOG.debug("Checking dom:%s, proto:%s to see if it has been whitelisted by a wildcard" % (domain,protocol)) qs = self.is_whitelisted(domain, protocol) if qs: i = qs.get() return ('whitelist/whitelist_already_listed.html', { 'url': url, 'domain': i.domain, 'client_ip': i.client_ip, 'prev_user': i.user, 'date_added': i.date_added }) w, created = Whitelist.objects.get_or_create( domain=domain, protocol=protocol, defaults={ 'user': user, 'url': url, 'comment': comment, 'enabled': True, 'client_ip': src_ip, 'wildcard': Whitelist.get_wildcard_choice(settings.AUTO_WILDCARD) }) if not url: #Handle SSL by refreshing to the domain added if protocol == Whitelist.get_protocol_choice('SSL'): url = "https://%s" % domain else: url = "http://%s" % domain if not created and w.enabled: #already in the db, so just redirect, #show the info in the db but redirect to the new url #This often happens if people open tabs with links to same domain. return ('whitelist/whitelist_already_listed.html', { 'url': url, 'domain': w.domain, 'client_ip': w.client_ip, 'prev_user': w.user, 'date_added': w.date_added }) elif not created and not w.enabled: w.user = user w.url = url w.comment = comment w.enabled = True w.client_ip = src_ip w.wildcard = Whitelist.get_wildcard_choice(settings.AUTO_WILDCARD) w.save() return ('whitelist/whitelist_added.html', { 'url': url, 'protocol': protocol, 'domain': domain, 'client_ip': src_ip, 'comment': comment })