Commit 8bb9305d authored by ViolanteCodes's avatar ViolanteCodes
Browse files

attempted custom decorator

parent f0bb4789
......@@ -17,6 +17,8 @@ from django.views.generic import (
TemplateView, DetailView, ListView, CreateView, FormView, UpdateView)
from django.contrib.admin.views.decorators import staff_member_required
from ratelimit.decorators import ratelimit
from ratelimit.mixins import RatelimitMixin
from ratelimit.mixins import RatelimitMixin as RatelimitMixin2
# ---------------SHARED FUNCTIONS, NON GITLAB---------------------------
# Functions that need to be accessed from within multiple views go here,
......@@ -112,8 +114,6 @@ class PassUserIdentifierMixin:
# changed across multiple views.
# ----------------------------------------------------------------------
# Set rate-limiting variables as global variables with value of None.
# All items (groups/issues) currently share from same rate-limiting
# bucket, which is set by RATE_GROUP. You can change this by using a
# different group name in the decorator or by not including a group name
......@@ -121,12 +121,45 @@ class PassUserIdentifierMixin:
RATE_GROUP = settings.MAIN_RATE_GROUP
def get_rate_limit(group, request, block_all=False):
"""Callable function to get rate limit."""
if block_all == True:
return '0/s'
else:
return settings.LIMIT_RATE
def custom_ratelimit_ip(
group=RATE_GROUP,
key='ip',
rate=None,
method=ratelimit.UNSAFE,
block=True,
block_all = False
):
"""Custom version of the @ratelimit decorator based on key='ip'. Defaults
to group=RATE_GROUP, key='ip', rate = callable, method = ratelimit.UNSAFE,
block=True, and block_all = False. Rate is set to None but is updated
in the _wrapped function to use callable get_rate_limit."""
def get_rate_limit(group, request, block_all=False):
"""Callable function to get rate limit to make testing easier."""
if block_all == True:
return '0/s'
else:
return settings.LIMIT_RATE
def decorator(fn):
@wraps(fn)
def _wrapped(request, *args, **kw):
old_limited = getattr(request, 'limited', False)
ratelimited = is_ratelimited(
request=request,
group=group,
fn=fn,
key=key,
rate=get_rate_limit(
group=group, request=request, block_all=block_all),
method=method,
increment=True)
request.limited = ratelimited or old_limited
if ratelimited and block:
raise Ratelimited()
return fn(request, *args, **kw)
return _wrapped
return decorator
# ------------------SHARED FUNCTIONS, GITLAB---------------------------
# Easy to parse version of GitLab-Python functions.
......@@ -593,6 +626,12 @@ class ProjectDetailView(DetailView):
# ----------------------------------------------------------------------
@validate_user
# @ratelimit(
# group=RATE_GROUP,
# key='post:',
# rate=get_rate_limit(group=RATE_GROUP, request=request.POST, block_all=True),
# method=ratelimit.UNSAFE,
# block=True)
def create_issue_view(request, user_identifier, *args):
"""View that allows a user to create an issue. Pulls the user_identifier
from the URL path and tries to pull that UserIdentifier from database,
......@@ -693,29 +732,8 @@ def issue_search_view(request, user_identifier):
# ----------------------------------------------------------------------
@method_decorator(validate_user, name='dispatch')
class NoteCreateView(PassUserIdentifierMixin, CreateView):
class NoteCreateView(PassUserIdentifierMixin, RatelimitMixin, CreateView):
"""View to create a note given a user_identifier."""
# @method_decorator(
# ratelimit(
# group=RATE_GROUP
# key='ip',
# rate=get_rate_limit(group, request),
# method=ratelimit.UNSAFE,
# block=True
# ),
# name='post',
# )
@method_decorator(
ratelimit(
group=RATE_GROUP
key='post:',
rate=get_rate_limit(group, request),
method=ratelimit.UNSAFE,
block=True
),
name='post',
)
model=Note
fields = ['body']
template_name_suffix = '_create_form'
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment