October 2011

archive

ignoring files generated by eclipse for git Oct
18
1
1

If you're developing in Eclipse, like I am for Android, and you use version control you're going to want to ignore the default project files that are generated by Eclipse. You should update your .gitignore with the following list:

.project
.classpath
.settings/
bin/

comments

create/generate qr code with qrencode in ubuntu Oct
14
1
1

You can create qr codes easily in Ubuntu using qrencode. You can install it like so:

sudo apt-get install qrencode

and then create the qr code with the following command:

qrencode -o /path/to/qrcode.png -s 10 'http://example.com'

the `-s` option flag is the size of dot (pixel). The default is 3.

comments

how to rename a database in postgresql Oct
12
1
1

http://www.postgresonline.com/journal/archives/40-How-do-you-rename-a-database.html

First kick the users out of the database, you can find the users with the following command.

SELECT * 
    FROM pg_stat_activity 
    WHERE datname = 'myolddbname_goes_here'

now run the following:

ALTER DATABASE myolddbname_here RENAME TO mynewdbname_here

comments

manually writing to model filefield in django views Oct
11
1
1

Django models.FileField is very helpful in simplifying file management, but sometimes you need to write/overwrite into a file field object in a view without having the admin helper functions available. It's not inherently clear how to do this manually. Never fear though it's still an easy process.

It's worth noting that Django uses a custom File class that acts as a wrapper around the python file object to add some enhanced functionality. You simply need to initialize a Django File with a python file to get a valid model.

Say you have something similar to the following model:

class FileExample(models.Model):
    test = models.FileField(upload_to='uploads')

You can then write something like this in your view:

from django.core.files import File
from test.models import FileExample

file_example = FileExample()

filename = '/path/to/file.txt'
f = open(filename, 'r')
djangofile = File(f)
file_example.test.save('filename.txt', djangofile)
f.close()

Furthermore you can combine some statements to simplify and shorten the process:

file_example = FileExample()
file_example.test.save('filename.txt', File(open('/path/to/file.txt', 'r')))

comments

export a table into a csv file in postgresql Oct
05
1
1

I always have to look this up, so I'm placing this here. This exports a table into a csv file in PostgreSQL.

COPY (SELECT * FROM table) TO `/path/to/file.csv` WITH CSV HEADER;

comments