Firefox Untrusted SSL Issuer

Firefox error: sec_error_untrusted_issuer

If you are running into this problem with GoDaddy SSL check if you combined your .crt and gd_bundle.crt with the “cat” command if you’re running a linux server.

We renewed our SSL and our site had been unknowingly displaying this error for firefox users for a month, but not to Chrome or Internet Explorer users.

Check every browser folks. Who knows how many people we scared off.

Python / Django — Combine Querysets and Sort. Sort List of Any Objects.

Assuming it quacks like a duck, you can use operator.attrgetter()

Combine queryset by turning it into a list:
queryset_list = list(queryset1).extend(list(queryset2))

Sort the list by:
queryset_list.sort(key=operator.attrgetter(‘attribute to sort by’))

Update: 4/28/2010

Yeah, I forgot the sort function sorts in place. sort = list.sort would not be good.
Also, for those wondering, you can just import operator. “import operator”

Django — Admin Inline Styling / Widget Attrs / Overriding Widgets

Quickly style all of your inlines with formfield_for_dbfield

class PersonInline(admin.TabularInline):
	model = Person
	extra = 6
	def formfield_for_dbfield(self, db_field, **kwargs):
		attrs = { 'size': 15 }
		if db_field.attname == 'interest_level':
			attrs = { 'size': 2 }
		kwargs['widget'] = forms.TextInput(attrs=attrs)
		return super(PersonInline, self).formfield_for_dbfield(db_field,**kwargs)

I see this used for things like custom widgets, ReadOnlyWidget, ColoredWidget, etc, but I wanted a quick and dirty(?) way to fit more fields on my inlines, since the admin just overflows.

Django — Limit Queryset For ModelAdmin List View

This is documented:

To modify a query based on user, override the ‘queryset’ for ModelAdmin like so. Should give enough ideas:

	def queryset(self, request):
		qs = super(PanelTasting, self).queryset(request)
		if request.user.is_superuser:
			return qs
		return qs.filter(host__user=request.user)

Django — Django Admin Model Name Override / Verbose Name

Since so much admin functionality has moved to AdminSite, I couldn’t find the answer to this very easily on google searches.

Despite the move towards towards ModelAdmin for admin-specific stuff, verbose_name’s are still defined in your Model.

Add the following to your models Meta class.

class Meta:
    verbose_name = "foo"
    verbose_name_plural = "foobars" 

to avoid names like Entrys.

Django / Python — Dynamically Create Queries from a String and the OR operator

Dynamically create queries from strings in django.

My problem: I wanted to make a searchable, sorted list of an arbitrary amount of fields to check against.

Normally, we’d use a  Q object to get to the | “or” operator. Our search might look like the following:

Person.objects.filter( Q(first_name__icontains='Yuji') | Q(last_name__icontains='Tomita')

What if I don’t want to hard code the field names? I want to use dynamic field names so I can use a basic factory on multiple models very easily.

With much needed prodding in the right direction by mattmcc, I figured it out.

The key was how to use keyword arguments as strings.

Person.objects.filter(first_name__icontains='Yuji')
is equal to:
Person.objects.filter( **{'first_name__icontains':'Yuji'})

With this knowledge, and operator.or_, I could do a Q and | lookup via strings.

query_string = "Some Name" # the search string
argument_list = []
fields = ('search_field1','search_field2','search_field3')
for query in query_string.split(' '): # split search words
for field in fields:
argument_list.append( Q(**{field+'__icontains': query} )
query = MyModel.objects.filter(  reduce(operator.or_, argument_list)  ) # join the arguments in the list with the or operator

Python on Windows — Setting PYTHONPATH Environment Variable

What is the PYTHONPATH environment variable?

The PYTHONPATH environment variable is the list of folders that the import statement will look through when attempting to resolve a module name.

A per session Python Path is visible in sys.path (per python shell session)

import sys
print sys.path # returns list of directories

sys.path.append('C:\Foobar') 
# now, we can import pyton modules in C:\Foobar

The above solution requires appending to the python path every session. If we don’t want that, we can edit the PYTHONPATH environment variable.

How to set the PYTHONPATH environment variable on Windows XP, Vista, Windows 7, etc.

For Windows the PYTHONPATH is set in the windows registry.

Run regedit

First, open up “Run” from the start menu (Ctrl + R).

Browse to the PythonPath folder

Browse to HKEY_LOCAL_MACHINE -> SOFTWARE -> Python -> PythonCore -> [ Version Number ] -> PythonPath.

Don’t alter the pythonpath you see here.

Create new key

Right click the PythonPath folder and select New –> Key

Name it what you like.

Select the new folder and right click the one blank value sitting on the right side of the screen and select Modify.

For the value field, add your new paths separated by ” ; ”

Run python and try an import.

If that doesn’t work

Updated 1/2014

There are people in comments below with a ton of different solutions. I know that windows is at 8 now, and I haven’t tested with 8.

An alternative method is described here that doesn’t use REGEX. My gut tells me it may be more reliable method. http://stackoverflow.com/questions/3701646/how-to-add-to-the-pythonpath-in-windows-7

Done!

Django/Python on Windows — Setting PATH Environment Variable

What is the PATH?

The PATH environment variable is in general a list of directories that the operating system will search through when programs attempt to execute a command.

For example, say we go into the windows command prompt or Ubuntu’s bash shell and type in “foobar” – the operating system will attempt to locate an executable (program) named “foobar” in the folders listed in the PATH. Replace “foobar” with “python” or “django-admin.py” and you start to understand why this is useful.

How do we set the PATH?

To set the PATH on Vista or Windows 7:

Push the windows button and type “environment variable” on the search bar and select “Edit environment variables for your account”.

Non Vista/Windows 7 users just right click on My Computer, go to Properties, click Advanced Properties, then the Environment Variables button to get to the same place.

In the Environment Variables screen, look for the “Path” variable under the “System Variables” section on the bottom. If it doesn’t exist, just make a new one.

Edit or enter in folder paths separated by ; — Append whatever you like to the string ending with a “;”

Do not delete the default path – append to it.

Path2;Path2;Path3;

Done!