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. Sorl Thumbnail + Amazon S3 by skoczen 3 years, 11 months ago
  2. models.py with django_dag models for parts hierarchy by j_syk 1 year, 9 months ago
  3. djangopath: conveniently set sys.path and DJANGO_SETTINGS_MODULE by alia_khouri 4 years, 9 months ago
  4. Model merging function by xaralis 2 years, 5 months ago
  5. Admin Apps Names Translation by Nad/ 3 years, 4 months ago

Comments

(Forgotten your password?)