Howsthe.com Blog

Avatar for vbabiy@howsthe.com

django.contrib.auth User object login

Published Feb. 1, 2011 by Vitaly Babiy

While working on the sign up process I noticed it takes to many steps for a user to login to their new account. Where I wanted to get to was once the user completes the sign up process they're automatically redirected in to their account, and logged in. In order to make this work with django.contrib.auth it requires us to write a custom auth backend. Due to the clean api this is very trivial to do. Here is what my auth backend looks like:

from django.contrib.auth.backends import ModelBackend

class UserObjectAuthBackend(ModelBackend):
    def authenticate(self, username=None, password=None, user=None):
        if not user:
            try:
                user = self.user_class.login.get(username=username)
            except self.user_class.DoesNotExist:
                pass

            if user and user.check_password(password):
                return user
            return None
        return user

If you‘ve ever created a auth backend you will notice something a little different about my version, I will explain this a little later. First a little explanation of what the backend is doing. In order to implement a backend we will need to over write one method, authenticate. This method is suppose to take a username and password, and using this information it needs to look up the user and then verify that the password is correct, if its correct return the user object. Our goal is to login a user using only the User object, to reach our goal we will change the default api just a little to allow us to pass in a user object to authenticate. In this version the only time we need to check the users password is when we don’t already have a user object, and if we do we just return it. To wrap up the work on the auth backend we will need to let the auth app we want to use our custom backend, to do this we need to add the following to our settings.py file:

AUTHENTICATION_BACKENDS = (
    'lib.UserObjectAuthBackend',
)

Now its time to see the view that will login in a user and redirect them to where ever they need to be:

from django.contrib.auth import authenticate, login

def signup(request):
    # ...
    # Sign up process
    # `user` is the new user that just signed up.
    # ...
    user = authenticate(user=user)
    login(request, user)
    return HttpResponseRedirect('/my_account')

That’s it folks, when the user see the next page they will be logged in. For more information also check out the great Django documentation. If you got any question or suggestions leave them below.

Tags
  • auth
  • Django
Avatar for vbabiy@howsthe.com

New features for gedit-openfiles

Published Dec. 14, 2010 by Vitaly Babiy

I finally got around to writing a post about a couple new features that I have added to gedit-openfiles. Here is a screenshot of what the dialog window looks like now:

gedit-openfiles

The first feature is now you see the file-type icon from the current gnome theme, currently I am using the Faenza icon set which has a really nice set of file-type icons. This has been very helpful to me when working on a project that uses many languages, like most web applications. The above is me searching the Django project.

The other addition is some more meta data about the file; right now it is displaying the type of file in text format and also how many times you have opened the file. These are very cool stats after working on a project for a long time but also they are used in the sorting algorithm that displays the files.

Lastly I would like to talk about a feature I would love to see implemented and that is integration with the Zeitgeist project. If anyone would like to help out with this please let me know.

That's it for now, if you got any suggestion of new features please let me know.

Tags
  • features
  • gedit
  • gedit-openfiles
Avatar for vbabiy@howsthe.com

Making celery and pymongo play nicely

Published Dec. 13, 2010 by Vitaly Babiy

UPDATE: If you are still seeing this problem then try the git master branch (pymongo-bug). With pip it's very easy to install

  pip install -e git+https://github.com/mongodb/mongo-python-driver.git#egg=pymongo

Recently I was moving some of http://www.howsthe.com data over to MongoDB, out of MySQL. Once I got the data in to MongoDB I though the world would be full rainbows, but I came across an a weird exception the didn’t make sense to me:

Traceback (most recent call last):
…
  File "/home/vbabiy/.virtualenvs/howsthedotcom/lib/python2.6/site-packages/pymongo/cursor.py", line 332, in __getitem__
    for doc in clone:
  File "/home/vbabiy/.virtualenvs/howsthedotcom/lib/python2.6/site-packages/pymongo/cursor.py", line 601, in next
    if len(self.__data) or self._refresh():
  File "/home/vbabiy/.virtualenvs/howsthedotcom/lib/python2.6/site-packages/pymongo/cursor.py", line 564, in _refresh
    self.__query_spec(), self.__fields))
  File "/home/vbabiy/.virtualenvs/howsthedotcom/lib/python2.6/site-packages/pymongo/cursor.py", line 521, in __send_message
    **kwargs)
  File "/home/vbabiy/.virtualenvs/howsthedotcom/lib/python2.6/site-packages/pymongo/connection.py", line 700, in _send_message_with_response
    return self.__send_and_receive(message, sock)
  File "/home/vbabiy/.virtualenvs/howsthedotcom/lib/python2.6/site-packages/pymongo/connection.py", line 681, in __send_and_receive
    return self.__receive_message_on_socket(1, request_id, sock)
  File "/home/vbabiy/.virtualenvs/howsthedotcom/lib/python2.6/site-packages/pymongo/connection.py", line 671, in __receive_message_on_socket
    struct.unpack("<i", header[8:12])[0])
AssertionError: ids don't match -2015873347 1667330676

After doing some searching I got a better understanding of the error and the cause, I was able to verify that the error was being generated due to multiple processes trying to write to the same connection. A simple way to avoid this is to run celery with a single process but that kinda defeats the purpose of celery.

Luckily I found a very quick and easy fix, thanks to the pymongo driver support for connection pooling. The connection class takes the pool_size as a argument and then will take care of the rest. I found when I set this to about the same size or a few more than the process count in celery everything works like a charm, and now I am in my world of rainbows. This is what my connection looks like:

import pymongo
mongodb = pymongo.connection.Connection(pool_size=10).my_database

Enjoy using pymongo with celery.

Tags
  • celery
  • pymong
  • Python
Avatar for vbabiy@howsthe.com

Gedit Open Files with Multi-window support

Published July 27, 2010 by Vitaly Babiy

Recently I took some time to rewrite the gedit-openfiles plug-in and also added one big feature. Before I get in to the details of the new feature I wanted to give a brief description of what the plug-in does.

gedit-openfiles uses the root of the file browser to index all the files in the working directory and sub directories. Doing this allows for quickly searching with in your project. The plug-in was designed to handle very large projects, I have personal tested it on a project with 4,000 files and it worked perfectly.

Image

The new feature to the plugin is it now supports multiple windows. If you are working on a project in a gedit window and you open another window to work on a different project, when opening gedit-openfiles (ctrl+alt+o) in a window it will only search files specific to that window (screen cast demo below).

Gedit Open Files from Vitaly Babiy on Vimeo.

You can find the plug-in on github http://github.com/vbabiy/gedit-openfiles, enjoy!

Tags
  • gedit
  • gedit-openfiles
Avatar for vbabiy@howsthe.com

MySQL Quick Tip: Clear query cache

Published July 26, 2010 by Vitaly Babiy

I was working with mysql today and I needed to clear the query cache (without restarting the server :)). The following statement does just that:

RESET QUERY CACHE;

Thats all, enjoy.

Tags
  • mysql
  • quicktip
  • sql
Avatar for vbabiy@howsthe.com

jQuery Object Oriented Event Handling

Published April 13, 2010 by Vitaly Babiy

Using jQuery, its easy to write event handlers for all kind of events that can occur in the DOM. It gets a little harder when the callback function is part of a object, and you want the function to have access to the object using this.

In jQuery you write a event handler as the following:

function clicked(event) {
    alert(this)
}
$("#element").bind('click', clicked)

The this variable is going to contain the element that triggered the event.

Tags
  • Event
  • Handlers
  • Javascript
  • JQuery
  • OOP
Avatar for charles.boyung@nexustechnologiesllc.com

jQuery Twitter Library

Published April 7, 2010 by Charles Boyung

The world of jQuery is kind of like the iPhone app store. Just like “there’s an app for that”, if you want to do something specific with jQuery, there’s probably a plugin for it. And just like the iPhone app store, the quality of the items you find can vary from utter garbage to brilliant masterpieces. The jQuery Twitter library known as jTwitter falls somewhere in the middle. It has some decent functionality, and can definitely prove useful if you aren’t able to make use of any of the more robust options out there (such as any of the server-side libraries or the API itself).

What it Does
jTwitter does one thing: it allows you to retrieve and display recent Twitter posts for a specified Twitter user. The demo provided is a good starting point, but it only goes as far as showing you the basics of querying for posts and displaying the results. I’m going to take this a step further and show you how you can extend this with additional Javascript to make the results somewhat more useful. To better follow along with this tutorial, I recommend downloading the full tutorial source.

Avatar for vbabiy@howsthe.com

An Outsider's Perspective

Published March 31, 2010 by Vitaly Babiy

Rob Walling from http://www.softwarebyrob.com/ had recently posted a request for businesses to submit their site for a quick review, I submitted howsthe.com in a heart beat. I have to thank Rob again for taking the time to record the review. The video will be video embedded after the break. I want to take this time and talk about how important an outside perspective is.

I want to first go over a couple of the suggestions Rob had pointed out in the video which were just so obvious in hindsight. I will start with the quick check tool, its a great way for people to see how the service works, and it is used by many users everyday. Which is awesome, but once the user see the result of the website check there is no actionable item to help the user flow through to the sign up page. Rob has suggested that I add a workflow from the result page to the sign up section, this is just brilliant and yet so obvious. It just goes to show how important it is to get feedback from people that are not working on the product everyday.

Tags
  • great-products
  • reviews
Avatar for vbabiy@howsthe.com

Stream mic to speakers on Ubuntu 10.04

Published March 30, 2010 by Vitaly Babiy

Quick Tip:

To enable a loopback to your speakers from your mic, this is helpful for screencasting and recording audio.

pactl load-module module-loopback
Avatar for vbabiy@howsthe.com

Crockford on Javascript

Published March 29, 2010 by Vitaly Babiy

Douglas Crockford is giving a public lecture on JavaScript, the link is below. Anyone that is interested in JavaScript or is using it (really any web developer) should watch them.

Douglas Crockford

Douglas is the author of one of my favorite book JavaScript: The Good Parts Amazon Link

http://www.yuiblog.com/crockford/

A blog about development, marketing, and design.

The next version of Ubuntu is coming soon