Saturday, March 17, 2012

Resetting password in Django

If a user forgets his password ,he may try to do a reset.To enable this ,you need 6 templates in registration folder.

1.password_reset_form.html
2.password_reset_done.html
3.password_reset_email.html
4.password_reset_subject.txt
5.password_reset_confirm.html
6.password_reset_complete.html

Among these password_reset_form.html and password_reset_confirm.html contain forms which take input from user.The former asks the user to input an email address,while the later asks him to enter a new password.

The password_reset_subject.txt contains

Password reset on {{ site_name }}

which is used to set the subject of email sent to the user.the template password_reset_email.html is used to show the user a clickable link to bring up the password_reset form.However,when you try this,the user may get an email with subject

Password reset on example.com

This is because,the default site in database(with pk=1) has the domain and name 'example.com'.You can see this by querying in your database

select * from django_site where id=1;
id | domain | name
----+-----------------------+-------------
1 | example.com | example.com
(1 row)

To remedy this ,you can create a new site as below.
python manage.py shell
In [1]: from django.contrib.sites.models import Site
In [2]: newsite=Site()
In [3]: newsite.domain='127.0.0.1:8000'
In [4]: newsite.name='localhost'
In [5]: newsite.save()
In [6]:from django.core.serializers import serialize
In [7]:newsite.pk
Out [7]: 2
In [8]: sitedata=serialize("json", Site.objects.filter(pk=2))
In [9]: sitedata
Out[9]: '[{"pk": 2, "model": "sites.site", "fields": {"domain": "127.0.0.1:8000", "name": "localhost"}}]'

This can be added to the initial_data.json in fixtures.When syncdb is run,the record will be added to database.

Now you can set the site_id in settings.py to this new site

SITE_ID=2

When the password reset is requested,the email subject will be

Password reset on localhost

and the mail content will show a link to the reset confirm page correctly.

The mail sending occurs when the save() method in django.contrib.auth.forms.PasswordResetForm class is executed. You can browse the source here.The variables are set to the following values:

current_site = get_current_site(request)
site_name = current_site.name
domain = current_site.domain

get_current_site(request) is a method in django.contrib.sites.models and returns the current site.It defaults to the previously mentioned 'example.com'. You can find these variables used for creating the link in the password_reset_email.html template.

Thursday, March 15, 2012

Proof of conjecture:if n is not prime ,then 2^n-1 is not prime

Proof of conjecture:if n is not prime ,then 2^n-1 is not prime

For n>1,and n is not prime, Prove that (2^n)-1 is not prime.

Assume n=ab ,where a < n ,b < n

Let x=(2^b)-1
Let y =1+(2^b)+(2^2b)+...+(2^(a-1)b

xy=( (2^b)-1)(1+(2^b)+(2^2b)+...+(2^(a-1)b) )

=(2^b+2^2b+2^3b+...+2^ab) -(1+(2^b)+(2^2b)+...+(2^(a-1)b)

xy=(2^ab)-1 = (2^n)-1

Since b< n,
(2^b)-1 < (2^n)-1

ie, x < (2^n)-1 //x is an integer less than (2^n)-1

Since ab=n and a
b>1
Since x=(2^b)-1 and b>1, x > (2^1)-1
ie , x>1 //x is greater than 1

xy=n
y1
y<(2^n)-1 //y is an integer less than (2^n)-1

Given y=1+(2^b)+(2^2b)+...+(2^(a-1)b
So, y>1 //y is greater than 1


Since,x is an integer less than (2^n)-1
and y is an integer less than (2^n)-1
and x is greater than 1
and y is greater than 1

and xy=(2^n)-1

(2^n)-1 can be written as the product of x and y which are greater than 1 and less than (2^n)-1

So,(2^n)-1 is not prime

Wednesday, March 14, 2012

sending email from django application using gmail smtp server

When you want to send emails from your Django application, you need to configure email related constants in the settings.py

Suppose you decide to use gmail as your outgoing smtp server, you need to set the following values.

EMAIL_BACKEND='django.core.mail.backends.smtp.EmailBackend'
#smtp is the default email backend.But,here I am setting this explicitly.
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'sajanjoseph@gmail.com'
EMAIL_HOST_PASSWORD = 'mysecretpass'
EMAIL_USE_TLS = True

Gmail uses TLS3 for outgoing smtp server for communication security.So,you need to set the EMAIL_USE_TLS to True.Otherwise,you will get an SMTPException.
The port used by gmail for TLS is 587.

In django,the django.core.mail module uses send_mail() method for sending emails.The third argument to this method is the email address of sender or more precisely this alias(it can be god@heaven.com).But,Gmail does not use alias,so you can even set it to None or empty string.It doesn't matter.The recepient will get an email from your gmail address.The same is true for EmailMessage class.