django-ratelimit icon indicating copy to clipboard operation
django-ratelimit copied to clipboard

Added increment_ratelimit

Open TreyWW opened this issue 1 year ago • 3 comments

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 avatar Feb 23 '24 12:02 TreyWW

(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 avatar Feb 23 '24 12:02 TreyWW

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.

TreyWW avatar Feb 23 '24 21:02 TreyWW

Thanks @jaap3 😄

TreyWW avatar Oct 08 '24 10:10 TreyWW