thefekete.net

… better than hitting a bird with a golf ball.

Aptitude key error fix for VirtualBox

Fix found at the Ubuntu Forums:

#!/bin/bash

wget -q http://download.virtualbox.org/virtualbox/debian/oracle_vbox.asc -O- | sudo apt-key add -
sudo apt-get clean
sudo cd /var/lib/apt
sudo mv lists lists.old
sudo mkdir -p lists/partial
sudo apt-get clean
sudo apt-get update

Project Pointers

Got this from a comment on slashdot by raddan. Seems like good advice…

  • Never pass unvalidated input to your database
  • Never pass unvalidated input to the system
  • Always validate on the server-side; client-side validation should only function as a convenience to the user
  • Validate data coming from other servers (if you’re doing any web services stuff).
  • Encrypt connections to the server
  • Enforce inactivity timeouts
  • Do not allow multiple logins to the same account (unless you want your game to application to work that way)
  • Always authenticate users; consider using two-factor authentication (CAPTCHA + password, etc)
  • Allow administrators to revoke accounts
  • Make it easy for administrators/force administrators to sandbox/chroot your application
  • If your applications needs to use server storage, consider DoS attacks (a user uploading lots of stuff)
  • Make sure all privileged actions hit the same authentication class/function; if you change your authentication code, this ensures that the changes are applied across the board <– I catch newbie programmers making this mistake all the time!

If you do all of the above, your app might still not be “secure”, but breaking it will be a PITA.

Django: Extending Model Queryset / Manager Methods

Django models automatically come with some very useful methods under the objects Manager. Methods like Model.objects.all(), Model.objects.filter() and Model.objects.get() work quite well.

But what if you are constantly using the same QuerySet over and over? You can either keep typing MyModel.objects.filter(way-to-many-kwargs).order_by(something).etc... over and over -or- extend the objects Manager with your own crazy method.

Let’s say you’ve got the following Model:

class MyModel(models.Model):
    ...
    ...
    ...

All you need to do is subclass Manager, add your method and override your model’s old Manager:

class MyModelManager(models.Manager):
    def crazy_query(self):
        ...
        # A bunch of queries, joins, logic, etc...
        ...

class MyModel(models.Model):
    ...
    ...
    ...

    objects = MyModelManager()

Enabling AdminDocs in Django

In order to make a handy Documentation link available in the upper right portion of the Django admin site, simply add the admindocs app to your settings.py and the admindocs urls to your urls.py

# In your settings.py:
INSTALLED_APPS = (
    ...
    'django.contrib.admindocs',
    ...
)

# In your urls.py:
urlpatterns = patterns('',
    ...
    (r'^admin/doc/', include('django.contrib.admindocs.urls')),
    (r'^admin/', include(admin.site.urls)),
    ...
)

NOTE: The admindocs urlconf must precede the admin site urlconf to work properly.

RSync include only certain file types, or exclude all but…

I was recently working on something for my company and needed to transfer all of our product pictures to my home computer. Unfortunately, all the actual product PNGs are intermingled with theirĀ corresponding andĀ huge XCFs and other working files. Thus, I needed to exclude all files except the PNGs. Or, to include only the PNGs.

Here’s how to do it:

#!/bin/bash

rsync --include='*/' --include='*.png' --exclude='*' \
example.com:/path/to/source ./

I pulled this from here.

EncFS Encrypted Folder Video Tutorial / HowTo

This is from datastorageunit.com’s support section. They provide data storage services over ssh/rsync. There’s no audio, but the author comments in the terminal and has an overview text to the left.

I know its a windows computer in the screen, but its all done on a linux server through ssh.

Enjoy…

Sorting Hierarchical Categories in Django

I had almost given up on my dream of hierarchical (multi-level) categories sorted by their self-referential parent to form a tree. Almost, but I finally figured it out.

I’m not sure if it’s a hack or the “one obvious way”, as Tim Peters puts it (probably not), but it works and I’m using it.

The whole problem starts with a simple model:

class Category(models.Model):
    name = models.CharField(max_length=100)
    parent = models.ForeignKey('self', blank=True,
        null=True, related_name='child')

    def __unicode__(self):
        if self.parent:
            prefix = str(self.parent)
        else:
            return self.name
        return ' > '.join((prefix,self.name))

It works great for categories with sub categories. And when you call the __unicode__() method, it even spits out the whole ancestry to the category (eg. u'GrandParent > Parent > Category').

But alas, as soon as you try to sort them by their parents, you find out that the full name is not a real field and you can’t order by fake fields. Of course you might get the bright idea to add the order_with_respect_to = 'parent' Meta class attribute, but that will only lead you here.

Read the rest of this entry »

How to use *args and **kwargs in Python

The SaltyCrane Blog has a great post on the use of *args and **kwargs in python functions. Very simple and a good reference. You can check it out here:

http://www.saltycrane.com/blog/2008/01/how-to-use-args-and-kwargs-in-python/

Slug urlconf regex pattern for Django urlpatterns

If you want to match a slug in your urls.py, use (?P[-\w]+) as a named match pattern:

So, if you wanted to match http://example.com/some-slug-name/, you would use

urlpatterns = patterns('',
    # ...
    url(r'^(?P<slug>[-\w]+)/$', some_view),
    # ...
)

I got this info from a Django Users Thread on google groups.

Remove a remote branch in git

To remove a branch on a remote repository in git, use:

git push <remote-repo> :<branch-to-delete>