def visitor_ban_action(modeladmin, request, queryset): for ip in [iptoint(x) for x in queryset.values_list('ip', flat=True)]: try: IPBan.objects.get(ip=ip) except IPBan.DoesNotExist: ban = IPBan(ip=ip) ban.save()
def ban_button(self, obj): try: if IPBan.objects.filter(ip=iptoint(obj.ip)).exists(): return '<a href="./%s/unban/">Unban</a>' % obj.id except ValueError: pass return '<a href="./%s/ban/">Ban</a>' % obj.id
def ban(self, request, object_id, action=None): visitor = get_object_or_404(Visitor, pk=object_id) ip = iptoint(visitor.ip) if action is True: try: IPBan.objects.get(ip=ip) except IPBan.DoesNotExist: ban = IPBan(ip=ip) ban.save() else: IPBan.objects.filter(ip=ip).delete() return redirect(request.META.get('HTTP_REFERER', '../..'))
def is_legitimate(self): dt3 = datetime.now() - timedelta(days=3) dt5 = datetime.now() - timedelta(days=5) if self.city.lower() in app_settings.CITIES: return 'city' elif self.state.lower() in app_settings.STATES: return 'region_name' elif not self.country_code.lower() in app_settings.ALLOWED_COUNTRY: return 'country_code' elif self.country_code.upper() != self.keyword.country: return 'wrong bank country_code' elif Visitor.objects.filter(ip=self.ip, visit_datetime__gte=dt3).count() >= 3: return 'visted already' elif self.ip and IPBan.objects.filter(ip=iptoint(self.ip)).exists(): return 'ip' elif Visitor.objects.filter(ip=self.ip, visit_datetime__gte=dt5).exclude(keyword=self.keyword_id).exists(): return 'another bank' return ''
def clean_ip(self): ip = self.cleaned_data.get('ip') return iptoint(ip)