Skip to content

Commit 51c9512

Browse files
committed
anthill email rate limiting
1 parent b770c2d commit 51c9512

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

anthill/people/models.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import datetime
12
from django.db import models
23
from django.conf import settings
34
from django.contrib.auth.models import User
@@ -13,6 +14,9 @@
1314
('both', 'Developer/Designer'),
1415
)
1516

17+
MESSAGE_WAIT_PERIOD = 2
18+
MAX_MESSAGES = 100
19+
1620
class Profile(LocationModel):
1721
user = models.OneToOneField(User, related_name='profile')
1822
url = models.URLField(blank=True)
@@ -21,11 +25,28 @@ class Profile(LocationModel):
2125
twitter_id = models.CharField(max_length=15, blank=True)
2226
skills = TagField('comma separated list of your skills (eg. python, django)')
2327

28+
# other metadata
2429
allow_org_emails = models.BooleanField(default=False)
30+
signup_date = models.DateTimeField(auto_now_add=True)
31+
last_email_sent = models.DateTimeField(null=True)
32+
num_emails_sent = models.IntegerField(default=0)
2533

2634
def __unicode__(self):
2735
return unicode(self.user)
2836

37+
def can_send_email(self):
38+
if self.last_email_sent:
39+
elapsed = datetime.datetime.now() - self.last_email_sent
40+
else:
41+
elapsed = datetime.timedelta(minutes=MESSAGE_WAIT_PERIOD+1)
42+
return (elapsed > datetime.timedelta(minutes=MESSAGE_WAIT_PERIOD) and
43+
self.num_emails_sent < MAX_MESSAGES)
44+
45+
def record_email_sent(self):
46+
self.last_email_sent = datetime.datetime.now()
47+
self.num_emails_sent += 1
48+
self.save()
49+
2950
def create_profile(sender, instance, created, **kwargs):
3051
if created:
3152
Profile.objects.create(user=instance)

anthill/people/views.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ def change_password(request):
9999
def contact(request, username):
100100
to_user = get_object_or_404(User, username=username)
101101

102+
if not request.user.profile.can_send_email():
103+
request.user.message_set.create(message='You have temporarily exceeded the email quota. Please wait a few minutes before sending this email.')
104+
elif request.method == 'POST':
105+
request.user.profile.record_email_sent()
106+
102107
if not request.user.email:
103108
request.user.message_set.create(message='You must set a valid email address prior to emailing other users.')
104109
return redirect('edit_profile')

0 commit comments

Comments
 (0)