create and edit tasks

This commit is contained in:
Thomas Steen Rasmussen 2017-11-25 13:02:32 +01:00
parent c20771da8a
commit 4dab2c1a4f
6 changed files with 97 additions and 38 deletions

View file

@ -6,6 +6,7 @@ from utils.models import CampRelatedModel
from .email import add_new_membership_email from .email import add_new_membership_email
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.core.urlresolvers import reverse_lazy
import logging import logging
logger = logging.getLogger("bornhack.%s" % __name__) logger = logging.getLogger("bornhack.%s" % __name__)
@ -140,14 +141,16 @@ class TeamTask(CampRelatedModel):
ordering = ['name'] ordering = ['name']
unique_together = (('name', 'team'), ('slug', 'team')) unique_together = (('name', 'team'), ('slug', 'team'))
def get_absolute_url(self):
return reverse_lazy('teams:task_detail', kwargs={'camp_slug': self.team.camp.slug, 'team_slug': self.team.slug, 'slug': self.slug})
@property @property
def camp(self): def camp(self):
return self.team.camp return self.team.camp
def save(self, **kwargs): def save(self, **kwargs):
if not self.slug: if not self.slug:
slug = slugify(self.name) self.slug = slugify(self.name)
self.slug = slug
super().save(**kwargs) super().save(**kwargs)
@property @property

View file

@ -1,20 +0,0 @@
{% extends 'base.html' %}
{% load commonmark %}
{% block title %}
Create Task for {{ task.team.name }} Team
{% endblock %}
{% block content %}
<div class="panel panel-default">
<div class="panel-heading"><h4>Create Task</h4></div>
<div class="panel-body">
<form method="POST">
{{ form }}
<button type="submit" class="btn btn-primary">
</form>
</div>
<div class="panel-footer"><i>This task belongs to the <a href="{% url 'teams:detail' team_slug=team.slug camp_slug=team.camp.slug %}">{{ team.name }} Team</a></i></div>
</div>
{% endblock %}

View file

@ -0,0 +1,36 @@
{% extends 'base.html' %}
{% load commonmark %}
{% load bootstrap3 %}
{% block title %}
{% if form.instance.id %}
Edit Task: {{ form.instance.name }}
{% else %}
Create Task
{% endif %}
for {{ team.name }} Team
{% endblock %}
{% block content %}
<div class="panel panel-default">
<div class="panel-heading">
<h4>
{% if form.instance.id %}
Edit Task: {{ form.instance.name }}
{% else %}
Create Task
{% endif %}
for {{ team.name }} Team
</h4>
</div>
<div class="panel-body">
<form method="POST">
{% csrf_token %}
{% bootstrap_form form %}
<button type="submit" class="btn btn-primary">{% if form.instance.id %}Save{% else %}Create{% endif %}</button>
</form>
</div>
<div class="panel-footer"><i>This task belongs to the <a href="{% url 'teams:detail' team_slug=team.slug camp_slug=team.camp.slug %}">{{ team.name }} Team</a></i></div>
</div>
{% endblock %}

View file

@ -14,7 +14,7 @@ Team: {{ team.name }} | {{ block.super }}
<div class="panel-body"> <div class="panel-body">
{{ team.description|unsafecommonmark }} {{ team.description|unsafecommonmark }}
{% if request.user in team.responsible.all %} {% if request.user in team.responsible.all %}
<a href="{% url 'teams:manage' camp_slug=camp.slug team_slug=team.slug %}" class="btn btn-success">Manage Team</a> <a href="{% url 'teams:manage' camp_slug=camp.slug team_slug=team.slug %}" class="btn btn-primary">Manage Team</a>
{% endif %} {% endif %}
<hr> <hr>
@ -71,6 +71,7 @@ Team: {{ team.name }} | {{ block.super }}
<tr> <tr>
<th>Name</th> <th>Name</th>
<th>Description</th> <th>Description</th>
<th>Action</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@ -78,11 +79,17 @@ Team: {{ team.name }} | {{ block.super }}
<tr> <tr>
<td><a href="{% url 'teams:task_detail' slug=task.slug camp_slug=camp.slug team_slug=team.slug %}">{{ task.name }}</a></td> <td><a href="{% url 'teams:task_detail' slug=task.slug camp_slug=camp.slug team_slug=team.slug %}">{{ task.name }}</a></td>
<td>{{ task.description }}</td> <td>{{ task.description }}</td>
<td>
<a href="{% url 'teams:task_detail' camp_slug=camp.slug team_slug=team.slug slug=task.slug %}" class="btn btn-primary btn-sm">Details</a>
<a href="{% url 'teams:task_update' camp_slug=camp.slug team_slug=team.slug slug=task.slug %}" class="btn btn-primary btn-sm">Edit Task</a>
</td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>
</table> </table>
{% if request.user in team.responsible.all %}
<a href="{% url 'teams:task_create' camp_slug=camp.slug team_slug=team.slug %}" class="btn btn-primary">Create Task</a>
{% endif %}
</div> </div>
</div> </div>

View file

@ -51,10 +51,20 @@ urlpatterns = [
name='task_create', name='task_create',
), ),
url( url(
r'^(?P<slug>[-_\w+]+)/$', r'^(?P<slug>[-_\w+]+)/', include([
url(
r'^$',
TaskDetailView.as_view(), TaskDetailView.as_view(),
name='task_detail', name='task_detail',
), ),
url(
r'^update/$',
TaskUpdateView.as_view(),
name='task_update',
),
]),
),
]), ]),
), ),
]), ]),

View file

@ -157,7 +157,7 @@ class TaskDetailView(CampViewMixin, DetailView):
class TaskCreateView(LoginRequiredMixin, CampViewMixin, EnsureTeamResponsibleMixin, CreateView): class TaskCreateView(LoginRequiredMixin, CampViewMixin, EnsureTeamResponsibleMixin, CreateView):
model = TeamTask model = TeamTask
template_name = "task_create.html" template_name = "task_form.html"
fields = ['name', 'description'] fields = ['name', 'description']
def get_context_data(self, *args, **kwargs): def get_context_data(self, *args, **kwargs):
@ -165,13 +165,36 @@ class TaskCreateView(LoginRequiredMixin, CampViewMixin, EnsureTeamResponsibleMix
context['team'] = self.team context['team'] = self.team
return context return context
def get_success_url(self): def form_valid(self, form):
return reverse_lazy( task = form.save(commit=False)
'task_detail', task.team = self.team
kwargs={ if not task.name:
'camp_slug': self.camp.slug, task.name = "noname"
'team_slug': self.team.slug, task.save()
'slug': self.get_object().slug return HttpResponseRedirect(task.get_absolute_url())
}
) def get_success_url(self):
return self.get_object().get_absolute_url()
class TaskUpdateView(LoginRequiredMixin, CampViewMixin, EnsureTeamResponsibleMixin, UpdateView):
model = TeamTask
template_name = "task_form.html"
fields = ['name', 'description']
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(**kwargs)
context['team'] = self.team
return context
def form_valid(self, form):
task = form.save(commit=False)
task.team = self.team
if not task.name:
task.name = "noname"
task.save()
return HttpResponseRedirect(task.get_absolute_url())
def get_success_url(self):
return self.get_object().get_absolute_url()