Changeset View
Changeset View
Standalone View
Standalone View
users/views/api.py
| """API for notification.""" | """API for notification.""" | ||||
| from django.contrib.auth.mixins import LoginRequiredMixin | from django.contrib.auth.mixins import LoginRequiredMixin | ||||
| from django.db import transaction | |||||
| from django.http import HttpResponseForbidden | from django.http import HttpResponseForbidden | ||||
| from django.http.response import JsonResponse | from django.http.response import JsonResponse | ||||
| from django.utils import timezone | |||||
| from django.views import View | from django.views import View | ||||
| from django.views.generic.detail import SingleObjectMixin | from django.views.generic.detail import SingleObjectMixin | ||||
| from django.utils import timezone | from django.contrib.auth import get_user_model | ||||
| from users.models import Notification | from users.models import Notification | ||||
| import users.tasks as tasks | |||||
| User = get_user_model() | |||||
| class NotificationMarkReadView(LoginRequiredMixin, SingleObjectMixin, View): | class NotificationMarkReadView(LoginRequiredMixin, SingleObjectMixin, View): | ||||
| """Allow users to mark specific notifications as read.""" | """Allow users to mark specific notifications as read.""" | ||||
| raise_exception = True | raise_exception = True | ||||
| model = Notification | model = Notification | ||||
| Show All 20 Lines | def post(self, request, *args, **kwargs): | ||||
| unread = self.model.objects.filter(user=request.user, date_read__isnull=True) | unread = self.model.objects.filter(user=request.user, date_read__isnull=True) | ||||
| now = timezone.now() | now = timezone.now() | ||||
| for notification in unread: | for notification in unread: | ||||
| notification.date_read = now | notification.date_read = now | ||||
| Notification.objects.bulk_update(unread, ['date_read']) | Notification.objects.bulk_update(unread, ['date_read']) | ||||
| return JsonResponse({}) | return JsonResponse({}) | ||||
| class UserAPIView(LoginRequiredMixin, SingleObjectMixin, View): | |||||
| """Allow users to request deletion of their accounts.""" | |||||
| raise_exception = True | |||||
| model = User | |||||
| @transaction.atomic | |||||
| def delete(self, request, *args, **kwargs): | |||||
| """Mark this user account for deletion.""" | |||||
| user = request.user | |||||
| # Already marked for deletion | |||||
| if user.date_deletion_requested: | |||||
| error_msg = 'Deletion of this account had already been requested' | |||||
| return JsonResponse({'errors': [error_msg]}, status=400) | |||||
| # Check if account can be deleted | |||||
| if not user.can_be_deleted: | |||||
| error_msg = 'This account cannot be deleted' | |||||
| return JsonResponse({'errors': [error_msg]}, status=400) | |||||
| tasks.confirm_deletion_request(pk=user.pk) | |||||
| return JsonResponse({}, status=202) | |||||