example
This commit is contained in:
@@ -0,0 +1,18 @@
|
|||||||
|
from django.contrib import admin
|
||||||
|
from unfold.admin import ModelAdmin
|
||||||
|
from .models import NotifModel
|
||||||
|
from import_export.admin import ImportExportModelAdmin
|
||||||
|
from unfold.contrib.import_export.forms import ExportForm, ImportForm, SelectableFieldsExportForm
|
||||||
|
|
||||||
|
@admin.register(NotifModel)
|
||||||
|
class NotifModelAdmin(ModelAdmin, ImportExportModelAdmin):
|
||||||
|
import_form_class = ImportForm
|
||||||
|
export_form_class = ExportForm
|
||||||
|
compressed_fields = True
|
||||||
|
warn_unsaved_form = True
|
||||||
|
|
||||||
|
list_display = ('subject', 'priority', 'send_time', 'send_by', 'send_to_all')
|
||||||
|
search_fields = ('subject', 'description', 'send_by__email', 'send_to_branch__name')
|
||||||
|
list_filter = ('priority', 'send_time', 'send_to_all')
|
||||||
|
ordering = ('-send_time',)
|
||||||
|
filter_horizontal = ('send_to_branch', 'read_by')
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
from rest_framework import serializers
|
||||||
|
|
||||||
|
|
||||||
|
class GetNotifSerializer(serializers.ModelSerializer):
|
||||||
|
send_by = UserSerializer()
|
||||||
|
is_seen = serializers.SerializerMethodField()
|
||||||
|
read_by = UserSerializer(many=True)
|
||||||
|
class Meta:
|
||||||
|
model = NotifModel
|
||||||
|
fields = ('id', 'subject', 'priority', 'description', 'send_time', 'is_seen', 'send_by', 'read_by')
|
||||||
|
read_only_fields = ("read_by", "send_by")
|
||||||
|
def get_is_seen(self, obj):
|
||||||
|
user = self.context['user']
|
||||||
|
return user in obj.read_by.all()
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
from django.core.paginator import Paginator
|
||||||
|
from rest_framework.views import APIView
|
||||||
|
from .models import *
|
||||||
|
from .serializers import *
|
||||||
|
from rest_framework import status
|
||||||
|
from rest_framework.response import Response
|
||||||
|
from django.db.models import Q
|
||||||
|
from django.shortcuts import get_object_or_404
|
||||||
|
from rest_framework.permissions import IsAuthenticatedOrReadOnly
|
||||||
|
|
||||||
|
|
||||||
|
class CommentView(APIView):
|
||||||
|
serializer_class = CommentSerializer
|
||||||
|
permission_classes = [IsAuthenticatedOrReadOnly]
|
||||||
|
def get(self, request, pk):
|
||||||
|
product = get_object_or_404(ProductModel, id=pk)
|
||||||
|
comments = product.comments.filter(show=True)
|
||||||
|
comments_ser = self.serializer_class(instance=comments, many=True)
|
||||||
|
return Response({'comments': comments_ser.data}, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
|
def post(self, request, pk):
|
||||||
|
comment_ser = CommentSerializer(data=request.data)
|
||||||
|
product = get_object_or_404(ProductModel, id=pk)
|
||||||
|
if comment_ser.is_valid():
|
||||||
|
comment_ser.save(product=product, user=request.user)
|
||||||
|
return Response(comment_ser.data, status=status.HTTP_201_CREATED)
|
||||||
|
return Response(comment_ser.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
def delete(self, request, pk):
|
||||||
|
comment = get_object_or_404(CommentModel, pk=pk)
|
||||||
|
if comment.user == request.user:
|
||||||
|
comment.delete()
|
||||||
|
return Response(status=status.HTTP_204_NO_CONTENT)
|
||||||
|
else:
|
||||||
|
return Response({"detail": "شما اجازه ی پاک کردن این کامنت را ندارید"}, status=status.HTTP_403_FORBIDDEN)
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user