Add ability to edit teams and events

This commit is contained in:
Michael Hall 2018-01-21 12:09:18 -05:00
parent dd9a836a66
commit 54adf71434
10 changed files with 181 additions and 7 deletions

View file

@ -38,7 +38,6 @@ class Lookup(TextInput):
self.source = source
self.key = key
self.label = label
self.name = 'place'
def get_context(self, name, value, attrs):
context = super().get_context(name, value, attrs)
@ -47,8 +46,14 @@ class Lookup(TextInput):
context['widget']['label'] = self.label
return context
def format_value(self, value):
if value is not None:
return mark_safe('<option value="%s">%s</option>' % (value, _('No change')))
else:
return mark_safe('<option value="">--------</option>')
class DateWidget(DateInput):
"""A more-friendly date widget with a pop-up calendar.
"""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'
def __init__(self, attrs=None):
@ -144,7 +149,13 @@ class DateTimeWidget(SplitDateTimeWidget):
class TeamForm(ModelForm):
class Meta:
model = Team
fields = '__all__'
fields = ['name', 'country', 'spr', 'city', 'web_url', 'tz']
widgets = {
'country': Lookup(source='/api/country/', label='name'),
'spr': Lookup(source='/api/spr/', label='name'),
'city': Lookup(source='/api/cities/', label='name'),
}
raw_id_fields = ('country','spr','city')
class NewTeamForm(ModelForm):
class Meta:
@ -162,9 +173,9 @@ class TeamEventForm(ModelForm):
model = Event
fields = ['name', 'start_time', 'end_time', 'summary', 'place', 'web_url', 'announce_url', 'tags']
widgets = {
'country': Lookup(source='/api/country/', label='name'),
'spr': Lookup(source='/api/spr/', label='name'),
'city': Lookup(source='/api/cities/', label='name'),
'place': Lookup(source='/api/places/', label='name'),
'start_time': DateTimeWidget,
'end_time': DateTimeWidget
}
class NewTeamEventForm(ModelForm):

View file

@ -4,6 +4,9 @@
<td><b>Time:</b></td><td>{{ event.start_time }} - {{ event.end_time }}</td>
</tr><tr>
<td><b>Place:</b></td><td>{{ event.place }}</td>
<tr>
<td><b>Host:</b></td><td>{{ event.created_by }}</td>
</tr><tr>
{% if event.web_url %}
</tr><tr>
<td><b>Website:</b></td><td><a href="{{ event.web_url }}" target="_blank">{{ event.web_url }}</a></td>

View file

@ -1,5 +1,5 @@
<select autocomplete="false" id="{{ widget.name }}_select" name="{{ widget.name }}"{% include "django/forms/widgets/attrs.html" %} style="width: 300px">
<option value="">--------</option>
{{ widget.value }}
</select>
<input autocomplete="false" id="{{ widget.name }}_search" name="{{ widget.name }}_search" />

View file

@ -19,6 +19,9 @@ body {
.starter-template {
padding: 3rem 1.5rem;
text-align: center;
}
form {
display: inline;
}
</style>
</head>

View file

@ -0,0 +1,43 @@
{% extends "get_together/base.html" %}
{% block content %}
<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>
{% endblock %}
{% block javascript %}
<script type="text/javascript">
$(document).ready(function(){
{% with event_form.fields.place.widget as widget %}
$("#{{ widget.name }}_search").keyup(function() {
var searchText = this.value;
$.getJSON("{{ widget.source }}?q="+searchText, function(data) {
var selectField = $("#{{ widget.name }}_select");
selectField.empty();
$.each(data, function(){
selectField.append('<option value="'+ this.{{ widget.key }} +'">'+ this.name+' '+this.city + '</option>')
});
});
});
{% endwith %}
$.datepicker.setDefaults({
showOn: 'focus',
dateFormat: 'yy-mm-dd',
});
$("#id_start_time_0").datepicker({altField: "#id_end_time_0", altFormat: "yy-mm-dd"});
$("#id_end_time_0").datepicker();
});
</script>
{% endblock %}

View file

@ -0,0 +1,51 @@
{% extends "get_together/base.html" %}
{% block content %}
<h2>Update {{team.name}}</h2>
<form action="{% url "create-team" %}" method="post">
{% csrf_token %}
{% include "events/team_form.html" %}
<br />
<button type="submit" class="btn btn-primary">Save</button>
</form>
{% endblock %}
{% block javascript %}
<script type="text/javascript">
$(document).ready(function(){
$("#country_search").keyup(function() {
var searchText = this.value;
$.getJSON("/api/countries/?q="+searchText, function(data) {
var selectField = $("#country_select");
selectField.empty();
$.each(data, function(){
selectField.append('<option value="'+ this.id +'">'+ this.name + '</option>')
});
});
});
$("#spr_search").keyup(function() {
var searchText = this.value;
var country_id = $("#country_select")[0].selectedOptions[0].value;
$.getJSON("/api/spr/?q="+searchText+"&country="+country_id, function(data) {
var selectField = $("#spr_select");
selectField.empty();
$.each(data, function(){
selectField.append('<option value="'+ this.id +'">'+ this.name + '</option>')
});
});
});
$("#city_search").keyup(function() {
var searchText = this.value;
var spr_id = $("#spr_select")[0].selectedOptions[0].value;
$.getJSON("/api/cities/?q="+searchText+"&spr="+spr_id, function(data) {
var selectField = $("#city_select");
selectField.empty();
$.each(data, function(){
selectField.append('<option value="'+ this.id +'">'+ this.name + '</option>')
});
});
});
});
</script>
{% endblock %}

View file

@ -6,5 +6,10 @@
<h4>Hosted by <a href="{% url "show-team" team.id %}">{{ team.name }}</a></h4>
{% include "events/event_details.html" %}
{% if request.user.profile == event.created_by %}
<form action="{% url 'edit-event' event.id %}" method="get">
<button type="submit" class="btn btn-secondary">Edit Event</button>
</form>
{% endif %}
{% endblock %}

View file

@ -10,6 +10,10 @@
<form action="{% url 'create-event' team.id %}" method="get">
<button type="submit" class="btn btn-primary">Plan a Get Together</button>
</form>
<form action="{% url 'edit-team' team.id %}" method="get">
<button type="submit" class="btn btn-secondary">Edit Team</button>
</form>
{% endif %}
{% endblock %}

View file

@ -31,7 +31,9 @@ urlpatterns = [
path('create-team/', views.create_team, name='create-team'),
path('teams/', views.teams_list, name='teams'),
path('team/<int:team_id>/', views.show_team, name='show-team'),
path('team/<int:team_id>/edit/', views.edit_team, name='edit-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>/<str:event_slug>/', views.show_event, name='show-event'),
path('places/', views.places_list, name='places'),

View file

@ -51,6 +51,32 @@ def create_team(request, *args, **kwargs):
else:
return redirect('home')
def edit_team(request, team_id):
team = Team.objects.get(id=team_id)
if request.method == 'GET':
form = TeamForm(instance=team)
context = {
'team': team,
'team_form': form,
}
return render(request, 'get_together/edit_team.html', context)
elif request.method == 'POST':
form = TeamForm(request.POST, instance=team)
if form.is_valid:
new_team = form.save()
new_team.owner_profile = request.user.profile
new_team.save()
return redirect('show-team', team_id=new_team.pk)
else:
context = {
'team': team,
'team_form': form,
}
return render(request, 'get_together/edit_team.html', context)
else:
return redirect('home')
def teams_list(request, *args, **kwargs):
teams = Team.objects.all()
context = {
@ -69,6 +95,32 @@ def show_team(request, team_id, *args, **kwargs):
}
return render(request, 'get_together/show_team.html', context)
def edit_event(request, event_id):
event = Event.objects.get(id=event_id)
if request.method == 'GET':
form = TeamEventForm(instance=event)
context = {
'team': event.team,
'event': event,
'event_form': form,
}
return render(request, 'get_together/edit_event.html', context)
elif request.method == 'POST':
form = TeamEventForm(request.POST,instance=event)
if form.is_valid:
new_event = form.save()
return redirect(new_event.get_absolute_url())
else:
context = {
'team': event.team,
'event': event,
'event_form': form,
}
return render(request, 'get_together/edit_event.html', context)
else:
return redirect('home')
def create_event(request, team_id):
team = Team.objects.get(id=team_id)
if request.method == 'GET':