About Blog Gallery Twit

All articles, tagged with “django”

Model Inheritance

I’m working on several projects in my spare time, all of them probably will never see the light of day. That’s OK, I mainly work on these to help me keep up with what’s going on outside of the box I live in. Anyways, I was interested in creating some defaults to all of my models on a project where there has to be a strong level of auditing. On the models themselves I want to be able to store some basic information of create user, create date, update user and update date. Seems pretty normal, but there’s a ton of models in one of my applications, so I didn’t want to type these things over and over and over again.

class ModelDefaults(models.Model):
    created_by = models.ForeignKey(User, related_name="%(class)s_created_by")
    created_at = models.DateTimeField()
    updated_by = models.ForeignKey(User, related_name="%(class)s_updated_by")
    updated_at = models.DateTimeField()

    def save(self):
        if self.id == None:
            self.created_at = datetime.datetime.now()
            self.created_by = user_id
            self.updated_at = datetime.datetime.now()
            self.updated_by = user_id
        else:
            self.updated_at = datetime.datetime.now()
            self.updated_by = user_id

        super(ModelDefaults, self).save()

    class Meta:
        abstract = True

This is my ModelDefaults, as you can see. All of the models that use this are written as such:

class MyNewModel(ModelDefaults):
    name = ...

I haven’t really tested it, but I’m assuming that it will work. I’m thinking of what else I want to store in here, but I’ve already hacked an object audit log similar to the Admin pages auditing. That too has to be tested, as this app should really only be used from outside the Admin pages, including the admins of the application. Interesting stuff, maybe if I ever get a full app complete I’ll light it up on here for fun.

Next I need to add Lightbox

My next project will be to clean up my CSS a bit for photologue and then use Lightbox to view the galleries. I think I’ll stick with the cached smaller images and provide links to the source, but we’ll see.

Photologue Installed

Wow, I was quite surprised how easy it was to get photologue installed on byteflow. I know a lot of people have been making comments in the groups about getting byteflow to be a application instead of a project, but it’s quite easy to get things installed on it. Photologue only took about 3 minutes. Here’s the steps I did to install it.

I keep a downloads directory in my home path for bringing in projects and files, but you can dump your checked out dir anywhere.

cd ~/downloads
svn checkout http://django-photologue.googlecode.com/svn/trunk/ django-photologue
cd ~/django_projects/byteflow/apps
mv ~/downloads/django-photologue/photologue .
cd ../templates
mv ../apps/photologue/templates/photologue .

That’s the hard part, now the django side. Follow normal procedures of commenting out all the INSTALLED_APPS and add photologue. I find it easier to do in settings.py directly.

INSTALLED_APPS = (
    #'django.contrib.auth',
    #'django.contrib.contenttypes',
    #'django.contrib.sessions',
    #'django.contrib.sites',
    #'django.contrib.admin',
    #'django.contrib.sitemaps',
    #'django.contrib.flatpages',
    #'django.contrib.markup',
    #'lib',
    #'pytils',
    #'accounts',
    #'blog',
    #'discussion',
    #'openidconsumer',
    #'pingback',
    #'tagging',
    #'typogrify',
    #'render',
    #'robots',
    #'textblocks',
    #'livejournal',
    #'blogroll',
    #'openidserver',
    #'recaptcha',
    'photologue',
)

Run ./manage.py syncdb. Uncomment all the apps that you just commented out in settings.py. Now decide what you want to do with the urls, I just decided to keep photologue in my path.

url(r'^photologue/', include('photologue.urls')),

Then I edited the settings_local.py and added to the STATIC_PAGES this:

('Gallery', '/photologue/gallery/', 'Photos'),

That simple, and everything looks like it worked. Created a test gallery, but going to move over a bunch of photos to see how well it works.

Byteflow on Dreamhost

I have been hacking at my Dreamhost account for some time now. I’ve installed my own copy of Python so I can have 2.5 compatibility on my account.

[homer]$ python
Python 2.5.1 (r251:54863, Jan 30 2008, 04:48:32)
[GCC 3.3.5 (Debian 1:3.3.5-13)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
[homer]$

My dispatch.fcgi file looks like this:

#!/home/wstearns/opt/bin/python
import sys
sys.path += ['/home/wstearns/django_src']
sys.path += ['/home/wstearns/django_projects/byteflow']
from fcgi import WSGIServer
from django.core.handlers.wsgi import WSGIHandler
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
WSGIServer(WSGIHandler()).run()

I was a bad boy and edited a file I’m not supposed to in Byteflow, settings.py. I suppose I could have overrode the settings in my local, but I’m still learning.

STATIC_ROOT = '/home/wstearns/media.wstearns.com/static'
STATIC_URL = 'http://media.wstearns.com/static/'
ADMIN_MEDIA_PREFIX = 'http://media.wstearns.com/admin-media/'

Pretty much everything else I followed the directions linked on the wiki.

UPDATE — The overridden section can be placed anywhere in the settings_local.py file. Oh, and my CSS skills suck. As always you can consult the wiki entry at Dreamhost for Django.

Welcome to my new Domain

I finally decided to get a domain close to my name, so here it is. I also decided to not bother with writing my own blogging software, since I’m knee deep in other projects at the moment. I did choose a Django based project called Byteflow. It was rather easy to get up and running. I did change the theme from the default. I’m not a web designer, so I just chose some colors that I’ve seen that I like.