Add ability to delete events and teams. Fixes #14

This commit is contained in:
Michael Hall 2018-01-26 22:38:11 -05:00
parent 37f4ca7869
commit 4e031ae07d
8 changed files with 121 additions and 16 deletions

View file

@ -1,5 +1,5 @@
from django.utils.safestring import mark_safe
from django.forms import ModelForm, Field, SplitDateTimeWidget, DateInput, MultiWidget, Select
from django import forms
from django.forms.widgets import TextInput, Media
from django.utils.translation import ugettext_lazy as _
@ -52,7 +52,7 @@ class Lookup(TextInput):
else:
return mark_safe('<option value="">--------</option>')
class DateWidget(DateInput):
class DateWidget(forms.DateInput):
"""A more-friendly date widget with a p% if widget.value != None %} value="{{ widget.value|stringformat:'s' }}"{% endif %op-up calendar.
"""
template_name = 'forms/widgets/date.html'
@ -68,7 +68,7 @@ class DateWidget(DateInput):
super(DateWidget, self).__init__(attrs=attrs)
class TimeWidget(MultiWidget):
class TimeWidget(forms.MultiWidget):
"""A more-friendly time widget.
"""
def __init__(self, attrs=None):
@ -81,9 +81,9 @@ class TimeWidget(MultiWidget):
attrs['class'] = 'time'
widgets = (
Select(attrs=attrs, choices=[(i + 1, "%02d" % (i + 1)) for i in range(0, 12)]),
Select(attrs=attrs, choices=[(i, "%02d" % i) for i in range(00, 60, 15)]),
Select(attrs=attrs, choices=[('AM', _('AM')), ('PM', _('PM'))])
forms.Select(attrs=attrs, choices=[(i + 1, "%02d" % (i + 1)) for i in range(0, 12)]),
forms.Select(attrs=attrs, choices=[(i, "%02d" % i) for i in range(00, 60, 15)]),
forms.Select(attrs=attrs, choices=[('AM', _('AM')), ('PM', _('PM'))])
)
super(TimeWidget, self).__init__(widgets, attrs)
@ -120,7 +120,7 @@ class TimeWidget(MultiWidget):
rendered_widgets[0], rendered_widgets[1], rendered_widgets[2]
)
class DateTimeWidget(SplitDateTimeWidget):
class DateTimeWidget(forms.SplitDateTimeWidget):
"""
A more-friendly date/time widget.
"""
@ -146,7 +146,7 @@ class DateTimeWidget(SplitDateTimeWidget):
values = super(DateTimeWidget, self).value_from_datadict(data, files, name)
return ' '.join(values)
class TeamForm(ModelForm):
class TeamForm(forms.ModelForm):
class Meta:
model = Team
fields = ['name', 'description', 'country', 'spr', 'city', 'web_url', 'tz']
@ -157,7 +157,7 @@ class TeamForm(ModelForm):
}
raw_id_fields = ('country','spr','city')
class NewTeamForm(ModelForm):
class NewTeamForm(forms.ModelForm):
class Meta:
model = Team
fields = ['name', 'description', 'city', 'web_url', 'tz']
@ -168,7 +168,10 @@ class NewTeamForm(ModelForm):
}
raw_id_fields = ('city')
class TeamEventForm(ModelForm):
class DeleteTeamForm(forms.Form):
confirm = forms.BooleanField(label="Yes, delete team", required=True)
class TeamEventForm(forms.ModelForm):
class Meta:
model = Event
fields = ['name', 'start_time', 'end_time', 'summary', 'place', 'web_url', 'announce_url', 'tags']
@ -178,7 +181,7 @@ class TeamEventForm(ModelForm):
'end_time': DateTimeWidget
}
class NewTeamEventForm(ModelForm):
class NewTeamEventForm(forms.ModelForm):
class Meta:
model = Event
fields = ['name', 'start_time', 'end_time', 'summary', 'place']
@ -188,7 +191,10 @@ class NewTeamEventForm(ModelForm):
'end_time': DateTimeWidget
}
class NewPlaceForm(ModelForm):
class DeleteEventForm(forms.Form):
confirm = forms.BooleanField(label="Yes, delete event", required=True)
class NewPlaceForm(forms.ModelForm):
class Meta:
model = Place
fields = ['name', 'address', 'city', 'longitude', 'latitude', 'place_url', 'tz']

View file

@ -0,0 +1,15 @@
{% extends "get_together/base.html" %}
{% block content %}
<h2>Confirm deletion</h2>
Are you sure you want to delete <strong>{{event.name}}</strong>?
<form action="{% url "delete-event" event.id %}" method="post">
{% csrf_token %}
<div class="form-group">
{{ delete_form }}
<br />
<button type="submit" class="btn btn-danger">Delete Event</button>
</div>
</form>
{% endblock %}

View file

@ -4,12 +4,11 @@
<h2>Updating {{event.name}}</h2>
<form action="{% url "edit-event" event.id%}" method="post">
{% csrf_token %}
<div class="form-group">
{% include "events/event_form.html" %}
<br />
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
<a href="{% url 'delete-event' event.id %}" class="btn btn-danger">Delete</a>
{% endblock %}
{% block javascript %}

View file

@ -0,0 +1,20 @@
{% extends "get_together/base.html" %}
{% block content %}
<h2>Confirm deletion</h2>
Are you sure you want to delete <strong>{{team.name}}</strong>?
{% if team.event_set.count > 0 %}
<div class="alerts">
<div class="alert alert-danger">This team has {{ team.event_set.count }} events!</div>
</div>
{% endif %}
<form action="{% url "delete-team" team.id %}" method="post">
{% csrf_token %}
<div class="form-group">
{{ delete_form }}
<br />
<button type="submit" class="btn btn-danger">Delete Team</button>
</div>
</form>
{% endblock %}

View file

@ -8,6 +8,7 @@
<br />
<button type="submit" class="btn btn-primary">Save</button>
</form>
<a href="{% url 'delete-team' team.id %}" class="btn btn-danger">Delete</a>
{% endblock %}
{% block javascript %}

View file

@ -40,9 +40,12 @@ urlpatterns = [
path('team/<int:team_id>/+edit/', views.edit_team, name='edit-team'),
path('team/<int:team_id>/+join/', event_views.join_team, name='join-team'),
path('team/<int:team_id>/+leave/', event_views.leave_team, name='leave-team'),
path('team/<int:team_id>/+delete/', views.delete_team, name='delete-team'),
path('team/<int:team_id>/+create-event/', views.create_event, name='create-event'),
path('events/<int:event_id>/+edit/', views.edit_event, name='edit-event'),
path('events/<int:event_id>/+attend/', event_views.attend_event, name='attend-event'),
path('events/<int:event_id>/+delete/', views.delete_event, name='delete-event'),
path('events/<int:event_id>/<str:event_slug>/', views.show_event, name='show-event'),
path('places/', views.places_list, name='places'),

View file

@ -6,7 +6,7 @@ from django.shortcuts import render, redirect
from django.http import HttpResponse, JsonResponse
from events.models.profiles import Team, UserProfile, Member
from events.forms import TeamForm, NewTeamForm, TeamEventForm, NewTeamEventForm, NewPlaceForm
from events.forms import TeamEventForm, NewTeamEventForm, DeleteEventForm
from events.models.events import Event, Place, Attendee
@ -93,3 +93,35 @@ def edit_event(request, event_id):
else:
return redirect('home')
def delete_event(request, event_id):
event = Event.objects.get(id=event_id)
if not request.user.profile.can_edit_event(event):
messages.add_message(request, messages.WARNING, message=_('You can not make changes to this event.'))
return redirect(event.get_absolute_url())
if request.method == 'GET':
form = DeleteEventForm()
context = {
'team': event.team,
'event': event,
'delete_form': form,
}
return render(request, 'get_together/events/delete_event.html', context)
elif request.method == 'POST':
form = DeleteEventForm(request.POST)
if form.is_valid() and form.cleaned_data['confirm']:
team_id = event.team_id
event.delete()
return redirect('show-team', team_id)
else:
context = {
'team': event.team,
'event': event,
'delete_form': form,
}
return render(request, 'get_together/events/delete_event.html', context)
else:
return redirect('home')

View file

@ -6,7 +6,7 @@ from django.shortcuts import render, redirect
from django.http import HttpResponse, JsonResponse
from events.models.profiles import Team, UserProfile, Member
from events.forms import TeamForm, NewTeamForm, TeamEventForm, NewTeamEventForm, NewPlaceForm
from events.forms import TeamForm, NewTeamForm, DeleteTeamForm
from events.models.events import Event, Place, Attendee
@ -90,3 +90,32 @@ def edit_team(request, team_id):
else:
return redirect('home')
def delete_team(request, team_id):
team = Team.objects.get(id=team_id)
if not request.user.profile.can_edit_team(team):
messages.add_message(request, messages.WARNING, message=_('You can not make changes to this team.'))
return redirect('show-team', team_id)
if request.method == 'GET':
form = DeleteTeamForm()
context = {
'team': team,
'delete_form': form,
}
return render(request, 'get_together/teams/delete_team.html', context)
elif request.method == 'POST':
form = DeleteTeamForm(request.POST)
if form.is_valid() and form.cleaned_data['confirm']:
team.delete()
return redirect('teams')
else:
context = {
'team': team,
'delete_form': form,
}
return render(request, 'get_together/teams/delete_team.html', context)
else:
return redirect('home')