Reputation: 1245
I am learning django and I have a test page that allows a user to "contact us" - and it is working - the emails are being delivered on my local development server.
I have tried for two days now (I have read SO posts, django docs, google and a few tutorials), and cannot workout how to do the following:
How do I place a time delay so the user would have to wait 3 minutes (or so) before sending the 'contact us' another e-mail? This would be a feature that prevents the same user bombarding the 'contact_us' with e-mails - the user would have to wait 3 minutes (or so) between e-mails.
I thought of completing this using JQuery, but wouldn't a user be able to bypass a setup of this on the client side?
Here is my models.py file:
class Contact(models.Model):
name = models.CharField(null=True, blank=True, max_length=100)
email = models.EmailField(null=False, blank=False, max_length=250)
reason = models.PositiveIntegerField(choices=REASON_TYPES, default=SELECT_REASON_TYPE, validators=[MinValueValidator(1)])
message = models.TextField(null=False, blank=False, max_length=2000)
timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
def __unicode__(self, ):
return "Message from: " + str(self.email)
Here is my forms.py file:
class ContactForm(forms.ModelForm):
class Meta:
model = Contact
fields = (
'name',
'email',
'reason',
'message',
)
labels = {
'name': _('Name'),
'email': _('E-mail'),
'reason': _('Reason'),
'message': _('Message'),
}
help_texts = {
'message': _('2,000 character limit'),
}
error_messages = {
'reason': {'validate_min': _('This field is required.')}, # validate_min used on achievement_type in lieu of required field.
}
Here is my views.py file:
def contact_us(request):
form = ContactForm(request.POST or None)
if form.is_valid():
save_form = form.save(commit=False)
send = send_mail(str(save_form.reason), str(save_form.message), str(save_form.email), ['[email protected]'], fail_silently=True)
save_form.save()
messages.success(request, _('message successfully delivered.'))
return render_to_response('contact_us/contact_us.html', locals(), context_instance=RequestContext(request))
Here is my urls.py file:
urlpatterns = patterns(
'',
....
url(r'^contact_us/', 'contact.views.contact_us', name='contact_us'),
....
)
Upvotes: 1
Views: 1710
Reputation: 22697
You can use django session variable to store the exact time when the user sent a last email. If the current time is less than 3 minutes, just update your session variable to current time and send the email, otherwise, dont allow user to send the email.
def contact_us(request):
last_send_time = request.session.get('last_email_time',None)
if last_send_time:
current_time = datetime.datetime.now()
diff = current_time - last_send_time
minutes_diff = divmod(diff.days * 86400 + diff.seconds, 60)
if minudes_diff[0] > 3: #minutes_diff[0] has difference between current_time and last_send_time
//Handle this case, user cant send email again
return render_to_response('contact_us/contact_us.html', locals(), context_instance=RequestContext(request))
else:
request.session['last_email_time'] = current_time
form = ContactForm(request.POST or None)
if form.is_valid():
save_form = form.save(commit=False)
send = send_mail(str(save_form.reason), str(save_form.message), str(save_form.email), ['[email protected]'], fail_silently=True)
save_form.save()
messages.success(request, _('message successfully delivered.'))
return render_to_response('contact_us/contact_us.html', locals(), context_instance=RequestContext(request))
Upvotes: 3