Skip to content

Commit

Permalink
Change base_url context processor into a middleware...
Browse files Browse the repository at this point in the history
which makes it possible to also use it in views, making the base_url BetterView function unnecessary.

Fixes bug "'WSGIRequest' object has no attribute 'base_url'" upon creating event with calendar entry.
  • Loading branch information
Compizfox committed Nov 16, 2017
1 parent ee6fc14 commit f3f1b3e
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 17 deletions.
4 changes: 0 additions & 4 deletions app/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,3 @@

def now(request):
return {'now': timezone.now()}


def base_url(request):
return {'base_url': request.build_absolute_uri('/').strip("/")}
12 changes: 12 additions & 0 deletions app/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,15 @@ def middleware(request):
return response

return middleware


def base_url_middleware(get_response):
"""
Add base url to request
"""
def middleware(request):
request.base_url = request.build_absolute_uri('/').strip("/")

return get_response(request)

return middleware
2 changes: 1 addition & 1 deletion app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
'app.middleware.last_seen_middleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'app.middleware.base_url_middleware',
]

ROOT_URLCONF = 'app.urls'
Expand All @@ -60,7 +61,6 @@
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'app.context_processors.now',
'app.context_processors.base_url'
],
},
},
Expand Down
8 changes: 4 additions & 4 deletions app/templates/mails/registration_notification.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<p>Beste {{ recipient }},</p>

<p>Je wordt erop geattendeerd dat {{ sender }} je heeft ingeschreven voor de activiteit <a href="{{ base_url }}{% url 'event-detail' event.pk %}">{{ event.name }}</a>.</p>
<p>Je wordt erop geattendeerd dat {{ sender }} je heeft ingeschreven voor de activiteit <a href="{{ request.base_url }}{% url 'event-detail' event.pk %}">{{ event.name }}</a>.</p>

<p>
Het is mogelijk om dit te annuleren door jezelf (via bovenstaande link) weer uit te schrijven.
Expand All @@ -14,13 +14,13 @@
{% endif %}
</p>

<p><i>Verzonden door het <a href="{{ base_url }}{% url 'home' %}">Alembic Inschrijfsysteem</a></i></p>
<p><i>Verzonden door het <a href="{{ request.base_url }}{% url 'home' %}">Alembic Inschrijfsysteem</a></i></p>

<hr>

<p>Dear {{ recipient }},</p>

<p>You are notified that {{ sender }} has enrolled you for the event <a href="{{ base_url }}{% url 'event-detail' event.pk %}">{{ event.name }}</a>.</p>
<p>You are notified that {{ sender }} has enrolled you for the event <a href="{{ request.base_url }}{% url 'event-detail' event.pk %}">{{ event.name }}</a>.</p>

<p>
It is possible to cancel this by disenrolling using the link above.
Expand All @@ -29,5 +29,5 @@
{% endif %}
</p>

<p><i>Sent by <a href="{{ base_url }}{% url 'home' %}">Alembic Inschrijfsysteem</a></i></p>
<p><i>Sent by <a href="{{ request.base_url }}{% url 'home' %}">Alembic Inschrijfsysteem</a></i></p>
{% endblock %}
8 changes: 4 additions & 4 deletions app/views/AdminView.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ class AdminView(StaffRequiredMixin, BetterView):
def __init__(self, route, request):
super().__init__(route, request)
# Instantiate FlowService
self.flow = FlowService(self.base_url() + reverse('admin-calendar-flow'))
self.flow = FlowService(request.base_url + reverse('admin-calendar-flow'))

def show(self, request):
# Get authorization URL for link
authorize_url = self.flow.get_authorize_url()

# Try to instantiate GoogleCalendarService and get list of calendars
try:
cal_service = GoogleCalendarService(self.base_url())
cal_service = GoogleCalendarService(request.base_url)
cals = cal_service.get_calendars()
active_cal = cal_service.calendar
except RuntimeError:
Expand All @@ -42,7 +42,7 @@ def show(self, request):

def calendar(self, request):
# Set 'active' calendar
cal_service = GoogleCalendarService(self.base_url())
cal_service = GoogleCalendarService(request.base_url)
cal_service.calendar = request.POST['calender_id']

messages.success(request, _("Kalender ingesteld!"))
Expand All @@ -55,7 +55,7 @@ def calendar_flow(self, request):
self.flow.exchange(request.GET['code'])

# Empty active calendar setting
cal_service = GoogleCalendarService(self.base_url())
cal_service = GoogleCalendarService(request.base_url)
cal_service.calendar = None

messages.success(request, _("Google-account gekoppeld!"))
Expand Down
2 changes: 1 addition & 1 deletion app/views/CalendarView.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ class CalendarView(BetterView):
}

def show(self, request):
cal_service = GoogleCalendarService(self.base_url())
cal_service = GoogleCalendarService(request.base_url)

return render(request, 'calendar.html', {'calendar': cal_service.calendar})
3 changes: 0 additions & 3 deletions lib/BetterView.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ def check_admin_of(self, committee):
if not self.request.user.is_admin_of_committee(committee):
raise PermissionDenied

def base_url(self):
return self.request.build_absolute_uri('/').strip("/")


class StaffRequiredMixin:
"""
Expand Down

0 comments on commit f3f1b3e

Please sign in to comment.