Add precondition viewset method implementation.
parent
7bac976b8b
commit
7ad7fe9081
|
@ -1,16 +1,35 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
from rest_framework import viewsets
|
from rest_framework import viewsets
|
||||||
|
|
||||||
from .pagination import HeadersPaginationMixin, ConditionalPaginationMixin
|
from .pagination import HeadersPaginationMixin, ConditionalPaginationMixin
|
||||||
|
|
||||||
|
|
||||||
class ModelCrudViewSet(HeadersPaginationMixin,
|
class PreconditionMixin(object):
|
||||||
|
def pre_conditions_on_save(self, obj):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def pre_conditions_on_delete(self, obj):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def pre_save(self, obj):
|
||||||
|
super().pre_save(obj)
|
||||||
|
self.pre_conditions_on_save(obj)
|
||||||
|
|
||||||
|
def pre_delete(self, obj):
|
||||||
|
super().pre_delete(obj)
|
||||||
|
self.pre_conditions_on_delete(obj)
|
||||||
|
|
||||||
|
|
||||||
|
class ModelCrudViewSet(PreconditionMixin,
|
||||||
|
HeadersPaginationMixin,
|
||||||
ConditionalPaginationMixin,
|
ConditionalPaginationMixin,
|
||||||
viewsets.ModelViewSet):
|
viewsets.ModelViewSet):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class ModelListViewSet(HeadersPaginationMixin,
|
class ModelListViewSet(PreconditionMixin,
|
||||||
|
HeadersPaginationMixin,
|
||||||
ConditionalPaginationMixin,
|
ConditionalPaginationMixin,
|
||||||
viewsets.ReadOnlyModelViewSet):
|
viewsets.ReadOnlyModelViewSet):
|
||||||
pass
|
pass
|
||||||
|
|
|
@ -45,6 +45,13 @@ class PermissionDenied(exceptions.PermissionDenied):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class PreconditionError(BaseException):
|
||||||
|
"""
|
||||||
|
Error raised on precondition method on viewset.
|
||||||
|
"""
|
||||||
|
default_detail = "Precondition error"
|
||||||
|
|
||||||
|
|
||||||
class InternalError(BaseException):
|
class InternalError(BaseException):
|
||||||
"""
|
"""
|
||||||
Exception for internal errors.
|
Exception for internal errors.
|
||||||
|
|
|
@ -25,8 +25,8 @@ class ProjectViewSet(ModelCrudViewSet):
|
||||||
return qs.distinct()
|
return qs.distinct()
|
||||||
|
|
||||||
def pre_save(self, obj):
|
def pre_save(self, obj):
|
||||||
super(ProjectViewSet, self).pre_save(obj)
|
|
||||||
obj.owner = self.request.user
|
obj.owner = self.request.user
|
||||||
|
super(ProjectViewSet, self).pre_save(obj)
|
||||||
|
|
||||||
|
|
||||||
# User Stories commin ViewSets
|
# User Stories commin ViewSets
|
||||||
|
|
|
@ -3,9 +3,9 @@
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
from rest_framework.permissions import IsAuthenticated
|
from rest_framework.permissions import IsAuthenticated
|
||||||
from rest_framework.exceptions import ParseError
|
|
||||||
|
|
||||||
from greenmine.base import filters
|
from greenmine.base import filters
|
||||||
|
from greenmine.base import exceptions as exc
|
||||||
from greenmine.base.api import ModelCrudViewSet
|
from greenmine.base.api import ModelCrudViewSet
|
||||||
from greenmine.base.notifications.api import NotificationSenderMixin
|
from greenmine.base.notifications.api import NotificationSenderMixin
|
||||||
|
|
||||||
|
@ -24,11 +24,16 @@ class MilestoneViewSet(NotificationSenderMixin, ModelCrudViewSet):
|
||||||
update_notification_template = "update_milestone_notification"
|
update_notification_template = "update_milestone_notification"
|
||||||
destroy_notification_template = "destroy_milestone_notification"
|
destroy_notification_template = "destroy_milestone_notification"
|
||||||
|
|
||||||
def pre_save(self, obj):
|
def pre_conditions_on_save(self, obj):
|
||||||
super(MilestoneViewSet, self).pre_save(obj)
|
super().pre_conditions_on_save(obj)
|
||||||
if not obj.id:
|
|
||||||
obj.owner = self.request.user
|
|
||||||
|
|
||||||
if (obj.project.owner != self.request.user and
|
if (obj.project.owner != self.request.user and
|
||||||
obj.project.memberships.filter(user=self.request.user).count() == 0):
|
obj.project.memberships.filter(user=self.request.user).count() == 0):
|
||||||
raise ParseError(detail=_("You must not add a new milestone to this project."))
|
raise exc.PreconditionError("You must not add a new milestone to this project.")
|
||||||
|
|
||||||
|
def pre_save(self, obj):
|
||||||
|
if not obj.id:
|
||||||
|
obj.owner = self.request.user
|
||||||
|
|
||||||
|
super().pre_save(obj)
|
||||||
|
|
||||||
|
|
|
@ -4,9 +4,9 @@ from django.utils.translation import ugettext_lazy as _
|
||||||
from django.contrib.contenttypes.models import ContentType
|
from django.contrib.contenttypes.models import ContentType
|
||||||
|
|
||||||
from rest_framework.permissions import IsAuthenticated
|
from rest_framework.permissions import IsAuthenticated
|
||||||
from rest_framework.exceptions import ParseError
|
|
||||||
|
|
||||||
from greenmine.base import filters
|
from greenmine.base import filters
|
||||||
|
from greenmine.base import exceptions as exc
|
||||||
from greenmine.base.api import ModelCrudViewSet
|
from greenmine.base.api import ModelCrudViewSet
|
||||||
from greenmine.base.notifications.api import NotificationSenderMixin
|
from greenmine.base.notifications.api import NotificationSenderMixin
|
||||||
from greenmine.projects.permissions import AttachmentPermission
|
from greenmine.projects.permissions import AttachmentPermission
|
||||||
|
@ -29,16 +29,17 @@ class UserStoryAttachmentViewSet(ModelCrudViewSet):
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
ct = ContentType.objects.get_for_model(models.UserStory)
|
ct = ContentType.objects.get_for_model(models.UserStory)
|
||||||
qs = super(UserStoryAttachmentViewSet, self).get_queryset()
|
qs = super().get_queryset()
|
||||||
qs = qs.filter(content_type=ct)
|
qs = qs.filter(content_type=ct)
|
||||||
return qs.distinct()
|
return qs.distinct()
|
||||||
|
|
||||||
def pre_save(self, obj):
|
def pre_save(self, obj):
|
||||||
super(UserStoryAttachmentViewSet, self).pre_save(obj)
|
|
||||||
if not obj.id:
|
if not obj.id:
|
||||||
obj.content_type = ContentType.objects.get_for_model(models.UserStory)
|
obj.content_type = ContentType.objects.get_for_model(models.UserStory)
|
||||||
obj.owner = self.request.user
|
obj.owner = self.request.user
|
||||||
|
|
||||||
|
super().pre_save(obj)
|
||||||
|
|
||||||
|
|
||||||
class UserStoryViewSet(NotificationSenderMixin, ModelCrudViewSet):
|
class UserStoryViewSet(NotificationSenderMixin, ModelCrudViewSet):
|
||||||
model = models.UserStory
|
model = models.UserStory
|
||||||
|
@ -46,25 +47,31 @@ class UserStoryViewSet(NotificationSenderMixin, ModelCrudViewSet):
|
||||||
permission_classes = (IsAuthenticated, permissions.UserStoryPermission)
|
permission_classes = (IsAuthenticated, permissions.UserStoryPermission)
|
||||||
filter_backends = (filters.IsProjectMemberFilterBackend,)
|
filter_backends = (filters.IsProjectMemberFilterBackend,)
|
||||||
filter_fields = ['project', 'milestone', 'milestone__isnull']
|
filter_fields = ['project', 'milestone', 'milestone__isnull']
|
||||||
|
|
||||||
create_notification_template = "create_userstory_notification"
|
create_notification_template = "create_userstory_notification"
|
||||||
update_notification_template = "update_userstory_notification"
|
update_notification_template = "update_userstory_notification"
|
||||||
destroy_notification_template = "destroy_userstory_notification"
|
destroy_notification_template = "destroy_userstory_notification"
|
||||||
|
|
||||||
def pre_save(self, obj):
|
def pre_conditions_on_save(self, obj):
|
||||||
super(UserStoryViewSet, self).pre_save(obj)
|
super().pre_conditions_on_save(obj)
|
||||||
if not obj.id:
|
|
||||||
obj.owner = self.request.user
|
|
||||||
|
|
||||||
if (obj.project.owner != self.request.user and
|
if (obj.project.owner != self.request.user and
|
||||||
obj.project.memberships.filter(user=self.request.user).count() == 0):
|
obj.project.memberships.filter(user=self.request.user).count() == 0):
|
||||||
raise ParseError(detail=_("You must not add a new user story to this project."))
|
raise exc.PreconditionError("You must not add a new user story to this project.")
|
||||||
|
|
||||||
if obj.milestone and obj.milestone.project != obj.project:
|
if obj.milestone and obj.milestone.project != obj.project:
|
||||||
raise ParseError(detail=_("You must not add a new user story to this milestone."))
|
raise exc.PreconditionError("You must not add a new user story to this milestone.")
|
||||||
|
|
||||||
|
def pre_save(self, obj):
|
||||||
|
if not obj.id:
|
||||||
|
obj.owner = self.request.user
|
||||||
|
|
||||||
|
super().pre_save(obj)
|
||||||
|
|
||||||
def post_save(self, obj, created=False):
|
def post_save(self, obj, created=False):
|
||||||
with reversion.create_revision():
|
with reversion.create_revision():
|
||||||
if "comment" in self.request.DATA:
|
if "comment" in self.request.DATA:
|
||||||
# Update the comment in the last version
|
# Update the comment in the last version
|
||||||
reversion.set_comment(self.request.DATA['comment'])
|
reversion.set_comment(self.request.DATA['comment'])
|
||||||
super(UserStoryViewSet, self).post_save(obj, created)
|
|
||||||
|
super().post_save(obj, created)
|
||||||
|
|
Loading…
Reference in New Issue