def process_view(self, request, view_func, view_args, view_kwargs): if (self.category_subdomain is not None): self.log.debug("process view: %s, %s, %s", view_func, view_args, view_kwargs) # skip ignored paths if is_path_ignored(request.path_info): return view_kwargs[ 'category'] = self.category_subdomain.category.tree_path self.log.debug("process view modified: %s, %s, %s", view_func, view_args, view_kwargs) return None
def get_url(url): from ella_category_subdomain.models import CategorySubdomain # parse url parsed_url = urlparse(url) # skip ignored paths if is_path_ignored(parsed_url.path): return url # get non-empty URL path items path_items = [item for item in parsed_url.path.split('/') if (len(item) > 0)] # if the path is not a root one check if the first item of path does not match category # subdomain and change the URL accordingly if (len(path_items) > 0): # get the first path item first_path_item = path_items[0] # search for the particular subdomain category category_subdomain_list = CategorySubdomain.objects.filter(category__slug=first_path_item) # change the URL if found if (len(category_subdomain_list) == 1): url = get_url_with_subdomain(parsed_url, category_subdomain_list[0]) return url # get the URL domain parts domain_items = parsed_url.netloc.split('.') # get the first domain part first_domain_item = domain_items[0] # search for the particular subdomain category category_subdomain_list = CategorySubdomain.objects.filter(subdomain_slug=first_domain_item) # the URL already modified if exists if (len(category_subdomain_list) > 0): return url # fill in the default subdomain otherwise else: url = get_url_without_subdomain(parsed_url) return url
def _get_redirect_if_old_url(self, request): """Returns a HttpResponseRedirect instance when the category configured for a subdomain is detected in the path (i.e. as if there was no subdomain) and redirects the request to correct subdomain URL. """ # skip for ignored paths if is_path_ignored(request.path_info): return # get the site domain domain = get_domain_for_category(category=None, strip_www=False) # get the current request host host = request.get_host() self.log.debug("Host: %s, domain: %s", host, domain) # try to find a subdomain for a first category in the path if they match if host == domain: # search for a category subdomain matching the first path of the domain category_subdomain = CategorySubdomain.objects.get_for_path( request.path) # if category_subdomain is not None: # get the absolute uri of the request request_absolute_uri = request.build_absolute_uri() # parse the uri parsed_url_list = list(urlparse(request_absolute_uri)) # get domain name for the category subdomain new_domain = category_subdomain.get_subdomain() # cut off the first category part of the path new_path = request.path[len(category_subdomain.category. tree_path) + 1:] # replace domain and path in uri parsed_url_list[1:3] = new_domain, new_path # redirect to the new uri return HttpResponseRedirect(urlunparse(parsed_url_list))
def test_ignored_path(self): tools.assert_true(is_path_ignored('/ign_path/')) tools.assert_true(is_path_ignored('/ign_path/sub_path'))
def test_normal_path(self): tools.assert_false(is_path_ignored('/normal_path/sub_path/'))