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,27 +30,29 @@
</div> </div>
{% endif %} {% endif %}
<div class="d-md-none"> {% if user.is_authenticated %}
<h2> Log a Maintenance Event </h2> <div class="d-md-none">
<form class="form-group" method="post"> <h2> Log a Maintenance Event </h2>
{% csrf_token %} <form class="form-group" method="post">
<fieldset class="form-group"> {% csrf_token %}
{{ form.date.label_tag }} <fieldset class="form-group">
{% render_field form.date type="date" class="form-control" %} {{ form.date.label_tag }}
</fieldset> {% render_field form.date type="date" class="form-control" %}
<fieldset class="form-group"> </fieldset>
{{ form.user.label_tag }} <fieldset class="form-group">
{% render_field form.user class="form-control" %} {{ form.user.label_tag }}
</fieldset> {% render_field form.user class="form-control" %}
<fieldset class="form-group"> </fieldset>
{{ form.notes.label_tag }} <fieldset class="form-group">
{% render_field form.notes class="form-control form-control-sm" rows="2" %} {{ form.notes.label_tag }}
</fieldset> {% render_field form.notes class="form-control form-control-sm" rows="2" %}
<input type="submit" class="btn btn-primary" value="Submit"> </fieldset>
</form> <input type="submit" class="btn btn-primary" value="Submit">
</form>
{{ 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,15 +64,17 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr class="d-none d-md-table-row"> {% if user.is_authenticated %}
<form method="post"> <tr class="d-none d-md-table-row">
{% csrf_token %} <form method="post">
<td> {% render_field form.date type="date" class="form-control" %} </td> {% csrf_token %}
<td> {% render_field form.user class="form-control" style="width: initial;" %} </td> <td> {% render_field form.date type="date" class="form-control" %} </td>
<td> {% render_field form.notes class="form-control form-control-sm" rows="2" %} </td> <td> {% render_field form.user class="form-control" style="width: initial;" %} </td>
<td> <input type="submit" class="btn btn-primary" value="Submit"> </td> <td> {% render_field form.notes class="form-control form-control-sm" rows="2" %} </td>
</form> <td> <input type="submit" class="btn btn-primary" value="Submit"> </td>
</tr> </form>
</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':
event = Event(task=task) if request.user.is_authenticated:
form = EventForm(request.POST, instance=event) event = Event(task=task)
if form.is_valid(): form = EventForm(request.POST, instance=event)
form.save() if form.is_valid():
pass form.save()
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(),