<?xml version="1.0" encoding="utf-8" ?>
<feed xmlns="http://www.w3.org/2005/Atom"
      xmlns:dc="http://purl.org/dc/elements/1.1/"
      xml:base="http://reinout.vanrees.org/" xml:lang="en">
  <link rel="self"
        href="http://reinout.vanrees.org/weblog/plonefeed.xml" />
  <link href="http://reinout.vanrees.org/weblog/"
        rel="alternate" type="text/html" />

  <div xmlns="http://www.w3.org/1999/xhtml">
    <a href="http://www.atomenabled.org/feedvalidator/check.cgi?url=http://reinout.vanrees.org/weblog/plonefeed.xml">
      <img title="Validate my Atom feed" width="88"
           height="31"
           src="http://www.atomenabled.org/feedvalidator/images/valid-atom.png"
           alt="[Valid Atom]" border="0px" />
    </a>
    <p>
      <span>
        This is an Atom formatted XML site feed. It is intended to be viewed in
        a Newsreader or syndicated to another site. Please visit
      </span>
      <a href="http://www.atomenabled.org/">Atom Enabled</a>
      <span>
        for more info.
      </span>
    </p>
  </div>

  <title type="html">Reinout van Rees' weblog</title>
  <subtitle>Python, grok, books, history, faith, etc.</subtitle>
  <updated>2009-04-04T21:44:00+01:00</updated>
  <id>urn:syndication:a55644db8591c020bd38852775819a9a</id>

  
  <entry>
    <title>Upgrading your svn checkouts to 1.7 with checkoutmanager</title>
    <link rel="alternate" type="text/html"
          href="http://reinout.vanrees.org/./weblog/2012/02/09/upgrading-svn-with-checkoutmanager.html" />
      <id>http://reinout.vanrees.org/./weblog/2012/02/09/upgrading-svn-with-checkoutmanager.html</id>
      <author>
        <name>Reinout van Rees</name>
      </author>
      <published>2012-02-09T00:00:00+01:00</published>
      <updated>2012-02-09T11:24:00+01:00</updated>

      
      <category term="python" />
      

      <content type="xhtml"
               xml:base="http://reinout.vanrees.org/"
               xml:lang="en-US"
               xml:space="preserve">
        <div xmlns="http://www.w3.org/1999/xhtml">
          <div class="document">
<p>I updated subversion yesterday (because I installed git-svn). Subversion
version 1.7 has a new repository structure and requires you to upgrade all
your existing checkouts:</p>
<pre class="literal-block">
$&gt; svn up
svn: E155036: Please see the 'svn upgrade' command
svn: E155036: Working copy 'xyz' is too old (format 10, created by Subversion 1.6)
</pre>
<p>Calling <tt class="docutils literal">svn upgrade</tt> by hand for all checkouts is boring. And I made
<a class="reference external" href="http://pypi.python.org/pypi/checkoutmanager">checkoutmanager</a> to make
checkouts less boring. A simple <tt class="docutils literal">checkoutmanager up</tt> in the morning does an
<tt class="docutils literal">svn up</tt>, <tt class="docutils literal">git pull</tt> or <tt class="docutils literal">hg pull <span class="pre">-u</span></tt> in every one of my checkouts.</p>
<p>And <a class="reference external" href="http://maurits.vanrees.org/">my brother</a> luckily added a hidden command
<tt class="docutils literal">upgrade</tt> to checkoutmanager. It is called hidden because it is only listed
on the <a class="reference external" href="http://pypi.python.org/pypi/checkoutmanager">pypi page</a>, not when
you call <tt class="docutils literal">checkoutmanager <span class="pre">--help</span></tt>, because it is so rarely needed.</p>
<p>But anyway, a simple <tt class="docutils literal">checkoutmanager upgrade</tt> call later and all my svn
checkouts were upgraded! Nice.</p>
</div>

        </div>
      </content>

    </entry>
    
  <entry>
    <title>Apple lion reinstall experience and surprise</title>
    <link rel="alternate" type="text/html"
          href="http://reinout.vanrees.org/./weblog/2012/02/05/apple-lion-reinstall.html" />
      <id>http://reinout.vanrees.org/./weblog/2012/02/05/apple-lion-reinstall.html</id>
      <author>
        <name>Reinout van Rees</name>
      </author>
      <published>2012-02-05T00:00:00+01:00</published>
      <updated>2012-02-05T21:12:00+01:00</updated>

      
      <category term="python" />
      
      <category term="django" />
      
      <category term="apple" />
      

      <content type="xhtml"
               xml:base="http://reinout.vanrees.org/"
               xml:lang="en-US"
               xml:space="preserve">
        <div xmlns="http://www.w3.org/1999/xhtml">
          <div class="document">
<p>My year-old macbook installation was showing its age. Or rather, there were
some things wrong with it:</p>
<ul class="simple">
<li>The original OS was 10.6, snow leopard. I upgraded it to lion (10.7) half a
year ago. This was an in-place upgrade, not a fresh install. I wanted a
fresh install to clean some stuff up and because it started to feel slow. I
heard that a clean install would help a lot regarding speed.</li>
<li>I work a lot with geographic libraries, Django and geodjango. So originally
I installed everything via the <a class="reference external" href="http://www.kyngchaos.com/software/unixport">kyngchaos packages</a>. Mapnik, gdal, spatialite and
so on. But after the lion upgrade, I couldn't compile any python packages
with C extensions anymore as gcc 4.0 (which everything had been build with)
had been replaced by 4.2. And spatialite never would work right anyway. So I
wanted to replace this.</li>
<li>I used <a class="reference external" href="http://mxcl.github.com/homebrew/">homebrew</a> as a package manager
for the gnu/unix side of things instead of <a class="reference external" href="http://www.macports.org/">macports</a> I'd been using before. It works, but I missed
some things, like <a class="reference external" href="http://www.qgis.org/">Quantum GIS (QGIS)</a>, which is
included in macports. I hoped to get everything python+gis related done with
one package manager, in my case macports.</li>
</ul>
<p>So I made sure my backups were OK, that my code was all committed, that my
repositories were cleaned up, that all my dotfiles in my homedir were in
version control and so on. Most of it was already OK, but of course there were
some small things left. I'll do a write-up later on of my backup strategy and
how I handle my dotfiles and so.</p>
<p>Time for the <strong>actual lion reinstall</strong>. How does that work? I bought Lion from
the app store, so it was downloaded and installed by my mac: I didn't have an
install DVD. Turns out to be <strong>easy</strong>: just restart and <a class="reference external" href="http://support.apple.com/kb/HT4718">press command-r</a> during bootup and you'll get a &quot;lion
recovery&quot; menu. Choose the reinstall option and it will download the latest
full version and install it for you. Simple and works.</p>
<p>The big <strong>surprise</strong> came when the computer rebooted. I expected a dialog to
set up a main user. Instead, I got the regular login screen. Ok... Logging
in... Hey! All my stuff is still there! All the settings, all my documents,
all my music... No need to restore backups.</p>
<p>So: an OSX lion restore wipes <strong>only the OS</strong> and reinstalls it. Including
xcode, btw. The rest (your own data, applications, settings) is
retained. Actually pretty handy.</p>
<p>This <em>did</em> mean I had to clean up the kyngchaos packages and homebrew by
hand. Just a matter of deleting some directories, telling <a class="reference external" href="http://superuser.com/a/203740/90698">homebrew to erase
itself</a> and adjusting my paths.</p>
</div>

        </div>
      </content>

    </entry>
    
  <entry>
    <title>Website changes: sphinx code, layout, twitter bootstrap, less</title>
    <link rel="alternate" type="text/html"
          href="http://reinout.vanrees.org/./weblog/2012/01/22/website-changes.html" />
      <id>http://reinout.vanrees.org/./weblog/2012/01/22/website-changes.html</id>
      <author>
        <name>Reinout van Rees</name>
      </author>
      <published>2012-01-22T00:00:00+01:00</published>
      <updated>2012-01-22T22:49:00+01:00</updated>

      
      <category term="python" />
      
      <category term="django" />
      

      <content type="xhtml"
               xml:base="http://reinout.vanrees.org/"
               xml:lang="en-US"
               xml:space="preserve">
        <div xmlns="http://www.w3.org/1999/xhtml">
          <div class="document">
<p>I've made a couple of changes to my website (and thus weblog):</p>
<ul class="simple">
<li>I moved from svn to git.</li>
<li>I made my weblog-related sphinx customizations public.</li>
<li>I'm using <a class="reference external" href="http://twitter.github.com/bootstrap/">twitter bootstrap</a> as the
css framework instead of the yahoo one I've been using before.</li>
<li>I changed the layout. Bigger font, less clutter. More bare-bones. Most of
the looks (font type, headers) is from the bootstrap css, but the ordering
is mine. I especially like the bigger font.</li>
</ul>
<div class="section" id="svn-to-git">
<h1>Svn to git</h1>
<p>I'm getting used to git at the office. And my website was in svn. The svn is
on me and <a class="reference external" href="http://maurits.vanrees.org">my brother</a>'s server. And we're going
to re-build that server in the near future. And not having to set up svn there
saves quite some time.</p>
<p>So I moved part of the code to github and the actual content to a simple
directory in our own server. Git works just fine with a repository URL like
<tt class="docutils literal"><span class="pre">ssh://vanrees.org/~/git/websitecontent</span></tt>. Just init a repo in
<tt class="docutils literal">~/git/websitecontent</tt> on the server and off you go.</p>
</div>
<div class="section" id="sphinx-as-weblog-code-is-now-available-somewhat">
<h1>Sphinx as weblog: code is now available (somewhat)</h1>
<p>My site is static html now for two years or so. Generated with
<a class="reference external" href="Sphinx">http://sphinx.pocoo.org/</a>. No Plone (which it was when I worked
with Plone) and no Django (which I'd like, as I work with it, but my current
text-only setup works real fine and Sphinx is an essential Django
documentation ingredient, too).</p>
<p>I also make my weblog with Sphinx. I need a couple of custom restructuredtext
tags for that (like <tt class="docutils literal">..tags::</tt> for handling tags). I basically point a
script at a sphinx directory that contains my entries and it generates
<tt class="docutils literal">index.txt</tt> files for all the directories with a list of entries. And it
generates <tt class="docutils literal">tags/TAGNAME.txt</tt> files for the tags. Afterwards, I just run
Sphinx and everything gets generated OK.</p>
<p>I got some questions in the past about my code, but never followed up by
publishing my terribly Reinout-specific code. It <em>is</em> still Reinout-specific,
but <strong>I've published it now</strong>. It might provide some ideas. The full code is
at <a class="reference external" href="https://github.com/reinout/reinout.vanrees.org">https://github.com/reinout/reinout.vanrees.org</a> . Again: this is not
something you can <tt class="docutils literal">pip install</tt>. Best you can do is borrow and copy at the
moment. (Threat the Python code in there as public domain).</p>
<p>Most of interest are probably
<a class="reference external" href="https://github.com/reinout/reinout.vanrees.org/blob/master/rvo/rst.py">https://github.com/reinout/reinout.vanrees.org/blob/master/rvo/rst.py</a> which
provides the custom restructuredtext (and thus sphinx) tags. And
<a class="reference external" href="https://github.com/reinout/reinout.vanrees.org/blob/master/rvo/weblog.py">https://github.com/reinout/reinout.vanrees.org/blob/master/rvo/weblog.py</a> which
creates all the index and tags files.</p>
<p>It is not well-documented and quite specific to my case. But there are some
docstrings and the code is nicely PEP8 compliant :-) If you need any info,
feel free to contact me.</p>
</div>
<div class="section" id="twitter-bootstrap-css">
<h1>Twitter bootstrap css</h1>
<p>I have to re-visit the layout of the websites we make at my work (<a class="reference external" href="http://www.nelen-schuurmans.nl">Nelen &amp;
Schuurmans</a>). One of the things we want is
to have a more attractive layout. And after some browsing, <a class="reference external" href="http://twitter.github.com/bootstrap/">twitter bootstrap</a> struck me as a good basis.</p>
<p>And, as I do often, I first experiment on myself. So I set out to change the
layout of my own website by using twitter bootstrap. I'm not even using half
of bootstrap's possibilities, but I'm quite happy with the result. The basic
typography is fine. There are some handy helpers for common things like labels
(which I used to liven up the tags below the weblog headers on my weblog). And
I quite like the simple header at the top of the page: just a simple bar.</p>
<p>And there's another good thing about twitter bootstrap: less. See below.</p>
</div>
<div class="section" id="less">
<h1>Less</h1>
<p>What coffeescript is for javascript, <a class="reference external" href="http://lesscss.org/">less</a> is for
css. A simpler syntax that helps you write better and <em>less duplicated</em> css.</p>
<p>I like it! Take for example the headings of this entry. Hover over one and
you'll see a paragraph token at the end. Sphinx adds this so you can link to
individual sections in a bigger file. Sphinx also has some css that hides the
paragraph token normally and only shows it when you hover over the heading:</p>
<pre class="literal-block">
a.headerlink {
    visibility: hidden;
}

h1:hover &gt; a.headerlink,
h2:hover &gt; a.headerlink,
h3:hover &gt; a.headerlink,
h4:hover &gt; a.headerlink,
h5:hover &gt; a.headerlink,
h6:hover &gt; a.headerlink,
dt:hover &gt; a.headerlink {
    visibility: visible;
}
</pre>
<p>I restricted that to:</p>
<pre class="literal-block">
.headerlink {
  visibility: hidden;
  }

h1, h2, h3, h4, h5, h6, dt {
  &amp;:hover {
    .headerlink {
      visibility: visible;
      }
    }
  }
</pre>
<p>It looks just like css, as you see in that first <tt class="docutils literal">.headerlink</tt> rule. The
second one has the special less stuff. The ampersand means an extra condition,
so the <tt class="docutils literal">&amp;:hover</tt> effectively adds <tt class="docutils literal">:hover</tt> to each of the
<tt class="docutils literal">h1</tt>..``dt``. This really shines if you have a couple of other extra
conditions on the same set of elements.</p>
<p>Anyway, head over to <a class="reference external" href="http://lesscss.org/">http://lesscss.org/</a> where they have a better explanation.</p>
</div>
<div class="section" id="closing-off">
<h1>Closing off</h1>
<p>I'm happy with the change. One extra change that I didn't mention yet: I've
removed deliverance from the mix. Deliverance is a tool that I used to change
the layout of a couple of inputs (sphinx, docbook2html, latex2html) into my
vanrees.org layout. It did that on the fly. But the versions kept changing and
the versions I had were getting old and hard to update. And I had to run a
wsgi process to run it, even though the actual website was all plain html.</p>
<p>I'm back to plain html now. I've implemented the layout in a Sphinx
template. Simple to do, as it is just jinja2, which has the same syntax as
Django. And the docbook and latex html exports are just that. I'll leave them
that way. It isn't that important to have my logo on those pages :-)</p>
<p>One extra advantage: the site ought to be faster!</p>
</div>
</div>

        </div>
      </content>

    </entry>
    
  <entry>
    <title>(Dutch) Python/Django vacature in Utrecht</title>
    <link rel="alternate" type="text/html"
          href="http://reinout.vanrees.org/./weblog/2011/10/17/utrecht-python-django-vacature.html" />
      <id>http://reinout.vanrees.org/./weblog/2011/10/17/utrecht-python-django-vacature.html</id>
      <author>
        <name>Reinout van Rees</name>
      </author>
      <published>2011-10-17T00:00:00+01:00</published>
      <updated>2011-10-17T07:08:00+01:00</updated>

      
      <category term="nelenschuurmans" />
      
      <category term="django" />
      
      <category term="python" />
      

      <content type="xhtml"
               xml:base="http://reinout.vanrees.org/"
               xml:lang="en-US"
               xml:space="preserve">
        <div xmlns="http://www.w3.org/1999/xhtml">
          <div class="document">
<p>English note: this is about a job opening, but we really want someone who can
speak Dutch, so I'm keeping the text in Dutch, too.</p>
<p>Na <a class="reference external" href="http://reinout.vanrees.org/weblog/2010/12/14/python-django-cms-vacature-utrecht.html">twee</a>
<a class="reference external" href="http://reinout.vanrees.org/weblog/2010/08/24/python-django-vacature-utrecht.html">eerdere</a>
vacatures hebben we er <em>weer</em> eentje! We doen het goed, want onze klanten
geven ons veel werk :-) Zie de <a class="reference external" href="http://www.nelen-schuurmans.nl/NL/bedrijfsprofiel/carriere/13">officiële vacature</a>.</p>
<p>Ik zou die twee eerdere vacatures even doorlezen, dat geeft je een aardig
idee. Een paar zaken die ik eraan toe wil voegen:</p>
<ul class="simple">
<li>Het gros van onze code is open source. We zijn net bezig onze spullen van
een intere subversion <a class="reference external" href="https://github.com/lizardsystem/">naar github</a> over
te brengen.</li>
<li>Iets wat ik me afgelopen woensdag op de Django meetup pas realiseerde toen
ik er veel positieve feedback op kreeg: we hebben al best een lekker Django
team. 10 personen groot! 10 van de 40 in het hele bedrijf. Lekkere club met
veel kennisuitwisseling waarbij je veel nieuwe zaken leert.</li>
<li>Hartje Utrecht, dus het zou voor jou wel eens lekker bereikbaar kunnen
zijn. 10 minuten lopen vanaf het station. Ken je dat grote kruispunt met de
Bijenkorf/We/C&amp;A? We zitten in de achtertuin van de We.</li>
<li>Beginners prima! Je moet wel kunnen programmeren, maar je hoeft geen Django
core committer te zijn. Ook als je alleen in de avonduren wat met Python
&quot;speelt&quot; en overdag met Java werkt: prima.</li>
</ul>
<p>Voor verdere vragen kan je bij mij terecht, maar voor een sollicitatiebrief is
het makkelijker rechtstreeks te mailen naar Helga, zie onderaan de
<a class="reference external" href="http://www.nelen-schuurmans.nl/NL/bedrijfsprofiel/carriere/13">officiële vacature</a>.</p>
<a class="reference external image-reference" href="http://photos.reinout.vanrees.org/Grok-zope-plone-python/nelen-schuurmans/11586589_bzdfkF#1062781161_dwsuv-A-LB"><img alt="Fietsen in de buurt van Utrecht (Langbroekerwetering)" src="http://photos.reinout.vanrees.org/photos/1062781161_dwsuv-M.jpg" /></a>
</div>

        </div>
      </content>

    </entry>
    
  <entry>
    <title>Nose testing corner case: executable .py files</title>
    <link rel="alternate" type="text/html"
          href="http://reinout.vanrees.org/./weblog/2011/10/14/nose-testing-corner-case.html" />
      <id>http://reinout.vanrees.org/./weblog/2011/10/14/nose-testing-corner-case.html</id>
      <author>
        <name>Reinout van Rees</name>
      </author>
      <published>2011-10-14T00:00:00+01:00</published>
      <updated>2011-10-14T13:39:00+01:00</updated>

      
      <category term="python" />
      

      <content type="xhtml"
               xml:base="http://reinout.vanrees.org/"
               xml:lang="en-US"
               xml:space="preserve">
        <div xmlns="http://www.w3.org/1999/xhtml">
          <div class="document">
<p>We're using nose to run our tests. Easy test discovery and so. So I also went
after some existing internal libraries and simplified the test running there.</p>
<p>There were problems though with one library: <strong>the docstrings inside the
python modules weren't being run</strong>. In a similar setup in another library,
everything was fine.</p>
<p>I googled some and found <a class="reference external" href="http://stackoverflow.com/questions/6333495/run-all-tests-in-directory-using-nose/6334161#6334161">the answer on stackoverflow</a>:
nose by default skips executable <tt class="docutils literal">*.py</tt> files...</p>
<p>And the files in that single library were executable for historical reasons...</p>
<p>Solution: pass the <a class="reference external" href="http://readthedocs.org/docs/nose/en/latest/usage.html#cmdoption--exe">--exe option to nose</a> and
they're not skipped anymore.</p>
</div>

        </div>
      </content>

    </entry>
    
  <entry>
    <title>Moving to github from our company-internal svn</title>
    <link rel="alternate" type="text/html"
          href="http://reinout.vanrees.org/./weblog/2011/10/11/moving-svn-to-github.html" />
      <id>http://reinout.vanrees.org/./weblog/2011/10/11/moving-svn-to-github.html</id>
      <author>
        <name>Reinout van Rees</name>
      </author>
      <published>2011-10-11T00:00:00+01:00</published>
      <updated>2011-10-14T13:39:00+01:00</updated>

      
      <category term="python" />
      
      <category term="nelenschuurmans" />
      

      <content type="xhtml"
               xml:base="http://reinout.vanrees.org/"
               xml:lang="en-US"
               xml:space="preserve">
        <div xmlns="http://www.w3.org/1999/xhtml">
          <div class="document">
<p>We're slowly moving internal svn projects over to github. I wrote up some notes
to help colleagues moving projects themselves. Which can be helpful for others,
so I've cleaned my notes up a bit and I'm posting them here.</p>
<div class="section" id="initial-one-time-setup">
<h1>Initial one-time setup</h1>
<p>First things first: create a <tt class="docutils literal"><span class="pre">~/.svn2git/authors</span></tt> file. <tt class="docutils literal">svn2git</tt>
uses that to couple <em>svn users</em> with <em>emails known to
github</em>. (<strong>Update 2011-10-13</strong>: I used to say &quot;usernames on github&quot;
instead of &quot;emails known to github&quot;, Adam Reeve corrected me in the
comments below. He's right that the username doesn't matter, you can
use a real name.)</p>
<p>Here's a sample from mine:</p>
<pre class="literal-block">
# svn_username = Real Name &lt;email&#64;you.use.in.github&gt;
Reinout = Reinout van Rees &lt;reinout&#64;vanrees.org&gt;
Gijs = Gijs Nijholt &lt;gijs.nijholt&#64;gmail.com&gt;
reinout.vanrees = Reinout van Rees &lt;reinout&#64;vanrees.org&gt;
...
</pre>
<p>If there are usernames missing, <tt class="docutils literal">svn2git</tt> will warn you about it, btw.</p>
<p>Next, install <tt class="docutils literal">svn2git</tt>. See <a class="reference external" href="https://github.com/nirvdrum/svn2git#readme">https://github.com/nirvdrum/svn2git#readme</a>
. I've copied the instructions for debian/ubuntu here:</p>
<pre class="literal-block">
$&gt; sudo apt-get install git-core git-svn ruby rubygems
$&gt; sudo gem install svn2git --source http://gemcutter.org
</pre>
</div>
<div class="section" id="converting-something-to-github">
<h1>Converting something to github</h1>
<p>Create a temporary directory and chdir to it:</p>
<pre class="literal-block">
$&gt; mkdir /tmp/something
$&gt; cd /tmp/something
</pre>
<p>Call svn2git on the main directory, <em>not</em> on trunk:</p>
<pre class="literal-block">
$&gt; svn2git https://office.nelen-schuurmans.nl/svn/Products/Library/timeseries
Using higher level of URL: ...
W: Ignoring error from SVN, path probably does not exist: ...
W: Do not be alarmed at the above message git-svn is just ...
This may take a while on large repositories
W: +empty_dir: Products/Library/timeseries/trunk/doc/source/_static
W: +empty_dir: Products/Library/timeseries/trunk/timeseries.egg-info
...
Lots of warnings and fatal errors, btw.
...
HEAD is now at a6be9f3... Tagging 0.7
Previous HEAD position was a6be9f3... Tagging 0.7
Switched to branch 'master'
Counting objects: 367, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (359/359), done.
Writing objects: 100% (367/367), done.
Total 367 (delta 220), reused 0 (delta 0)
</pre>
<p>Your temporary directory is now a full local git repository. Now we have to
make the github repository (&quot;the remote&quot;) and push our repository to it.</p>
<p>Create a repository on github and follow the instructions for importing an
existing repository:</p>
<pre class="literal-block">
$&gt; git remote add origin git&#64;github.com:nens/timeseries.git
$&gt; git push -u origin master
</pre>
<p>And also push the tags:</p>
<pre class="literal-block">
$&gt; git push --tags
</pre>
<p>Then, run <tt class="docutils literal">git branch</tt> to see if there are branches that you want to push
over to git as they're NOT pushed by default:</p>
<pre class="literal-block">
$&gt; git branch
* master
reinout-experimental
$&gt; git push origin reinout-experimental
</pre>
</div>
<div class="section" id="corner-case-moved-svn-directories">
<h1>Corner case: moved svn directories</h1>
<p>Several of our projects are old enough to have been moved inside our svn
repository. Such a move is not something that <tt class="docutils literal">svn2git</tt> likes. In such a
case, find the revision number of the move in svn and run <tt class="docutils literal">svn2git</tt> as
follows:</p>
<pre class="literal-block">
$ svn2git https://office.nel...nl/timeseries --revision 11070
</pre>
<p>This starts the conversion at <em>that</em> revision number. You <em>do</em> lose the
specific changesets from before that time, though.</p>
<a class="reference external image-reference" href="http://photos.reinout.vanrees.org/Construction-demolition/Demolition/6693311_HMHXxF#703804382_s3DAL-A-LB"><img alt="Removal of old houses for a train tunnel in Delft, the Netherlands" src="http://photos.reinout.vanrees.org/photos/703804382_s3DAL-M.jpg" /></a>
</div>
</div>

        </div>
      </content>

    </entry>
    
  <entry>
    <title>Github introduction for my colleagues</title>
    <link rel="alternate" type="text/html"
          href="http://reinout.vanrees.org/./weblog/2011/10/04/github.html" />
      <id>http://reinout.vanrees.org/./weblog/2011/10/04/github.html</id>
      <author>
        <name>Reinout van Rees</name>
      </author>
      <published>2011-10-04T00:00:00+01:00</published>
      <updated>2011-10-05T00:51:00+01:00</updated>

      
      <category term="python" />
      
      <category term="nelenschuurmans" />
      

      <content type="xhtml"
               xml:base="http://reinout.vanrees.org/"
               xml:lang="en-US"
               xml:space="preserve">
        <div xmlns="http://www.w3.org/1999/xhtml">
          <div class="document">
<p><a class="reference external" href="http://www.nelen-schuurmans.nl">We</a> are <a class="reference external" href="http://reinout.vanrees.org/weblog/2011/09/30/github.html">moving to github</a>. I haven't done
much with git and tend to get confused now and then. And I'm more comfortable
with mercurial. But... I get to give an introduction/explanation on git this
afternoon for my colleauges anyway.</p>
<p>Perhaps this relatively fresh look at git/github can help others, so I'm
writing it down here. Handy in any case as one of my colleagues isn't here now
as he's preparing for a marathon :-)</p>
<p>Oh, and I'll probably get things wrong in here as I'm not familiar enough
yet. So consider yourself warned.</p>
<div class="section" id="basic-mental-model-of-how-git-github-works">
<h1>Basic mental model of how Git/github works</h1>
<p>The basic concept of git, paraphrased a bit:</p>
<ul class="simple">
<li>You've got multiple respositories in various places.</li>
<li>Every repository is basically a big bucket of changesets and a handful of
pointers.</li>
<li>Git effectively starts out empty and and applies a string of changesets and
ends up at a directory filled with your source code.</li>
<li>Which string of changesets? That is determined by the pointer. You point at
a certain changeset and you get that one and its parents. (They all have a
pointer to their parent).</li>
<li>A pointer can be a tag or a branch or even a pointer at something in another
repository.</li>
</ul>
<p>&quot;Multiple repositories in various places&quot;? Yep. A project on github is a
repository. If you grab a local copy (=you clone it), your local copy is also
a repository with all the contents. If you &quot;fork&quot; a project on github? Yep, a
full copy. Getting stuff from one to the other means &quot;push&quot; and &quot;pull&quot;.</p>
<p>But if your local thingy is a full repository, including all
branches/tags/whatever, what ends up in your actual code directory? This is
because git has two layers (and effectively three if you count a remote
repository):</p>
<ul class="simple">
<li>Your actual code directory. This is your visible directory. The git name for
it is your &quot;working copy&quot;.</li>
<li>The full contents of the repository including pointers and
changesets. Hidden in your working copy in the <tt class="docutils literal">.git/</tt> directory. When you
grab stuff from a remote repository, it ends up in here. And when you push,
it is the contents of the <tt class="docutils literal">.git</tt> directory that you push over.</li>
<li>One or more remote repositories. (Technically those repositories' indexes:
you can't muck around directly in someone else's working copy, of course).</li>
</ul>
<p>Let me sneak in one extra layer:</p>
<ul class="simple">
<li>The so-called &quot;index&quot;. These are the local changes you've collected for your
next commit. So if you commit them, the index is empty again and what's in
your index is now in your repository.</li>
</ul>
<p>So, compared to subversion, you've got one extra layer to keep track of, which
of course complicates the mental model of what's happening. With subversion,
you only have a central repository and a local working copy. Git (and
mercurial, bzr and the rest) add that full local repository (git's &quot;index&quot;).</p>
</div>
<div class="section" id="most-basic-git-commands">
<h1>Most basic git commands</h1>
<p>Here are the git commands I've personally used in the last few weeks:</p>
<pre class="literal-block">
62 git status
31 git diff
26 git push
20 git add
18 git commit
17 git show
13 git mv
13 git help
10 git pull
</pre>
<p>(Output of <tt class="docutils literal">history|grep git|awk '{print $2 &quot; &quot; <span class="pre">$3}'|sort|uniq</span> <span class="pre">-c|sort</span>
<span class="pre">-nr|head</span></tt>).</p>
<p>I won't repeat git's own documentation here. You'll probably have to do quite
some googling before you're comfortable with those commands. I'll have to do
that googling myself, too.</p>
<p>Some comments on most of those commands that might help. Note that I'm using
italics explicitly here to differentiate <em>your</em> repository from the <em>remote</em>
repository (assuming you have a remote repository at github). Here you go:</p>
<ul>
<li><p class="first"><tt class="docutils literal">git status</tt>. Relatively helpful, as it often suggests what you need to
do. If files are changed, but they haven't been &quot;added to the index&quot; yet, it
suggests that you might want to <tt class="docutils literal">git add</tt> them. The index is what ends up
as your next commit, so this is the set of changes you want to stuff into
<em>your</em> repository:</p>
<pre class="literal-block">
$ git status
# On branch master
# Changes not staged for commit:
#   (use &quot;git add &lt;file&gt;...&quot; to update what will be committed)
#   (use &quot;git checkout -- &lt;file&gt;...&quot; to discard changes in working directory)
#
#   modified:   MANIFEST.in
#
no changes added to commit (use &quot;git add&quot; and/or &quot;git commit -a&quot;)
</pre>
</li>
<li><p class="first"><tt class="docutils literal">git diff</tt>.  All by itself, without options, it is the difference between
your local working copy and the index. So the changes that you haven't
marked yet for inclusion in your next commit:</p>
<pre class="literal-block">
$ git diff
diff --git a/MANIFEST.in b/MANIFEST.in
index c57b4ff..fe6332b 100644
--- a/MANIFEST.in
+++ b/MANIFEST.in
&#64;&#64; -1,2 +1,2 &#64;&#64;
 include *.rst
-recursive-include lizard_map *.rst *.py *.html *.css *.js *.jpg *.png *.gif *.pdf *.shp *.json
+recursive-include lizard_map *.rst *.py *.html *.css *.js *.jpg *.png *.gif *.pdf *.shp *.json *.po *.mo
</pre>
<p>After adding a couple of things to the index, you probably want to review
what you've &quot;staged for commit in the index&quot;:</p>
<pre class="literal-block">
$ git diff --staged
diff --git a/MANIFEST.in b/MANIFEST.in
index c57b4ff..fe6332b 100644
--- a/MANIFEST.in
+++ b/MANIFEST.in
&#64;&#64; -1,2 +1,2 &#64;&#64;
 include *.rst
-recursive-include lizard_map *.rst *.py *.html *.css *.js *.jpg *.png *.gif *.pdf *.shp *.json
+recursive-include lizard_map *.rst *.py *.html *.css *.js *.jpg *.png *.gif *.pdf *.shp *.json *.po *.mo
</pre>
</li>
<li><p class="first"><tt class="docutils literal">git add</tt>. Just add filenames as suggested by <tt class="docutils literal">git status</tt>.  A helpful
variant is <tt class="docutils literal">git add <span class="pre">-u</span></tt>, this automatically adds changes to all
already-known files. (<tt class="docutils literal"><span class="pre">-a</span></tt> adds all changes, but at the risk of including
unwanted files).</p>
</li>
<li><p class="first"><tt class="docutils literal">git commit</tt>. This stuffs the changes you added to (=staged in) the index
to <em>your</em> repository.</p>
</li>
<li><p class="first"><tt class="docutils literal">git push</tt>. This pushes what's in <em>your</em> repository to the <em>remote</em>
repository. (Assuming git knows what your <em>remote</em> repository is and which
branch in the <em>remote</em> repository it has to talk to. And when the push
doesn't break the <em>remote</em> repository. And when you've got access to that
<em>remote</em> repository.)</p>
</li>
<li><p class="first"><tt class="docutils literal">git pull</tt>. This is basically a combination of <tt class="docutils literal">git fetch</tt> followed by
<tt class="docutils literal">git merge</tt>. Fetch fetches the current state of the <em>remote</em> repository
(all changesets and pointers), merge merges those changes (=*your*
repository) into your working copy. So just do a <tt class="docutils literal">git pull</tt> to grab the
changes of the <em>remote</em> repository and get them in <em>your</em> repository and
your working copy.</p>
</li>
<li><p class="first"><tt class="docutils literal">git help</tt>. <tt class="docutils literal">git help some_command</tt> or <tt class="docutils literal">git some_command <span class="pre">--help</span></tt> both
do the same. Quite decent documentation, actually! <strong>Use &quot;git help&quot;  often
when starting out with git!</strong></p>
</li>
</ul>
<p>Don't forget you can add <tt class="docutils literal"><span class="pre">-v</span></tt> to get more verbose output of what git is
doing. This can be very helpful to see what's happening! Compare:</p>
<pre class="literal-block">
$ git pull
Already up-to-date.
</pre>
<p>with the more explanatory:</p>
<pre class="literal-block">
$ git pull -v
From github.com:lizardsystem/lizard-map
 = [up to date]      alexandr-workspace-changes -&gt; origin/alexandr-workspace-changes
 = [up to date]      gijs-nepal -&gt; origin/gijs-nepal
 = [up to date]      master     -&gt; origin/master
 = [up to date]      reinout-api -&gt; origin/reinout-api
 = [up to date]      reinout-tastypie -&gt; origin/reinout-tastypie
Already up-to-date.
</pre>
</div>
<div class="section" id="github-remote-repository">
<h1>Github remote repository</h1>
<p>When you create a new repository on github, you get a &quot;clone url&quot;. Something
like <tt class="docutils literal"><span class="pre">https://github.com/jcrocholl/pep8.git</span></tt>. To get a local copy, clone it:</p>
<pre class="literal-block">
$ git clone https://github.com/jcrocholl/pep8.git
Cloning into pep8...
remote: Counting objects: 861, done.
remote: Compressing objects: 100% (369/369), done.
remote: Total 861 (delta 424), reused 833 (delta 398)
Receiving objects: 100% (861/861), 160.20 KiB, done.
Resolving deltas: 100% (424/424), done.
</pre>
<p>This sets up some defaults in <em>your</em> repository. &quot;<em>Your</em> repository&quot; means
that &quot;pep8&quot; directory that got set up (and more specifically the <tt class="docutils literal">.git/</tt>
directory inside it).</p>
<p>Some of the defaults that are helpful to know and that partially explains why
several of the commands I showed don't need that many options:</p>
<ul>
<li><p class="first"><tt class="docutils literal">git branch</tt> shows just a &quot;master&quot; branch. That's &quot;trunk&quot; in
svn-speak. The star in front of it shows it as the default branch:</p>
<pre class="literal-block">
$ git branch
* master
</pre>
</li>
<li><p class="first"><tt class="docutils literal">git branch <span class="pre">-a</span></tt> shows also the remote branches:</p>
<pre class="literal-block">
$ git branch -a
* master
  remotes/origin/HEAD -&gt; origin/master
  remotes/origin/master
</pre>
<p>That <tt class="docutils literal">remotes/origin/master</tt> remote is a pointer at the master (so: the trunk) of
the remote github repository.</p>
</li>
<li><dl class="first docutils">
<dt><em>Your</em> respository's &quot;master&quot; branch has been configured with the <em>remote</em></dt>
<dd><p class="first last">repository's &quot;master&quot; as its default push/pull location. So <tt class="docutils literal">git pull</tt>
grabs new revisions from github into <em>your</em> repository and updates your
working copy.</p>
</dd>
</dl>
</li>
</ul>
</div>
<div class="section" id="integration-with-checkoutmanager">
<h1>Integration with checkoutmanager</h1>
<p>Many colleages (and others!) use my <a class="reference external" href="http://pypi.python.org/pypi/checkoutmanager">checkoutmanager</a> tool. Based on a config file,
it manages your various svn/git/hg/bzr checkouts. Normally you have a couple
of directories with checkouts and doing &quot;svn up&quot; in all of them is a bit of a
chore. That's where checkoutmanager comes in. <tt class="docutils literal">checkoutmanager up</tt> does an
<tt class="docutils literal">svn up</tt> in all your svn directories.</p>
<p>And.... for git it does a <tt class="docutils literal">git pull</tt>. Yes, it supports git.</p>
<p><tt class="docutils literal">checkoutmanager status</tt> does <tt class="docutils literal">git status</tt> in your git directories that it
knows about.</p>
<p><strong>Very</strong> handy: <tt class="docutils literal">checkoutmanager out</tt>, which tells you which of <em>your</em>
respositories have commits that you haven't pushed to github yet!</p>
<p>As a quick example, here's a snippet from my <tt class="docutils literal">.checkoutmanager.cfg</tt>:</p>
<pre class="literal-block">
[git]
vcs = git
basedir = ~/git/
checkouts =
    git&#64;github.com:reinout/pep8.git
    git&#64;github.com:reinout/gitignore.git

[nensgit]
vcs = git
basedir = ~/git/nens/
checkouts =
    git&#64;github.com:lizardsystem/nensskel.git
    git&#64;github.com:lizardsystem/lizard-ui.git
    git&#64;github.com:lizardsystem/lizard-map.git
</pre>
</div>
<div class="section" id="integration-with-buildout">
<h1>Integration with buildout</h1>
<p>We use buildout for all our projects. Handier than pip/virtualenv as it
provides some extra functionality like easy django setup, apache config files
from templates, cronjob setup and so on. And... it has <a class="reference external" href="http://pypi.python.org/pypi/mr.developer">mr.developer</a>. A buildout extension for
managing a couple of git (or svn/bzr/hg) checkouts inside your project.</p>
<p>Because.... git and mercurial don't have something that works like
svn:externals. Or at least that doesn't work as
nice/reliable/integrated/whatever. But mr.developer takes care of that in
buildout.  Here's a relevant snippet from a <tt class="docutils literal">buildout.cfg</tt>:</p>
<pre class="literal-block">
[buildout]
...
extensions =
    mr.developer
    buildout-versions
parts =
    mkdir
    django
    ...
develop = .
...

[sources]
lizard-ui = git git&#64;github.com:lizardsystem/lizard-ui.git

...
</pre>
<p>This does three things:</p>
<ul class="simple">
<li>It adds a <tt class="docutils literal">bin/develop</tt> command for managing your sources.</li>
<li>It adds a <tt class="docutils literal">src/</tt> directory where it places any checkouts/clones it makes.</li>
<li>bin/develop now knows that the &quot;lizard-ui&quot; package is a git checkout with a
certain clone url.</li>
</ul>
<p>mr.developer does a couple of things. Here's the help:</p>
<pre class="literal-block">
$ bin/develop -h
usage: develop [-h] [-v]  ...

optional arguments:
  -h, --help        show this help message and exit
  -v, --version     show program's version number and exit

commands:

    activate, a     Add packages to the list of development packages.
    checkout, co    Checkout packages
    deactivate, d   Remove packages from the list of development packages.
    help, h         Show help
    info            Lists informations about packages.
    list, ls        Lists tracked packages.
    rebuild, rb     Run buildout with the last used arguments.
    reset           Resets the packages develop status.
    status, stat, st
                    Shows the status of tracked packages.
    update, up      Updates all known packages currently checked out.
</pre>
<p>Once you want to work on the &quot;trunk&quot; of one of the packages managed by
mr.developer, check it out (which also activates it):</p>
<pre class="literal-block">
$ bin/develop checkout lizard-ui
INFO: Queued 'lizard-ui' for checkout.
INFO: Cloned 'lizard-ui' with git.
INFO: Activated 'lizard-ui'.
WARNING: Don't forget to run buildout again, so the checked out packages are used as develop eggs.
</pre>
<p>As mentioned in that message, run buildout again and it will be installed as a
development egg.</p>
<p><tt class="docutils literal">bin/develop status</tt> shows the current status of the checkouts, but the
output isn't very clear. Run <tt class="docutils literal">bin/develop status <span class="pre">--help</span></tt> to see what it all
means.</p>
<p><tt class="docutils literal">bin/develop update</tt> does a <tt class="docutils literal">git pull</tt> for every package.</p>
</div>
<div class="section" id="creating-a-new-project">
<h1>Creating a new project</h1>
<p>Starting a new project and getting it in github is pretty simple:</p>
<ul class="simple">
<li>First create it in github (just clicky-clicky on the github website).</li>
<li>Then set up your project locally (we use <a class="reference external" href="http://pypi.python.org/pypi/nensskel">nensskel</a>).</li>
<li>Run <tt class="docutils literal">git init</tt> in that directory to turn it into a git
directory. Add/commit/adjust where needed.</li>
<li>Do a <tt class="docutils literal">git push</tt> to the clone url you see in your git webpage!</li>
</ul>
</div>
<div class="section" id="upcoming-branches-and-workflow">
<h1>Upcoming: branches and workflow</h1>
<p>What I haven't covered yet: branches and workflow. I'll do that in a later article.</p>
</div>
</div>

        </div>
      </content>

    </entry>
    
  <entry>
    <title>We're moving to github</title>
    <link rel="alternate" type="text/html"
          href="http://reinout.vanrees.org/./weblog/2011/09/30/github.html" />
      <id>http://reinout.vanrees.org/./weblog/2011/09/30/github.html</id>
      <author>
        <name>Reinout van Rees</name>
      </author>
      <published>2011-09-30T00:00:00+01:00</published>
      <updated>2011-09-30T01:12:00+01:00</updated>

      
      <category term="django" />
      
      <category term="nelenschuurmans" />
      
      <category term="plone" />
      

      <content type="xhtml"
               xml:base="http://reinout.vanrees.org/"
               xml:lang="en-US"
               xml:space="preserve">
        <div xmlns="http://www.w3.org/1999/xhtml">
          <div class="document">
<p>I'm working at <a class="reference external" href="http://www.nelen-schuurmans.nl">Nelen &amp; Schuurmans</a> and
we've got some 10 people working with Django (out of 30-40 total). Quite a
sizable group. And virtually all our software is open source (GPL). Quite
specific software, but GPL nonetheless.</p>
<p>Of course, <strong>everything is in version control</strong>. In our case subversion,
combined with trac for issue tracking. Works fine. But all those new-fangled
distributed version control systems (bzr, git, hg/mercurial) looked mighty
fine, too.  I won't delve into too many details, but here are some
observations:</p>
<ul class="simple">
<li>Git, bzr, hg. Well, bzr ('bazaar') is out. I've used it personally for a
while and it is/was slow. After two years of &quot;but the next version is fast&quot;,
I just don't bother anymore. And the repository format kept changing. At a
certain point I couldn't push anymore to the repository on my server as the
bzr version there was a bit older... It took my an hour of version-juggling
to get everything working again. Bye bye bzr.</li>
<li>Oh, and git and hg are the two most popular ones. Bzr doesn't come close.</li>
<li>Hm. Likewise, hg doesn't come close to git. But hg is big, too.</li>
<li>Actually, the difference is more between &quot;github&quot; and &quot;bitbucket&quot;. If we're
going to use git, we'll use github. And for hg we'll use bitbucket. Our code
is open source, so we want to make it available.</li>
<li>Actually, that's a pretty good reason for a distributed version control
system: we can move from our internal subversion system to a public
distributed one. Yeah, we can move subversion over to sourceforge or so, but
that doesn't make much sense.</li>
<li>Distributed version control is much better at merging. We ought to use
branches more, so this is a good point.</li>
<li>And it is quicker. Important, as our internal subversion <em>used</em> to be dog
slow. Since the server crashed (well, it ran out of disk space and our
sysadmin was on holiday and we didn't have the password for this one single
server...), we moved it off the windows machine onto a fresh ubuntu server
and it has been <em>fast</em> since.</li>
</ul>
<p>Also important is the <strong>psychological effect of djangocon.eu</strong>. We attended
with three of us. When you're watching three days of presentations and see
that most of them have a github link on their last slide... The bitbucket talk
was one of the few without a github link :-) Some conclusions we/I drew from
that:</p>
<ul class="simple">
<li>I sure like(d) bitbucket a bit more than github, but github is really where
the action is.</li>
<li>Do you still use subversion? Hand me that shovel, will you? Then I can bury
you with yonder obsolete dinosaurs.</li>
<li>Everything is on github.</li>
<li>To be a good open source citizen, your project needs to be on github (or
bitbucket). Oh, and on readthedocs.org.</li>
</ul>
<p>In the end, we had to make a choice between github and bitbucket. I was in
favour of bitbucket, but switched to the github camp after much thinkwork. The
basic dilemma is simple:</p>
<ul class="simple">
<li>Git and hg are functionally basically equivalent.</li>
<li>Hg is much friendlier than git. They accomplish the same, but hg manages to
have sane names for its commands where git goes out of its way to be a pain
in the ass. Why o why o why. Git still irritates me when a <tt class="docutils literal">git st</tt>
doesn't work. You need to type in the full <tt class="docutils literal">git status</tt> command. Every
subversion user types <tt class="docutils literal">svn st</tt> instead of <tt class="docutils literal">svn status</tt>. &quot;It is good to
irritate everyone used to subversion in order to get them to understand git
better&quot; is apparently the thought. Even though hg, which does virtually the
same as git, <em>has</em> sane names and commands for the very same concepts.</li>
<li>Github blows bitbucket out of the water. At the moment, github is a bit
ahead in features, but the important point is the user base. Github's user
base is probably an order of magnitude bigger than bitbucket's.</li>
<li>So what you want is a github that works with hg....</li>
</ul>
<p>The reason for me to eventually make my choice and to suggest github:
<a class="reference external" href="https://github.com/plone">https://github.com/plone</a>. <a class="reference external" href="http://plone.org">Plone</a> (the biggest Python CMS,
for those that don't know it) is switching over to github from their own
subversion repository. I worked with Plone for a long time and know and
respect a lot of technically very adept people there. So their judgment is
important to me.</p>
<p>There was a <a class="reference external" href="http://www.doodle.com/d9x9x74b97g3mtbv">questionaire a year ago</a> or so about the options. Keep svn,
own git, github, own hg, bitbucket. The <a class="reference external" href="http://plone.293351.n2.nabble.com/Result-DVCS-options-for-Plone-Core-td6297887.html">results were clear</a>:
<em>I'm amazed how clear the vote result is! We don't have to wait any longer to
draw a conclusion: We want to move to Github.</em></p>
<p>Well, we at Nelen &amp; Schuurmans have two accounts now:</p>
<ul class="simple">
<li><a class="reference external" href="https://github.com/lizardsystem">https://github.com/lizardsystem</a> for our django applications and so. A the
moment (2011-09-30), I've got two projects in there. More will be moved next
week.</li>
<li><a class="reference external" href="https://github.com/nens">https://github.com/nens</a> for the django sites. These will be private. They're
GPL, but there's customer data in there sometimes so we don't actually
distribute it ourselves. Most sites are pretty empty, though, with just a
<tt class="docutils literal">setings.py</tt>, some fixtures and a customer logo. We <em>did</em> want a separate
account for this as it makes it easier to treat the django apps as the
re-usable generic apps that we want them to be. And they're part of our
Lizard information system that we intend as an open source platform for
water-related geographical data, so it is good to keep that separate from
our company's github account.</li>
</ul>
<p>Github, here we come.</p>
<p>Now if I only can get all those commands loaded in my head... :-)</p>
<p>Well, at least github publicly says that <a class="reference external" href="https://github.com/blog/948-biking-to-work-is-awesome">biking to work is awesome</a>, so that earns them
a couple of points in their favour from me.</p>
</div>

        </div>
      </content>

    </entry>
    
  <entry>
    <title>Django class based view walkthrough: TemplateView</title>
    <link rel="alternate" type="text/html"
          href="http://reinout.vanrees.org/./weblog/2011/08/24/class-based-views-walkthrough.html" />
      <id>http://reinout.vanrees.org/./weblog/2011/08/24/class-based-views-walkthrough.html</id>
      <author>
        <name>Reinout van Rees</name>
      </author>
      <published>2011-08-24T00:00:00+01:00</published>
      <updated>2011-08-25T10:14:00+01:00</updated>

      
      <category term="django" />
      
      <category term="plone" />
      

      <content type="xhtml"
               xml:base="http://reinout.vanrees.org/"
               xml:lang="en-US"
               xml:space="preserve">
        <div xmlns="http://www.w3.org/1999/xhtml">
          <div class="document">
<p>In my previous entry I showed <a class="reference external" href="http://reinout.vanrees.org/weblog/2011/08/24/class-based-views-usage.html">a simple TemplateView example</a>. I'll
make it even simpler. <tt class="docutils literal">views.py</tt>:</p>
<pre class="literal-block">
from django.views.generic.base import TemplateView


class SomeView(TemplateView):
    template_name = 'my_app/someview.html'

    def get_context_data(self, **kwargs):
        return {'name': 'reinout}
</pre>
<p>And in <tt class="docutils literal">urls.py</tt>:</p>
<pre class="literal-block">
import ...

urlpatterns = patterns(
    ...
    url(r'^class_based_view/$',
        my_app.views.SomeView.as_view(),
        name='class_based_view'),
</pre>
<p>As you can probably see, there are two things you need to somehow know when
subclassing from TemplateView:</p>
<ul class="simple">
<li>Add a <tt class="docutils literal">template_name</tt> attribute on the class.</li>
<li>Add a <tt class="docutils literal">.get_context_data()</tt> method that returns the context dictionary.</li>
<li>In your <tt class="docutils literal">urls.py</tt>, import the class and append <tt class="docutils literal">.as_view()</tt>.</li>
</ul>
<p>&quot;You need to know something somehow&quot; is perfectly fine: that's what
documentation is for. You need to know a lot of those things, like that models
go into <tt class="docutils literal">models.py</tt> and templates into a <tt class="docutils literal">templates/</tt> subdirectory, for
instance.</p>
<p><strong>For better understanding, let's dive into the django code and take a
detailed look at what happens</strong>. That way, we can better understand what
happens and that way we can make our own view and perhaps our own mixins.</p>
<p>First things first: open <tt class="docutils literal">django/views/generic/base.py</tt>. Or <a class="reference external" href="https://code.djangoproject.com/browser/django/tags/releases/1.3/django/views/generic/base.py">look at it
online</a>
in a second tab.</p>
<div class="section" id="class-view">
<h1>class View</h1>
<p><tt class="docutils literal">View</tt> is the base class for all views. It takes care of two things that you
should know about:</p>
<ul class="simple">
<li>The <tt class="docutils literal">.as_view()</tt> method you call on the view in your <tt class="docutils literal">urls.py</tt>.</li>
<li>It calls <tt class="docutils literal">.get()</tt> for a GET request, <tt class="docutils literal">.post()</tt> for a POST request and so
on.</li>
</ul>
<p>In your urls.py, you need to import the class and feed it to your urls with a
<tt class="docutils literal">.as_view()</tt> appended. You might ask why that .as_view() is needed. It is
needed because you want your view objects to be thread-safe. That's an
expensive word for &quot;self.bla_bla = 3 should be safe to use and self.bla_bla
should not end up as a value in another view instance&quot;. The way urls.py works
(it expects callables) wouldn't work nicely with classes. Anyway, that's High
Python magery. Nothing to worry about. Just accept that the <tt class="docutils literal">.as_view()</tt> is a pretty fine and decent solution :-)</p>
<p>You can even pass keyword arguments to as_view() which will be set as attributes on your
view objects. So <tt class="docutils literal"><span class="pre">YourView(template_name='my_view.html')</span></tt> will give your
view a <tt class="docutils literal">self.template_name</tt> with the value <tt class="docutils literal">'my_view.html'</tt>.  Provided your
view had some default value for that attribute, otherwise you'd get an error:
nicely explicit.</p>
<p>Behind the scenes, what as_view() returns is the <tt class="docutils literal">.dispatcher()</tt> method that
basically just calls <tt class="docutils literal">.get()</tt> for a GET request, <tt class="docutils literal">.post()</tt> for a POST
request and so on. Of course it passes along arguments and keyword arguments
that your url config wishes to pass along.</p>
<p>Now, you don't need to worry about that dispatch method. You won't ever have
to customize that one. Likewise the as_view method. Remember the two
customization points:</p>
<ul class="simple">
<li>If your view class always has some attribute (<tt class="docutils literal">self.template_name</tt> will be
a common one), you can customize that right in your <tt class="docutils literal">urls.py</tt> by passing
it a long as a parameter (<tt class="docutils literal"><span class="pre">.as_view(template_name='something.html')</span></tt>).</li>
<li>Args and keyword args you defined in your urls.py get passed just fine to
the get()/post()/delete()/head() whatever.</li>
</ul>
</div>
<div class="section" id="class-templateresponsemixin">
<h1>class TemplateResponseMixin</h1>
<p>Hey, our first mixin! Python support multiple inheritance. So your view can
inherit from more than one class. This way, you can mix in one or two methods
or attributes into your class by inheriting from an extra mixin
class.</p>
<p>Warning: using mixins looks fine on the first sight, but experience has shown
that debugging can quickly become an utter hell as you have to look in 10
different spots before you figure out which method does what to your hapless
data... (8 years of zope/plone experience speaking here!)</p>
<p>Anyway... TemplateResponseMixin mixes in two really important things:</p>
<ul class="simple">
<li>A <tt class="docutils literal">template_name</tt> attribute. The value of this gets used as the
template. (Actually, it is a little bit more involved: there's a
<tt class="docutils literal">.get_template_names()</tt> that returns the value of template_name, but
that's not something you'll commonly customize).</li>
<li>A render_to_response() method that accepts a context dictionary and renders
the template with that context.  Behind the scenes, it wraps the context
dictionary automatically in a RequestContext so that all the so-called
context processors are active. Not wrapping the context in a RequestContext
is a common error (&quot;hey, my STATIC_URL variable seems to be empty in my
templates!), so getting it handled automatically is great.</li>
</ul>
<p>As it is a mixin, this render_to_response() isn't actually called
anywhere within the class itself. So onwards to...</p>
</div>
<div class="section" id="class-templateview">
<h1>class TemplateView</h1>
<p>... TemplateView inherits from both View and TemplateResponseMixin. It is
indended just for <strong>rendering a template with a context</strong>. Technically, it
means responding to a GET request and returning a response.</p>
<p>What you need to know about TemplateView itself:</p>
<ul class="simple">
<li>It has a <tt class="docutils literal">.get_context_data()</tt> method that you ought to implement in your
subclass. It should return a context dictionary. So implement this method
and the dictionary you return will be given to your template. Note that this
method gets passed any keyword arguments you defined in your url.</li>
<li>It implements the <tt class="docutils literal">.get()</tt> method that View's dispatcher will call when it
gets a GET request. This one calls <tt class="docutils literal">.get_context_data()</tt> to get the
context and feeds it to TemplateResponseMixin's
<tt class="docutils literal">.render_to_response()</tt>. This happens behind the scenes and you shouldn't
ordinarily have to customize it.</li>
</ul>
<p>So. TemplateView is <strong>useful by itself</strong>. For many views, this is what you
ought to be subclassing. Despite the many layers and
methods-calling-other-methods that happens, there are just two items you <em>need</em> to
implement in your subclass:</p>
<ul class="simple">
<li>The <tt class="docutils literal">template_name</tt> attribute that points at your template.</li>
<li>The <tt class="docutils literal">.get_context_data()</tt> that ought to return a context dictionary.</li>
</ul>
<p>Just for clarity, here's the example view again:</p>
<pre class="literal-block">
from django.views.generic.base import TemplateView


class SomeView(TemplateView):
    template_name = 'my_app/someview.html'

    def get_context_data(self, **kwargs):
        return {'name': 'reinout'}
</pre>
</div>
<div class="section" id="conclusion">
<h1>Conclusion</h1>
<p>In the end, the TemplateView class is pretty simple when you actually use it.</p>
<p>The underlying mechanism is relatively elaborate and depends quite a lot on
every part doing exactly what it has to do. And it consists out of quite some
parts. So I hope the complexity you can build into it doesn't grow out of hand.</p>
<p>Getting the RequestContext automatically is a big plus for TemplateView.</p>
<p>Have fun writing class based views! I'll probably write more about them in the
weeks to come as I discover more things. There are handy views for
redirecting, for handling forms and so on. Lots to discover.</p>
</div>
</div>

        </div>
      </content>

    </entry>
    
  <entry>
    <title>Using Django's class based views</title>
    <link rel="alternate" type="text/html"
          href="http://reinout.vanrees.org/./weblog/2011/08/24/class-based-views-usage.html" />
      <id>http://reinout.vanrees.org/./weblog/2011/08/24/class-based-views-usage.html</id>
      <author>
        <name>Reinout van Rees</name>
      </author>
      <published>2011-08-24T00:00:00+01:00</published>
      <updated>2011-08-23T22:35:00+01:00</updated>

      
      <category term="django" />
      
      <category term="plone" />
      

      <content type="xhtml"
               xml:base="http://reinout.vanrees.org/"
               xml:lang="en-US"
               xml:space="preserve">
        <div xmlns="http://www.w3.org/1999/xhtml">
          <div class="document">
<p>Django's class based views are a welcome departure from the old-style views
that were just a function. An example is best. I'll show the same view as a
function and as a class.  From the <tt class="docutils literal">views.py</tt>:</p>
<pre class="literal-block">
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.views.generic.base import TemplateView


def _expensive_calculation(request):
    # Calculate something and cache the result.
    return 'something'


def some_view(request):
    template = 'my_app/someview.html'
    data = _expensive_calculation(request)
    return render_to_response(
        template,
        {'data': data},
        context_instance=RequestContext(request))


class SomeView(TemplateView):
    template_name = 'my_app/someview.html'

    def _expensive_calculation(self):
        # Calculate something and cache the result.
        # You can use self.request!
        return 'something'

    def get_context_data(self, **kwargs):
        return {'data': self._expensive_calculation()}
</pre>
<p>So here you have it. Regarding the function based view:</p>
<ul class="simple">
<li>The <tt class="docutils literal">some_view()</tt> function is clear and explicit.</li>
<li>But you often have helper functions strewn all over the place.</li>
<li>You have quite some repetitive code (&quot;render_to_response(template...)&quot;) in
every view function.</li>
</ul>
<p>Regarding the class based view:</p>
<ul class="simple">
<li>You need to know some characteristics of the django base class
(<tt class="docutils literal">TemplateView</tt>), like that you need to set <tt class="docutils literal">template_name</tt>.</li>
<li>You need to know similar things for function based views.</li>
<li>Nice: the helper function is now encapsulated within the class. Neat and
clean.</li>
<li>Less boilerplate.</li>
</ul>
<p>Oh, and you need to hook it up in your <tt class="docutils literal">urls.py</tt> of course:</p>
<pre class="literal-block">
import ...

urlpatterns = patterns(
    ...
    url(r'^function_based_view/$',
        'my_app.views.some_view',
        name='function_based_view'),
    url(r'^class_based_view/$',
        my_app.views.SomeView.as_view(),
        name='class_based_view'),
    ...
</pre>
<p>No real difference there, except that you need to call <tt class="docutils literal">.as_view()</tt> on your
view class. There's a sensible reason for that <tt class="docutils literal">.as_view()</tt>, but it involves
python and django internals that I won't bore you with :-) Just accept it: it
is a very sensible and clean solution from my point of view.</p>
<p>Now, this is a simple example. In real life, I think class based views will
get even more useful the more you use them:</p>
<ul class="simple">
<li>Common functionality in your project can be wrapped in a base class.</li>
<li>Adjusting existing views is easier: just subclass them and change a detail.</li>
<li>I <em>think</em> multiple output options are easier. One subclass for REST output
and one for regular html? Don't know yet, but it might help.</li>
<li>Encapsulation of helper methods inside the classes might mean a big cleanup
of the regular huge amount of helper methods strewn all over the place.</li>
</ul>
</div>

        </div>
      </content>

    </entry>
    

</feed>

