mirror of
https://github.com/lemeow125/DRF_Template.git
synced 2025-06-28 16:15:44 +08:00
Clean up docker-compose and run Black formatter over entire codebase
This commit is contained in:
parent
6c232b3e89
commit
069aba80b1
60 changed files with 1946 additions and 1485 deletions
|
@ -6,5 +6,5 @@ from .models import Notification
|
|||
@admin.register(Notification)
|
||||
class NotificationAdmin(ModelAdmin):
|
||||
model = Notification
|
||||
search_fields = ('id', 'content')
|
||||
list_display = ['id', 'dismissed']
|
||||
search_fields = ("id", "content")
|
||||
list_display = ["id", "dismissed"]
|
||||
|
|
|
@ -2,8 +2,8 @@ from django.apps import AppConfig
|
|||
|
||||
|
||||
class NotificationsConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'notifications'
|
||||
default_auto_field = "django.db.models.BigAutoField"
|
||||
name = "notifications"
|
||||
|
||||
def ready(self):
|
||||
import notifications.signals
|
||||
|
|
|
@ -15,13 +15,27 @@ class Migration(migrations.Migration):
|
|||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Notification',
|
||||
name="Notification",
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('content', models.CharField(max_length=1000, null=True)),
|
||||
('timestamp', models.DateTimeField(auto_now_add=True)),
|
||||
('dismissed', models.BooleanField(default=False)),
|
||||
('recipient', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
|
||||
(
|
||||
"id",
|
||||
models.BigAutoField(
|
||||
auto_created=True,
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
verbose_name="ID",
|
||||
),
|
||||
),
|
||||
("content", models.CharField(max_length=1000, null=True)),
|
||||
("timestamp", models.DateTimeField(auto_now_add=True)),
|
||||
("dismissed", models.BooleanField(default=False)),
|
||||
(
|
||||
"recipient",
|
||||
models.ForeignKey(
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
to=settings.AUTH_USER_MODEL,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
]
|
||||
|
|
|
@ -2,8 +2,7 @@ from django.db import models
|
|||
|
||||
|
||||
class Notification(models.Model):
|
||||
recipient = models.ForeignKey(
|
||||
'accounts.CustomUser', on_delete=models.CASCADE)
|
||||
recipient = models.ForeignKey("accounts.CustomUser", on_delete=models.CASCADE)
|
||||
content = models.CharField(max_length=1000, null=True)
|
||||
timestamp = models.DateTimeField(auto_now_add=True, editable=False)
|
||||
dismissed = models.BooleanField(default=False)
|
||||
|
|
|
@ -3,10 +3,9 @@ from notifications.models import Notification
|
|||
|
||||
|
||||
class NotificationSerializer(serializers.ModelSerializer):
|
||||
timestamp = serializers.DateTimeField(
|
||||
format="%m-%d-%Y %I:%M %p", read_only=True)
|
||||
timestamp = serializers.DateTimeField(format="%m-%d-%Y %I:%M %p", read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = Notification
|
||||
fields = '__all__'
|
||||
read_only_fields = ('id', 'recipient', 'content', 'timestamp')
|
||||
fields = "__all__"
|
||||
read_only_fields = ("id", "recipient", "content", "timestamp")
|
||||
|
|
|
@ -9,5 +9,5 @@ from django.core.cache import cache
|
|||
@receiver(post_save, sender=Notification)
|
||||
def clear_cache_after_notification_update(sender, instance, **kwargs):
|
||||
# Clear cache
|
||||
cache.delete('notifications')
|
||||
cache.delete(f'notifications_user:{instance.recipient.id}')
|
||||
cache.delete("notifications")
|
||||
cache.delete(f"notifications_user:{instance.recipient.id}")
|
||||
|
|
|
@ -9,5 +9,4 @@ def cleanup_notifications():
|
|||
three_days_ago = timezone.now() - timezone.timedelta(days=3)
|
||||
|
||||
# Delete notifications that are older than 3 days and dismissed
|
||||
Notification.objects.filter(
|
||||
dismissed=True, timestamp__lte=three_days_ago).delete()
|
||||
Notification.objects.filter(dismissed=True, timestamp__lte=three_days_ago).delete()
|
||||
|
|
|
@ -3,8 +3,7 @@ from notifications.views import NotificationViewSet
|
|||
from rest_framework.routers import DefaultRouter
|
||||
|
||||
router = DefaultRouter()
|
||||
router.register(r'', NotificationViewSet,
|
||||
basename="Notifications")
|
||||
router.register(r"", NotificationViewSet, basename="Notifications")
|
||||
urlpatterns = [
|
||||
path('', include(router.urls)),
|
||||
path("", include(router.urls)),
|
||||
]
|
||||
|
|
|
@ -6,30 +6,33 @@ from django.core.cache import cache
|
|||
|
||||
|
||||
class NotificationViewSet(viewsets.ModelViewSet):
|
||||
http_method_names = ['get', 'patch', 'delete']
|
||||
http_method_names = ["get", "patch", "delete"]
|
||||
serializer_class = NotificationSerializer
|
||||
queryset = Notification.objects.all()
|
||||
|
||||
def get_queryset(self):
|
||||
user = self.request.user
|
||||
key = f'notifications_user:{user.id}'
|
||||
key = f"notifications_user:{user.id}"
|
||||
queryset = cache.get(key)
|
||||
if not queryset:
|
||||
queryset = Notification.objects.filter(
|
||||
recipient=user).order_by('-timestamp')
|
||||
cache.set(key, queryset, 60*60)
|
||||
queryset = Notification.objects.filter(recipient=user).order_by(
|
||||
"-timestamp"
|
||||
)
|
||||
cache.set(key, queryset, 60 * 60)
|
||||
return queryset
|
||||
|
||||
def update(self, request, *args, **kwargs):
|
||||
instance = self.get_object()
|
||||
if instance.recipient != request.user:
|
||||
raise PermissionDenied(
|
||||
"You do not have permission to update this notification.")
|
||||
"You do not have permission to update this notification."
|
||||
)
|
||||
return super().update(request, *args, **kwargs)
|
||||
|
||||
def destroy(self, request, *args, **kwargs):
|
||||
instance = self.get_object()
|
||||
if instance.recipient != request.user:
|
||||
raise PermissionDenied(
|
||||
"You do not have permission to delete this notification.")
|
||||
"You do not have permission to delete this notification."
|
||||
)
|
||||
return super().destroy(request, *args, **kwargs)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue