Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
raise UnpermittedActionError()
class TopicViewSet(DispatchModelViewSet):
"""Viewset for Topic model views."""
model = Topic
serializer_class = TopicSerializer
def get_queryset(self):
queryset = Topic.objects.order_by('-last_used')
q = self.request.query_params.get('q', None)
if q is not None:
# If a search term (q) is present, filter queryset by term against `name`
queryset = queryset.filter(name__icontains=q)
return queryset
class FileViewSet(DispatchModelViewSet):
"""Viewset for File model views."""
model = File
serializer_class = FileSerializer
def get_queryset(self):
queryset = File.objects.all()
q = self.request.query_params.get('q', None)
if q is not None:
# If a search term (q) is present, filter queryset by term against `name`
queryset = queryset.filter(name__icontains=q)
return queryset
class IssueViewSet(DispatchModelViewSet):
"""Viewset for Issue model views."""
model = Issue
serializer_class = IssueSerializer
settings = get_settings(token)
return Response({
'token_valid': True,
'settings': settings
})
def delete(self, request):
token = get_object_or_404(Token, user=request.user)
token.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
class PodcastViewSet(DispatchModelViewSet):
"""Viewset for Podcast model views."""
model = Podcast
serializer_class = PodcastSerializer
def get_queryset(self):
queryset = Podcast.objects.all()
q = self.request.query_params.get('q', None)
if q is not None:
# If a search term (q) is present, filter queryset by term against `title`
queryset = queryset.filter(title__icontains=q)
return queryset
class PodcastEpisodeViewSet(DispatchModelViewSet):
"""Viewset for PodcastEpisode model views."""
model = PodcastEpisode
serializer_class = PodcastEpisodeSerializer
from dispatch.theme.exceptions import ZoneNotFound, TemplateNotFound
class SectionViewSet(DispatchModelViewSet):
"""Viewset for Section model views."""
model = Section
serializer_class = SectionSerializer
def get_queryset(self):
queryset = Section.objects.all()
q = self.request.query_params.get('q', None)
if q is not None:
# If a search term (q) is present, filter queryset by term against `name`
queryset = queryset.filter(name__icontains=q)
return queryset
class ArticleViewSet(DispatchModelViewSet, DispatchPublishableMixin):
"""Viewset for Article model views."""
model = Article
serializer_class = ArticleSerializer
lookup_field = 'parent_id'
def get_queryset(self):
"""Optionally restricts the returned articles by filtering against a `topic`
query parameter in the URL."""
# Get base queryset from DispatchPublishableMixin
queryset = self.get_publishable_queryset()
# Optimize queries by prefetching related data
queryset = queryset \
.select_related('featured_image', 'featured_video', 'topic', 'section', 'subsection') \
.prefetch_related(
serializer_class = SubsectionSerializer
def get_queryset(self):
queryset = Subsection.objects.all()
q = self.request.query_params.get('q', None)
section = self.request.query_params.get('section', None)
if q is not None:
queryset = queryset.filter(name__icontains=q)
if section is not None:
queryset = queryset.filter(section_id=section)
return queryset
class PageViewSet(DispatchModelViewSet, DispatchPublishableMixin):
"""Viewset for Page model views."""
model = Page
serializer_class = PageSerializer
lookup_field = 'parent_id'
def get_queryset(self):
"""Only display unpublished content to authenticated users, filter by
query parameter if present."""
# Get base queryset from DispatchPublishableMixin
queryset = self.get_publishable_queryset()
queryset = queryset.order_by('-updated_at')
# Optionally filter by a query parameter
q = self.request.query_params.get('q')
return queryset
class FileViewSet(DispatchModelViewSet):
"""Viewset for File model views."""
model = File
serializer_class = FileSerializer
def get_queryset(self):
queryset = File.objects.all()
q = self.request.query_params.get('q', None)
if q is not None:
# If a search term (q) is present, filter queryset by term against `name`
queryset = queryset.filter(name__icontains=q)
return queryset
class IssueViewSet(DispatchModelViewSet):
"""Viewset for Issue model views."""
model = Issue
serializer_class = IssueSerializer
def get_queryset(self):
queryset = Issue.objects.all()
q = self.request.query_params.get('q', None)
if q is not None:
# If a search term (q) is present, filter queryset by term against `name`
queryset = queryset.filter(title__icontains=q)
return queryset
class TagViewSet(DispatchModelViewSet):
"""Viewset for Tag model views."""
model = Tag
serializer_class = TagSerializer
class TagViewSet(DispatchModelViewSet):
"""Viewset for Tag model views."""
model = Tag
serializer_class = TagSerializer
def get_queryset(self):
queryset = Tag.objects.all()
q = self.request.query_params.get('q', None)
if q is not None:
# If a search term (q) is present, filter queryset by term against `name`
queryset = queryset.filter(name__icontains=q)
return queryset
class VideoViewSet(DispatchModelViewSet):
"""Viewset for Video model views."""
model = Video
serializer_class = VideoSerializer
filter_backends = (filters.OrderingFilter,)
update_fields = ('title', 'authors', 'tags')
def get_queryset(self):
queryset = Video.objects.order_by('-updated_at')
author = self.request.query_params.get('author', None)
tags = self.request.query_params.getlist('tags', None)
q = self.request.query_params.get('q', None)
if author is not None:
queryset = queryset.filter(authors__person_id=author)
if tags is not None:
return queryset
class IssueViewSet(DispatchModelViewSet):
"""Viewset for Issue model views."""
model = Issue
serializer_class = IssueSerializer
def get_queryset(self):
queryset = Issue.objects.all()
q = self.request.query_params.get('q', None)
if q is not None:
# If a search term (q) is present, filter queryset by term against `name`
queryset = queryset.filter(title__icontains=q)
return queryset
class TagViewSet(DispatchModelViewSet):
"""Viewset for Tag model views."""
model = Tag
serializer_class = TagSerializer
def get_queryset(self):
queryset = Tag.objects.all()
q = self.request.query_params.get('q', None)
if q is not None:
# If a search term (q) is present, filter queryset by term against `name`
queryset = queryset.filter(name__icontains=q)
return queryset
class VideoViewSet(DispatchModelViewSet):
"""Viewset for Video model views."""
model = Video
@detail_route(methods=['get'])
def invite(self, request, pk=None):
queryset = Person.objects.all()
person = get_object_or_404(queryset, pk=pk)
try:
invite = Invite.objects.get(person=person)
serializer = InviteSerializer(invite)
except Invite.DoesNotExist:
return Response({'detail': 'Person has no invitation'})
return Response(serializer.data)
class InviteViewSet(DispatchModelViewSet):
"""Viewset for the Invite model views."""
model = Invite
serializer_class = InviteSerializer
permission_classes = (DjangoModelPermissions,)
def get_queryset(self):
queryset = Invite.objects.all()
q = self.request.query_params.get('q', None)
if q is not None:
queryset = queryset.filter(person__id=q)
return queryset
class UserViewSet(DispatchModelViewSet):
"""Viewset for User model views."""
model = User
class InviteViewSet(DispatchModelViewSet):
"""Viewset for the Invite model views."""
model = Invite
serializer_class = InviteSerializer
permission_classes = (DjangoModelPermissions,)
def get_queryset(self):
queryset = Invite.objects.all()
q = self.request.query_params.get('q', None)
if q is not None:
queryset = queryset.filter(person__id=q)
return queryset
class UserViewSet(DispatchModelViewSet):
"""Viewset for User model views."""
model = User
serializer_class = UserSerializer
queryset = User.objects.all()
def get_permissions(self):
if self.request.method == 'PATCH':
self.permission_classes = [IsAuthenticated, ]
else:
self.permission_classes = [DjangoModelPermissions, ]
return super(UserViewSet, self).get_permissions()
def get_queryset(self):
queryset = User.objects.all()
q = self.request.query_params.get('q', None)
permissions = request.data.get('permissions', None)
user.modify_permissions(permissions)
return super(UserViewSet, self).partial_update(request)
@detail_route(methods=['post'])
def reset_password(self, request, pk=None):
user = get_object_or_404(User.objects.all(), pk=pk)
if request.user.has_perm('dispatch.change_user'):
reset_password(user.email, request)
return Response(status.HTTP_202_ACCEPTED)
else:
raise UnpermittedActionError()
class TopicViewSet(DispatchModelViewSet):
"""Viewset for Topic model views."""
model = Topic
serializer_class = TopicSerializer
def get_queryset(self):
queryset = Topic.objects.order_by('-last_used')
q = self.request.query_params.get('q', None)
if q is not None:
# If a search term (q) is present, filter queryset by term against `name`
queryset = queryset.filter(name__icontains=q)
return queryset
class FileViewSet(DispatchModelViewSet):
"""Viewset for File model views."""
model = File
serializer_class = FileSerializer