Skip to content

Commit feb3374

Browse files
committed
per-user email limiting
1 parent 8f064f7 commit feb3374

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

anthill/people/models.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
)
1616

1717
MESSAGE_WAIT_PERIOD = 2
18-
MAX_MESSAGES = 100
18+
INITIAL_MAX_MESSAGES = 100
1919

2020
class Profile(LocationModel):
2121
user = models.OneToOneField(User, related_name='profile')
@@ -30,6 +30,7 @@ class Profile(LocationModel):
3030
signup_date = models.DateTimeField(auto_now_add=True)
3131
last_email_sent = models.DateTimeField(null=True)
3232
num_emails_sent = models.IntegerField(default=0)
33+
allowed_emails = models.IntegerField(default=INITIAL_MAX_MESSAGES)
3334

3435
def __unicode__(self):
3536
return unicode(self.user)
@@ -40,7 +41,7 @@ def can_send_email(self):
4041
else:
4142
elapsed = datetime.timedelta(minutes=MESSAGE_WAIT_PERIOD+1)
4243
return (elapsed > datetime.timedelta(minutes=MESSAGE_WAIT_PERIOD) and
43-
self.num_emails_sent < MAX_MESSAGES)
44+
self.num_emails_sent < self.allowed_emails)
4445

4546
def record_email_sent(self):
4647
self.last_email_sent = datetime.datetime.now()

anthill/people/views.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,27 +99,28 @@ 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-
102+
# only users with a valid email can send email
107103
if not request.user.email:
108104
request.user.message_set.create(message='You must set a valid email address prior to emailing other users.')
109105
return redirect('edit_profile')
110106

107+
# if this user can't send email inform them of the fact
108+
if not request.user.profile.can_send_email():
109+
request.user.message_set.create(message='You have currently exceeded the message quota. Please wait a few minutes before sending this message.')
110+
111111
if request.method == 'GET':
112112
form = UserContactForm()
113113
else:
114114
form = UserContactForm(request.POST)
115-
if form.is_valid():
115+
if form.is_valid() and request.user.profile.can_send_email():
116116
data = {'from_user': request.user, 'to_user': to_user,
117117
'subject': form.cleaned_data['subject'],
118118
'body': form.cleaned_data['body']}
119119
subject = render_to_string('people/contact_email_subject.txt', data)
120120
body = render_to_string('people/contact_email_body.txt', data)
121121
to_user.email_user(subject.strip(), body, request.user.email)
122122
request.user.message_set.create(message='Your email has been delivered to %s' % (to_user.first_name or to_user.username))
123+
request.user.profile.record_email_sent()
123124
return redirect('user_profile', username)
124125

125126
return render_to_response('people/contact.html',

0 commit comments

Comments
 (0)