jsocol / django-ratelimit

Cache-based rate-limiting for Django
https://django-ratelimit.readthedocs.io/en/latest/
Other
1.06k stars 188 forks source link

Added increment_ratelimit #313

Open TreyWW opened 6 months ago

TreyWW commented 6 months ago

I added an increment_ratelimit function to just increment the group ratelimit count. This is practically just an alias of is_ratelimited but makes more sense in terms of its name. Takes only request and group (should I add keys, and fn too?) and returns the count of limits on return (can be a boolean True if successful instead if preferred). This was suggested in #308 as it makes more logical sense to call a function to "increment" the ratelimit rather than call "is_ratelimited" if you're just incrementing it.

Where would this be useful? Well in situations like unsuccessful login attempts, in the logic for your function you may want to add 1 to the count (in this case you dont use decorators), so you need to call a function.

increment_ratelimit("unsuccessful_login")

This snippet makes a little more sense then this snippet (logically)

is_ratelimited(request, group="unsuccessful_login", key="ip", rate="5/5m", increment=True)

Closes issue: #308

TreyWW commented 6 months ago

(converting to draft as I have not yet been able to run the tests, I'll try again when I get home just to make sure it passes and works fine)

TreyWW commented 6 months ago

Hi @jsocol,

I have added the increment_ratelimit function in the core file. I have also included a new test (test_increment_ratelimit) and added some documentation for this addition as well.

view documentation image

I hope this is what you was looking for, if there's anything I can change to make it better let me know!

Thanks

Note: I couldn't manage to get it to work without the rate, method and key so I've left them in. My original idea was to not have to use any of them though - so is this still okay? The only difference between this and "is_ratelimited" is that increment is true by default and it has a better name.