FORGOTTEN LABS

django-spooncms 0.1 is out!

24th October 2012 by artur with tags: #django #github #python #spooncms

Keeping up with the last theme – putting django applications on github – I just released the 0.1 version of django-spooncms, which is meant to be a easy to use pluggable and reusable articles/blog application for all your django projects. Unlike the previous appliacation – django-ilogin this one doesn't have default templates yet, and they will be added at some later time.

Also -- both of the applications will get their setup.py scripts by the end of the week, and hopefully their READMEs will be updated with usage examples as well.

You can find django-spooncms on github

django-ilogin 0.1.0 is out!

23rd October 2012 by artur with tags: #django #github #ilogin #python #registration

After few hours of development last week I decided to make public an initial version of an application which we call "django-ilogin". Basically django-ilogin provides both registration and authentication using one form. Here's a quick tour on how it works:

  1. It displays a form with two fields - email & password

  2. if the user with given email is already registered, check if password match the user account. If no, go back go 1) and display errors. If yes, then authenticate the user and redirect to homepage.

  3. If the email is not in the database, it creates new account (with given password) and send user an activation email with token (standard register procedure)

  4. When user click the link in the email he's automatically authenticated with his new account.

You can get the code here: http://github.com/czartur/django-ilogin

And see it live here: http://beta.cogdziejak.pl/login/

Hello World!

30th July 2012 by admin with tags: #forgottenlabs #helloworld #organizational

Welcome from our new blog, redesigned and rethought from ground up. This time we will be posting more various content, not just about our projects or django development, as we did in a previous version. Hope you will like that, and if you want to get updates every now and then about what we are up to, put your email address in the input on the right, and subscribe to our newsletter.

But what about old content? It will still be available as-is at old.forgottenlabs.com, so if google will point you to some nonexisting page, then you just need to add 'old' in front of it and you should be fine. Some of the posts however will be translated, updated and posted again here, but time will tell which posts that will be.

Thanks for reading.

Sorting versions in python

5th December 2011 by admin with tags: #python #sorting #versions

Let g be a list of some versions of some package:

g = ["1.1", "1.2", "1.10", "1.6", "1.65", "1.59", "1.23"]

How would you sort that ascending in python, so that you'll end up with a chronological order?

['1.1', '1.2', '1.6', '1.10', '1.23', '1.59', '1.65']

We'll just use a distutils.version:

In [1]: from distutils.version import StrictVersion

In [2]: g = ["1.1", "1.2", "1.10", "1.6", "1.65", "1.59", "1.23"]

In [3]: f = [StrictVersion(x) for x in g]

In [4]: sorted(f)
Out[4]: 
[StrictVersion ('1.1'),
 StrictVersion ('1.2'),
 StrictVersion ('1.6'),
 StrictVersion ('1.10'),
 StrictVersion ('1.23'),
 StrictVersion ('1.59'),
 StrictVersion ('1.65')]

In [5]: [str(x) for x in sorted(f)]
Out[5]: ['1.1', '1.2', '1.6', '1.10', '1.23', '1.59', '1.65']

Special thanks to Restless Being for pointing that out. ;)

How to authenticate in Django via urllib2?

21st June 2011 by admin with tags: #crawler #django #python #urllib2

Let's say you want to write a python script, using urllib2 to access some @login_required protected typical django website, with django.contrib.auth authentication and default csrf protection. In order to do this, you have to first login, and then keep the cookie with session id you received. Also, you need to keep in mind that csrf will raise a 403 if you would try to POST to the page without csrf_tokenin your POST data. First you have to do some initial setup that will later allow you to use cookies:

import urllib, urllib2
import cookielib

cj = cookielib.CookieJar()

opener = urllib2.build_opener(
    urllib2.HTTPCookieProcessor(cj), 
    urllib2.HTTPHandler(debuglevel=1)
)

HTTPHandler(debuglevel=1)part is actually optional, in this case it would just print more data that you might be interested in (headers mostly). Now you use your shiny new opener to open a login page and grab a csrf token from it:

from lxml import html
login_form = opener.open(login_url).read()
csrf_token = html.fromstring(login_form).xpath(
    '//input[@name="csrfmiddlewaretoken"]/@value'
)[0]

Then you create dictionary with username, password and newly grabbed token:

# make values dict
values = {
    'username': username,
    'password': password,
    'csrfmiddlewaretoken': csrf_token,
}

# Convert to params
params = urllib.urlencode(values)

Then you can login:

login_page = opener.open(login_url, params)
print login_page.read()

And check out some sites that needs authentication:

staff_page = opener.open(staff_url)
print staff_page.read()

Enjoy!

On a daily basis we develop webapps, both for our clients as well as our own original productions. Because of that we acquired a lot of knowledge and experience in the field of development, and we'll be pleased to share it with you.

If you have any questions, please let us know.

If you want to stay up to date, you might want to subscribe to our newsletter:

You can contact us on

Also – we have a website, which you might want to check out at arturstudio.com