From 9ad9bdd9363dedf50ab3fbe70375bd817f92512b Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Thu, 15 Dec 2022 15:55:33 -0700 Subject: Implement post rate limits, move to signed cookies Also improve the test harness a little Fixes #112 --- activities/views/compose.py | 19 ++++++++++++++++++- activities/views/timelines.py | 3 +++ 2 files changed, 21 insertions(+), 1 deletion(-) (limited to 'activities') diff --git a/activities/views/compose.py b/activities/views/compose.py index 8e3c96d..6889864 100644 --- a/activities/views/compose.py +++ b/activities/views/compose.py @@ -2,6 +2,7 @@ from django import forms from django.conf import settings from django.core.exceptions import PermissionDenied from django.shortcuts import get_object_or_404, redirect, render +from django.utils import timezone from django.utils.decorators import method_decorator from django.views.generic import FormView @@ -54,8 +55,9 @@ class Compose(FormView): ) reply_to = forms.CharField(widget=forms.HiddenInput(), required=False) - def __init__(self, *args, **kwargs): + def __init__(self, request, *args, **kwargs): super().__init__(*args, **kwargs) + self.request = request self.fields["text"].widget.attrs[ "_" ] = f""" @@ -74,8 +76,20 @@ class Compose(FormView): def clean_text(self): text = self.cleaned_data.get("text") + # Check minimum interval + last_post = self.request.identity.posts.order_by("-created").first() + if ( + last_post + and (timezone.now() - last_post.created).total_seconds() + < Config.system.post_minimum_interval + ): + raise forms.ValidationError( + f"You must wait at least {Config.system.post_minimum_interval} seconds between posts" + ) + print(last_post) if not text: return text + # Check post length length = len(text) if length > Config.system.post_length: raise forms.ValidationError( @@ -83,6 +97,9 @@ class Compose(FormView): ) return text + def get_form(self, form_class=None): + return self.form_class(request=self.request, **self.get_form_kwargs()) + def get_initial(self): initial = super().get_initial() if self.post_obj: diff --git a/activities/views/timelines.py b/activities/views/timelines.py index f55e331..d797cfd 100644 --- a/activities/views/timelines.py +++ b/activities/views/timelines.py @@ -17,6 +17,9 @@ class Home(FormView): form_class = Compose.form_class + def get_form(self, form_class=None): + return self.form_class(request=self.request, **self.get_form_kwargs()) + def get_context_data(self): context = super().get_context_data() context["events"] = list( -- cgit v1.2.3