Commits

Anonymous committed d02be66

Added class based team views

  • Participants
  • Parent commits 10bbe05
  • Branches class-based-views

Comments (0)

Files changed (4)

File projector/templates/projector/project/teams/home.html

 
 {% block col_main_extra %}
 
-{% autosort teams %}
-{% autopaginate teams %}
+{% autosort team_list %}
+{% autopaginate team_list %}
 
 <div class="richtemplates-panel">
     <h5>{% trans "Projects' teams" %}</h5>
                 </tr>
             </thead>
             <tbody class="datatable-tbody">
-                {% for team in teams %}
+                {% for team in team_list %}
                 <tr class="{% cycle "odd" "even" %} hoverable">
                     <td>{{ team.group }}</td>
                     <td><a class="block-link" href="{{ team.get_absolute_url }}">{% trans "Manage" %}</a></td>

File projector/urls.py

     url(r'^(?P<username>[-\w]+)/(?P<project_slug>[-\w]+)/$',
         view='ProjectDetailView', name='projector_project_details'),
 
-    # Teams
-    url(r'^(?P<username>[-\w]+)/(?P<project_slug>[-\w]+)/teams/$',
-        view='project_teams',
-        name='projector_project_teams'),
-    url(r'^(?P<username>[-\w]+)/(?P<project_slug>[-\w]+)/teams/add/$',
-        view='project_teams_add',
-        name='projector_project_teams_add'),
-    url(r'^(?P<username>[-\w]+)/(?P<project_slug>[-\w]+)/teams/(?P<name>[-_ \w]+)/$',
-        view='project_teams_edit',
-        name='projector_project_teams_edit'),
-
 )
 
 # Milestones
         name='projector_project_member_delete'),
 )
 
+# Teams
+urlpatterns += patterns('projector.views.project_team',
+    url(r'^(?P<username>[-\w]+)/(?P<project_slug>[-\w]+)/teams/$',
+        view='TeamListView',
+        name='projector_project_teams'),
+    url(r'^(?P<username>[-\w]+)/(?P<project_slug>[-\w]+)/teams/add/$',
+        view='TeamAddView',
+        name='projector_project_teams_add'),
+    url(r'^(?P<username>[-\w]+)/(?P<project_slug>[-\w]+)/teams/(?P<name>[-_ \w]+)/$',
+        view='TeamEditView',
+        name='projector_project_teams_edit'),
+)
+
 # Project's repository browsing
 urlpatterns += patterns('projector.views.project_repository',
     url(r'^(?P<username>[-\w]+)/(?P<project_slug>[-\w]+)/src/diff/(?P<revision_old>[\w]*)-(?P<revision_new>[\w]*)/(?P<rel_repo_url>.*)$',

File projector/views/project.py

 from django.core.exceptions import PermissionDenied
 from django.db.models import Count
 from django.http import HttpResponseRedirect, Http404
-from django.shortcuts import render_to_response, get_object_or_404, redirect
-from django.template import RequestContext
+from django.shortcuts import get_object_or_404
 from django.utils.translation import ugettext as _
 from django.utils.decorators import method_decorator
 
-from projector.decorators import permission_required_or_403
-
 from projector.core.controllers import View
-from projector.models import Project, Team
+from projector.models import Project
 from projector.forms import ProjectForm
-from projector.forms import TeamForm
-from projector.forms import ProjectTeamPermissionsForm
 from projector.permissions import ProjectPermission
 from projector.settings import get_config_value
 
         }
         return context
 
-# ========================== #
-# Membership - user & groups #
-# ========================== #
-
-# Teams
-
-def project_teams(request, username, project_slug,
-        template_name='projector/project/teams/home.html'):
-    """
-    Shows/updates project's teams view.
-    """
-    project = get_object_or_404(Project, slug=project_slug,
-        author__username=username)
-    if project.is_private():
-        check = ProjectPermission(request.user)
-        if not check.has_perm('project_permission.view_teams_project',
-            project):
-            raise PermissionDenied()
-    teams = Team.objects.filter(project=project)
-
-    context = {
-        'project': project,
-        'teams': teams,
-    }
-    return render_to_response(template_name, context, RequestContext(request))
-
-@permission_required_or_403('project_permission.add_team_project',
-    (Project, 'slug', 'project_slug', 'author__username', 'username'))
-def project_teams_add(request, username, project_slug,
-        template_name='projector/project/teams/add.html'):
-    """
-    Adds team for a project.
-    """
-    project = get_object_or_404(Project, slug=project_slug,
-        author__username=username)
-    team = Team(
-        project = project,
-    )
-    form = TeamForm(request.POST or None, instance=team)
-
-    if request.method == 'POST' and form.is_valid():
-        logging.info("Saving team %s for project '%s'"
-            % (form.instance.group, form.instance.project))
-        form.save()
-        return redirect(project.get_teams_url())
-    elif form.errors:
-        logging.error("Form contains errors:\n%s" % form.errors)
-
-    context = {
-        'project': form.instance.project,
-        'form': form,
-    }
-    return render_to_response(template_name, context, RequestContext(request))
-
-@permission_required_or_403('project_permission.change_team_project',
-    (Project, 'slug', 'project_slug', 'author__username', 'username'))
-def project_teams_edit(request, username, project_slug, name,
-        template_name='projector/project/teams/edit.html'):
-    """
-    Manages settings and permissions of project's team.
-    """
-    team = get_object_or_404(Team, project__author__username=username,
-        project__slug=project_slug, group__name=name)
-    project = team.project
-    team_permissions = team.perms
-    codenames = [str(p.codename) for p in team_permissions]
-
-    form = ProjectTeamPermissionsForm(request.POST or None,
-        team = team,
-        initial_permissions = codenames,
-        request = request)
-    if request.method == 'POST':
-        if form.is_valid():
-            form.save()
-            messages.success(request, _("Permissions updated"))
-        else:
-            messages.error(request,
-                _("Errors occured while processing the form"))
-        return redirect(team.get_absolute_url())
-    context = {
-        'project': project,
-        'form': form,
-        'team': team,
-        'team_permissions': team_permissions,
-    }
-
-    return render_to_response(template_name, context, RequestContext(request))
-

File projector/views/project_team.py

+import logging
+
+from django.shortcuts import get_object_or_404, redirect
+from django.utils.translation import ugettext as _
+from django.contrib import messages
+
+from projector.models import Team
+from projector.views.project import ProjectView
+from projector.forms import TeamForm, ProjectTeamPermissionsForm
+
+class TeamListView(ProjectView):
+    """
+    Returns teams view.
+    """
+
+    perms = ProjectView.perms + ['view_teams_project']
+    template_name = 'projector/project/teams/home.html'
+
+    def response(self, request, username, project_slug):
+        teams = Team.objects.filter(project=self.project)
+        context = {
+            'project': self.project,
+            'team_list': teams,
+        }
+        return context
+
+class TeamAddView(ProjectView):
+    """
+    Adds team for a project.
+    """
+
+    perms = ProjectView.perms + ['add_team_project']
+    template_name = 'projector/project/teams/add.html'
+
+    def response(self, request, username, project_slug):
+        team = Team(
+            project = self.project,
+        )
+        form = TeamForm(request.POST or None, instance=team)
+
+        if request.method == 'POST' and form.is_valid():
+            logging.info("Saving team %s for project '%s'"
+                % (form.instance.group, form.instance.project))
+            form.save()
+            return redirect(self.project.get_teams_url())
+        elif form.errors:
+            logging.error("Form contains errors:\n%s" % form.errors)
+
+        context = {
+            'project': form.instance.project,
+            'form': form,
+        }
+        return context
+
+class TeamEditView(ProjectView):
+    """
+    Manages settings and permissions of project's team.
+    """
+
+    perms = ProjectView.perms + ['change_team_project']
+    template_name = 'projector/project/teams/edit.html'
+
+    def response(self, request, username, project_slug, name):
+        team = get_object_or_404(Team, project__author__username=username,
+            project__slug=project_slug, group__name=name)
+        team_permissions = team.perms
+        codenames = [str(p.codename) for p in team_permissions]
+
+        form = ProjectTeamPermissionsForm(request.POST or None,
+            team = team,
+            initial_permissions = codenames,
+            request = request)
+        if request.method == 'POST':
+            if form.is_valid():
+                form.save()
+                messages.success(request, _("Permissions updated"))
+            else:
+                messages.error(request,
+                    _("Errors occured while processing the form"))
+            return redirect(team.get_absolute_url())
+        context = {
+            'project': self.project,
+            'form': form,
+            'team': team,
+            'team_permissions': team_permissions,
+        }
+        return context
+