Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 316 lines (246 sloc) 11.322 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
import logging
from datetime import date

from django.conf import settings
from django.contrib import messages
from django.contrib.auth.decorators import permission_required
from django.core.urlresolvers import reverse
from django.http import (HttpResponse, HttpResponseForbidden,
                         HttpResponsePermanentRedirect)
from django.shortcuts import get_object_or_404, redirect, render
from django.template.context import Context
from django.utils import simplejson as json
from django.views.generic import (CreateView, DeleteView, DetailView,
                                  ListView, TemplateView, UpdateView, View)

from scrum.forms import (CreateProjectForm, CreateTeamForm, BZURLForm,
                         ProjectBugsForm, ProjectForm, SprintBugsForm,
                         SprintForm, TeamForm)
from scrum.models import BugzillaURL, BZError, Project, Sprint, Team, Bug
from scrum.utils import get_blocked_bugs


log = logging.getLogger(__name__)


class ProtectedCreateView(CreateView):
    def dispatch(self, request, *args, **kwargs):
        @permission_required('%s.add_%s' % (self.model._meta.app_label,
                                            self.model._meta.module_name))
        def wrap(request, *args, **kwargs):
            return super(ProtectedCreateView, self).dispatch(request,
                                                             *args, **kwargs)
        return wrap(request, *args, **kwargs)


class ProtectedUpdateView(UpdateView):
    def dispatch(self, request, *args, **kwargs):
        @permission_required('%s.change_%s' % (self.model._meta.app_label,
                                               self.model._meta.module_name))
        def wrap(request, *args, **kwargs):
            return super(ProtectedUpdateView, self).dispatch(request,
                                                             *args, **kwargs)
        return wrap(request, *args, **kwargs)


class ProtectedDeleteView(DeleteView):
    def dispatch(self, request, *args, **kwargs):
        @permission_required('%s.delete_%s' % (self.model._meta.app_label,
                                               self.model._meta.module_name))
        def wrap(request, *args, **kwargs):
            return super(ProtectedDeleteView, self).dispatch(request,
                                                             *args, **kwargs)
        return wrap(request, *args, **kwargs)


class BugsDataMixin(object):
    def get_context_data(self, **kwargs):
        context = super(BugsDataMixin, self).get_context_data(**kwargs)
        self.bugs_kwargs = {}
        # clear cache if requested
        if self.request.META.get('HTTP_CACHE_CONTROL') == 'no-cache':
            self.bugs_kwargs['refresh'] = True
            messages.info(self.request, "The bugs will be refreshed from "
                          "Bugzilla in 10 minutes or so.")
        if 'all' in self.request.GET:
            self.bugs_kwargs['scrum_only'] = False
        try:
            context['bz_search_url'] = self.object.get_bz_search_url().url
        except AttributeError:
            pass
        context['scrum_only'] = self.bugs_kwargs.get('scrum_only', True)
        context['refresh'] = self.bugs_kwargs.get('refresh', False)
        try:
            bugs = self.object.get_bugs(**self.bugs_kwargs)
            context['blocked_bugs'] = get_blocked_bugs(bugs)
            context['bugs'] = bugs
            context['bugs_data'] = self.object.get_graph_bug_data()
            context['bugs_data_json'] = json.dumps(context['bugs_data'])
            context['bzerror'] = False
        except BZError:
            context['bzerror'] = True
        return context


class ProjectsMixin(object):
    model = Project

    def get_context_data(self, **kwargs):
        context = super(ProjectsMixin, self).get_context_data(**kwargs)
        context['projects'] = Project.objects.all()
        if hasattr(self, 'project'):
            context['project'] = self.project
        return context


class HomeView(TemplateView):
    template_name = 'scrum/home.html'
home = HomeView.as_view()


class ProjectView(BugsDataMixin, ProjectsMixin, DetailView):
    template_name = 'scrum/project.html'

    def get_context_data(self, **kwargs):
        context = super(ProjectView, self).get_context_data(**kwargs)
        today = date.today()
        bugs = Bug.objects.filter(sprint__start_date__lte=today,
                                  sprint__end_date__gte=today,
                                  project=self.object)
        context['sprinting'] = bugs
        context['sprinting_blocked'] = get_blocked_bugs(bugs)
        return context


class TeamView(DetailView):
    model = Team
    template_name = 'scrum/team.html'


class ListTeamsView(ListView):
    model = Team
    template_name = 'scrum/teams_list.html'
    context_object_name = 'teams'


class CreateTeamView(ProtectedCreateView):
    model = Team
    form_class = CreateTeamForm
    template_name = 'scrum/team_form.html'


class ListProjectsView(ProjectsMixin, ListView):
    template_name = 'scrum/projects_list.html'
    context_object_name = 'projects'


class CreateProjectView(ProjectsMixin, ProtectedCreateView):
    model = Project
    form_class = CreateProjectForm
    template_name = 'scrum/project_form.html'


class EditProjectView(ProjectsMixin, ProtectedUpdateView):
    form_class = ProjectForm
    template_name = 'scrum/project_form.html'


class CreateSprintView(ProtectedCreateView):
    model = Sprint
    form_class = SprintForm
    template_name = 'scrum/sprint_form.html'

    def get_context_data(self, **kwargs):
        context = super(CreateSprintView, self).get_context_data(**kwargs)
        context['team'] = self.team
        return context

    def get_success_url(self):
        return reverse('scrum_sprint_bugs', args=[self.team.slug,
                                                  self.object.slug])

    def get_form_kwargs(self):
        kwargs = super(CreateSprintView, self).get_form_kwargs()
        if 'data' in kwargs:
            print kwargs['data']
            form_data = kwargs['data'].copy()
            form_data['team'] = self.team.id
            kwargs['data'] = form_data
        return kwargs

    def get_initial(self):
        self.team = get_object_or_404(Team, slug=self.kwargs['slug'])
        return super(CreateSprintView, self).get_initial()

    def form_valid(self, form):
        return super(CreateSprintView, self).form_valid(form)


class SprintMixin(object):
    model = Sprint
    context_object_name = 'sprint'

    def get_object(self, queryset=None):
        tslug = self.kwargs.get('slug')
        sslug = self.kwargs.get('sslug')
        sprint = get_object_or_404(Sprint, team__slug=tslug, slug=sslug)
        self.team = sprint.team
        return sprint

    def get_context_data(self, **kwargs):
        context = super(SprintMixin, self).get_context_data(**kwargs)
        context['team'] = self.team
        return context


class SprintView(BugsDataMixin, SprintMixin, DetailView):
    template_name = 'scrum/sprint.html'

    def get_context_data(self, **kwargs):
        context = super(SprintView, self).get_context_data(**kwargs)
        context['team'] = self.team
        if not context['bzerror']:
            context['bugs_data'].update(self.object.get_burndown_data())
            context['bugs_data_json'] = json.dumps(context['bugs_data'])
        return context


class EditSprintView(SprintMixin, ProjectsMixin, ProtectedUpdateView):
    form_class = SprintForm
    template_name = 'scrum/sprint_form.html'


class EditTeamView(ProtectedUpdateView):
    model = Team
    form_class = TeamForm
    template_name = 'scrum/team_form.html'


class ManageSprintBugsView(BugsDataMixin, SprintMixin, ProtectedUpdateView):
    form_class = SprintBugsForm
    template_name = 'scrum/sprint_bugs.html'

    def get_context_data(self, **kwargs):
        context = super(ManageSprintBugsView, self).get_context_data(**kwargs)
        if not context['bzerror']:
            try:
                bugs = self.team.get_bugs(**self.bugs_kwargs)
                context['backlog_bugs'] = bugs
                context['blocked_backlog_bugs'] = get_blocked_bugs(bugs)
                context['bugs_data'].update(self.object.get_burndown_data())
                context['bugs_data_json'] = json.dumps(context['bugs_data'])
            except BZError:
                context['bzerror'] = True
        return context


class ManageProjectBugsView(BugsDataMixin, ProtectedUpdateView):
    model = Project
    form_class = ProjectBugsForm
    context_object_name = 'project'
    template_name = 'scrum/project_bugs.html'

    def get_context_data(self, **kwargs):
        context = super(ManageProjectBugsView, self).get_context_data(**kwargs)
        if not context['bzerror']:
            try:
                bugs = self.object.get_backlog(**self.bugs_kwargs)
                context['backlog_bugs'] = bugs
                context['blocked_backlog_bugs'] = get_blocked_bugs(bugs)
            except BZError:
                context['bzerror'] = True
        return context


class CreateBZUrlView(ProtectedCreateView):
    model = BugzillaURL
    form_class = BZURLForm
    template_name = 'scrum/bzurl_list.html'

    def get_context_data(self, **kwargs):
        obj = get_object_or_404(Project, slug=self.kwargs['slug'])
        kwargs['target_obj'] = obj
        kwargs['bzurls'] = obj.urls.all()
        return super(CreateBZUrlView, self).get_context_data(**kwargs)

    def form_valid(self, form):
        url = form.save(commit=False)
        url.project = get_object_or_404(Project, slug=self.kwargs['slug'])
        url.save()
        if self.request.is_ajax():
            return self.render_to_response(self.get_context_data(form=form))
        else:
            return redirect(self.target_obj.get_edit_url())

    def form_invalid(self, form):
        if self.request.is_ajax():
            return HttpResponse(json.dumps(form.errors), status=400)
        else:
            target_obj = get_object_or_404(Project, slug=self.kwargs['slug'])
            messages.error(self.request, form['url'].errors[0])
            return redirect(target_obj.get_edit_url())


class DeleteBZUlrView(ProtectedDeleteView):
    model = BugzillaURL
    success_url = '/'

    def __init__(self):
        # remove GET from allowed methods to throw 405
        # copy list or modify global copy
        self.http_method_names = self.http_method_names[:]
        self.http_method_names.remove('get')
        super(DeleteBZUlrView, self).__init__()

    def delete(self, request, *args, **kwargs):
        if request.is_ajax():
            super(DeleteBZUlrView, self).delete(request, *args, **kwargs)
            return HttpResponse(status=204)
        return HttpResponseForbidden()


def server_error(request):
    context = {
        'STATIC_URL': getattr(settings, 'STATIC_URL', '/static/'),
        'ENABLE_GA': getattr(settings, 'ENABLE_GA', False),
        'request': {'path': '/'},
    }
    return render(request, '500.html', context_instance=Context(context),
                  status=500)


class RedirectOldURLsView(View):
    def get(self, request, *args, **kwargs):
        path = kwargs.get('path', '')
        return HttpResponsePermanentRedirect('/p/' + path)
Something went wrong with that request. Please try again.