-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from mattrobenolt/refactor
Minor refactor to take advantage of pre-calculated settings
- Loading branch information
Showing
12 changed files
with
216 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,24 @@ | ||
from django.conf import settings | ||
from django_seo_js import settings | ||
from django_seo_js.backends import SelectedBackend | ||
|
||
|
||
DEFAULT_IGNORED_EXTENSIONS = [ | ||
".js", | ||
".css", | ||
".xml", | ||
".less", | ||
".png", | ||
".jpg", | ||
".jpeg", | ||
".gif", | ||
".pdf", | ||
".doc", | ||
".txt", | ||
".ico", | ||
".rss", | ||
".zip", | ||
".mp3", | ||
".rar", | ||
".exe", | ||
".wmv", | ||
".doc", | ||
".avi", | ||
".ppt", | ||
".mpg", | ||
".mpeg", | ||
".tif", | ||
".wav", | ||
".mov", | ||
".psd", | ||
".ai", | ||
".xls", | ||
".mp4", | ||
".m4a", | ||
".swf", | ||
".dat", | ||
".dmg", | ||
".iso", | ||
".flv", | ||
".m4v", | ||
".torrent" | ||
] | ||
|
||
|
||
def update_cache_for_url(url): | ||
if getattr(settings, "SEO_JS_ENABLED", not settings.DEBUG): | ||
if settings.ENABLED: | ||
selector = SelectedBackend() | ||
return selector.backend.update_url(url) | ||
return False | ||
|
||
|
||
def request_should_be_ignored(request): | ||
# TODO: move these to a central settings/default area ala appconf. | ||
# Note it's tougher than it looks because of the override_settings | ||
# magical injection in tests. | ||
if getattr(settings, "SEO_JS_IGNORE_URLS", None): | ||
IGNORE_URLS = settings.SEO_JS_IGNORE_URLS | ||
else: | ||
IGNORE_URLS = ["/sitemap.xml", ] | ||
|
||
ignore = False | ||
for url in IGNORE_URLS: | ||
for url in settings.IGNORE_URLS: | ||
if url in request.path: | ||
ignore = True | ||
break | ||
return True | ||
|
||
if not ignore: | ||
if getattr(settings, "SEO_JS_IGNORE_EXTENSIONS", None) is not None: | ||
IGNORED_EXTENSIONS = settings.SEO_JS_IGNORE_EXTENSIONS | ||
else: | ||
IGNORED_EXTENSIONS = DEFAULT_IGNORED_EXTENSIONS | ||
extension = None | ||
last_dot = request.path.rfind(".") | ||
if last_dot != -1: | ||
extension = request.path[last_dot:] | ||
if extension: | ||
for ext in IGNORED_EXTENSIONS: | ||
if extension == ext: | ||
ignore = True | ||
break | ||
extension = None | ||
last_dot = request.path.rfind(".") | ||
if last_dot == -1: | ||
# No extension found | ||
return False | ||
|
||
return ignore | ||
extension = request.path[last_dot:] | ||
return extension and extension in settings.IGNORE_EXTENSIONS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,24 @@ | ||
from django_seo_js import settings | ||
from django_seo_js.backends import SelectedBackend | ||
from django_seo_js.helpers import request_should_be_ignored | ||
from django.conf import settings | ||
|
||
import logging | ||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class HashBangMiddleware(SelectedBackend): | ||
def process_request(self, request): | ||
if not request_should_be_ignored(request) and\ | ||
getattr(settings, "SEO_JS_ENABLED", not settings.DEBUG) and\ | ||
"_escaped_fragment_" in request.GET: | ||
if not settings.ENABLED: | ||
return | ||
|
||
if request_should_be_ignored(request): | ||
return | ||
|
||
if "_escaped_fragment_" not in request.GET: | ||
return | ||
|
||
url = request.build_absolute_uri() | ||
try: | ||
return self.backend.get_response_for_url(url) | ||
except: | ||
pass | ||
url = request.build_absolute_uri() | ||
try: | ||
return self.backend.get_response_for_url(url) | ||
except Exception as e: | ||
logger.exception(e) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,34 @@ | ||
import re | ||
from django.conf import settings | ||
from django_seo_js import settings | ||
from django_seo_js.backends import SelectedBackend | ||
from django_seo_js.helpers import request_should_be_ignored | ||
|
||
|
||
DEFAULT_SEO_JS_USER_AGENTS = [ | ||
"Googlebot", | ||
"Yahoo", | ||
"bingbot", | ||
"Ask Jeeves", | ||
"baiduspider", | ||
"facebookexternalhit", | ||
"twitterbot", | ||
"rogerbot", | ||
"linkedinbot", | ||
"embedly", | ||
"quoralink preview'", | ||
"showyoubot", | ||
"outbrain", | ||
"pinterest", | ||
"developersgoogle.com/+/web/snippet", | ||
] | ||
import logging | ||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class UserAgentMiddleware(SelectedBackend): | ||
def __init__(self, *args, **kwargs): | ||
super(UserAgentMiddleware, self).__init__(*args, **kwargs) | ||
if getattr(settings, "SEO_JS_USER_AGENTS", None): | ||
agents = getattr(settings, "SEO_JS_USER_AGENTS") | ||
else: | ||
agents = DEFAULT_SEO_JS_USER_AGENTS | ||
regex_str = "|".join(agents) | ||
regex_str = "|".join(settings.USER_AGENTS) | ||
regex_str = ".*?(%s)" % regex_str | ||
self.USER_AGENT_REGEX = re.compile(regex_str, re.IGNORECASE) | ||
|
||
def process_request(self, request): | ||
# TODO: move to proper settings app pattern. | ||
if ( | ||
not request_should_be_ignored(request) and | ||
getattr(settings, "SEO_JS_ENABLED", not settings.DEBUG) and | ||
"HTTP_USER_AGENT" in request.META and | ||
self.USER_AGENT_REGEX.match(request.META["HTTP_USER_AGENT"]) | ||
): | ||
|
||
url = request.build_absolute_uri() | ||
|
||
try: | ||
return self.backend.get_response_for_url(url) | ||
except: | ||
pass | ||
if not request.ENABLED: | ||
return | ||
|
||
if request_should_be_ignored(request): | ||
return | ||
|
||
if "HTTP_USER_AGENT" not in request.META: | ||
return | ||
|
||
if not self.USER_AGENT_REGEX.match(request.META["HTTP_USER_AGENT"]): | ||
return | ||
|
||
url = request.build_absolute_uri() | ||
try: | ||
return self.backend.get_response_for_url(url) | ||
except Exception as e: | ||
logger.exception(e) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
from django.conf import settings as django_settings | ||
|
||
ENABLED = getattr(django_settings, 'SEO_JS_ENABLED', not django_settings.DEBUG) | ||
|
||
IGNORE_URLS = frozenset(getattr(django_settings, 'SEO_JS_IGNORE_URLS', ['/sitemap.xml'])) | ||
|
||
IGNORE_EXTENSIONS = frozenset(getattr(django_settings, 'SEO_JS_IGNORE_EXTENSIONS', ( | ||
".js", | ||
".css", | ||
".xml", | ||
".less", | ||
".png", | ||
".jpg", | ||
".jpeg", | ||
".gif", | ||
".pdf", | ||
".doc", | ||
".txt", | ||
".ico", | ||
".rss", | ||
".zip", | ||
".mp3", | ||
".rar", | ||
".exe", | ||
".wmv", | ||
".doc", | ||
".avi", | ||
".ppt", | ||
".mpg", | ||
".mpeg", | ||
".tif", | ||
".wav", | ||
".mov", | ||
".psd", | ||
".ai", | ||
".xls", | ||
".mp4", | ||
".m4a", | ||
".swf", | ||
".dat", | ||
".dmg", | ||
".iso", | ||
".flv", | ||
".m4v", | ||
".torrent", | ||
))) | ||
|
||
USER_AGENTS = frozenset(getattr(django_settings, 'SEO_JS_USER_AGENTS', ( | ||
"Googlebot", | ||
"Yahoo", | ||
"bingbot", | ||
"Ask Jeeves", | ||
"baiduspider", | ||
"facebookexternalhit", | ||
"twitterbot", | ||
"rogerbot", | ||
"linkedinbot", | ||
"embedly", | ||
"quoralink preview'", | ||
"showyoubot", | ||
"outbrain", | ||
"pinterest", | ||
"developersgoogle.com/+/web/snippet", | ||
))) | ||
|
||
BACKEND = getattr(django_settings, 'SEO_JS_BACKEND', 'django_seo_js.backends.PrerenderIO') | ||
|
||
PRERENDER_TOKEN = getattr(django_settings, 'SEO_JS_PRERENDER_TOKEN', None) | ||
PRERENDER_URL = getattr(django_settings, 'SEO_JS_PRERENDER_URL', None) | ||
PRERENDER_RECACHE_URL = getattr(django_settings, 'SEO_JS_PRERENDER_RECACHE_URL', None) |
Oops, something went wrong.