Skip to content

Commit

Permalink
Use POST for timer quick add functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
cdubz committed Jul 31, 2021
1 parent 824d2af commit c1dab44
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
12 changes: 9 additions & 3 deletions babybuddy/templates/babybuddy/nav-dropdown.html
Expand Up @@ -8,9 +8,15 @@
<span class="text-primary">Baby</span> Buddy
</a>

<div class="d-lg-none d-md-none ml-auto p-0 mr-2">
<a class="text-success mr-2" href="{% url 'core:timer-add-quick' %}"><i class="icon icon-2x icon-timer" aria-hidden="true"></i></a>
<div class="dropdown show d-inline">
<div class="d-lg-none d-md-none d-flex ml-auto p-0 mr-2">
<form action="{% url 'core:timer-add-quick' %}" role="form" method="post" class="mr-2">
{% csrf_token %}
<label class="sr-only">{% trans "Quick Start Timer" %}</label>
<button class="btn m-0 p-0 text-success">
<i class="icon icon-2x icon-timer" aria-hidden="true"></i>
</button>
</form>
<div class="dropdown show">
<a class="text-success"
href="#"
role="button"
Expand Down
11 changes: 8 additions & 3 deletions core/templates/core/timer_nav.html
Expand Up @@ -11,9 +11,14 @@
</a>
<div class="dropdown-menu" aria-labelledby="nav-timer-menu-link">
{% if perms.core.add_timer %}
<a class="dropdown-item" href="{% url 'core:timer-add-quick' %}">
<i class="icon icon-timer" aria-hidden="true"></i> {% trans "Quick Start Timer" %}
</a>
<div class="dropdown-item">
<form action="{% url 'core:timer-add-quick' %}" role="form" method="post" class="d-inline">
{% csrf_token %}
<button class="btn text-left p-0">
<i class="icon icon-timer" aria-hidden="true"></i> {% trans "Quick Start Timer" %}
</button>
</form>
</div>
<a class="dropdown-item" href="{% url 'core:timer-add' %}">
<i class="icon icon-add" aria-hidden="true"></i> {% trans "Start Timer" %}
</a>
Expand Down
4 changes: 3 additions & 1 deletion core/tests/tests_views.py
Expand Up @@ -115,7 +115,9 @@ def test_timer_views(self):
page = self.c.get('/timers/add/')
self.assertEqual(page.status_code, 200)

page = self.c.get('/timers/add/quick/', follow=True)
page = self.c.get('/timers/add/quick/')
self.assertEqual(page.status_code, 405)
page = self.c.post('/timers/add/quick/', follow=True)
self.assertEqual(page.status_code, 200)

entry = models.Timer.objects.first()
Expand Down
3 changes: 2 additions & 1 deletion core/views.py
Expand Up @@ -310,9 +310,10 @@ def get_success_url(self):


class TimerAddQuick(PermissionRequired403Mixin, RedirectView):
http_method_names = ['post']
permission_required = ('core.add_timer',)

def get(self, request, *args, **kwargs):
def post(self, request, *args, **kwargs):
instance = models.Timer.objects.create(user=request.user)
# Add child relationship if there is only Child instance.
if models.Child.count() == 1:
Expand Down

0 comments on commit c1dab44

Please sign in to comment.