게시글모델에 작성시간및 기간정해두기
startdate =models.DateTimeField("노출 시작", default=datetime.datetime.now())
enddate = models.DateTimeField("노출 종료", default=(datetime.datetime.now()+timedelta(days=7)))
class GenericAPIException(APIException):
def __init__(self, status_code, detail=None, code=None):
self.status_code = status_code
super().__init__(detail=detail, code=code)
class IsAdminOrIsAuthenticatedReadOnly(BasePermission):
"""
admin 사용자는 모두 가능, 로그인 사용자는 조회만 가능
"""
SAFE_METHODS = ('GET',)
message = '접근 권한이 없습니다.'
def has_permission(self, request, view):
user = request.user
if not user.is_authenticated:
response = {
"detail": "서비스를 이용하기 위해 로그인 해주세요.",
}
raise GenericAPIException(status_code=status.HTTP_401_UNAUTHORIZED, detail=response)
if request.method=='GET':
if user.is_authenticated and user.is_admin:
return True
elif user.is_authenticated and request.method in self.SAFE_METHODS:
return True
return False
if request.method=='POST':
if user.is_authenticated and user.is_admin:
return True
return bool(request.user and request.user.join_date < (timezone.now() - timedelta(days=3)))
class BlogView(APIView):
permission_classes = [IsAdminOrIsAuthenticatedReadOnly]
def get(self, request):
time = timezone.now()
articles = Article.objects.filter(user=request.user,enddate__gt=time).order_by('-startdate')
titles = [article.title for article in articles]
for article in articles:
if article.enddate > time:
titles.append(article.title)
return Response({'titles': titles})
class UserAdmin(BaseUserAdmin):
list_display = ('id', 'username', 'fullname', 'email')
list_display_links = ('username', )
list_filter = ('username', )
search_fields = ('username',)
fieldsets = (
("info", {'fields': ('username', 'password', 'email', 'fullname', 'join_date',)}),
('Permissions', {'fields': ('is_admin', 'is_active', )}),)
filter_horizontal = []
def get_readonly_fields(self, request, obj=None):
if obj:
return ('username', 'join_date', )
else:
return ('join_date', )
inlines = (
UserProfileInline,
)