Check if user is authenticated for showing/accepting task event form

This commit is contained in:
Adam Goldsmith 2020-12-22 15:32:58 -05:00
parent 86fe80b375
commit e4b45ad948
2 changed files with 41 additions and 34 deletions

View File

@ -30,6 +30,7 @@
</div> </div>
{% endif %} {% endif %}
{% if user.is_authenticated %}
<div class="d-md-none"> <div class="d-md-none">
<h2> Log a Maintenance Event </h2> <h2> Log a Maintenance Event </h2>
<form class="form-group" method="post"> <form class="form-group" method="post">
@ -51,6 +52,7 @@
{{ form.media }} {{ form.media }}
</div> </div>
{% endif %}
<h2> Event Log </h2> <h2> Event Log </h2>
<table class="table table-striped table-hover"> <table class="table table-striped table-hover">
@ -62,6 +64,7 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{% if user.is_authenticated %}
<tr class="d-none d-md-table-row"> <tr class="d-none d-md-table-row">
<form method="post"> <form method="post">
{% csrf_token %} {% csrf_token %}
@ -71,6 +74,7 @@
<td> <input type="submit" class="btn btn-primary" value="Submit"> </td> <td> <input type="submit" class="btn btn-primary" value="Submit"> </td>
</form> </form>
</tr> </tr>
{% endif %}
{% for event in events|dictsortreversed:"date" %} {% for event in events|dictsortreversed:"date" %}
<tr> <tr>
<td class="text-nowrap"> {{ event.date }} </td> <td class="text-nowrap"> {{ event.date }} </td>

View File

@ -31,11 +31,14 @@ def taskDetail(request, asset_tag, task_slug):
events = task.event_set.all() events = task.event_set.all()
if request.method == 'POST': if request.method == 'POST':
if request.user.is_authenticated:
event = Event(task=task) event = Event(task=task)
form = EventForm(request.POST, instance=event) form = EventForm(request.POST, instance=event)
if form.is_valid(): if form.is_valid():
form.save() form.save()
pass else:
form = EventForm(request.POST)
form.add_error(None, "Not authenticated: please login")
else: else:
form = EventForm(initial={ form = EventForm(initial={
'date': datetime.now(), 'date': datetime.now(),