Skip to content

Commit 24752b7

Browse files
authored
#A03. As Admin, I would like to update existing Hackathon events. (#106)
1 parent 121d053 commit 24752b7

5 files changed

Lines changed: 94 additions & 5 deletions

File tree

hackathon/forms.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class HackathonForm(forms.ModelForm):
4444
input_formats=['%d/%m/%Y %H:%M'],
4545
required=True,
4646
widget=forms.DateTimeInput(
47+
format='%d/%m/%Y %H:%M',
4748
attrs={
4849
'placeholder': 'DD/MM/YYYY HH:MM',
4950
'autocomplete': 'off'
@@ -55,6 +56,7 @@ class HackathonForm(forms.ModelForm):
5556
input_formats=['%d/%m/%Y %H:%M'],
5657
required=True,
5758
widget=forms.DateTimeInput(
59+
format='%d/%m/%Y %H:%M',
5860
attrs={
5961
'placeholder': 'DD/MM/YYYY HH:MM',
6062
'autocomplete': 'off'

hackathon/templates/hackathon/create-event.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
<section class="h-100">
1515
<div class="row mb-3">
1616
<div class="col-12">
17-
<h1>Create Hackathon</h1>
17+
{% if hackathon_id %}
18+
<h1>Edit Hackathon</h1>
19+
{% else %}
20+
<h1>Create Hackathon</h1>
21+
{% endif %}
1822
</div>
1923
</div>
2024

hackathon/templates/hackathon/includes/hackathon_card.html

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,26 @@ <h6 class="card-subtitle mb-2 text-muted">{{ hackathon.start_date }} - {{ hackat
88
{% endif %}
99
<a href="#" class="btn btn-ci mr-3">Read More</a>
1010

11-
<!-- Delete button, only visible for admin users -->
11+
<!-- Edit and Delete buttons, only visible for admin users -->
1212
{% if user.is_authenticated and user.is_superuser %}
13+
<!-- Warn user if hackathon is ongoing or ended before loading edit form - trigger edit modal -->
14+
{% if hackathon.start_date|date:"jS F Y H:i" <= today|date:"jS F Y H:i" %}
15+
<button class="btn btn-ci mr-3" type="button" data-toggle="modal" data-target="#edit-modal-{{ hackathon.id }}">
16+
Edit
17+
</button>
18+
{% else %}
19+
<a href="{% url 'hackathon:update_hackathon' hackathon.id %}" class="btn btn-ci mr-3">
20+
Edit
21+
</a>
22+
{% endif %}
1323
<button class="btn btn-ci" type="button" data-toggle="modal" data-target="#delete-modal-{{ hackathon.id }}">
1424
Delete Event
1525
</button>
1626
{% endif %}
1727
</div>
1828
</article>
1929

20-
<!-- Modal - pass in hackathon id to modal id to enable rendering details and deleting correct hackathon event -->
30+
<!-- Delete modal - pass in hackathon id to modal id to enable rendering details and deleting correct hackathon event -->
2131
<div class="modal fade" id="delete-modal-{{ hackathon.id }}" tabindex="-1" aria-labelledby="delete-modal"
2232
aria-hidden="true">
2333
<div class="modal-dialog">
@@ -43,3 +53,31 @@ <h4 class="modal-title">Delete Hackathon</h4>
4353
</div>
4454
</div>
4555
</div>
56+
57+
<!-- Edit modal - pass in hackathon id to modal id to enable rendering details and editing correct hackathon event -->
58+
<div class="modal fade" id="edit-modal-{{ hackathon.id }}" tabindex="-1" aria-labelledby="edit-modal"
59+
aria-hidden="true">
60+
<div class="modal-dialog">
61+
<div class="modal-content">
62+
<div class="modal-header">
63+
<h4 class="modal-title">Delete Hackathon</h4>
64+
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
65+
<span aria-hidden="true">&times;</span>
66+
</button>
67+
</div>
68+
<div class="modal-body">
69+
<!-- Warn user if hackathon has started and is in progress - dates need to be formatted to work correctly -->
70+
{% if hackathon.start_date|date:"jS F Y H:i" <= today|date:"jS F Y H:i" and hackathon.end_date|date:"jS F Y H:i" >= today|date:"jS F Y H:i" %}
71+
{{ hackathon.display_name }} is currently ongoing, do you really want to update it?
72+
{% else %}
73+
<!-- Warn user if hackathon has ended - dates need to be formatted to work correctly -->
74+
{{ hackathon.display_name }} has ended, do you really want to update it?
75+
{% endif %}
76+
</div>
77+
<div class="modal-footer">
78+
<button type="button" class="btn btn-ci mr-3" data-dismiss="modal">No</button>
79+
<a href="{% url 'hackathon:update_hackathon' hackathon.id %}" type="button" class="btn btn-ci">Yes</a>
80+
</div>
81+
</div>
82+
</div>
83+
</div>

hackathon/urls.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
from django.urls import path
22

3-
from .views import HackathonListView, create_hackathon, delete_hackathon
3+
from .views import (
4+
HackathonListView, create_hackathon, update_hackathon, delete_hackathon
5+
)
46

57
urlpatterns = [
68
path('', HackathonListView.as_view(), name='hackathon-list'),
79
path("create_hackathon", create_hackathon, name='create_hackathon'),
10+
path("<int:hackathon_id>/update_hackathon", update_hackathon, name="update_hackathon"),
811
path("<int:hackathon_id>/delete_hackathon", delete_hackathon, name="delete_hackathon"),
912
]

hackathon/views.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,49 @@ def create_hackathon(request):
6767
messages.success(request, 'Thanks for submitting a new Hackathon event!')
6868
return redirect("hackathon:hackathon-list")
6969

70-
pass
70+
71+
@login_required
72+
def update_hackathon(request, hackathon_id):
73+
""" Allow users to edit hackathon event """
74+
75+
# Redirect user if they are not admin
76+
if not request.user.is_superuser:
77+
return redirect("hackathon:hackathon-list")
78+
79+
hackathon = get_object_or_404(Hackathon, pk=hackathon_id)
80+
if request.method == 'GET':
81+
form = HackathonForm(instance=hackathon)
82+
83+
context = {
84+
"form": form,
85+
"hackathon_id": hackathon_id,
86+
}
87+
88+
return render(request, "hackathon/create-event.html", context)
89+
90+
else:
91+
form = HackathonForm(request.POST, instance=hackathon)
92+
# Convert start and end date strings to datetime and validate
93+
start_date = datetime.strptime(request.POST.get('start_date'), '%d/%m/%Y %H:%M')
94+
end_date = datetime.strptime(request.POST.get('end_date'), '%d/%m/%Y %H:%M')
95+
now = datetime.now()
96+
97+
# Ensure start_date is a day in the future for hackathons that haven't started yet
98+
if hackathon.start_date.date() > now.date() >= start_date.date():
99+
messages.error(request, 'The start date must be a date in the future.')
100+
return redirect("hackathon:update_hackathon", hackathon_id)
101+
102+
# Ensure end_date is after start_date
103+
if end_date <= start_date:
104+
messages.error(request, 'The end date must be at least one day after the start date.')
105+
return redirect("hackathon:update_hackathon", hackathon_id)
106+
107+
# Submit form and save record
108+
if form.is_valid():
109+
form.instance.updated = now
110+
form.save()
111+
messages.success(request, f'Thanks, {hackathon.display_name} has been successfully updated!')
112+
return redirect("hackathon:hackathon-list")
71113

72114

73115
@login_required

0 commit comments

Comments
 (0)