Question

I'm trying to diagnose the error in my custom middleware somehow brings down my session middleware. The custom middleware is a simple app which deletes old files in some folders.

class DeleteMediaMiddleware(object):
    time_limit = 100.

    def check_folder(self,folder):
    '''Deletes the files older than "time_limit" '''
        os.chdir(folder)
        files_in_folder = os.listdir(os.curdir)
        for my_file in files_in_folder:
            creation_time = os.path.getmtime(my_file)
            file_lifetime = time.time() - creation_time
            if file_lifetime > self.time_limit:
                os.remove(my_file)

    def process_request(self, request):
    '''Standard middleware method which runs on every request'''
        self.check_folder(config.input_folder)
        self.check_folder(config.output_folder)
        self.check_folder(config.plot_folder)
        self.check_folder(config.report_folder)
    return None

It is located in the Django project folder. (The levels is how Django creates by default. In my case the project is named expofit, and the folder structure is expofit/expofit)

project_folder_lvl1/project_folder_lvl2/mymiddleware.py

I added the middleware to Djagno settings:

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'server.mymiddleware.DeleteMediaMiddleware',
    )

However, i end up with an error.

Exception Value:    no such table: django_session

which is from checking a session value in views.py:

....    
if request.session.has_key('user_id'):
....

As soon as i disable my middleware in Django settings, everything works normally.

UPDATE:

I sucesfully open the database, which is an sqlite file located in the project_folder_lvl1, and locate the table which isn't accessible.

Here is the complete Django traceback.

Environment:

Request Method: GET
Request URL: http://localhost:8000/expofit/

Django Version: 1.4.1
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'expofit_django_app',
'south',
'django.contrib.admin')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'server.mymiddleware.DeleteMediaMiddleware')


Traceback:
File "/home/alan/Desktop/expofit/expofit_env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/alan/Desktop/expofit/expofit_env/local/lib/python2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
77.         return view_func(*args, **kwargs)
File "/home/alan/Desktop/expofit/expofit_hg/py/server/expofit_django_app/views.py" in check
59.         return view(request, *args, **kwargs)
File "/home/alan/Desktop/expofit/expofit_hg/py/server/expofit_django_app/views.py" in start
23.     if request.session.has_key('user_id'):
File "/home/alan/Desktop/expofit/expofit_env/local/lib/python2.7/site-packages/django/contrib/sessions/backends/base.py" in has_key
103.         return key in self._session
File "/home/alan/Desktop/expofit/expofit_env/local/lib/python2.7/site-packages/django/contrib/sessions/backends/base.py" in _get_session
165.                 self._session_cache = self.load()
File "/home/alan/Desktop/expofit/expofit_env/local/lib/python2.7/site-packages/django/contrib/sessions/backends/db.py" in load
19.                 expire_date__gt=timezone.now()
File "/home/alan/Desktop/expofit/expofit_env/local/lib/python2.7/site-packages/django/db/models/manager.py" in get
131.         return self.get_query_set().get(*args, **kwargs)
File "/home/alan/Desktop/expofit/expofit_env/local/lib/python2.7/site-packages/django/db/models/query.py" in get
361.         num = len(clone)
File "/home/alan/Desktop/expofit/expofit_env/local/lib/python2.7/site-packages/django/db/models/query.py" in __len__
85.                 self._result_cache = list(self.iterator())
File "/home/alan/Desktop/expofit/expofit_env/local/lib/python2.7/site-packages/django/db/models/query.py" in iterator
291.         for row in compiler.results_iter():
File "/home/alan/Desktop/expofit/expofit_env/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in results_iter
763.         for rows in self.execute_sql(MULTI):
File "/home/alan/Desktop/expofit/expofit_env/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
818.         cursor.execute(sql, params)
File "/home/alan/Desktop/expofit/expofit_env/local/lib/python2.7/site-packages/django/db/backends/util.py" in execute
40.             return self.cursor.execute(sql, params)
File "/home/alan/Desktop/expofit/expofit_env/local/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py" in execute
337.             return Database.Cursor.execute(self, query, params)

Exception Type: DatabaseError at /expofit/
Exception Value: no such table: django_session

Looking for ideas on how to debug this problem?

Was it helpful?

Solution

The problem was fixed by adding the absolute path to the sqlite database.

settings.py which yielded the error:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'expofit_database',  
        ...
        ...      

new settings.py:

PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join( PROJECT_PATH, 'expofit_database'),  
        ...
        ...      

From what i figured, the problem was in calling the

os.chdir(folder)

inside the check_folder function which messed up the runtime paths. Although setting the absolute path solved the problem for accessing the database, i removed the os.chdir(folder) to prevent potential errors in the future.

def check_folder(folder):
    time_limit = 100.
    files_in_folder = os.listdir(folder)
    for file_name in files_in_folder:
        file_path = os.path.join(folder,file_name)
        creation_time = os.path.getmtime(file_path)
        file_lifetime = time.time() - creation_time
        if file_lifetime > time_limit:
            os.remove(file_path)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top