FirstRun Middleware

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from django.conf import settings
from django.core.files.storage import FileSystemStorage
from django.shortcuts import redirect

import django.core.exceptions

import logging
import re
import os

# Check for the existance of a lockfile.  If the lockfile doesn't exist, deny access to everywhere except settings.FIRSTRUN_APP_ROOT
# If the lockfile exists, throw django.core.exceptions.MiddlewareNotUsed() so that this class is unloaded

class FirstRunMiddleware:
    def __init__(self):
        if not self.is_first_run():
            logging.debug('FirstRunMiddleware: FirstRun not required, unloading for process lifetime.')
            raise django.core.exceptions.MiddlewareNotUsed()

        self.p = re.compile(''.join(['^', settings.FIRSTRUN_APP_ROOT]))

    def process_request(self, request):
        if self.p.match(request.path) == None:
            if self.is_first_run():
                logging.debug(''.join(['FirstRunMiddleware: Denying access to ', request.path, ' FirstRun needed.']));
                return redirect(settings.FIRSTRUN_APP_ROOT)
            else:
                # Will only be executed between the time firstrun has been completed, until the process is regenerated (typically on the scale of mins).
                logging.debug('FirstRunMiddleware: FirstRun completed, resuming normal flow.')

    def is_first_run(self):
        return not os.path.exists(''.join([settings.PROJECT_PATH, settings.FIRSTRUN_LOCKFILE]))

More like this

  1. Model merging function by xaralis 3 years, 4 months ago
  2. Sorl Thumbnail + Amazon S3 by skoczen 4 years, 10 months ago
  3. Little middleware that create a facebook user by marinho 6 years, 4 months ago
  4. Effective content caching for mass-load site using redirect feature by nnseva 2 years, 9 months ago
  5. View to retrieve objects meeting a complex tag query by nathangeffen 3 years, 4 months ago

Comments

(Forgotten your password?)