添加教程的API
This commit is contained in:
@@ -6,12 +6,8 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|||||||
|
|
||||||
DATABASES = {
|
DATABASES = {
|
||||||
"default": {
|
"default": {
|
||||||
"ENGINE": "django.db.backends.postgresql",
|
"ENGINE": "django.db.backends.sqlite3",
|
||||||
"HOST": get_env("POSTGRES_HOST", "127.0.0.1"),
|
"NAME": os.path.join(BASE_DIR, "db.sqlite3"),
|
||||||
"PORT": get_env("POSTGRES_PORT", "5432"),
|
|
||||||
"NAME": get_env("POSTGRES_DB", "onlinejudge"),
|
|
||||||
"USER": get_env("POSTGRES_USER", "onlinejudge"),
|
|
||||||
"PASSWORD": get_env("POSTGRES_PASSWORD", "onlinejudge"),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -52,7 +52,8 @@ LOCAL_APPS = [
|
|||||||
'options',
|
'options',
|
||||||
'judge',
|
'judge',
|
||||||
'message',
|
'message',
|
||||||
'comment'
|
'comment',
|
||||||
|
'tutorial',
|
||||||
]
|
]
|
||||||
|
|
||||||
INSTALLED_APPS = VENDOR_APPS + LOCAL_APPS
|
INSTALLED_APPS = VENDOR_APPS + LOCAL_APPS
|
||||||
@@ -209,10 +210,10 @@ def redis_config(db):
|
|||||||
"KEY_FUNCTION": make_key
|
"KEY_FUNCTION": make_key
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if production_env:
|
||||||
CACHES = {
|
CACHES = {
|
||||||
"default": redis_config(db=1)
|
"default": redis_config(db=1)
|
||||||
}
|
}
|
||||||
|
|
||||||
SESSION_ENGINE = "django.contrib.sessions.backends.cache"
|
SESSION_ENGINE = "django.contrib.sessions.backends.cache"
|
||||||
SESSION_CACHE_ALIAS = "default"
|
SESSION_CACHE_ALIAS = "default"
|
||||||
|
|||||||
@@ -17,4 +17,6 @@ urlpatterns = [
|
|||||||
path("api/", include("message.urls.oj")),
|
path("api/", include("message.urls.oj")),
|
||||||
path("api/", include("comment.urls.oj")),
|
path("api/", include("comment.urls.oj")),
|
||||||
path("api/admin/", include("comment.urls.admin")),
|
path("api/admin/", include("comment.urls.admin")),
|
||||||
|
path("api/", include("tutorial.urls.tutorial")),
|
||||||
|
path("api/admin/", include("tutorial.urls.admin")),
|
||||||
]
|
]
|
||||||
|
|||||||
1
tutorial/__init__.py
Normal file
1
tutorial/__init__.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
|
||||||
17
tutorial/models.py
Normal file
17
tutorial/models.py
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
from django.db import models
|
||||||
|
from account.models import User
|
||||||
|
|
||||||
|
class Tutorial(models.Model):
|
||||||
|
title = models.CharField(max_length=128)
|
||||||
|
content = models.TextField()
|
||||||
|
created_by = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||||
|
created_at = models.DateTimeField(auto_now_add=True)
|
||||||
|
updated_at = models.DateTimeField(auto_now=True)
|
||||||
|
is_public = models.BooleanField(default=False)
|
||||||
|
order = models.IntegerField(default=0)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
ordering = ['order', '-created_at']
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.title
|
||||||
12
tutorial/serializers.py
Normal file
12
tutorial/serializers.py
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
from rest_framework import serializers
|
||||||
|
from .models import Tutorial
|
||||||
|
from account.serializers import UserSerializer
|
||||||
|
|
||||||
|
class TutorialSerializer(serializers.ModelSerializer):
|
||||||
|
created_by = UserSerializer(read_only=True)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = Tutorial
|
||||||
|
fields = ['id', 'title', 'content', 'created_by', 'created_at',
|
||||||
|
'updated_at', 'is_public', 'order']
|
||||||
|
read_only_fields = ['created_by', 'created_at', 'updated_at']
|
||||||
1
tutorial/urls/__init__.py
Normal file
1
tutorial/urls/__init__.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
|
||||||
10
tutorial/urls/admin.py
Normal file
10
tutorial/urls/admin.py
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
from django.urls import path, include
|
||||||
|
from rest_framework.routers import DefaultRouter
|
||||||
|
from ..views.admin import AdminTutorialViewSet
|
||||||
|
|
||||||
|
router = DefaultRouter()
|
||||||
|
router.register(r'tutorials', AdminTutorialViewSet)
|
||||||
|
|
||||||
|
urlpatterns = [
|
||||||
|
path('', include(router.urls)),
|
||||||
|
]
|
||||||
10
tutorial/urls/tutorial.py
Normal file
10
tutorial/urls/tutorial.py
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
from django.urls import path, include
|
||||||
|
from rest_framework.routers import DefaultRouter
|
||||||
|
from ..views.tutorial import TutorialViewSet
|
||||||
|
|
||||||
|
router = DefaultRouter()
|
||||||
|
router.register(r'tutorials', TutorialViewSet)
|
||||||
|
|
||||||
|
urlpatterns = [
|
||||||
|
path('', include(router.urls)),
|
||||||
|
]
|
||||||
17
tutorial/views/admin.py
Normal file
17
tutorial/views/admin.py
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
from rest_framework import viewsets, permissions
|
||||||
|
from account.decorators import super_admin_required
|
||||||
|
from ..models import Tutorial
|
||||||
|
from ..serializers import TutorialSerializer
|
||||||
|
|
||||||
|
class AdminTutorialViewSet(viewsets.ModelViewSet):
|
||||||
|
queryset = Tutorial.objects.all()
|
||||||
|
serializer_class = TutorialSerializer
|
||||||
|
permission_classes = [permissions.IsAuthenticated]
|
||||||
|
|
||||||
|
def get_permissions(self):
|
||||||
|
if self.action in ['create', 'update', 'partial_update', 'destroy']:
|
||||||
|
return [super_admin_required()]
|
||||||
|
return [permissions.AllowAny()]
|
||||||
|
|
||||||
|
def perform_create(self, serializer):
|
||||||
|
serializer.save(created_by=self.request.user)
|
||||||
8
tutorial/views/tutorial.py
Normal file
8
tutorial/views/tutorial.py
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
from rest_framework import viewsets, permissions
|
||||||
|
from ..models import Tutorial
|
||||||
|
from ..serializers import TutorialSerializer
|
||||||
|
|
||||||
|
class TutorialViewSet(viewsets.ReadOnlyModelViewSet):
|
||||||
|
queryset = Tutorial.objects.filter(is_public=True)
|
||||||
|
serializer_class = TutorialSerializer
|
||||||
|
permission_classes = [permissions.AllowAny]
|
||||||
Reference in New Issue
Block a user