Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix filtering Hit model by ip to include the HitCount model instance #38

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
14 changes: 7 additions & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,23 @@ jobs:

tests:
name: Python ${{ matrix.python-version }}
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this change?


strategy:
fail-fast: false
max-parallel: 5
matrix:
python-version:
- 3.7
- 3.8
- 3.9
- '3.7'
- '3.8'
- '3.9'
- '3.10'
- '3.11-dev'
- '3.11'

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
uses: actions/setup-python@v5
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please make the changes to CI in a separate commit or PR? the rationale is to keep the commit history clean, so that it makes sense to look back at a change set for the future us.

with:
python-version: ${{ matrix.python-version }}

Expand Down
4 changes: 2 additions & 2 deletions hitcount/managers/hits.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ def filter_active(self, *args, **kwargs):
period = timezone.now() - timedelta(**grace)
return self.filter(created__gte=period).filter(*args, **kwargs)

def has_limit_reached_by_ip(self, ip=None):
def has_limit_reached_by_ip(self, ip, hitcount):
hits_per_ip_limit = settings.HITCOUNT_HITS_PER_IP_LIMIT
if not ip or not hits_per_ip_limit:
return False

return self.filter_active(ip=ip).count() >= hits_per_ip_limit
return self.filter_active(ip=ip, hitcount=hitcount).count() >= hits_per_ip_limit

def has_limit_reached_by_session(self, session, hitcount):
hits_per_session_limit = settings.HITCOUNT_HITS_PER_SESSION_LIMIT
Expand Down
2 changes: 1 addition & 1 deletion hitcount/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def hit_count(request, hitcount):
# active hits to see if we should count another one

# check limit on hits from a unique ip address (HITCOUNT_HITS_PER_IP_LIMIT)
if Hit.objects.has_limit_reached_by_ip(ip):
if Hit.objects.has_limit_reached_by_ip(ip, hitcount):
return UpdateHitCountResponse(
False, 'Not counted: hits per IP address limit reached')

Expand Down
Loading
Loading