0

I have a question about Django's namespace. Firstly, I implemented my own authentication system so I can have features like inline site editing for authenticated users. Bluntly put, I just didn't like the default admin setup.

Consequently, I have consciously not included any of the authentication middleware or admin features.

Anyway, I have a model called User in vxd.auth.models which I use in vxd.auth.control to read the database for my system. When I run my authentication check I'm getting this error:

DatabaseError at /
column auth_user.first_name does not exist
LINE 1: SELECT "auth_user"."id", "auth_user"."username", "auth_user"...

Well of course it doesn't, my User model doesn't implement a first name field. Django's, however, does.

This wasn't a problem until I upgraded Django and got hit by the CSRF middleware issue...

Relevant parts of settings.py:

TEMPLATE_LOADERS = ( 'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
)
MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
)

AUTHENTICATION_BACKENDS = ('')

TEMPLATE_CONTEXT_PROCESSORS = ( "vxd.auth.contexts.Authentication", )

INSTALLED_APPS = (
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'reversetag',
    'markitup',
    'vxd.auth',
    'Testbed.authadmin',
    'Testbed.testapp',
)

If it's relevant, running Django 1.2.3 on Fedora.

As I said, this worked fine previously. I am under the strong suspicion that the django User model is being included somewhere by default.

What's going on, anybody know? One solution is to rename vxd.auth which I suspect would fix the issue.

Updates for various people's debug suggestions (thanks by the way!): * Yep, I've tried dropping the tables. I can even manipulate my object via the shell like so:

$ python manage.py shell
>>> from vxd.auth.models import *
>>> u = User.objects.get(username="admin")
>>> print u
User object
>>> u.blue
u'baa26f39c47dd222a04aa8123b141e62ef5e0cffa658207b0754f811e6444ab9'
>>>

So something is clearly sneaking in somewhere.

Edit: authentication context processor:

def Authentication(request):
    if AuthenticationCheck(sess=request.session, timeofaction=datetime.datetime.now(), ipaddress=request.META['REMOTE_ADDR']) == True:
        return dict({'username': request.session["username"]})
    else:
        return dict()

Authentication Check code:

def AuthenticationCheck(sess, timeofaction, ipaddress): 

    try:
        user = sess.get("username", None)
        if user is None:
            return False
        else:
            pass
    except MultiValueDictKeyError:
        # not a session object
        # or no such key exists.
        return False

    # some computations based on sess variables which are set when login happens 
    # these are non-conflicting keys unless sess["vxd-blue"] is used elsewhere...

    # Find "User" from DB.
    print "User is " + user

    #try: # don't catch so I can see the error
    usr = User.objects.get(username=user)    # this throws the exception shown.

Imports (contexts.py):

from datetime import date, datetime, timedelta
from vennarddjango.auth.control import *

imports (control.py):

from django.db import models
from django.contrib.sessions.models import *
from django.utils.datastructures import MultiValueDictKeyError

import hashlib
import datetime

from vxd.auth.models import *
from vxd.auth.openid.store import *
from vxd.auth.openid.methods import *
from vxd.utility.datetimefuncs import *
from vxd.utility.hashwrapper import *
Community
  • 1
  • 1

3 Answers3

2

You could edit the django source, and drop into a pdb session when django.contrib.auth.models is imported, and follow the traceback up until you find the culprit. Pop the following at the top of that file.

import pdb; pdb.set_trace()

Then, at the prompt that appears, type tb. (Or bt, I can never remember which).

Matthew Schinckel
  • 35,041
  • 6
  • 86
  • 121
  • I might give that a go, thanks v much. I've refactored now anyway, but I will at some point revert and give it a go. –  Jan 07 '11 at 11:10
1

Did you syncdb with contrib.auth installed before creating vxd.auth? It looks like your table structure might be out of date. If it's feasible, try dropping the auth_user table and then re-syncing, and see if that fixes the issue.

girasquid
  • 15,121
  • 2
  • 48
  • 58
  • Yes, I thought that might be an issue. I've got a script that drops all tables and reloads them with default data. Stupidly, that code actually works. However, somewhere inside my project, something goes wrong... –  Jan 06 '11 at 21:51
  • Can you paste the contents of your `Authentication` context processor? – girasquid Jan 06 '11 at 21:51
  • Can you paste the import statements for your `context_processors.py`? It seems like it might be importing the wrong `User` model somewhere. – girasquid Jan 06 '11 at 22:01
0

I fixed it by refactoring vxd.auth to vxd.myauth. Whatever part of django was tripping up on requests to auth_user now isn't. Not massively impressed with the namespace pollution, but never mind.

  • Apparently I have to wait 2 days before accepting this, so consider the q still open. –  Jan 06 '11 at 22:44
  • Sorry I couldn't help out more - I poked through the Django source for the modules you were importing, and none of them seemed to be importing `User` either. – girasquid Jan 06 '11 at 22:46