Django — Registering Multiple Admin Sites

To register multiple admin sites with Django, import AdminSite from django.contrib.admin.sites

Instantiate the class and you have your new admin site that you can import anywhere and register the urls for.

Note that the register function is directly in the AdminSite, it is not AdminSite().site.register but AdminSite().register

#!admin.py
from django.contrib.admin.sites import AdminSite

my_new_admin = AdminSite(name='My New Admin')

my_new_admin.index_template = "myadmin/index.html"

#!urls.py
from myproject.admin import my_new_admin

(r'^my_new_admin/(*.)', my_new_admin.root)

#!model admin.py
from my_admin import my_new_admin
my_new_admin.register(MyClass)

Linux — mmmm back in a pure programming environment (minus OS independent monkey work)

Run linux to administer a linux server. I’m running the same OS on my desktop as my server partition — doesn’t it make sense to do so?

To me it’s more about the focus I get from switching over. Minimizing distractions is the goal of programming. Working on Ubuntu puts me in the same place as my server, not cluttered with a completely different OS, remotely working with a combination of putty terminals and or tortoise SVN on windows.

Nah. This is much better.

It helps that there are all of daft punk’s albums on this side of the border. What better to drown out all noise?

Alright, here we go.

Python CSV — Reading and Writing Simultaneously

Pythons CSV module turns your csv into an iterator for efficiency and your data is gone after you do myreader.next() 

I just wanted a quick way to add and edit entries in our database of wines via a spreadsheet. I suppose I could have used pyExcelerator, but I magically decided to go with CSV. 

Trying to write on the same file as you are reading from, even if you do open(‘blah’, ‘rw+’) won’t exactly work. 

My solution was just to write a function that takes a path as it’s argument, and have the function read from the CSV from that path & create a reader/writer instance, with the writer file being temporary. It’s important to keep a reference to it, like:

fread=open(path, 'r')
fwrite=open(path.replace('.csv','_temp'), 'w')

Then I read as I wrote into this temporary file called whateverfile_temp. Specifically, I needed the csv to be updated with primary key values from the database if an object was created so that future uploads of the same file would not result in duplicates but UPDATE.

I kept having trouble because I couldn’t close the file when I started using it as the official docs do: reader/writer = csv.reader/writer(open('blah','rw')) It just resulted in a mangled csv.

But now I could close the files, then replace the original csv that was just read with the new one written.


fread.close()
fwrite.close()
os.rename(mypath.replace('.csv', '_temp'), mypath)

Sure, inefficient, but for me it will come in very useful.

Python — File Write Modes

As a newbie, I often couldn’t find information on file write modes, like rb, U, etc. 

Python has great documentation…. open a python interpreter, type help(file) and you will see:

class file(object)

file(name[, mode[, buffering]]) -> file object

Open a file.  The mode can be ‘r’, ‘w’ or ‘a’ for reading (default),

writing or appending.  The file will be created if it doesn’t exist

when opened for writing or appending; it will be truncated when

opened for writing.  Add a ‘b’ to the mode for binary files.

Add a ‘+’ to the mode to allow simultaneous reading and writing.

If the buffering argument is given, 0 means unbuffered, 1 means line

buffered, and larger numbers specify the buffer size.

Add a ‘U’ to mode to open the file for input with universal newline

support.  Any line ending in the input file will be seen as a ‘\n’

in Python.  Also, a file so opened gains the attribute ‘newlines’;

the value for this attribute is one of None (no newline read yet),

  ‘\r’, ‘\n’, ‘\r\n’ or a tuple containing all the newline types seen.

‘U’ cannot be combined with ‘w’ or ‘+’ mode.

 

Mmmm. Perfect. One of the reason why I like Django so much is the documentation. Freaking amazing! So many python questions have been self answered by just typing help( blah ). 

 

Django — Setting DJANGO_SETTINGS_MODULE / Environment Variables from Python

You know, I learned this and completely forgot after months of not dealing with it. Google searches are so difficult to reach the right subject.

I’m certain this will be something I refer to in the future..

Setting environment variables via Python is as simple as:

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'

Of course, this doesn’t permanently set the variable, but if you have a script that needs that envvar, perfect.

Enjoy.

Photoshop CS3 — AdobeAssetServicesCS3.exe Slowdown

Okay, I’ve been having intermittent photoshop SUPER slowdowns and finally tracked it down (i think). We’re talking, “I can’t even use the internet” slowdowns, with mouse skippage and all of that stuff. 

 

The bottom line might be that you are using Adobe’s file viewer in thumbnail view. I think the thumbnailer is looking through / reading the contents of EVERY file you flip across to try to generate a thumbnail, which would explain the seemingly random processes I found where AdobeAssetServicesCS3 was reading into stuff like huge CD ISOs on my desktop.

Googling this term gives me absolutely nothing. I expect this blog post to get to the top of the google results for it (AdobeAssetServicesCS3) because all I found were auto generated pages.

Anyways, I tracked down an old post from some CS3 beta people who asked about this same exe. I can’t read the thread they linked, but they posted an excerpt:

“It sounds like you are using the Adobe Dialog for Open or Save correct and most likely have the view set to show Thumbnails. That process provides the ability to generate thumbnails for various file types in those dialogs. We’re going to take a look into what might be going on here. Thanks for the post.
– Adobe Photoshop QE “

So there you have it. Go back to OS filesystem view.

My System specs are:

Vista Ultimate 64 bit
E6600 @ 3.0ghz
4gb DDR2-1066 RAM
Seagate 7200.9 RAID 0 

I’m not sure what systems are affected, but it could be a 64bit issue if nobody is crying about it on the internet aside from myself. 

Hope it helped somebody!