Skip to content

Commit

Permalink
add comment to availability
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-Janssen-dev committed Oct 1, 2024
1 parent a9ee130 commit 1ae6f44
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 17 deletions.
22 changes: 22 additions & 0 deletions alexia/apps/scheduling/migrations/0021_auto_20241001_1357.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 2.2.28 on 2024-10-01 14:38

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('scheduling', '0020_availability_position'),
]

operations = [
migrations.AlterModelOptions(
name='availability',
options={'ordering': ['position'], 'verbose_name': 'availability type', 'verbose_name_plural': 'availability types'},
),
migrations.AddField(
model_name='bartenderavailability',
name='comment',
field=models.TextField(blank=True, default='', max_length=100, verbose_name='comment'),
),
]
1 change: 1 addition & 0 deletions alexia/apps/scheduling/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ class BartenderAvailability(models.Model):
verbose_name=_('availability'),
on_delete=models.CASCADE,
)
comment = models.TextField(_('comment'), blank=True, default='', max_length=100)

class Meta:
verbose_name = _('bartender availability')
Expand Down
1 change: 1 addition & 0 deletions alexia/apps/scheduling/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@
# AJAT (Asynchroon Javascript en Tekst... wie gebruikt er nog in
# hemelsnaam XML!?)
url(r'^ajax/bartender_availability/$', views.set_bartender_availability),
url(r'^ajax/bartender_availability/comment/$', views.set_bartender_availability_comment),
]
34 changes: 28 additions & 6 deletions alexia/apps/scheduling/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.contrib.auth.models import User
from django.core.exceptions import PermissionDenied, SuspiciousOperation
from django.db.models import Prefetch, Q
from django.http import HttpResponse, JsonResponse
from django.http import HttpResponse, HttpResponseBadRequest, JsonResponse
from django.shortcuts import get_object_or_404, redirect, render
from django.urls import reverse_lazy, reverse
from django.utils import timezone
Expand Down Expand Up @@ -108,7 +108,7 @@ def event_list_view(request):
availabilities = list(request.organization.availabilities.exclude(nature=Availability.ASSIGNED))
# Net als onze BartenderAvailabilities
bartender_availabilities = BartenderAvailability.objects.filter(
user_id=request.user.pk).values('event_id', 'availability_id')
user_id=request.user.pk).values('event_id', 'availability_id', 'comment')

return render(request, 'scheduling/event_list.html', locals())

Expand Down Expand Up @@ -193,11 +193,11 @@ def get_tenders(self, events):
tenders = []
for tender in tenders_list:
tender_availabilities = [
next((a.availability for a in e.bartender_availabilities.all() if a.user == tender.user), None)
next((a for a in e.bartender_availabilities.all() if a.user == tender.user), None)
for e in events
]
tender_events = [
{'event': event, 'availability': availability}
{'event': event, 'bartender_availability': availability}
for event, availability in zip(events, tender_availabilities)
]
tended = [
Expand Down Expand Up @@ -235,13 +235,13 @@ def get_tender_list(self):
tender_list = []
for tender in tenders:
try:
availability = bas[tender.user].availability
availability = bas[tender.user]
except KeyError:
availability = None

tender_list.append({
'tender': tender,
'availability': availability,
'bartender_availability': availability,
'last_tended': tender.last_tended()
})
return {'tender_list': tender_list}
Expand Down Expand Up @@ -328,6 +328,28 @@ def get_success_url(self):
return super(EventDelete, self).get_success_url()


@login_required
def set_bartender_availability_comment(request):
event = get_object_or_404(Event, pk=request.POST.get('event_id'))

if (request.organization not in event.participants.all()) or \
not request.user.profile.is_tender(request.organization):
raise PermissionDenied

if not (request.method == 'POST' and request.is_ajax()):
return HttpResponseBadRequest("NOTOK")

comment = request.POST.get('comment')
if len(comment) > BartenderAvailability.comment.max_length:
return HttpResponseBadRequest("TOOLONG")
availability = get_object_or_404(BartenderAvailability, event=event, user=request.user)
availability.comment = comment
availability.save()


return render(request, 'scheduling/partials/availability_comment.html', {'e': event, 'ba': availability})


@login_required
def set_bartender_availability(request):
event = get_object_or_404(Event, pk=request.POST.get('event_id'))
Expand Down
28 changes: 28 additions & 0 deletions assets/js/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,32 @@ $.ajaxSetup({
}
});

function resetCommentPrompts() {
$('.bartender_availability_comment').click(function () {
let event_id = $(this).data('event-id');
let comment = prompt('Enter a comment');

if (comment == null || comment == '') {
return;
}

if (comment.length > 100) {
alert('Comment is too long. Maximum 100 characters allowed.');
return;
}

const $this = $(this);

$.post('/scheduling/ajax/bartender_availability/comment/', {
event_id,
comment
}, function (data) {
$this.replaceWith(data);
resetCommentPrompts();
}, "text");
});
}

$(function () {

$('.bartender_availability').change(function () {
Expand All @@ -56,6 +82,8 @@ $(function () {
}, "text");
});

resetCommentPrompts();

$('.dateinput').datepicker({
autoclose: true,
format: dateformats[LOCALE],
Expand Down
9 changes: 6 additions & 3 deletions templates/scheduling/event_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,16 @@ <h3>{% trans 'Bartenders' %}</h3>
{{ obj.tender.user.get_full_name }}
</a>
</td>
<td class="col-xs-6">
<td class="col-xs-2 text-right">
<a href="{% url 'edit-event-bartender-availability' pk=event.pk user_pk=obj.tender.user.pk %}">
<span title="{% trans 'Last tended' %}: {{ obj.last_tended|default:_("Never") }}" class="label label-{{ obj.availability.css_class|default:'default' }}">
{{ obj.availability|default:_("Unknown") }}
<span title="{% trans 'Last tended' %}: {{ obj.last_tended|default:_("Never") }}" class="label label-{{ obj.bartender_availability.availability.css_class|default:'default' }}">
{{ obj.bartender_availability.availability|default:_("Unknown") }}
</span>
</a>
</td>
<td class="col-xs-4">
{{ obj.bartender_availability.comment }}
</td>
</tr>
{% endfor %}
</table>
Expand Down
30 changes: 24 additions & 6 deletions templates/scheduling/event_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,30 @@ <h1>
<td>
{% if not e.is_closed and request.organization in e.participants.all %}
{% if not e.organizer.assigns_tenders or not user in e.get_assigned_bartenders %}
<select class="bartender_availability form-control" data-event-id="{{ e.pk }}">
<option value="-1">---</option>
{% for a in availabilities %}
<option value="{{ a.pk }}"{% for ba in bartender_availabilities %}{% if ba.event_id == e.pk and ba.availability_id == a.pk %} selected="selected"{% endif %}{% endfor %}>{{ a.name }}</option>
{% endfor %}
</select>
{% for ba in bartender_availabilities %}
{% if ba.event_id == e.pk or forloop.last %}
<div style="display: flex; height: 100%; align-items: center">
{% include "scheduling/partials/availability_comment.html" %}
<select class="bartender_availability form-control" data-event-id="{{ e.pk }}">
<option value="-1">---</option>
{% for a in availabilities %}
<option value="{{ a.pk }}"{% if ba.availability_id == a.pk %} selected="selected"{% endif %}>{{ a.name }}</option>
{% endfor %}
</select>
</div>
{% endif %}
{% empty %}
<div style="display: flex; height: 100%; align-items: center">
{% include "scheduling/partials/availability_comment.html" %}
<select class="bartender_availability form-control" data-event-id="{{ e.pk }}">
<option value="-1">---</option>
{% for a in availabilities %}
<option value="{{ a.pk }}"{% if ba.availability_id == a.pk %} selected="selected"{% endif %}>{{ a.name }}</option>
{% endfor %}
</select>
</div>
{% endfor %}

{% endif %}
{% endif %}
</td>
Expand Down
4 changes: 2 additions & 2 deletions templates/scheduling/event_matrix.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ <h2>
</span>
</td>
{% for e in t.events %}
<td class="text-center">
<td title="{{e.bartender_availability.comment}}" class="text-center" style="display: flex; height: 100%; align-items: center; justify-content: center">
<a href="{% url 'edit-event-bartender-availability' pk=e.event.pk user_pk=t.tender.user.pk %}">
<span class="label label-{{ e.availability.css_class|default:'default' }}">{{ e.availability|default:'?' }}</span>
<span style="border-width: thick" class="label label-{{ e.bartender_availability.availability.css_class|default:'default' }}">{{ e.bartender_availability.availability|default:'?' }}</span>
</a>
</td>
{% endfor %}
Expand Down
3 changes: 3 additions & 0 deletions templates/scheduling/partials/availability_comment.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div title="{{ba.comment}}" class="bartender_availability_comment" style="margin-right: 1rem" data-event-id="{{e.pk}}">
<span class="glyphicon glyphicon-comment"></span>
</div>

0 comments on commit 1ae6f44

Please sign in to comment.