Enable editing of shifts. With timezone rabbit hole.
This commit is contained in:
parent
102dfa7330
commit
c103400046
|
@ -309,6 +309,10 @@ class TeamTask(CampRelatedModel):
|
|||
|
||||
|
||||
class TeamShift(CampRelatedModel):
|
||||
|
||||
class Meta:
|
||||
ordering = ("shift_range",)
|
||||
|
||||
team = models.ForeignKey(
|
||||
'teams.Team',
|
||||
related_name='shifts',
|
||||
|
|
|
@ -5,6 +5,13 @@
|
|||
|
||||
{% block content %}
|
||||
|
||||
<a class="btn btn-info"
|
||||
href="{% url 'teams:shift_list' camp_slug=camp.slug team_slug=team.slug %}">
|
||||
<i class="fas fa-chevron-left"></i> Cancel
|
||||
</a>
|
||||
|
||||
<hr />
|
||||
|
||||
{% if form.errors %}
|
||||
{{ form.errors }}
|
||||
{% endif %}
|
||||
|
@ -13,7 +20,7 @@
|
|||
{% csrf_token %}
|
||||
{% bootstrap_form form %}
|
||||
<button type="submit" class="btn btn-success">
|
||||
Create
|
||||
{% if object.pk %}Update{% else %}Create{% endif %}
|
||||
</button>
|
||||
</form>
|
||||
|
||||
|
|
|
@ -37,6 +37,8 @@
|
|||
People required
|
||||
<th>
|
||||
People
|
||||
<th>
|
||||
Actions
|
||||
{% endifchanged %}
|
||||
|
||||
<tr>
|
||||
|
@ -48,6 +50,16 @@
|
|||
{{ shift.people_required }}
|
||||
<td>
|
||||
{{ shift.team_members }}
|
||||
<td>
|
||||
{% if request.user in team.responsible_members.all %}
|
||||
<a class="btn btn-info"
|
||||
href="{% url 'teams:shift_update' camp_slug=camp.slug team_slug=team.slug pk=shift.pk %}">
|
||||
<i class="fas fa-edit"></i> Edit
|
||||
</a>
|
||||
<a class="btn btn-danger" href="">
|
||||
<i class="fas fa-trash"></i> Delete
|
||||
</a>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
</table>
|
||||
{% endblock %}
|
||||
|
|
|
@ -21,6 +21,7 @@ from teams.views.tasks import (
|
|||
from teams.views.shifts import (
|
||||
ShiftListView,
|
||||
ShiftCreateView,
|
||||
ShiftUpdateView,
|
||||
)
|
||||
|
||||
app_name = 'teams'
|
||||
|
@ -130,6 +131,11 @@ urlpatterns = [
|
|||
ShiftCreateView.as_view(),
|
||||
name="shift_create"
|
||||
),
|
||||
path(
|
||||
'<int:pk>/',
|
||||
ShiftUpdateView.as_view(),
|
||||
name="shift_update"
|
||||
)
|
||||
]))
|
||||
]),
|
||||
),
|
||||
|
|
|
@ -4,6 +4,7 @@ from django import forms
|
|||
from django.contrib.postgres.forms.ranges import RangeWidget
|
||||
from django.utils import timezone
|
||||
from django.urls import reverse
|
||||
|
||||
from psycopg2.extras import DateTimeTZRange
|
||||
|
||||
from camps.mixins import CampViewMixin
|
||||
|
@ -61,8 +62,26 @@ class ShiftForm(forms.ModelForm):
|
|||
'people_required'
|
||||
]
|
||||
|
||||
def __init__(self, instance=None, **kwargs):
|
||||
super().__init__(instance=instance, **kwargs)
|
||||
if instance:
|
||||
current_tz = timezone.get_current_timezone()
|
||||
|
||||
lower = instance.shift_range.lower.astimezone(current_tz)
|
||||
upper = instance.shift_range.upper.astimezone(current_tz)
|
||||
|
||||
from_date = lower.strftime('%Y-%m-%d')
|
||||
from_time = lower.strftime('%H:%M')
|
||||
to_date = upper.strftime('%Y-%m-%d')
|
||||
to_time = upper.strftime('%H:%M')
|
||||
|
||||
self.fields['from_date'].initial = from_date
|
||||
self.fields['from_time'].initial = from_time
|
||||
self.fields['to_date'].initial = to_date
|
||||
self.fields['to_time'].initial = to_time
|
||||
|
||||
from_date = forms.DateField(
|
||||
help_text="Format is YYYY-MM-DD - ie. 2018-08-15"
|
||||
help_text="Format is YYYY-MM-DD"
|
||||
)
|
||||
|
||||
from_time = forms.ChoiceField(
|
||||
|
@ -70,7 +89,7 @@ class ShiftForm(forms.ModelForm):
|
|||
)
|
||||
|
||||
to_date = forms.DateField(
|
||||
help_text="Format is YYYY-MM-DD - ie. 2018-08-15"
|
||||
help_text="Format is YYYY-MM-DD"
|
||||
)
|
||||
|
||||
to_time = forms.ChoiceField(
|
||||
|
@ -87,10 +106,18 @@ class ShiftForm(forms.ModelForm):
|
|||
self.cleaned_data['to_time']
|
||||
)
|
||||
datetime_format = '%Y-%m-%d %H:%M'
|
||||
self.instance.shift_range = DateTimeTZRange(
|
||||
timezone.datetime.strptime(from_string, datetime_format),
|
||||
timezone.datetime.strptime(to_string, datetime_format)
|
||||
current_timezone = timezone.get_current_timezone()
|
||||
lower = (
|
||||
timezone.datetime
|
||||
.strptime(from_string, datetime_format)
|
||||
.astimezone(current_timezone)
|
||||
)
|
||||
upper = (
|
||||
timezone.datetime
|
||||
.strptime(to_string, datetime_format)
|
||||
.astimezone(current_timezone)
|
||||
)
|
||||
self.instance.shift_range = DateTimeTZRange(lower, upper)
|
||||
return super().save(commit=commit)
|
||||
|
||||
|
||||
|
@ -113,8 +140,28 @@ class ShiftCreateView(LoginRequiredMixin, CampViewMixin, CreateView):
|
|||
kwargs=self.kwargs
|
||||
)
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
context['team'] = Team.objects.get(
|
||||
camp=self.camp,
|
||||
slug=self.kwargs['team_slug']
|
||||
)
|
||||
return context
|
||||
|
||||
|
||||
class ShiftUpdateView(LoginRequiredMixin, CampViewMixin, UpdateView):
|
||||
model = TeamShift
|
||||
template_name = "shifts/shift_form.html"
|
||||
form_class = ShiftForm
|
||||
|
||||
def get_success_url(self):
|
||||
self.kwargs.pop('pk')
|
||||
return reverse(
|
||||
'teams:shift_list',
|
||||
kwargs=self.kwargs
|
||||
)
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
context['team'] = self.object.team
|
||||
return context
|
||||
|
|
Loading…
Reference in a new issue