Skip to content

Commit

Permalink
Cache the bug detail page
Browse files Browse the repository at this point in the history
We can do this with no timeout because we have the bug's modified_at
timestamp, which changes whenever any action is done on the bug.

If we ever implement editing of titles, this will be problematic,
because bug mentions include the other bug's title. If the other bug's
title gets updated, the cached rendered markdown won't be updated.
  • Loading branch information
gavinwahl committed Apr 27, 2017
1 parent f89c975 commit a88dbca
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
7 changes: 7 additions & 0 deletions buggy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ def number(self):
def get_absolute_url(self):
return urls.reverse('buggy:bug_detail', kwargs={'bug_number': self.number})

@property
def actions_preloaded(self):
return self.actions.select_related(
'user', 'comment', 'setpriority', 'setassignment__assigned_to',
'setstate', 'setproject', 'settitle',
).prefetch_related('attachments')


class Action(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT)
Expand Down
9 changes: 7 additions & 2 deletions buggy/templates/buggy/bug_detail.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{% extends "buggy/base.html" %}

{% load buggy_tags thumbnail argonauts absoluteuri %}
{% load buggy_tags thumbnail argonauts absoluteuri cache %}

{% block title %}{{ block.super }} - {{ bug.title }}{% endblock %}

{% block content %}
{% cache 31536000 bug_detail bug.id bug.modified_at %}

<script>
window._harvestPlatformConfig = {
"applicationName": "Buggy",
Expand All @@ -13,6 +15,7 @@
</script>
<script async src="https://platform.harvestapp.com/assets/platform.js"></script>


<div class="harvest-timer"
data-item='{"id": {{ bug.number|json|force_escape }}, "name": {{ bug.title|json|force_escape }}}'
data-group='{"id": {{ bug.project.id|json|force_escape }}, "name": {{ bug.project.name|json|force_escape }}}'
Expand All @@ -25,7 +28,7 @@ <h1><input readonly value="#{{ bug.number }}"><a href="{{ bug.project.get_absolu
state is {{ bug.state }}{% if bug.assigned_to %}, currently assigned to {{ bug.assigned_to.get_short_name }}{% endif %}.
</p>

{% for action in bug.actions.all %}
{% for action in bug.actions_preloaded %}
<section>
<h1>{{ action.created_at|absolutedate }} {{ action.description }}</h1>
{% if action.comment %}
Expand All @@ -52,6 +55,8 @@ <h3>Attachments</h3>
</section>
{% endfor %}

{% endcache %}

{% include "buggy/_bug_form.html" %}

{% endblock %}
7 changes: 1 addition & 6 deletions buggy/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,7 @@ def get_form_kwargs(self):
class BugDetailView(LoginRequiredMixin, BugMutationMixin, FormView):
template_name = 'buggy/bug_detail.html'

queryset = Bug.objects.all().prefetch_related(
Prefetch('actions', queryset=Action.objects.select_related(
'user', 'comment', 'setpriority', 'setassignment__assigned_to',
'setstate', 'setproject', 'settitle',
).prefetch_related('attachments'))
).select_related(
queryset = Bug.objects.select_related(
'created_by', 'assigned_to', 'project'
)

Expand Down

0 comments on commit a88dbca

Please sign in to comment.