<commit_before>import re
import datetime

from django.conf import settings
from django.contrib import messages
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse

class CheckSetupMiddleware(object):
  """This middleware checks if the logged in user has completed the setup process."""
  
  def process_request(self, request):
    user = request.user
    path = request.path
    is_mobile = request.mobile
    # We need to check if the user is going to the home page so we don't get caught in a redirect loop.
    # We do need to filter out requests for CSS and other resources.
    pattern = re.compile("^/(m\/admin|m\/setup|account|home|site_media|tc|media|favicon.ico)/")
    needs_setup = user.is_authenticated() and not user.get_profile().setup_complete
    if is_mobile and needs_setup and not pattern.match(path):
      return HttpResponseRedirect(reverse("mobile_setup"))
    elif needs_setup and not pattern.match(path):
      return HttpResponseRedirect(reverse("home_index"))
    return None

class CompetitionMiddleware(object):
  """
  This middleware checks if the competition is over.
  """
  def process_request(self, request):
    """
    Checks if we are still in the competition. If the user is logged in, 
    they are redirected to a competition status page.
    """
    if request.user.is_authenticated():
      path = request.path
      
      today = datetime.datetime.today()
      start = datetime.datetime.strptime(settings.COMPETITION_START, "%Y-%m-%d")
      end = datetime.datetime.strptime(settings.COMPETITION_END, "%Y-%m-%d")
      
      pattern = re.compile("^/(m\/|home\/restricted|site_media|media|favicon.ico)/")
      
      if today < start and not pattern.match(path):
        return HttpResponseRedirect(reverse("home_restricted"))

      if today > end and not pattern.match(path):
        return HttpResponseRedirect(reverse("home_restricted"))

    return None
<commit_msg>Changing middleware so that admins can access the site when the competition is not active.<commit_after>import re
import datetime

from django.conf import settings
from django.contrib import messages
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse

class CheckSetupMiddleware(object):
  """This middleware checks if the logged in user has completed the setup process."""
  
  def process_request(self, request):
    user = request.user
    path = request.path
    is_mobile = request.mobile
    # We need to check if the user is going to the home page so we don't get caught in a redirect loop.
    # We do need to filter out requests for CSS and other resources.
    pattern = re.compile("^/(m\/admin|m\/setup|account|home|site_media|tc|media|favicon.ico)/")
    needs_setup = user.is_authenticated() and not user.get_profile().setup_complete
    if is_mobile and needs_setup and not pattern.match(path):
      return HttpResponseRedirect(reverse("mobile_setup"))
    elif needs_setup and not pattern.match(path):
      return HttpResponseRedirect(reverse("home_index"))
    return None

class CompetitionMiddleware(object):
  """
  This middleware checks if the competition is over.
  """
  def process_request(self, request):
    """
    Checks if we are still in the competition. If the user is logged in, 
    they are redirected to a competition status page.
    """
    if request.user.is_authenticated():
      path = request.path
      
      today = datetime.datetime.today()
      start = datetime.datetime.strptime(settings.COMPETITION_START, "%Y-%m-%d")
      end = datetime.datetime.strptime(settings.COMPETITION_END, "%Y-%m-%d")
      
      pattern = re.compile("^/(m\/|home\/restricted|site_media|media|favicon.ico)/")
      
      if today < start and not request.user.is_staff and not pattern.match(path):
        return HttpResponseRedirect(reverse("home_restricted"))

      if today > end and not request.user.is_staff and not pattern.match(path):
        return HttpResponseRedirect(reverse("home_restricted"))

    return None

{'fraction_non_alphanumeric': 0.09459125710051865, 'fraction_numerical': 0.0, 'mean_word_length': 3.7202797202797204, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2202966', 'n_tokens_mistral': 1128, 'n_tokens_neox': 1095, 'n_words': 360}