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.

Saturday, February 5, 2011

Enabling assertions in eclipse projects(to a single one or all of them)

If you want to use java 1.4 assertions in java code ,you need to do these,
1.Compile using javac -source 1.4 YourCode.java .This enables assertions .Default flag is 5 .If -source flag is omitted ,compiler defaults to version 5 behavior.
2. run with java -ea YourCode

In eclipse ,you can enable assertions either for all projects or for a particular project.

Enabling assertions for all eclipse projects:
Windows-->Preferences-->Java-->Installed JREs
Select one of the listed JREs (eg:java-6-sun-1.6.0.20)
click Edit.
In the textfield labelled Default VM Arguments add -ea
click Finish.

Now if you put an assert statement in any of your projects ,it will be executed

Enabling assertions for a particular run configuration:
Select the particular project and get Properties(or click Alt+Enter).
Select Run/Debug settings
From the listed launch configurations select the one you need to enable assertions for.
Click Edit.
Select the Arguments tab.
In VM Arguments textbox add -ea

Now the assert statements in this project will be executed.

an an aside,check out google's contracts for java blog
and http://code.google.com/p/cofoja/

Wednesday, December 22, 2010

python paradox...

An Excerpt from http://www.paulgraham.com..

The language to learn, if you want to get a good job, is a language that people don't learn merely to get a job.

Read the complete article..

Tuesday, December 21, 2010

SAXParseException: White spaces are required between publicId and systemId

An error often encountered by newbies building webapps using Spring3 ..

org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 1 in XML document from ServletContext resource [/WEB-INF/myservlet-servlet.xml] is invalid; nested exception is org.xml.sax.SAXParseException: White spaces are required between publicId and systemId.

This occurs due to improper sequence of entries in schemaLocation definition in spring configuration (say /WEB-INF/myservlet-servlet.xml).

sample of incorrect entries
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/context
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

This will cause,
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 1 in XML document from ServletContext resource [/WEB-INF/myservlet-servlet.xml] is invalid; nested exception is org.xml.sax.SAXParseException: White spaces are required between publicId and systemId.


Correct the order of entries so that Namespace and Schemalocation are defined alternately within the schemaLocation attribute.

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd">

DTD:
Defines xml document structure .
Saved as .dtd file and can be referenced in an xml document.

Here is a good tutorial on dtd.

Schema definition:
Successor to dtd.
Superior.
Is written as xml and saved in .xsd file.

More on schema can be found here.

Friday, December 17, 2010

java support for tiff and other image formats

Java ImageIO supports most common image formats like jpeg,png,gif,bmp etc..Tiff format is not one them.This caused some inconvenience when I was working on a face recognition app that uses tiff images from the JAFFE image database (http://www.kasrl.org/jaffe.html).So I had to use plugins from jai-imageio .To check availability of image readers for various image formats,I modified the code in my previous post slightly.
...
public static void checkImage(String name) throws IOException{
ImageInputStream imgInStream = ImageIO.createImageInputStream(new FileInputStream(name));
Iterator
readers = ImageIO.getImageReaders(imgInStream);
if (readers.hasNext()) {
ImageReader reader = readers.next();
System.out.println("At least one image reader exists for "+name);
System.out.println("image reader ="+reader);
}else{
System.out.println("No image reader exists for "+name);
}
}
public static void printAllSupportedImageFormats(){
String[] names = ImageIO.getWriterFormatNames();
System.out.println("supports "+names.length+" formats");
for ( String name: names ){
System.out.print( name +" ");
}
System.out.println();
}
public static void main(String[] args) throws IOException {
printAllSupportedImageFormats();
String img1 = "/home/sajan/Pictures/Bounty_Ship.jpg";
String img2 = "/home/sajan/Pictures/PGPHX82.GIF";
String img3 = "/home/sajan/Pictures/abe_natsumi.ppm";
String img4 = "/home/sajan/Pictures/abe_natsumi.pgm";
String img5 = "/home/sajan/images/JAFFEPRB/KLH158.tiff";
String[] imgs = new String[]{img1,img2,img3,img4,img5};
for (String img : imgs){
checkImage(img);
}
}


...
Using standard ImageIO,I got this result...

supports 12 formats
jpg BMP bmp JPG jpeg wbmp png JPEG PNG WBMP GIF gif
At least one image reader exists for /home/sajan/Pictures/Bounty_Ship.jpg
image reader =com.sun.imageio.plugins.jpeg.JPEGImageReader@1f42b49
At least one image reader exists for /home/sajan/Pictures/PGPHX82.GIF
image reader =com.sun.imageio.plugins.gif.GIFImageReader@87a5cc
No image reader exists for /home/sajan/Pictures/abe_natsumi.ppm
No image reader exists for /home/sajan/Pictures/abe_natsumi.pgm
No image reader exists for /home/sajan/images/JAFFEPRB/KLH158.tiff


After adding jai-imageio-1.1.jar to the classpath,the result was...

supports 24 formats
BMP raw JPEG2000 RAW jpeg tif WBMP jpeg2000 GIF TIF TIFF bmp jpg PNM JPG pnm wbmp png JPEG PNG jpeg 2000 gif JPEG 2000 tiff
at least one image reader exists for /home/sajan/Pictures/Bounty_Ship.jpg
image reader =com.sun.imageio.plugins.jpeg.JPEGImageReader@1adc30
at least one image reader exists for /home/sajan/Pictures/PGPHX82.GIF
image reader =com.sun.imageio.plugins.gif.GIFImageReader@1bc887b
at least one image reader exists for /home/sajan/Pictures/abe_natsumi.ppm
image reader =com.sun.media.imageioimpl.plugins.pnm.PNMImageReader@17ee8b8
at least one image reader exists for /home/sajan/Pictures/abe_natsumi.pgm
image reader =com.sun.media.imageioimpl.plugins.pnm.PNMImageReader@1995d80
at least one image reader exists for /home/sajan/images/JAFFEPRB/KLH158.tiff
image reader =com.sun.media.imageioimpl.plugins.tiff.TIFFImageReader@1df280b


The ImageReader implementations for PNMImageReader and TIFFImageReader above are from jai-imageio .