Using Someone Else’s SVN Repository with Git

Posted by Trey on March 29, 2008

If you have a repository URL that looks like this:

http://code.yourmom.com/project/trunk/

Issue this command (note that you leave off trunk/):

git svn clone -s http://code.yourmom.com/project/ project

After it’s done, see how big it is:

du -hs project

And you’ll see something like this:

20M project/

If it’s particularly big, go into the folder and garbage collect:

cd project
git gc

From within the project folder, set your local repository to the trunk (it’s set to whatever branch had the last commit otherwise):

git reset --hard trunk

Create your own branch and get to work:

git co -b treys_changes

When you want to pull in the changes from the original author to stay up to date:

git svn rebase

If you’ve cloned this repo (after posting it to GitHub or elsewhere) and want to use it on another computer, you’ll have to use do more step in order to track the original SVN repo again:

git update-ref refs/remotes/trunk origin/master
git svn init -T trunk http://code.yourmom.com/project/

Sources

Use Django’s Permalink Decorator with Generic Views

Posted by Trey on March 10, 2008

The permalink decorator is the way to keep your URLs DRY. The only place you need to define where something lives is in your urls.py. In your templates, just point to {{ object.get_absolute.url }}. The problem with how they tell you to do it in the book, the documentation, and elsewhere is that it doesn’t work right if you’re using the same view function more than once in all your urls.py files (which is bound to happen if you’re using a bunch of generic views). That’s because Django has no way of telling which one you mean.

Named URL patterns to the rescue.

If you have a URLpattern that looks like this:

(r'^(?P<slug>[-\w]+)/$’, list_detail.object_detail, log_detail),

Change it to this:

url(r'^(?P<slug>[-\w]+)/$’, list_detail.object_detail, log_detail, name=’log-detail’),

Note the addition of “url” to the start of the line and the “name=” bit at the end.

Now, in your model file(s), do the following:

Add this line to the top of the file:

from django.db.models import permalink

At the bottom of the model class do something similar to this:

@permalink
def get_absolute_url(self):
    return ('log-detail', (), { 'slug': self.slug })

Any other question you have about this stuff is probably in the other sources.

Sources:

Python Datatypes

Posted by Trey on March 05, 2008

dictionary = {'a':'apple', 'b':'banana', 'c':'cat'}
    # Associative array / hash / array with non-numeric indices.
list = ['a', 'b', 'c']
    # Normal array with 0-based indices.
tuple = (’a', ‘b’, ‘c’)
    # Read-only array.

More…

Django Template System Basics

Posted by Trey on March 05, 2008

The long way

  1. Load a template
  2. Fill a Context
  3. Return a HttpResponse object

Like so:

from django.template import Template, Context
from django.http import HttpResponse
...
t = get_template('current_datetime.html')
html = t.render(Context({'current_date': now}))
return HttpResponse(html)

The short way: render_to_response

Like so:

from django.shortcuts import render_to_response
...
return render_to_response('current_datetime.html', {'current_date': now})

Source: