<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
> <channel><title>Adrian Kosmaczewski &#187; Django</title> <atom:link href="http://kosmaczewski.net/category/django/feed/" rel="self" type="application/rss+xml" /><link>http://kosmaczewski.net</link> <description></description> <lastBuildDate>Sat, 05 May 2012 22:16:45 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.2</generator> <item><title>Installing PostgreSQL 8.3 on Leopard</title><link>http://kosmaczewski.net/postgresql-leopard/</link> <comments>http://kosmaczewski.net/postgresql-leopard/#comments</comments> <pubDate>Wed, 23 Apr 2008 15:40:24 +0000</pubDate> <dc:creator>Adrian</dc:creator> <category><![CDATA[Django]]></category> <category><![CDATA[How to?]]></category> <category><![CDATA[Open Source]]></category> <category><![CDATA[Ruby on Rails]]></category> <category><![CDATA[PostgreSQL]]></category> <guid
isPermaLink="false">http://kosmaczewski.net/?p=1152</guid> <description><![CDATA[This is the documented path to my discovery of PostgreSQL 8.3, which I&#8217;ve never used before. Now that MySQL&#8216;s community is getting hammered to death by Sun, and thanks to all the good things I&#8217;ve heard about it over the &#8230; <a
href="http://kosmaczewski.net/postgresql-leopard/">Continue reading <span
class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<p><a
href="http://www.postgresql.org/"><img
src="http://kosmaczewski.net/wp-content/uploads/2008/04/postgre_logo.png" alt="" title="postgre_logo" width="228" height="56" align="left" hspace="10" vspace="5" border="0" /></a>This is the documented path to my discovery of <a
href="http://www.postgresql.org/">PostgreSQL 8.3</a>, which I&#8217;ve never used before. Now that <a
href="http://mysql.com/">MySQL</a>&#8216;s community is getting <a
href="http://blog.wired.com/monkeybites/2008/04/community-cries.html">hammered to death by Sun</a>, and thanks to all the good things I&#8217;ve heard about it over the years (including enhanced performance on multicore systems and greater scalability), I really wanted to install it and play with it.</p><p>Frankly, it&#8217;s not easy. At all (actually this is why I think MySQL is so popular, because of the ease of installation!) So hang tight and read on. <span
id="more-1152"></span></p><h2>Installation</h2><p><a
href="http://www2.russbrooks.com:8080/2007/11/4/install-postgresql-on-mac-os-x-10-5-leopard">Russ Brooks says it is better to avoid</a> <a
href="http://www.macports.org/">MacPorts</a>: I must say that indeed, I tried installing PostgreSQL with MacPorts and I got seriously stuck, unable to do anything with it, not even starting the server. So I uninstalled the PostgreSQL MacPorts package and found other options: a <a
href="http://www.postgresqlformac.com/">PostgreSQL for Mac</a> installer, and <a
href="http://www.entropy.ch/software/macosx/postgresql/">Marc Liyanage&#8217;s own installer</a>. I decided to go with the first one, since Marc&#8217;s installs version 8.1 of the server, and PostgreSQL for Mac offers version 8.3.</p><p>The installation goes without trouble, using a standard installer (which means that you need administrative privileges, as usual). However, you want to read <a
href="http://www2.russbrooks.com:8080/2007/11/4/install-postgresql-on-mac-os-x-10-5-leopard">these explanations</a> before doing anything  ;)</p><p>NOTE: if you have trouble installing PostgreSQL using the above installer (as I did after the unsuccessful MacPorts installation / desinstallation), open Terminal.app and try these commands:</p><p><code>$ sudo dscl localhost
&gt;cd Local/Default/Users/
&gt;ls </code></p><p>If you see a &#8220;postgres&#8221; entry in the list of local users, delete it and re-run the installer:</p><p><code>&gt;delete postgres</code></p><h2>After the Installation</h2><p>The installer puts the PostgreSQL installation into /Library/PostgreSQL8. You should add the path to the PostgreSQL binaries in your PATH environment variable before doing anything else:</p><p><code>export PATH=/Library/PostgreSQL8/bin:$PATH (bash)
setenv PATH /Library/PostgreSQL8/bin:$PATH (tcsh)</code></p><p>Then, type the following commands to create the default database, owned by the &#8220;postgres&#8221; user:</p><p><code>cd /Library/PostgreSQL8/
sudo rm -r data
sudo mkdir data
sudo chown postgres:postgres data
cd bin
sudo -u postgres initdb -E utf8 /Library/PostgreSQL8/data </code></p><p>In my case (Leopard-powered PowerBook G4) I had the &#8220;Shared Memory error&#8221; that Russ talks about. So I followed his advice: I created a file named /etc/sysctl.conf with the following contents, then rebooted my computer and everything went fine:</p><p><code>kern.sysv.shmmax=4194304
kern.sysv.shmmin=1
kern.sysv.shmmni=32
kern.sysv.shmseg=8
kern.sysv.shmall=1024 </code></p><p>I also used dscl to change the password of the postgres user (otherwise, how to know which is it??)</p><p><code>&gt;passwd postgres postgres</code></p><p><img
src="http://kosmaczewski.net/wp-content/uploads/2008/04/postgre_apps.png" alt="" title="postgre_apps" width="448" height="494" class="aligncenter size-full wp-image-1153" /></p><p>The installer adds some handy utilities on your /Application folder (see the image above). You can also use <a
href="http://www.pgadmin.org/">pgAdmin</a> to manage the database server. Very handy. I tried to use <a
href="http://phppgadmin.sourceforge.net/">phpPgAdmin</a> but without success (can&#8217;t get past the login screen&#8230; :(</p><p>And that&#8217;s it! There are interesting resources out there explaining how to use it from <a
href="http://wiki.rubyonrails.org/rails/pages/PostgreSQL">Rails</a> or <a
href="http://www.djangoproject.com/documentation/install/">Django</a>, so I&#8217;ll start reviewing them&#8230;</p> ]]></content:encoded> <wfw:commentRss>http://kosmaczewski.net/postgresql-leopard/feed/</wfw:commentRss> <slash:comments>8</slash:comments> </item> <item><title>Quick spec from your Python tests</title><link>http://kosmaczewski.net/quick-spec-for-python-tests/</link> <comments>http://kosmaczewski.net/quick-spec-for-python-tests/#comments</comments> <pubDate>Thu, 17 Apr 2008 16:25:58 +0000</pubDate> <dc:creator>Adrian</dc:creator> <category><![CDATA[Django]]></category> <category><![CDATA[How to?]]></category> <category><![CDATA[Quality]]></category> <category><![CDATA[Python]]></category> <guid
isPermaLink="false">http://kosmaczewski.net/?p=1148</guid> <description><![CDATA[Using Python&#8217;s own unittest package, here&#8217;s a small script that can iterate over your test suite to output a small, quick, nice list of the tests in your application: [source:python] import unittest loader = unittest.TestLoader() tests = loader.loadTestsFromName(&#8216;path.to.your.tests.package&#8217;) for test &#8230; <a
href="http://kosmaczewski.net/quick-spec-for-python-tests/">Continue reading <span
class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<p>Using Python&#8217;s own unittest package, here&#8217;s a small script that can iterate over your test suite to output a small, quick, nice list of the tests in your application:</p><p>[source:python]
import unittest</p><p>loader = unittest.TestLoader()
tests = loader.loadTestsFromName(&#8216;path.to.your.tests.package&#8217;)
for test in tests.<em>tests:
print test._tests[0].<strong>class</strong>.<strong>name</strong>.replace(&#8220;Test&#8221;, &#8220;&#8221;)
for method in test._tests:
print &#8221;   %s&#8221; % method._testMethodName.replace(&#8220;test</em>&#8220;, &#8220;&#8221;).capitalize().replace(&#8220;_&#8221;, &#8221; &#8220;)
[/source]</p><p>This would yield something like this: <code> Business
&nbsp;&nbsp;&nbsp;&nbsp;Accounts have at least one entry
&nbsp;&nbsp;&nbsp;&nbsp;Clerks cannot close accounts
Security
&nbsp;&nbsp;&nbsp;&nbsp;Users can create new accounts
&nbsp;&nbsp;&nbsp;&nbsp;Anonymous users cannot access private areas
... </code></p><p>Of course, you&#8217;ll get better results if you follow <a
href="http://googletesting.blogspot.com/2007/02/tott-naming-unit-tests-responsibly.html">Google&#8217;s naming conventions</a> for your tests&#8230; ;) This is not <a
href="http://rspec.info/">rspec</a> (nor an alternative to it) but it might be useful to some of you.</p><p>Just as a reminder for Django users: you might need to</p><p><code> setenv DJANGO_SETTINGS_MODULE application.settings </code></p><p>or</p><p><code> export DJANGO_SETTINGS_MODULE=application.settings </code></p><p>in order to make the script work properly! At least I had to :)</p> ]]></content:encoded> <wfw:commentRss>http://kosmaczewski.net/quick-spec-for-python-tests/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>Django Architecture Approaches</title><link>http://kosmaczewski.net/django-architecture-approaches/</link> <comments>http://kosmaczewski.net/django-architecture-approaches/#comments</comments> <pubDate>Fri, 04 Apr 2008 11:59:12 +0000</pubDate> <dc:creator>Adrian</dc:creator> <category><![CDATA[Architecture]]></category> <category><![CDATA[Django]]></category> <category><![CDATA[Open Source]]></category> <category><![CDATA[Python]]></category> <guid
isPermaLink="false">http://kosmaczewski.net/?p=1141</guid> <description><![CDATA[I&#8217;ve just had a very interesting conversation with my colleague Marco about different approaches to the organization of code inside a Django application. As you might know (and if you don&#8217;t I&#8217;ll tell you anyway), Django&#8217;s views (somehow occupying the &#8230; <a
href="http://kosmaczewski.net/django-architecture-approaches/">Continue reading <span
class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<p>I&#8217;ve just had a very interesting conversation with my colleague <a
href="http://djangopeople.net/mbi/">Marco</a> about different approaches to the organization of code inside a Django application.</p><p>As you might know (and if you don&#8217;t I&#8217;ll tell you anyway), Django&#8217;s views (somehow occupying the &#8220;Controller&#8221; level in an MVC architecture) must take (at least) an <a
href="http://www.djangoproject.com/documentation/request_response/">HttpRequest</a> instance as a parameter and must return an <a
href="http://www.djangoproject.com/documentation/request_response/">HttpResponse</a> instance. <strong>That&#8217;s how it goes in Django, this is </strong><a
href="http://classics.mit.edu/Hippocrates/hippolaw.html"><strong>the law</strong></a><strong>.</strong> This means that you must be sure that the last instruction in your request processing code (in whichever way you&#8217;ve organized it) must return an HttpResponse instance, usually calling the HttpResponse() constructor (or of any of its useful subclasses), or by calling the django.shortcuts.render_to_response() function, or something similar.</p><p>This has, in my opinion, a major drawback: it might limit code reuse and it increases the coupling in the code. Everything&#8217;s not lost, however.<span
id="more-1141"></span></p><p>Before you start the flame wars, let me explain, using an <a
href="http://www.djangoproject.com/documentation/tutorial03/">example coming from the Django website</a>; this represents a basic Django view function, returning some response containing data fetched from the database:</p><p>[source:python]
from django.shortcuts import render_to_response, get_object_or_404</p><h1>&#8230;</h1><p>def detail(request, poll_id):
p = get_object_or_404(Poll, pk=poll_id)
return render_to_response(&#8216;polls/detail.html&#8217;, {&#8216;poll&#8217;: p})
[/source]</p><p>Let&#8217;s say now that I want to reuse that particular data (the &#8216;p&#8217; variable) in another view: given that the return value is always an HttpResponse instance, you are screwed; sometimes you just need the data, to find something, or simply to render it in another format like JSON or XML (RESTful architectures, anyone?). This goes pretty much against the <a
href="http://en.wikipedia.org/wiki/Don't_repeat_yourself">DRY</a> principles, and if you don&#8217;t go deeper than the Django tutorials, your whole application might feature lots of repeated code.</p><p>Even worse, you have a direct reference to a template (&#8220;polls/detail.html&#8221;), and this kind of coupling does not scale well. It can become a real problem in big projects.</p><p>There are, however, strategies to avoid this: the first, the most common, is to refactor your code and to create a &#8220;layer&#8221; of data-specific functions, which will return instances (or arrays thereof) that you can reuse here and there. Doing this in a big project already started requires a good deal of unit testing first, to ensure that your refactoring is not breaking something elsewhere, but that&#8217;s another problem (because you DO unit test, right??). This approach might not scale well in complex projects, and thus you would like to organize your code in other ways.</p><p>I learnt about organizing views using <a
href="http://docs.python.org/ref/callable-types.html">callable objects</a> instead of functions while studying the code in the <a
href="http://code.google.com/p/django-rest-interface/">Django REST Interface project</a>. In this case, you create <a
href="http://code.google.com/p/django-rest-interface/source/browse/trunk/django_restapi/resource.py">code like this</a>:</p><p>[source:python]
class Resource(ResourceBase):
&#8220;&#8221;"
Generic resource class that can be used for
resources that are not based on Django models.
&#8220;&#8221;"</p><pre><code># ... snip ...
def __call__(self, request, *args, **kwargs):
    """
    Redirects to one of the CRUD methods depending
    on the HTTP method of the request. Checks whether
    the requested method is allowed for this resource.
    """
    # Check permission
    if not self.authentication.is_authenticated(request):
        response = HttpResponse(_('Authorization Required'), mimetype=self.mimetype)
        challenge_headers = self.authentication.challenge_headers()
        response._headers.update(challenge_headers)
        response.status_code = 401
        return response
    try:
        return self.dispatch(request, self, *args, **kwargs)
    except HttpMethodNotAllowed:
        response = HttpResponseNotAllowed(self.permitted_methods)
        response.mimetype = self.mimetype
        return response
</code></pre><p>[/source]</p><p>The important bit here is the &#8220;<strong>call</strong>&#8221; method, which allows an instance to be called as a function, without specifying any particular method. This makes me remember of the dreadful <a
href="http://www.vbmigration.com/detknowledgebase.aspx?Id=309">VB default methods</a> but in Python it&#8217;s not that bad, actually (<a
href="http://kosmaczewski.net/2005/09/15/land-of-the-forbidden-maneuver/">VB is horrible by default</a> anyway), and allows you to use a cool syntax to do complex tricks (&#8220;command pattern&#8221; way of doing things, without the method call overload). And of course, since you are using an object-oriented approach, you can use polymorphism and inheritance to organize and reuse code as much as you can (or want).</p><p>Finally, Marco told me that his team uses another cool approach: they avoid returning HttpResponse instances from the views, and instead use <a
href="http://www.python.org/dev/peps/pep-0318/">Python decorators</a> to generate those. This way, you can achieve another neat separation of concerns, and you can reuse code simply and effectively.</p><p>I understand that the <a
href="http://en.wikipedia.org/wiki/Python_philosophy#Programming_philosophy">Python philosophy</a> cares about explicitness, but the &#8220;easy&#8221; way of processing requests in Django leads to trouble in big applications: increased coupling, reduced DRY, more headaches. I think you should use some code-reuse strategy in your Django code, but this, of course, is more an architectural problem than a Django problem.</p> ]]></content:encoded> <wfw:commentRss>http://kosmaczewski.net/django-architecture-approaches/feed/</wfw:commentRss> <slash:comments>11</slash:comments> </item> <item><title>REST + HTTP (Basic + Digest) Authentication support for Django&#8217;s test Client class</title><link>http://kosmaczewski.net/django-rest-test-support/</link> <comments>http://kosmaczewski.net/django-rest-test-support/#comments</comments> <pubDate>Wed, 05 Mar 2008 09:42:39 +0000</pubDate> <dc:creator>Adrian</dc:creator> <category><![CDATA[Django]]></category> <category><![CDATA[How to?]]></category> <category><![CDATA[Quality]]></category> <category><![CDATA[REST]]></category> <guid
isPermaLink="false">http://kosmaczewski.net/2008/03/05/django-rest-test-support/</guid> <description><![CDATA[Django has a nice support for unit and functional testing; however, its django.test.client.Client class does not support PUT and DELETE requests, which might be useful if, like me, you&#8217;re doing some kind of REST implementation using that framework. There&#8217;s an &#8230; <a
href="http://kosmaczewski.net/django-rest-test-support/">Continue reading <span
class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<p><a
href="http://www.djangoproject.com/">Django</a> has a nice support for unit and functional testing; however, its django.test.client.Client class does not support PUT and DELETE requests, which might be useful if, like me, you&#8217;re doing some kind of <a
href="http://en.wikipedia.org/wiki/Representational_State_Transfer">REST</a> implementation using that framework. There&#8217;s an <a
href="http://code.djangoproject.com/ticket/5888">open ticket about it</a>, but for the time being, here&#8217;s my wrapper that supports those methods as well as GET and POST:</p><p>[source:python]
from cStringIO import StringIO
from django.test.client import Client as DjangoClient, encode_multipart
from django.utils.http import urlencode
import base64
import md5</p><p>class Client(DjangoClient):
&#8220;&#8221;"
Wrapper and drop-in replacement around Django&#8217;s own test
&#8220;Client&#8221; class, providing PUT, DELETE and OPTIONS support, as well as
HTTP Basic + Digest Authentication support.
NOTE: the django.test.client.Client does not directly support
PUT, DELETE or OPTIONS requests so we&#8217;re using the &#8220;request()&#8221;
method directly&#8230; there&#8217;s an open ticket about it:
http://code.djangoproject.com/ticket/5888
&#8220;&#8221;"</p><pre><code>auth = { }
def http_basic_login(self, username, password):
    base64string = base64.encodestring('%s:%s' % (username, password))[:-1]
    self.auth = { "HTTP_AUTHORIZATION": "Basic %s" % base64string }
def http_digest_login(self, method, url, params, response, username, password):
    (authmeth, auth) = response['WWW-Authenticate'].split(" ", 1)
    if authmeth.lower() != 'digest':
        return
    amap = {}
    for itm in auth.split(", "):
        (k, v) = [s.strip() for s in itm.split("=", 1)]
        amap[k] = v.replace('"', '')
    try:
        realm    = amap['realm']
        qop      = amap.get('qop', '')
        nonce    = amap['nonce']
        opaque   = amap['opaque']
    except:
        return
    cnonce = "01b6730aae57c007"
    nc = "00000001"
    query_string = "&amp;".join(["=".join(item) for item in zip(params.keys(), params.values())])
    uri = url + "?" + query_string
    ha1 = md5.md5('%s:%s:%s' % (username, realm, password)).hexdigest()
    ha2 = md5.md5('%s:%s' % (method, uri)).hexdigest()
    if qop:
        chk = "%s:%s:%s:%s:%s:%s" % (ha1, nonce, nc, cnonce, qop, ha2)
    else:
        chk = "%s:%s:%s" % (ha1, nonce, ha2)
    response = md5.md5(chk).hexdigest()
    self.auth = {
        "HTTP_AUTHORIZATION": 'Digest username="%s", realm="%s", nonce="%s", uri="%s", response="%s", opaque="%s", qop=auth, nc=%s, cnonce="%s"' % (username, realm, nonce, uri, response, opaque, nc, cnonce),
    }
def http_logout(self):
    self.auth = {}
def pre_request(self, url, data):
    r = {
        'CONTENT_LENGTH':  None,
        'CONTENT_TYPE':    'text/html; charset=utf-8',
        "HTTP_USER_AGENT": "Django Unit Test HTTP Client",
        'PATH_INFO':       url,
        'QUERY_STRING':    urlencode(data, doseq=True),
    }
    r.update(self.auth)
    return r
def get(self, url, data, **extra):
    r = {}
    r.update(extra)
    r.update(self.auth)
    return DjangoClient.get(self, url, data, **r)
def post(self, url, data, **extra):
    r = {}
    r.update(extra)
    r.update(self.auth)
    return DjangoClient.post(self, url, data, **r)
def delete(self, url, data):
    r = self.pre_request(url, data)
    r["REQUEST_METHOD"] = "DELETE"
    return self.request(**r)
def options(self, url, data):
    r = self.pre_request(url, data)
    r["REQUEST_METHOD"] = "OPTIONS"
    return self.request(**r)
def put(self, url, data, form):
    BOUNDARY = 'BoUnDaRyStRiNg'
    MULTIPART_CONTENT = 'multipart/form-data; boundary=%s' % BOUNDARY
    encoded = encode_multipart(BOUNDARY, form)
    r = self.pre_request(url, data)
    r.update({
        'CONTENT_LENGTH': len(encoded),
        'CONTENT_TYPE':   MULTIPART_CONTENT,
        'REQUEST_METHOD': 'PUT',
        'wsgi.input':     StringIO(encoded),
    })
    return self.request(**r)
def hello(self, url, data, **extra):
    """Sends a fake 'HELLO' request which returns a 405 answer :)"""
    r = self.pre_request(url, data)
    r["REQUEST_METHOD"] = "HELLO"
    r.update(extra)
    return self.request(**r)
</code></pre><p>[/source]</p><p>Hope it helps! Of course you could extend this to support OPTIONS, HEAD or other HTTP methods you could find in the <a
href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html">specification</a>.</p><p><strong>Update, 2008-03-05: </strong>Following Yoan&#8217;s comment below, I&#8217;ve DRYed the code a bit. Neat.</p><p><strong>Update, 2008-03-11: </strong>I&#8217;ve added HTTP Basic Authentication support to the class (and changed the post title accordingly).</p><p><strong>Update, 2008-03-13: </strong>Another modification: now the client supports HTTP Digest Authentication (Yay! To use it, make a first call to your server and then pass the response as a parameter to the http_digest_login method), plus support for the OPTIONS verb, plus another method which sends a method with a &#8220;HELLO&#8221; verb, which of course does not exist&#8230; and which will (normally) return a 405 response!</p> ]]></content:encoded> <wfw:commentRss>http://kosmaczewski.net/django-rest-test-support/feed/</wfw:commentRss> <slash:comments>4</slash:comments> </item> <item><title>Archimedes Reloaded</title><link>http://kosmaczewski.net/archimedes-reloaded/</link> <comments>http://kosmaczewski.net/archimedes-reloaded/#comments</comments> <pubDate>Fri, 29 Feb 2008 09:19:20 +0000</pubDate> <dc:creator>Adrian</dc:creator> <category><![CDATA[Django]]></category> <category><![CDATA[Software]]></category> <category><![CDATA[Opinion]]></category> <guid
isPermaLink="false">http://kosmaczewski.net/2008/02/29/archimedes-reloaded/</guid> <description><![CDATA[Give me an API and I&#8217;ll move the web. OK, I&#8217;m actually building the API myself this time. That&#8217;s why so few posts this month. RESTful, featuring multiple export formats, secure, configurable, usable on top of any existing Django application. &#8230; <a
href="http://kosmaczewski.net/archimedes-reloaded/">Continue reading <span
class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<blockquote>Give me an API and I&#8217;ll move the web.</blockquote><p>OK, I&#8217;m actually building the API myself this time. That&#8217;s why so few posts this month. RESTful, featuring multiple export formats, secure, configurable, usable on top of any existing Django application. It can even generate its own wrappers in a myriad of programming languages.</p> ]]></content:encoded> <wfw:commentRss>http://kosmaczewski.net/archimedes-reloaded/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Install MySQL_python in Leopard</title><link>http://kosmaczewski.net/install-mysql_python-in-leopard/</link> <comments>http://kosmaczewski.net/install-mysql_python-in-leopard/#comments</comments> <pubDate>Tue, 15 Jan 2008 10:42:08 +0000</pubDate> <dc:creator>Adrian</dc:creator> <category><![CDATA[Apple]]></category> <category><![CDATA[Django]]></category> <category><![CDATA[How to?]]></category> <category><![CDATA[Leopard]]></category> <category><![CDATA[MySQL]]></category> <category><![CDATA[Open Source]]></category> <guid
isPermaLink="false">http://kosmaczewski.net/2008/01/15/install-mysql_python-in-leopard/</guid> <description><![CDATA[I wanted to make my Django blog engine work on Leopard using MySQL as a database engine. I had a hard time making it work, partially because of my lack of knowledge of Python, partially because I am using MAMP &#8230; <a
href="http://kosmaczewski.net/install-mysql_python-in-leopard/">Continue reading <span
class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<p>I wanted to make my <a
href="/projects/django-blog-engine/">Django blog engine</a> work on Leopard using MySQL as a database engine. I had a hard time making it work, partially because of my lack of knowledge of Python, partially because I am using <a
href="http://www.mamp.info/">MAMP</a> instead of a &#8220;/usr/local/mysql&#8221;-like MySQL installation, partially because of Leopard itself.</p><p>The problem is, basically, that if you try to do the &#8220;easy_install MySQL_python&#8221; thing, it won&#8217;t work in Leopard (the compilation of the native code fails). Here&#8217;s how I made it work, following the instructions in <a
href="http://forums.mysql.com/read.php?50,175059,179979#msg-179979">this post in the MySQL forums</a>, and doing some tweaking manually. <span
id="more-1055"></span></p><ol><li>Download <a
href="http://downloads.sourceforge.net/mysql-python/MySQL-python-1.2.2.tar.gz?modtime=1172959928&#038;big_mirror=0">MySQL_python source code</a> from SourceForge</li><li>Download <a
href="http://dev.mysql.com/downloads/mysql/5.0.html#macosx">MySQL&#8217;s source code for Mac OS X in tarball form</a> (watch out, there&#8217;s code for Universal binaries, and for platform-specific ones &#8211; PowerPC or Intel!)</li><li>Unpack both files; this will generate two folders, one <strong>mysql-5.0.45-osx10.4-powerpc</strong> (I&#8217;m on a G4 Mac, your folder might end with &#8220;intel&#8221;) and <strong>MySQL-python-1.2.2</strong></li><li>In the MySQL-python-1.2.2 folder, edit the _mysql.c file; comment out lines 37 to 39:
[source:c:firstline(34)]
#else
#include &#8220;my_config.h&#8221;
#endif
//#ifndef uint
//#define uint unsigned int
//#endif
#include &#8220;mysql.h&#8221;
#include &#8220;mysqld_error.h&#8221;
#include &#8220;errmsg.h&#8221;
[/source]</li><li>If you use MAMP, you should add to your PATH the location of the &#8220;include&#8221; and &#8220;lib&#8221; directories (to allow proper compilation and linking of the extension, as you might have imagined); in tcsh you would do something like this: <code>setenv PATH /Users/adrian/Downloads/mysql-5.0.45-osx10.4-powerpc/include:/Users/adrian/Downloads/mysql-5.0.45-osx10.4-powerpc/lib:$PATH</code> (of course, replace &#8220;adrian&#8221; with your own user name ;)</li><li>Now build and install MySQL_python as you would normally do: <code>python setup.py build
sudo python setup.py install</code></li><li>If you have MySQL installed in /usr/local/mysql, then the MySQL_python code should work right now. If it doesn&#8217;t (because you use MAMP), you should copy the contents of the mysql-5.0.45-osx10.4-powerpc/lib folder into &#8220;/usr/local/mysql/lib/mysql/&#8221; (pay attention to the fact that there is &#8220;mysql&#8221; twice in that path name). This is needed because the code is compiled in such a way that it loads the MySQL library dynamically, and the code is configured to do so.</li></ol><p>After this, I could change the configuration of the <a
href="/projects/django-blog-engine/">Django blog</a> code to use the MAMP MySQL database:</p><p>[source:python:firstline(15)]
DATABASE_ENGINE = &#8216;mysql&#8217;
DATABASE_NAME = &#8216;blog&#8217;
DATABASE_USER = &#8216;root&#8217;
DATABASE_PASSWORD = &#8216;root&#8217;
DATABASE_HOST = &#8216;/Applications/MAMP/tmp/mysql/mysql.sock&#8217;
DATABASE_PORT = &#8217;8889&#8242;
[/source]</p><p>Pay attention to the fact that <a
href="http://code.djangoproject.com/ticket/1481">Django is prepared to use the MySQL socket</a> instead of the hostname, when the DATABASE_HOST variable starts with a slash (&#8220;/&#8221;).</p><p>Now, create an empty &#8220;blog&#8221; database using phpMyAdmin, and a simple <code>python manage.py syncdb</code> will create an empty schema for you to use in the application.</p><p>Phew! Hope this helps! :)</p> ]]></content:encoded> <wfw:commentRss>http://kosmaczewski.net/install-mysql_python-in-leopard/feed/</wfw:commentRss> <slash:comments>5</slash:comments> </item> <item><title>My first Django project</title><link>http://kosmaczewski.net/my-first-django-project/</link> <comments>http://kosmaczewski.net/my-first-django-project/#comments</comments> <pubDate>Fri, 11 Jan 2008 16:13:45 +0000</pubDate> <dc:creator>Adrian</dc:creator> <category><![CDATA[Django]]></category> <category><![CDATA[Opinion]]></category> <category><![CDATA[Ruby on Rails]]></category> <category><![CDATA[blogging]]></category> <category><![CDATA[project]]></category> <guid
isPermaLink="false">http://kosmaczewski.net/2008/01/11/my-first-django-project/</guid> <description><![CDATA[So here it is, my first Django project: the gazillionth blog engine on the planet!. As if there weren&#8217;t enough, right? :) Actually it was a practical and easy way to learn the Django project, and the result is pretty &#8230; <a
href="http://kosmaczewski.net/my-first-django-project/">Continue reading <span
class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<p>So here it is, my first Django project: <a
href="/projects/django-blog-engine/">the gazillionth blog engine on the planet!</a>. As if there weren&#8217;t enough, right?  :)   Actually it was a practical and easy way to learn the Django project, and the result is pretty neat. Feel free to <a
href="/wp-content/uploads/2008/01/blog.zip">download</a> it, play with it, and give me your feedback. Here&#8217;s a sample screenshot in Safari:</p><p><img
src='/wp-content/uploads/2008/01/screenshot.png' alt='screenshot.png' /></p><p>Creating this project I have had a practical experience comparing both Django and, of course, Rails. <a
href="/2007/12/05/rails-vs-django/">The subject is not new in this blog</a>; however, this time I could play with both frameworks and as such, I can bring my small amount of confusion in this big <a
href="http://www.laputan.org/mud/">framework tar pit</a>. <span
id="more-1050"></span> Personally, I found Django and Rails <strong>much more different than I previously thought</strong>. Both frameworks tackle similar problems, sometimes in similar ways; however; but this does not make for a real similarity between both. They have different philosophical approaches to the problem. This must surely have to do with the underlying programming language and their philosophies.</p><p>That said, <strong>I found Rails much easier to use.</strong> It took me far, far less time to do a similar small application in Rails than it took me to do it on Django. And I must say that when I first used Rails, I had never seen a line of Ruby in my life. I will list some pros and cons that I found in the process:</p><p>Good things about Django:</p><ul><li>The &#8220;automatic administration system&#8221;; impressive! (but you can get it in Rails with some external plugins like <a
href="http://streamlinedframework.org/">Streamlined</a>&#8230; but in any case it&#8217;s a handy thing to have it already in the system!)</li><li>The form subsystem; great!</li><li>Django does not force you a folder layout; nice!</li><li>The Django Book + documentation: really great resources, nothing to say about that.</li><li>The deployment procedure: really much easier than Rails! We&#8217;re in something really close to PHP here, while Capistrano has much more to do with Java&#8217;s Maven or even makefiles :)</li><li>The native support for RSS feeds! This is a godsend.</li></ul><p>Bad things about Django:</p><ul><li>No native support for &#8220;environments&#8221;, like in Rails; you cannot separate easily the settings for development, testing and production (and you can add more environments if you want!)</li><li>No native support for REST architectures, or at least &#8220;easy&#8221; AJAX + API support (like the one you find in Rails)</li><li>The &#8220;syncdb&#8221; system that ships with Django is, at most, primitive, compared to Rails migrations.</li><li><del
datetime="2008-01-18T14:23:35+00:00">There doesn&#8217;t seem to be a built-in infrastructure for tests.</del> There is a <strong>primitive</strong> built-in test infrastructure. You can add &#8220;doctests&#8221; to your views and models, and this way you&#8217;re &#8220;documenting and testing&#8221; at the same time. Sorry, but I don&#8217;t buy this. Tests are tests, docs are docs. Rails, thanks to rakefiles and unit, functional and integration tests, <del
datetime="2008-01-18T14:24:06+00:00">seems</del> <strong>is</strong> a much more advanced platform in this sense. You can get even stats of your projects with it! You can test them! Extract the docs! Everything! This is something lacking in Django. Really. Not to speak about the lack in Python of something similar to <a
href="http://rspec.info/">RSpec</a>, which just by itself justifies the choice of Ruby and Rails in this field.</li><li>Lack of integrated logging. Can you believe this? I had to find an <a
href="http://www.djangosnippets.org/snippets/16/">external source of inspiration</a> for this.</li><li>The naming of &#8220;Views&#8221; for &#8220;Controllers&#8221; (or said otherwise, the MTV instead of MVC thing) does not make sense to me. Even in WebObjects and Cocoa &#8220;views&#8221; are, well, &#8220;views&#8221;&#8230;</li><li>The commands for creating, starting or using an application from the command line are slightly harder to remember (or maybe in Rails are just far too easy to remember!)</li><li>The template system; I do not want to learn another language for that. Django uses a really limited one for this matter, and I prefer Rails&#8217; (default) one in this case.</li><li>Finally, Python; I just can&#8217;t get used to this language&#8217;s syntax. But I can live with that ;) It&#8217;s a matter of personal taste, simply.</li></ul><p>Of course, these impressions have nothing to do with the power of the platform! Python is a powerful language, and many of the &#8220;newbie&#8221; observations above have to do with my own lack of knowledge about it. But first impressions count!</p><p>In any case, I liked playing with Django &amp; Python; I&#8217;m happy to have learnt something new!</p><p><strong>Update, 2009-02-26:</strong> I&#8217;ve posted the code in <a
href="http://github.com/akosma/django-blog-engine/">Github</a> now, and also I&#8217;ve fixed its compatibility problems, and now it works with Django 1.0.2.</p> ]]></content:encoded> <wfw:commentRss>http://kosmaczewski.net/my-first-django-project/feed/</wfw:commentRss> <slash:comments>20</slash:comments> </item> <item><title>Starting with Django in Leopard</title><link>http://kosmaczewski.net/starting-with-django-in-leopard/</link> <comments>http://kosmaczewski.net/starting-with-django-in-leopard/#comments</comments> <pubDate>Wed, 09 Jan 2008 13:23:17 +0000</pubDate> <dc:creator>Adrian</dc:creator> <category><![CDATA[Books]]></category> <category><![CDATA[Django]]></category> <category><![CDATA[How to?]]></category> <category><![CDATA[Leopard]]></category> <category><![CDATA[starting]]></category> <guid
isPermaLink="false">http://kosmaczewski.net/2008/01/09/starting-with-django-in-leopard/</guid> <description><![CDATA[As I&#8217;ve written earlier, I&#8217;m playing with Django these days. It&#8217;s a refreshing change, I must say, even if I admit that I prefer Ruby&#8217;s syntax to Python&#8217;s. Of course that&#8217;s just a purely subjective impression (I&#8217;m writing this while &#8230; <a
href="http://kosmaczewski.net/starting-with-django-in-leopard/">Continue reading <span
class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<p><a
href="/2008/01/09/django-leopard-the-utf-8-error/">As I&#8217;ve written earlier</a>, I&#8217;m playing with <a
href="http://www.djangoproject.com/">Django</a> these days. It&#8217;s a refreshing change, I must say, even if I admit that I prefer Ruby&#8217;s syntax to Python&#8217;s. Of course that&#8217;s just a purely subjective impression (I&#8217;m writing this while I try to avoid the rotten tomatoes thrown by angry pythonistas reading this) that does nothing to do with the power of the Python language + framework, which is by all means absolutely impressive.</p><p>In any case, I had to install a working Django environment in my machine, and while following the excellent and free <a
href="http://www.djangobook.com/">Django Book</a> instructions, this is what I did in my Leopard installation to have it up and running (Leopard already comes bundled with Subversion 1.4.4, Python 2.5.1 and SQLite 3.4.0, so you don&#8217;t need to do anything about them): <span
id="more-1045"></span></p><ol><li>Checkout the latest version of Django from its Subversion repository:<br
/> <code>cd ~
svn co http://code.djangoproject.com/svn/django/trunk django_trunk</code> This will create a ~/django_trunk folder in your home directory.</li><li>Edit the .tcshrc file (yes, I use tcsh) with the following instructions: <code>setenv PATH /Users/adrian/django_trunk/django/bin:$PATH
setenv PYTHONPATH /Users/adrian/django_trunk</code> This will make the Django files available to your Python installation.</li><li>Open Terminal.app and cd to your ~/Desktop</li><li><a
href="/2008/01/09/django-leopard-the-utf-8-error/">Follow my instructions about how to fix the bug in Leopard&#8217;s Terminal.app</a>, which can be quite disappointing after all.</li><li>Create a new Django project: <code>django-admin.py startproject [PROJECT_NAME]</code></li><li>Start the development server: <code>python manage.py runserver [IP:PORT] [PORT]</code> By default it listens on the port 8000, so you can browse to <a
href="http://localhost:8000/">http://localhost:8000/</a> to see your fresh new system.</li></li><li>Start the command-line interface (similar to Rails&#8217; &#8220;script/console&#8221;): <code>python manage.py shell</code></li><li>If you follow the <a
href="http://www.djangobook.com/">Django Book</a> instructions, you might find that you cannot use models with &#8220;ImageField&#8221; columns; you need to install first the <a
href="http://www.pythonware.com/products/pil">Python Imaging Library</a> doing the following: <code>curl http://effbot.org/media/downloads/Imaging-1.1.6.tar.gz -o Imaging-1.1.6.tar.gz
tar xvfz Imaging-1.1.6.tar.gz
cd Imaging-1.1.6
python setup.py build
sudo python setup.py install
cd ..
rm Imaging-1.1.6.tar.gz
rm -r Imaging-1.1.6</code></li><li>That&#8217;s all!</li></ol><p>I hope this helps. Doing these steps I could have a working installation, and everything shown in the book worked perfectly well. It&#8217;s an impressive framework! I look forward to learn more about it.</p> ]]></content:encoded> <wfw:commentRss>http://kosmaczewski.net/starting-with-django-in-leopard/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Django &amp; Leopard &amp; the UTF-8 error</title><link>http://kosmaczewski.net/django-leopard-the-utf-8-error/</link> <comments>http://kosmaczewski.net/django-leopard-the-utf-8-error/#comments</comments> <pubDate>Wed, 09 Jan 2008 11:47:47 +0000</pubDate> <dc:creator>Adrian</dc:creator> <category><![CDATA[Apple]]></category> <category><![CDATA[Django]]></category> <category><![CDATA[How to?]]></category> <category><![CDATA[Leopard]]></category> <category><![CDATA[utf-8]]></category> <guid
isPermaLink="false">http://kosmaczewski.net/2008/01/09/django-leopard-the-utf-8-error/</guid> <description><![CDATA[If you use Django on Leopard, you might encounter a strange &#8220;Locale UTF-8 not found&#8221; error when running your application. This is due to a bug in Terminal.app, albeit an easy to fix one: just go to the Preferences pane &#8230; <a
href="http://kosmaczewski.net/django-leopard-the-utf-8-error/">Continue reading <span
class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<p>If you use <a
href="http://www.djangoproject.com/">Django</a> on Leopard, you might encounter a strange &#8220;Locale UTF-8 not found&#8221; error when running your application. This is due to a bug in Terminal.app, albeit an easy to fix one: just go to the Preferences pane / &#8220;Settings&#8221; page / &#8220;Advanced&#8221; tab and uncheck the &#8220;Set LANG environment variable on startup&#8221; checkbox. Reopen your Terminal session, load the Django application and voilà! Your bug has disappeared. The screenshot below might help too:</p><p><img
src='/wp-content/uploads/2008/01/solution.png' alt='solution.png' /></p><p>Hope this helps!</p><p>PS: yes, I&#8217;m doing Django and hence Python these days ;)</p> ]]></content:encoded> <wfw:commentRss>http://kosmaczewski.net/django-leopard-the-utf-8-error/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>Rails vs. Django</title><link>http://kosmaczewski.net/rails-vs-django/</link> <comments>http://kosmaczewski.net/rails-vs-django/#comments</comments> <pubDate>Wed, 05 Dec 2007 09:12:10 +0000</pubDate> <dc:creator>Adrian</dc:creator> <category><![CDATA[Code]]></category> <category><![CDATA[Django]]></category> <category><![CDATA[Ruby on Rails]]></category> <category><![CDATA[comparison]]></category> <guid
isPermaLink="false">http://kosmaczewski.net/2007/12/05/rails-vs-django/</guid> <description><![CDATA[While watching the Google TechTalk about Django, I came accross this interesting paper highlighting differences between Rails and Django, which might be interesting to many of us in the field. For those interested, Django is a powerful, MVC framework for &#8230; <a
href="http://kosmaczewski.net/rails-vs-django/">Continue reading <span
class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<p>While watching the <a
href="http://video.google.com/videoplay?docid=-70449010942275062">Google TechTalk about Django</a>, I came accross <a
href="http://www.vaporbase.com/rails-django">this interesting paper</a> highlighting differences between Rails and Django, which might be interesting to many of us in the field.</p><p>For those interested, <a
href="http://www.djangoproject.com/">Django</a> is a powerful, MVC framework for creating web applications using the <a
href="http://www.python.org/">Python programming language</a>. It is very similar in some respects to <a
href="http://www.rubyonrails.org/">Rails</a>, but some consider Django to be better suited for high-traffic websites. I haven&#8217;t used Django extensively (nor Python as a matter of fact, besides the typical &#8220;Hello World&#8221; thingy) so I won&#8217;t say anything else but the fact that I prefer <a
href="http://www.ruby-lang.org/">Ruby</a>&#8216;s syntax to Python&#8217;s.</p><p>Of course this is a subjective opinion! In any case, if it&#8217;s anything like Rails, it&#8217;s worth considering. Definitely. Dynamic languages are the way to go.</p><p><strong>Update, 2008-01-11:</strong> You might want to check out <a
href="/2008/01/11/my-first-django-project/">this other article of mine!</a></p> ]]></content:encoded> <wfw:commentRss>http://kosmaczewski.net/rails-vs-django/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> </channel> </rss>
