# -*- coding: utf-8 -*-
#
# django-codenerix
#
# Copyright 2017 Centrologic Computational Logistic Center S.L.
#
# Project URL : http://www.codenerix.com
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import re
from threading import local

from django.http import HttpResponsePermanentRedirect
from django.conf import settings

class SecureRequiredMiddleware(object):
    def __init__(self, get_response=None):
        self.get_response = get_response
        self.paths = getattr(settings, 'HTTPS_PATHS', getattr(settings, 'SECURE_REQUIRED_PATHS',('/',)))
        self.enabled = self.paths and getattr(settings, 'HTTPS_SUPPORT', True)
    
    def process_request(self, request):
        
        if self.enabled and not request.is_secure():
            for path in self.paths:
                full_path = request.get_full_path()
                if path[0]=='-' and (full_path.startswith(path[1:]) or re.compile(path[1:]).match(full_path)):
                    return None
            for path in self.paths:
                full_path = request.get_full_path()
                if full_path.startswith(path) or re.compile(path).match(full_path):
                    request_url = request.build_absolute_uri(request.get_full_path())
                    secure_url = request_url.replace('http://', 'https://')
                    return HttpResponsePermanentRedirect(secure_url)
    
    def __call__(self, request):
        # Code to be executed for each request before the view (and later middleware) are called.
        self.process_request(request)
        
        # Get response
        response = self.get_response(request)
        
        # Code to be executed for each request/response after the view is called
        # ... pass ...
        
        # Return response
        return response


class CurrentUserMiddleware(object):
    '''
    Let the system to have the username everywhere
    '''
    def __init__(self, get_response=None):
        self.get_response = get_response
    
    def process_request(self, request):
        _user.value = request.user
    
    def __call__(self, request):
        # Code to be executed for each request before the view (and later middleware) are called.
        self.process_request(request)
        
        # Get response
        response = self.get_response(request)
        
        # Code to be executed for each request/response after the view is called
        # ... pass ...
        
        # Return response
        return response
    
def get_current_user():
    '''
    Thought this function you can get the user loged in everywhere
    '''
    if 'value' in dir(_user):
        return _user.value
    else:
        return None
_user = local()


@@ -745,42 +745,26 @@
 ngo.
-http import HttpResponsePermanentR
+shortcuts import r
 edir
@@ -801,16 +801,17 @@
 ttings%0A%0A
+%0A
 class Se
@@ -1026,13 +1026,15 @@
 HS',
+ 
 ('/',
+ 
 )))%0A
@@ -1218,16 +1218,92 @@
 ecure():
+%0A%0A            # Check for negative matching, URLs we don't want to be secure
 %0A       
@@ -1413,10 +1413,12 @@
 h%5B0%5D
+ 
 ==
+ 
 '-' 
@@ -1528,16 +1528,102 @@
 rn None%0A
+                %0A            # Check for positive matching, URLs we want to be secure%0A
         
@@ -1979,49 +1979,98 @@
 urn 
-HttpResponsePermanentRedirect(secure_url)
+redirect(secure_url, permanent=True)%0A%0A        # Any other lateral case%0A        return None
 %0A   
@@ -2201,32 +2201,43 @@
  called.%0A       
+ response =
  self.process_re
@@ -2275,32 +2275,65 @@
  # Get response%0A
+        if response is None:%0A    
         response

Secure Required