diff --git a/taiga/projects/notifications/api.py b/taiga/projects/notifications/api.py new file mode 100644 index 00000000..ec4d8dd5 --- /dev/null +++ b/taiga/projects/notifications/api.py @@ -0,0 +1,59 @@ +# Copyright (C) 2014 Andrey Antukh +# Copyright (C) 2014 Jesús Espino +# Copyright (C) 2014 David Barragán +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +from django.utils.translation import ugettext_lazy as _ +from django.shortcuts import get_object_or_404 +from django.db.models import Q + +from rest_framework.permissions import IsAuthenticated +from rest_framework.response import Response + +from taiga.base import filters +from taiga.base import exceptions as exc +from taiga.base.decorators import detail_route +from taiga.base.api import ModelCrudViewSet + +from taiga.projects.models import Project + +from taiga.projects.notifications.choices import NotifyLevel + +from . import serializers +from . import models +from . import permissions +from . import services + + +class NotifyPolicyViewSet(ModelCrudViewSet): + serializer_class = serializers.NotifyPolicySerializer + permission_classes = (permissions.NotifyPolicyPermission,) + + def _build_needed_notify_policies(self): + projects = Project.objects.filter( + Q(owner=self.request.user) | + Q(memberships__user=self.request.user) + ) + for project in projects: + if not services.notify_policy_exists(project, self.request.user): + services.create_notify_policy(project, self.request.user, NotifyLevel.watch) + + def get_queryset(self): + self._build_needed_notify_policies() + + qs = models.NotifyPolicy.objects.filter( + Q(project__owner=self.request.user) | + Q(project__memberships__user=self.request.user) + ) + return qs.distinct() diff --git a/taiga/projects/notifications/permissions.py b/taiga/projects/notifications/permissions.py new file mode 100644 index 00000000..73b490df --- /dev/null +++ b/taiga/projects/notifications/permissions.py @@ -0,0 +1,25 @@ +# Copyright (C) 2014 Andrey Antukh +# Copyright (C) 2014 Jesús Espino +# Copyright (C) 2014 David Barragán +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +from taiga.base.api.permissions import (ResourcePermission, IsAuthenticated) + + +class NotifyPolicyPermission(ResourcePermission): + retrieve_perms = IsAuthenticated() + create_perms = IsAuthenticated() + update_perms = IsAuthenticated() + destroy_perms = IsAuthenticated() + list_perms = IsAuthenticated() diff --git a/taiga/projects/notifications/serializers.py b/taiga/projects/notifications/serializers.py new file mode 100644 index 00000000..f95a9f28 --- /dev/null +++ b/taiga/projects/notifications/serializers.py @@ -0,0 +1,28 @@ +# Copyright (C) 2014 Andrey Antukh +# Copyright (C) 2014 Jesús Espino +# Copyright (C) 2014 David Barragán +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +import json + +from rest_framework import serializers + +from . import models + + + +class NotifyPolicySerializer(serializers.ModelSerializer): + class Meta: + model = models.NotifyPolicy + fields = ('id', 'project', 'notify_level') diff --git a/taiga/routers.py b/taiga/routers.py index f33022e3..bb6f38d0 100644 --- a/taiga/routers.py +++ b/taiga/routers.py @@ -119,3 +119,7 @@ router.register(r"issues", IssueViewSet, base_name="issues") router.register(r"issues/(?P\d+)/voters", VotersViewSet, base_name="issue-voters") router.register(r"wiki", WikiViewSet, base_name="wiki") router.register(r"wiki-links", WikiLinkViewSet, base_name="wiki-links") + +# Notify policies +from taiga.projects.notifications.api import NotifyPolicyViewSet +router.register(r"notifications", NotifyPolicyViewSet, base_name="notifications")