from django.core.exceptions import PermissionDenied | from django.core.exceptions import PermissionDenied | ||||
from django.utils.translation import ugettext_lazy as _ | from django.utils.translation import ugettext_lazy as _ | ||||
from .models import Hit, HitCount, BlacklistIP, BlacklistUserAgent | |||||
from .models import Hit, HitCount | |||||
class HitAdmin(admin.ModelAdmin): | class HitAdmin(admin.ModelAdmin): | ||||
del actions['delete_selected'] | del actions['delete_selected'] | ||||
return actions | return actions | ||||
def blacklist_ips(self, request, queryset): | |||||
for obj in queryset: | |||||
ip, created = BlacklistIP.objects.get_or_create(ip=obj.ip) | |||||
if created: | |||||
ip.save() | |||||
msg = _("Successfully blacklisted %d IPs") % queryset.count() | |||||
self.message_user(request, msg) | |||||
blacklist_ips.short_description = _("Blacklist selected IP addresses") | |||||
def blacklist_user_agents(self, request, queryset): | |||||
for obj in queryset: | |||||
ua, created = BlacklistUserAgent.objects.get_or_create( | |||||
user_agent=obj.user_agent) | |||||
if created: | |||||
ua.save() | |||||
msg = _("Successfully blacklisted %d User Agents") % queryset.count() | |||||
self.message_user(request, msg) | |||||
blacklist_user_agents.short_description = _("Blacklist selected User Agents") | |||||
def blacklist_delete_ips(self, request, queryset): | |||||
self.blacklist_ips(request, queryset) | |||||
self.delete_queryset(request, queryset) | |||||
blacklist_delete_ips.short_description = _( | |||||
"Delete selected hits and blacklist related IP addresses") | |||||
def blacklist_delete_user_agents(self, request, queryset): | |||||
self.blacklist_user_agents(request, queryset) | |||||
self.delete_queryset(request, queryset) | |||||
blacklist_delete_user_agents.short_description = _( | |||||
"Delete selected hits and blacklist related User Agents") | |||||
def delete_queryset(self, request, queryset): | def delete_queryset(self, request, queryset): | ||||
if not self.has_delete_permission(request): | if not self.has_delete_permission(request): | ||||
return False | return False | ||||
admin.site.register(HitCount, HitCountAdmin) | admin.site.register(HitCount, HitCountAdmin) | ||||
class BlacklistIPAdmin(admin.ModelAdmin): | |||||
pass | |||||
admin.site.register(BlacklistIP, BlacklistIPAdmin) | |||||
class BlacklistUserAgentAdmin(admin.ModelAdmin): | |||||
pass | |||||
admin.site.register(BlacklistUserAgent, BlacklistUserAgentAdmin) |
# Generated by Django 2.1.2 on 2018-10-30 17:50 | |||||
from django.db import migrations | |||||
class Migration(migrations.Migration): | |||||
dependencies = [ | |||||
('hitcount', '0002_index_ip_and_session'), | |||||
] | |||||
operations = [ | |||||
migrations.DeleteModel( | |||||
name='BlacklistIP', | |||||
), | |||||
migrations.DeleteModel( | |||||
name='BlacklistUserAgent', | |||||
), | |||||
] |
super(Hit, self).delete() | super(Hit, self).delete() | ||||
@python_2_unicode_compatible | |||||
class BlacklistIP(models.Model): | |||||
ip = models.CharField(max_length=40, unique=True) | |||||
class Meta: | |||||
db_table = "hitcount_blacklist_ip" | |||||
verbose_name = _("Blacklisted IP") | |||||
verbose_name_plural = _("Blacklisted IPs") | |||||
def __str__(self): | |||||
return '%s' % self.ip | |||||
@python_2_unicode_compatible | |||||
class BlacklistUserAgent(models.Model): | |||||
user_agent = models.CharField(max_length=255, unique=True) | |||||
class Meta: | |||||
db_table = "hitcount_blacklist_user_agent" | |||||
verbose_name = _("Blacklisted User Agent") | |||||
verbose_name_plural = _("Blacklisted User Agents") | |||||
def __str__(self): | |||||
return '%s' % self.user_agent | |||||
class HitCountMixin(object): | class HitCountMixin(object): | ||||
""" | """ | ||||
HitCountMixin provides an easy way to add a `hit_count` property to your | HitCountMixin provides an easy way to add a `hit_count` property to your |
from django.views.generic import View, DetailView | from django.views.generic import View, DetailView | ||||
from hitcount.utils import get_ip | from hitcount.utils import get_ip | ||||
from hitcount.models import Hit, HitCount, BlacklistIP, BlacklistUserAgent | |||||
from hitcount.models import Hit, HitCount | |||||
from hitcount.utils import RemovedInHitCount13Warning | from hitcount.utils import RemovedInHitCount13Warning | ||||
hits_per_ip_limit = getattr(settings, 'HITCOUNT_HITS_PER_IP_LIMIT', 0) | hits_per_ip_limit = getattr(settings, 'HITCOUNT_HITS_PER_IP_LIMIT', 0) | ||||
exclude_user_group = getattr(settings, 'HITCOUNT_EXCLUDE_USER_GROUP', None) | exclude_user_group = getattr(settings, 'HITCOUNT_EXCLUDE_USER_GROUP', None) | ||||
# first, check our request against the IP blacklist | |||||
if BlacklistIP.objects.filter(ip__exact=ip): | |||||
return UpdateHitCountResponse( | |||||
False, 'Not counted: user IP has been blacklisted') | |||||
# second, check our request against the user agent blacklist | |||||
if BlacklistUserAgent.objects.filter(user_agent__exact=user_agent): | |||||
return UpdateHitCountResponse( | |||||
False, 'Not counted: user agent has been blacklisted') | |||||
# third, see if we are excluding a specific user group or not | # third, see if we are excluding a specific user group or not | ||||
if exclude_user_group and is_authenticated_user: | if exclude_user_group and is_authenticated_user: | ||||
if user.groups.filter(name__in=exclude_user_group): | if user.groups.filter(name__in=exclude_user_group): |