Adding notify_level to queryset for projects API
parent
58807fc964
commit
014941aec6
|
@ -45,13 +45,13 @@ from . import serializers
|
||||||
from . import models
|
from . import models
|
||||||
from . import permissions
|
from . import permissions
|
||||||
from . import services
|
from . import services
|
||||||
|
from . import utils
|
||||||
|
|
||||||
from .votes.mixins.viewsets import LikedResourceMixin, VotersViewSetMixin
|
from .votes.mixins.viewsets import LikedResourceMixin, VotersViewSetMixin
|
||||||
|
|
||||||
######################################################
|
######################################################
|
||||||
## Project
|
## Project
|
||||||
######################################################
|
######################################################
|
||||||
|
|
||||||
class ProjectViewSet(LikedResourceMixin, HistoryResourceMixin, WatchedResourceMixin, ModelCrudViewSet):
|
class ProjectViewSet(LikedResourceMixin, HistoryResourceMixin, WatchedResourceMixin, ModelCrudViewSet):
|
||||||
queryset = models.Project.objects.all()
|
queryset = models.Project.objects.all()
|
||||||
serializer_class = serializers.ProjectDetailSerializer
|
serializer_class = serializers.ProjectDetailSerializer
|
||||||
|
@ -65,7 +65,9 @@ class ProjectViewSet(LikedResourceMixin, HistoryResourceMixin, WatchedResourceMi
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
qs = super().get_queryset()
|
qs = super().get_queryset()
|
||||||
qs = self.attach_votes_attrs_to_queryset(qs)
|
qs = self.attach_votes_attrs_to_queryset(qs)
|
||||||
return self.attach_watchers_attrs_to_queryset(qs)
|
qs = self.attach_watchers_attrs_to_queryset(qs)
|
||||||
|
qs = utils.attach_notify_level_to_queryset(qs, self.request.user)
|
||||||
|
return qs
|
||||||
|
|
||||||
@detail_route(methods=["POST"])
|
@detail_route(methods=["POST"])
|
||||||
def watch(self, request, pk=None):
|
def watch(self, request, pk=None):
|
||||||
|
|
|
@ -214,7 +214,7 @@ class WatchedResourceModelSerializer(serializers.ModelSerializer):
|
||||||
|
|
||||||
def to_native(self, obj):
|
def to_native(self, obj):
|
||||||
#watchers is wasn't attached via the get_queryset of the viewset we need to manually add it
|
#watchers is wasn't attached via the get_queryset of the viewset we need to manually add it
|
||||||
if not hasattr(obj, "watchers"):
|
if obj is not None and not hasattr(obj, "watchers"):
|
||||||
obj.watchers = [user.id for user in services.get_watchers(obj)]
|
obj.watchers = [user.id for user in services.get_watchers(obj)]
|
||||||
|
|
||||||
return super(WatchedResourceModelSerializer, self).to_native(obj)
|
return super(WatchedResourceModelSerializer, self).to_native(obj)
|
||||||
|
|
|
@ -318,6 +318,7 @@ class ProjectSerializer(WatchersValidator, LikedResourceSerializerMixin, Watched
|
||||||
i_am_owner = serializers.SerializerMethodField("get_i_am_owner")
|
i_am_owner = serializers.SerializerMethodField("get_i_am_owner")
|
||||||
tags_colors = TagsColorsField(required=False)
|
tags_colors = TagsColorsField(required=False)
|
||||||
total_closed_milestones = serializers.SerializerMethodField("get_total_closed_milestones")
|
total_closed_milestones = serializers.SerializerMethodField("get_total_closed_milestones")
|
||||||
|
notify_level = serializers.IntegerField(read_only=True)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = models.Project
|
model = models.Project
|
||||||
|
@ -369,7 +370,6 @@ class ProjectDetailSerializer(ProjectSerializer):
|
||||||
|
|
||||||
roles = ProjectRoleSerializer(source="roles", many=True, read_only=True)
|
roles = ProjectRoleSerializer(source="roles", many=True, read_only=True)
|
||||||
members = serializers.SerializerMethodField(method_name="get_members")
|
members = serializers.SerializerMethodField(method_name="get_members")
|
||||||
notify_policy = serializers.SerializerMethodField(method_name="get_notify_policy")
|
|
||||||
|
|
||||||
def get_members(self, obj):
|
def get_members(self, obj):
|
||||||
qs = obj.memberships.filter(user__isnull=False)
|
qs = obj.memberships.filter(user__isnull=False)
|
||||||
|
@ -379,22 +379,6 @@ class ProjectDetailSerializer(ProjectSerializer):
|
||||||
serializer = ProjectMemberSerializer(qs, many=True)
|
serializer = ProjectMemberSerializer(qs, many=True)
|
||||||
return serializer.data
|
return serializer.data
|
||||||
|
|
||||||
def get_notify_policy(self, obj):
|
|
||||||
request= self.context.get("request", None)
|
|
||||||
if request is None:
|
|
||||||
return None
|
|
||||||
|
|
||||||
user = request.user
|
|
||||||
if not user.is_authenticated():
|
|
||||||
return None
|
|
||||||
|
|
||||||
try:
|
|
||||||
notify_policy = obj.notify_policies.get(user=user, project=obj)
|
|
||||||
return notify_policy.notify_level
|
|
||||||
|
|
||||||
except notify_models.NotifyPolicy.DoesNotExist:
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
class ProjectDetailAdminSerializer(ProjectDetailSerializer):
|
class ProjectDetailAdminSerializer(ProjectDetailSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
# Copyright (C) 2014 Andrey Antukh <niwi@niwi.be>
|
||||||
|
# Copyright (C) 2014 Jesús Espino <jespinog@gmail.com>
|
||||||
|
# Copyright (C) 2014 David Barragán <bameda@dbarragan.com>
|
||||||
|
# Copyright (C) 2014 Anler Hernández <hello@anler.me>
|
||||||
|
# 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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
from django.apps import apps
|
||||||
|
|
||||||
|
def attach_notify_level_to_queryset(queryset, user, as_field="notify_level"):
|
||||||
|
"""Attach notify level to each object of the queryset.
|
||||||
|
|
||||||
|
:param queryset: A Django queryset object.
|
||||||
|
:param as_field: Attach the notify level as an attribute with this name.
|
||||||
|
|
||||||
|
:return: Queryset object with the additional `as_field` field.
|
||||||
|
"""
|
||||||
|
|
||||||
|
if user.is_authenticated():
|
||||||
|
model = queryset.model
|
||||||
|
sql = ("""SELECT notifications_notifypolicy.notify_level
|
||||||
|
FROM notifications_notifypolicy
|
||||||
|
WHERE notifications_notifypolicy.user_id = {user_id}
|
||||||
|
AND notifications_notifypolicy.project_id = {tbl}.id""")
|
||||||
|
sql = sql.format(user_id=user.id, tbl=model._meta.db_table)
|
||||||
|
else:
|
||||||
|
sql = "SELECT CAST(NULL as text)"
|
||||||
|
|
||||||
|
qs = queryset.extra(select={as_field: sql})
|
||||||
|
return qs
|
Loading…
Reference in New Issue