diff --git a/events/forms.py b/events/forms.py index 2ab6bc7..82dd983 100644 --- a/events/forms.py +++ b/events/forms.py @@ -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('') -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'] diff --git a/get_together/templates/get_together/events/delete_event.html b/get_together/templates/get_together/events/delete_event.html new file mode 100644 index 0000000..69af713 --- /dev/null +++ b/get_together/templates/get_together/events/delete_event.html @@ -0,0 +1,15 @@ +{% extends "get_together/base.html" %} + +{% block content %} +

Confirm deletion

+Are you sure you want to delete {{event.name}}? +
+{% csrf_token %} +
+{{ delete_form }} +
+ +
+
+{% endblock %} + diff --git a/get_together/templates/get_together/events/edit_event.html b/get_together/templates/get_together/events/edit_event.html index 6687ed6..3625144 100644 --- a/get_together/templates/get_together/events/edit_event.html +++ b/get_together/templates/get_together/events/edit_event.html @@ -4,12 +4,11 @@

Updating {{event.name}}

{% csrf_token %} -
{% include "events/event_form.html" %}
-
+Delete {% endblock %} {% block javascript %} diff --git a/get_together/templates/get_together/teams/delete_team.html b/get_together/templates/get_together/teams/delete_team.html new file mode 100644 index 0000000..7324314 --- /dev/null +++ b/get_together/templates/get_together/teams/delete_team.html @@ -0,0 +1,20 @@ +{% extends "get_together/base.html" %} + +{% block content %} +

Confirm deletion

+Are you sure you want to delete {{team.name}}? +{% if team.event_set.count > 0 %} +
+
This team has {{ team.event_set.count }} events!
+
+{% endif %} +
+{% csrf_token %} +
+{{ delete_form }} +
+ +
+
+{% endblock %} + diff --git a/get_together/templates/get_together/teams/edit_team.html b/get_together/templates/get_together/teams/edit_team.html index 9ef9c65..5fff5f1 100644 --- a/get_together/templates/get_together/teams/edit_team.html +++ b/get_together/templates/get_together/teams/edit_team.html @@ -8,6 +8,7 @@
+Delete {% endblock %} {% block javascript %} diff --git a/get_together/urls.py b/get_together/urls.py index 9ef3018..f6c6fab 100644 --- a/get_together/urls.py +++ b/get_together/urls.py @@ -40,9 +40,12 @@ urlpatterns = [ path('team//+edit/', views.edit_team, name='edit-team'), path('team//+join/', event_views.join_team, name='join-team'), path('team//+leave/', event_views.leave_team, name='leave-team'), + path('team//+delete/', views.delete_team, name='delete-team'), + path('team//+create-event/', views.create_event, name='create-event'), path('events//+edit/', views.edit_event, name='edit-event'), path('events//+attend/', event_views.attend_event, name='attend-event'), + path('events//+delete/', views.delete_event, name='delete-event'), path('events///', views.show_event, name='show-event'), path('places/', views.places_list, name='places'), diff --git a/get_together/views/events.py b/get_together/views/events.py index 3827753..21bbb08 100644 --- a/get_together/views/events.py +++ b/get_together/views/events.py @@ -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') + + diff --git a/get_together/views/teams.py b/get_together/views/teams.py index 734c8a9..7082a7c 100644 --- a/get_together/views/teams.py +++ b/get_together/views/teams.py @@ -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') + +