μ΄λ²μλ serializer λ₯Ό μμ©νλ μ¬ν κ³Όμ μ λ°μλ€.
- permissions.py λ‘ κ΄λ¦¬μ λ° μ¬μ©μμ κΆνμ λΆμ¬νκ³
- μν©μ λ°λ₯Έ 쑰건μ μ μν΄μ μ‘°ν λ° μμ κΆνμ λΆλ¦¬ν μ μμ.
- ex) 7μΌμ΄ μ§λμ§ μμ μ¬μ©μλ κ²μκΈ μμ± λΆκ° λ±
λ¨ μκ°μ λ무λ λ§μ μ 보λμ μ΅λνλ €λ
λκ° μ₯κ° λκΈ° μμνλ€..
κ³μ μ£Όμ
νλ €λ λ°μ΄ν°λ₯Ό λλ§μ λ°©λ²μΌλ‘ μ‘°κΈμ©..
μ‘°κΈμ© μννκ³ μ νλ€..
λΆλ 체νμ§ λ§κ³ , μ ν‘μν μ μκΈ°λ₯Ό...
μλ¬ λ
ΈνΈ
λλΆλΆ μ€νλ‘ μΈν μλ¬..
λ―ΈμΉκ²λ€..
4μΌμ°¨ κ³Όμ μ¬ν
1. admin νμ΄μ§μ user adminμ λ±λ‘νκ³ , userprofile ν
μ΄λΈμ user admin νμ΄μ§μμ κ°μ΄ λ³΄κ³ μ€μ ν μ μλλ‘ ν΄μ£ΌμΈμ
class UserAdmin(BaseUserAdmin):
list_display = ('id', 'username', 'fullname', 'email')
list_display_links = ('username', )
list_filter = ('username', )
search_fields = ('username', 'email', )
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', )
2. article ν
μ΄λΈμ <λ
ΈμΆ μμ μΌμ, λ
ΈμΆ μ’
λ£ μΌμ>μ μΆκ°ν΄μ£ΌμΈμ
class Article(models.Model):
user = models.ForeignKey('user.User', verbose_name="κΈ μμ±μ", on_delete=models.CASCADE)
title = models.CharField("κΈ μ λͺ©", max_length=50)
content = models.TextField("κΈ λ³Έλ¬Έ")
category = models.ManyToManyField(Category, verbose_name="μΉ΄ν
κ³ λ¦¬")
exposure_start = models.DateField('λ
ΈμΆ μμ μΌμ', auto_now_add=True)
exposure_end = models.DateField('λ
ΈμΆ μ’
λ£ μΌμ', auto_now=True)
3. article viewμ κ²μκΈ μ‘°ν κΈ°λ₯μ λ§λ€λ, νμ¬ μΌμλ₯Ό κΈ°μ€μΌλ‘ λ
ΈμΆ μμ μΌμμ λ
ΈμΆ μ’
λ£ μΌμ μ¬μ΄μ μλ νλͺ©λ€λ§ 리ν΄ν΄μ£Όλλ‘ νν°λ₯Ό μ€μ ν΄μ£ΌμΈμ
- λ¦¬ν΄ λ°μ΄ν°λ κ²μκΈ μμ±μΌ κΈ°μ€μΌλ‘ μ λ ¬νμ¬ μ΅κ·Ό μ΄ κΈμ΄ κ°μ₯ λ¨Όμ μ¬λΌμ€λλ‘ ν΄μ£ΌμΈμ
class ArticleView(APIView):
# permission_classes = [permissions.IsAuthenticated]
# permission_classes = [permissions.AllowAny]
permission_classes = [RegisterdMoreThanThreeDaysUser] # 컀μ€ν
permissions
def get(self, request):
user = request.user
today = timezone.now()
articles = ArticleModel.objects.filter(
exposure_start__lte = today,
exposure_end__gte = today,
).order_by("-id")
serializer = ArticleSerializer(articles, many=True).data
# titles = [article.title for article in articles] # list μΆμ½ λ¬Έλ²
# titles = []
# for article in articles:
# titles.append(article.title)
return Response(serializer, status=status.HTTP_200_OK)
4. κΈ°μ‘΄ article μμ± κΈ°λ₯μ μ μ§νλ, articleμ admin user νΉμ κ°μ
ν 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 IsAdminOrAuthenticatedReadOnly(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 user.is_authenticated and request.method in self.SAFE_METHODS:
return True
if user.is_authenticated and user.is_admin or \
user.join_date < (datetime.now().date() - timedelta(days=7)):
return True
return False