<commit_before>from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_control
from django.views.decorators.http import last_modified
from django.views.decorators.vary import vary_on_headers
from djangorestframework_camel_case.parser import CamelCaseJSONParser
from djangorestframework_camel_case.render import CamelCaseJSONRenderer
from notification.models import NoticeSetting, NoticeType, NOTICE_MEDIA
from persistent_messages.models import Message
from rest_framework import viewsets, permissions
from rest_framework.decorators import list_route
from rest_framework.renderers import BrowsableAPIRenderer
from rest_framework.response import Response

from astrobin_apps_notifications.api.filters import NotificationFilter
from astrobin_apps_notifications.api.serializers import NotificationSerializer, NoticeSettingSerializers, \
    NoticeTypeSerializer
from common.permissions import ReadOnly
from common.services.caching_service import CachingService


@method_decorator([
    last_modified(CachingService.get_last_notification_time),
    cache_control(private=True, no_cache=True),
    vary_on_headers('Cookie', 'Authorization')
], name='dispatch')
class NotificationViewSet(viewsets.ModelViewSet):
    serializer_class = NotificationSerializer
    filter_class = NotificationFilter
    permission_classes = [permissions.IsAuthenticated]
    renderer_classes = [BrowsableAPIRenderer, CamelCaseJSONRenderer]
    parser_classes = [CamelCaseJSONParser]
    http_method_names = ['get', 'post', 'head', 'put']

    def get_queryset(self):
        return Message.objects.filter(user=self.request.user).order_by('-created')

    @list_route(methods=['get'])
    def get_unread_count(self, request):
        return Response(status=200, data=self.get_queryset().filter(read=False).count())

    @list_route(methods=['post'])
    def mark_all_as_read(self, request):
        self.get_queryset().update(read=True)
        return Response(status=200)


class NoticeTypeViewSet(viewsets.ModelViewSet):
    serializer_class = NoticeTypeSerializer
    permission_classes = [ReadOnly]
    renderer_classes = [BrowsableAPIRenderer, CamelCaseJSONRenderer]
    parser_classes = [CamelCaseJSONParser]
    queryset = NoticeType.objects.all()
    pagination_class = None


class NoticeSettingViewSet(viewsets.ModelViewSet):
    serializer_class = NoticeSettingSerializers
    permission_classes = [permissions.IsAuthenticated]
    renderer_classes = [BrowsableAPIRenderer, CamelCaseJSONRenderer]
    parser_classes = [CamelCaseJSONParser]
    pagination_class = None

    def get_queryset(self):
        notice_types = NoticeType.objects.all()
        for notice_type in notice_types:
            for medium_id, medium_display in NOTICE_MEDIA:
                # This will create it with default values if it doesn't exist.
                NoticeSetting.for_user(self.request.user, notice_type, medium_id)

        return NoticeSetting.objects.filter(user=self.request.user)<commit_msg>(notifications) Update modified timestamp when marking all as read<commit_after>from django.utils import timezone
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_control
from django.views.decorators.http import last_modified
from django.views.decorators.vary import vary_on_headers
from djangorestframework_camel_case.parser import CamelCaseJSONParser
from djangorestframework_camel_case.render import CamelCaseJSONRenderer
from notification.models import NoticeSetting, NoticeType, NOTICE_MEDIA
from persistent_messages.models import Message
from rest_framework import viewsets, permissions
from rest_framework.decorators import list_route
from rest_framework.renderers import BrowsableAPIRenderer
from rest_framework.response import Response

from astrobin_apps_notifications.api.filters import NotificationFilter
from astrobin_apps_notifications.api.serializers import NotificationSerializer, NoticeSettingSerializers, \
    NoticeTypeSerializer
from common.permissions import ReadOnly
from common.services.caching_service import CachingService


@method_decorator([
    last_modified(CachingService.get_last_notification_time),
    cache_control(private=True, no_cache=True),
    vary_on_headers('Cookie', 'Authorization')
], name='dispatch')
class NotificationViewSet(viewsets.ModelViewSet):
    serializer_class = NotificationSerializer
    filter_class = NotificationFilter
    permission_classes = [permissions.IsAuthenticated]
    renderer_classes = [BrowsableAPIRenderer, CamelCaseJSONRenderer]
    parser_classes = [CamelCaseJSONParser]
    http_method_names = ['get', 'post', 'head', 'put']

    def get_queryset(self):
        return Message.objects.filter(user=self.request.user).order_by('-created')

    @list_route(methods=['get'])
    def get_unread_count(self, request):
        return Response(status=200, data=self.get_queryset().filter(read=False).count())

    @list_route(methods=['post'])
    def mark_all_as_read(self, request):
        self.get_queryset().filter(read=False).update(read=True, modified=timezone.now())
        return Response(status=200)


class NoticeTypeViewSet(viewsets.ModelViewSet):
    serializer_class = NoticeTypeSerializer
    permission_classes = [ReadOnly]
    renderer_classes = [BrowsableAPIRenderer, CamelCaseJSONRenderer]
    parser_classes = [CamelCaseJSONParser]
    queryset = NoticeType.objects.all()
    pagination_class = None


class NoticeSettingViewSet(viewsets.ModelViewSet):
    serializer_class = NoticeSettingSerializers
    permission_classes = [permissions.IsAuthenticated]
    renderer_classes = [BrowsableAPIRenderer, CamelCaseJSONRenderer]
    parser_classes = [CamelCaseJSONParser]
    pagination_class = None

    def get_queryset(self):
        notice_types = NoticeType.objects.all()
        for notice_type in notice_types:
            for medium_id, medium_display in NOTICE_MEDIA:
                # This will create it with default values if it doesn't exist.
                NoticeSetting.for_user(self.request.user, notice_type, medium_id)

        return NoticeSetting.objects.filter(user=self.request.user)
{'fraction_non_alphanumeric': 0.07401395877292648, 'fraction_numerical': 0.0019477357571822756, 'mean_word_length': 5.520634920634921, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3818923', 'n_tokens_mistral': 1725, 'n_tokens_neox': 1676, 'n_words': 350}