Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/staticgenerator checks request is anonymous #48

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions mysite/base/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,28 @@

import mysite.profile.controllers
import mysite.project.controllers
import staticgenerator.middleware

def get_user_ip(request):
# return request.META['REMOTE_ADDR']
if request.META['REMOTE_ADDR'] == '127.0.0.1':
return "98.140.110.121"
else:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol whitespace changes lol

return request.META['REMOTE_ADDR']
return request.META['REMOTE_ADDR']

class HandleWannaHelpQueue(object):
def process_request(self, request):
if not hasattr(request, 'user') or not hasattr(request, 'session'):
return None

if request.user.is_authenticated() and 'wanna_help_queue_handled' not in request.session:
if (hasattr(request, 'user') and
request.user.is_authenticated() and
'wanna_help_queue_handled' not in request.session):
mysite.project.controllers.flush_session_wanna_help_queue_into_database(
request.user, request.session)
request.session['wanna_help_queue_handled'] = True
return None


class DetectLogin(object):
# called every time a page is gotten
Expand All @@ -48,3 +51,19 @@ def process_response(self, request, response):
mysite.project.controllers.take_control_of_our_answers(request.user, request.session)
request.session['post_login_stuff_run'] = True
return response

class StaticGeneratorMiddlewareOnlyWhenAnonymous(object):
'''This is a wrapper around
staticgenerator.middleware.StaticGeneratorMiddleware that only saves to the
cache when request.user.is_authenticated() is False.

We never want to do static generation for when people are logged in.'''
def process_response(self, request, response):
# If the request comes from a user that is authenticated, bail.
if request.user.is_authenticated():
return response
# Finally, pass the reponse to StaticGeneratorMiddleware. The middleware
# there is responsible for checking the settings.STATIC_GENERATOR_URLS
# to make sure the URL is permitted to be cached.
m = staticgenerator.middleware.StaticGeneratorMiddleware()
return m.process_response(request, response)
4 changes: 2 additions & 2 deletions mysite/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
MIDDLEWARE_CLASSES = [
'mysite.base.middleware.DetectLogin', # This must live on top of Auth + Session middleware
'django.middleware.common.CommonMiddleware',
'staticgenerator.middleware.StaticGeneratorMiddleware',
'mysite.base.middleware.StaticGeneratorMiddlewareOnlyWhenAnonymous',
'sessionprofile.middleware.SessionProfileMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
Expand Down Expand Up @@ -321,7 +321,7 @@
if sys.platform.startswith('win'):
# staticgenerator seems to act weirdly on Windows, so we disable it.
MIDDLEWARE_CLASSES.remove(
'staticgenerator.middleware.StaticGeneratorMiddleware')
'mysite.base.middleware.StaticGeneratorMiddlewareOnlyWhenAnonymous')

### Enable the low-quality, high-load bug recommendation system
RECOMMEND_BUGS=True
Expand Down