Monday, May 18, 2009

How to set up a development smtp server for Django

A cool feature in Django is the development server. It allows you to quickly test your code without dealing with a full blown Apache/web server deployment. When i played with the django-registration, i needed also a "development" SMTP server (during the registration process, an email is sent to activate a new account). Luckily, this is simple enough to do with python. Open a new console and type:


python -m smtpd -n -c DebuggingServer localhost:25

And you'll have a "fake smtp" server. All it does is to listen for smtp connections, and just dump the smtp data to the console (no real emails are sent).

Saturday, May 16, 2009

How to use GnuPG to encrypt emails in Gmail

Until about a year ago, i used Thunderbird as my favorite email client. I loved the fact that i could use it from both Windows and Linux, and the nice plugins it has. I dropped it in favor of Gmail for several reasons:
  • It consumes a lot of memory (yes IE consumes much more, but who uses IE? another tab weighs less than a full email client)
  • Mozilla, Thuderbird developers, shows no interest in Thunderbird any more. I guess they don't make enough money to justify it's development. That means that you won't see any significant improvements in Thuderbird anytime soon.
  • It is somewat sluggish - especially i hate the lag when pressing the Send button.
  • No calendar integration - true, the is the lightning project, but it looks horrible.
So now, i use Gmail. It is perfect, almost. The thing i missed the most was the Enigmail Thunderbird plugin, that enabled email encryption using GnuPG.

Now, it is possible to do the same with Gmail. Download the FireGPG plugin for Firefox. What it does is it extends the Gmail interface (using greasemonkey?) to support Encrypt and Decript ops.

While such a greasemonkey-like solution will probably not win the 2009 software elegance award, and i wonder how it will continue to work when Google updates Gmail next time.... But i have to say that it worked 100% for me and this is great!

How to add a "remember me" checkbox to django-registration

If you are using django-registration app, you might want to add a "remember me" checkbox to your login form. Usually, "remember me" functionality means that the cookie is set to expire for an extended period of time.

Create a view function inside your app:

from django.contrib.auth import views as auth_views
...
def login_user(request, template_name='registration/login.html', extra_context=None):
response = auth_views.login(request, template_name)
if request.POST.has_key('remember_me'):
request.session.set_expiry(1209600) # 2 weeks

This view practically calls Django's original login view function, and then checks if the request contains a "remember_me" field checked. Also, in the settings file, you should make sure to set SESSION_EXPIRE_AT_BROWSER_CLOSE = True to make the default behavior such that the cookie expires when the user closes the browser.

You should now edit your login template to add the checkbox to the login form:

...
<input type="checkbox" name="remember_me" value="true">Remember Me</input>
...

Last, you should hook you application url to the new login view we just defined. Edit you applications urls.py:

urlpatterns = patterns('',
...
url(r'^login/$', login_user, {'template_name': 'registration/login.html'}, name='auth_login'),

How to use SyntaxHighlighter in Blogger

The cool syntax highlighter control you see below is called SyntaxHighlighter. The guy who created this lovely control also provides a hosted version that enables us blogger users to get the JS code from his server directly. Is that cool or what?

If you are using blogger and you want to use SyntaxHighlighter control, add the following lines to your template HTML, inside the head section:

<link href='http://alexgorbatchev.com/pub/sh/2.0.320/styles/shCore.css' rel='stylesheet' type='text/css'/>
<link href='http://alexgorbatchev.com/pub/sh/2.0.320/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/>
<script src='http://alexgorbatchev.com/pub/sh/2.0.320/scripts/shCore.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/2.0.320/scripts/shBrushJScript.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/2.0.320/scripts/shBrushPython.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/2.0.320/scripts/shBrushCpp.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/2.0.320/scripts/shBrushDiff.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/2.0.320/scripts/shBrushXml.js' type='text/javascript'/>
<script type='text/javascript'>
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.all();
</script>

When writing a post, wrap it up with a <pre> tag like this:

<pre class="brush:xml">
...
</pre>

Title

The convenience offered by a TinyURL also introduces potential problems, which have led to criticism of the use of TinyURLs.

TinyURLs are subject to linkrot, in the case the service stops working, all URLs related to the service will become broken.

TinyURL obscures the original address, and as a result it's sometimes used to redirect to an unexpected site. Examples of this are rickrolling, redirecting to scam and affiliate websites, or shock sites; ZoneAlarm has given the warning "TinyURL may be unsafe. This website has been known to distribute spyware." TinyURL has countered this problem by offering an option to present a link when using TinyURL, instead of redirection.[2] In addition, even if the link does not include a preview, the preview may still be accessed by simply prefixing the word "preview" to the front of the URL (Ex: "tinyurl.com/8kmfp" could be retyped as "preview.tinyurl.com/8kmfp") to see where the link will lead. Opaqueness is also leveraged by spammers, who use such links in spam to bypass URL blacklists. TinyURL, in turn, disables spam related links from redirecting.[3]

Some websites have responded by blocking TinyURLs from being posted. In 2006, MySpace banned posting TinyURLs.[4] Yahoo! Answers blocks postings that contain TinyURLs.[citation needed] The Orkut social network recently suppressed all TinyURL addresses. Wikipedia also blocks TinyURLs from being posted as part of its spam filter. The Twitter network recently replaced TinyURL with Bit.ly as its default shortener of links longer than 26 characters. Panera Bread blocks access to TinyUrl within it's free Wifi network.