Archive for November, 2011

28
Nov
11

django create superuser automatically on syncdb


Found the following information here. Here’s how you can automatically create a superuser when syncing your db:


python manage.py dumpdata auth > initial_data.json 
python manage.py syncdb

Make sure to modify the initial.json file, if you want to change the name/mail of the superuser.

24
Nov
11

data URI scheme


This ( Data URI scheme ) is very helpful for serving images without storing them on server side.

21
Nov
11

always specify fields for django’s ModelForm as a tuple


So, I had a model that had a FileField called path, for which I wanted a ModelForm. Do not do this:


class MyModelForm(ModelForm):
   class Meta:
      model = MyModel
      fields= ("path")

It led to a strange error, saying django.core.exceptions.FieldError: Unknown field(s) (a, p, t, h). Of course, the fix was to specify the fields like:


class MyModelForm(ModelForm):
   class Meta:
      model = MyModel
      fields= ("path",)

21
Nov
11

django FileField and random names


To ensure that an uploaded file will have a random name, use a custom storage object, like described here.

18
Nov
11

training 17.11.2011


Bench Press:5x5x60kg
Low Row:5x5x60kg
Knee extension:5x5x30kg
Adductor:5x10x25kg

18
Nov
11

watir and no such file to load — ffi_c


After installing Watir, I received this error:

no such file to load — ffi_c

The fix was to install version 1.0.9 of the ffi gem:


gem uninstall ffi
gem install ffi -v 1.0.9

After that, it worked ok.

08
Nov
11

gmail via ruby


require "net/smtp"

msg = "Subject:test\n\nJust a test"
smtp = Net::SMTP.new("smtp.gmail.com",587)
smtp.enable_starttls
smtp.start("smtp.gmail.com","your_username","your_password",:login) do
  smtp.send_message(msg,"from_address","to_address")
end

01
Nov
11

python directory synchronization


I needed to synchronize two folders, and I came up with this:



def sync_dirs(source,dest):
	if not os.path.exists(dest):
		os.makedirs(dest)
		
	def _file_retriever(where):
		dir_ac = []
		file_ac = []
		
		for root,dirs,files in os.walk(where):
			for dir in dirs:
				abs_dir = os.path.join(root,dir)
				dir_ac.append((abs_dir,os.path.relpath(abs_dir,where)))
			for file in files:
				abs_f = os.path.join(root,file)
				file_ac.append((abs_f,os.path.relpath(abs_f,where)))
		return (dir_ac,file_ac)
	
	left_dirs,left_files  = _file_retriever(source)
	right_dirs,right_files = _file_retriever(dest)
	
	for left_dir in left_dirs:
		rel_path = left_dir[1]
		equivalent_right_dir = filter(lambda e:e[1] == rel_path,right_dirs)
		# it's not in the right side, copy it
		if len(equivalent_right_dir) == 0:
			os.makedirs(os.path.join(dest,rel_path))
	# check if only in the right side
	for right_dir in right_dirs:
		rel_path = right_dir[1]
		equivalent_left_dir = filter(lambda e:e[1] == rel_path,left_dirs)
		if(len(equivalent_left_dir) == 0) and (os.path.exists(right_dir[0])):
			shutil.rmtree(right_dir[0])
	
	for left_file in left_files:
		rel_path = left_file[1]
		equivalent_right_file = filter(lambda e:e[1] == rel_path,right_files)
		# file doesn't exist in right side
		if len(equivalent_right_file) == 0:
			shutil.copyfile(left_file[0],os.path.join(dest,rel_path))
		else:
			left_content = open(left_file[0],"rb").read()
			right_content= open(equivalent_right_file[0][0],"rb").read()
			if left_content != right_content:
				shutil.copyfile(left_file[0],os.path.join(dest,rel_path))
	
	for right_file in right_files:
		rel_path = right_file[1]
		equivalent_left_file = filter(lambda e:e[1] == rel_path,left_files)
		if (len(equivalent_left_file) == 0) and (os.path.exists(right_file[0])):
			os.unlink(right_file[0])
	





Blog Stats

  • 265,754 hits