#3 World of USO – Code Refactoring

Howdy,

This week I managed to refactor the views from the interface module. This module contains a lot of form processing views and I thought it will be a burden to refactor them. Thankfully, I found out that Django already had the perfect tools for dealing with forms, the generic class based views FormView, CreateView and UpdateView.

Those classes rely on the Python’s object orientation and multiple inheritance. Therefore, the documentation for them is spread across multiple files and it takes a long time to decipher. You have to go to several other pages to find what attributes and methods each class inherits. Fortunately, I read on Stackoverflow about a very useful site, which does exactly what I needed. It lists all the methods and attributes of each generic class, along with their source code.

The only thing that I’m not sure about this refactor is whether two views (edit_spell and add_spell) handle image upload correctly. I couldn’t make them work because of a glitch with my WoUSO development environment. I think I have an issue with Python Imaging Library.

I had a problem with moving a form from the view to forms.py file. The form was defined inside the view. Consequently, it was generating its fields dynamically when the view got called. When I moved it to another file, it wasn’t generating the correct number of fields anymore. Eventually the solution was overriding the default constructor for that form.

Another interesting thing that I learned during this week was that Python’s super() method is very powerful. It delegates method calls not only to a parent class, but also to a sibling class.

Now it’s time for a short vacation, I will be in Costinești the following week. I’ll keep you posted as soon as I get back to work.

Cheers!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>