Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
mtianyan committed Jul 25, 2020
1 parent e70c2c6 commit 179c04b
Show file tree
Hide file tree
Showing 499 changed files with 71,575 additions and 49 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,4 @@ dmypy.json

# Pyre type checker
.pyre/
./front_end/node_modules
3 changes: 3 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

759 changes: 713 additions & 46 deletions .idea/workspace.xml

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions Mxonline3/pagination.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response
from collections import OrderedDict


class CustomPageNumberPagination(PageNumberPagination):
page_query_param = 'current'
page_size_query_param = 'pageSize'

def get_paginated_response(self, data):
return Response(OrderedDict([
('total', self.page.paginator.count),
('data', data),
('current', self.page.number),
('success', True),
('pageSize', self.page.paginator.per_page)
]))

def get_results(self, data):
"""必须和上面的一起修改,否则引起Filter与分页无法共存显示的bug"""
return data['data']
37 changes: 37 additions & 0 deletions Mxonline3/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
'captcha',
'pure_pagination',
'DjangoUeditor',
'rest_framework', # 添加rest framework,api服务依赖
'django_filters', # 添加过滤器,后台xadmin filter支持,rest framework filter支持
]
# 此处重载是为了使我们的UserProfile生效
AUTH_USER_MODEL = "users.UserProfile"
Expand Down Expand Up @@ -157,3 +159,38 @@
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
# STATIC_ROOT = os.path.join(BASE_DIR, 'static')
X_FRAME_OPTIONS = 'sameorigin'

REST_FRAMEWORK = {
# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users.

# 使用默认权限类
'DEFAULT_PERMISSION_CLASSES': [
# Model级别权限,未登录(匿名)只可读
# 'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
],
# 使用Django Filter 过滤器
'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',),

# 使用自定义的分页类,替换result为data
'DEFAULT_PAGINATION_CLASS': 'Mxonline3.pagination.CustomPageNumberPagination',
# 分页单页内容数量控制
'PAGE_SIZE': 10,

# 配置默认的解析类: JSON 与 XML解析类
'DEFAULT_PARSER_CLASSES': (
'rest_framework.parsers.JSONParser',
'rest_framework.parsers.FormParser',
'rest_framework.parsers.MultiPartParser'
),
# 默认的限速设置,匿名与已登录用户限速
'DEFAULT_THROTTLE_CLASSES': (
'rest_framework.throttling.AnonRateThrottle',
'rest_framework.throttling.UserRateThrottle'
),
# 'DEFAULT_THROTTLE_RATES': {
# 'anon': '100/day',
# 'user': '1000/day'
# }

}
48 changes: 45 additions & 3 deletions Mxonline3/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,47 @@
from django.views.generic import TemplateView
# from users.views import user_login
from Mxonline3.settings import MEDIA_ROOT
from courses.api_views import CourseViewSet, LessonViewSet, VideoViewSet, CourseResourceViewSet
from operation.api_views import UserAskViewSet, CourseCommentsViewSet, UserFavoriteViewSet, UserMessageViewSet, UserCourseViewSet
from organization.api_views import CityDictViewSet, CourseOrgViewSet, TeacherViewSet
from organization.views import OrgView
from users.api_views import EmailVerifyRecordViewSet, BannerViewSet, UserProfileViewSet, CurrentUserView, AdminLoginView, DashBoardView
from users.views import LoginView, RegisterView, ActiveUserView, ForgetPwdView, ResetView, ModifyPwdView, LogoutView, \
IndexView
IndexView, CaptchaView, AdminIndexView
from rest_framework.routers import DefaultRouter

router = DefaultRouter(trailing_slash=False)
router.register('email_verify_record/?', EmailVerifyRecordViewSet)

router.register('banner/?', BannerViewSet)

router.register('city_dict/?', CityDictViewSet)

router.register('course_org/?', CourseOrgViewSet)

router.register('teacher/?', TeacherViewSet)

router.register('course/?', CourseViewSet)

router.register('lesson/?', LessonViewSet)

router.register('video/?', VideoViewSet)

router.register('course_resource/?', CourseResourceViewSet)

router.register('user_ask/?', UserAskViewSet)

router.register('course_comments/?', CourseCommentsViewSet)

router.register('user_favorite/?', UserFavoriteViewSet)

router.register('user_message/?', UserMessageViewSet)

router.register('user_course/?', UserCourseViewSet)
router.register('users/?', UserProfileViewSet)
urlpatterns = [
path('admin/', admin.site.urls),
path('xadmin/', xadmin.site.urls),
# path('admin/', admin.site.urls),
path('admin/', xadmin.site.urls),
# TemplateView.as_view会将template转换为view
# path('', TemplateView.as_view(template_name= "index.html"), name= "index"),
path('', IndexView.as_view(), name="index"),
Expand Down Expand Up @@ -70,4 +104,12 @@

# 富文本相关url
path('ueditor/', include('DjangoUeditor.urls')),
path('xadmin/', AdminIndexView.as_view(), name="admin_index"),

path('api/v1/', include(router.urls)),
path('api/v1/user/currentUser', CurrentUserView.as_view(), name='user_current_user'),
path('api/v1/captcha-generate/', CaptchaView.as_view(), name='captcha'),
path('api/v1/user/login', AdminLoginView.as_view(), name='admin_login'),
path('api/v1/fake_chart_data', DashBoardView.as_view(), name='dashboard'),

]
24 changes: 24 additions & 0 deletions apps/courses/api_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from rest_framework import viewsets

from courses.models import Course, Lesson, Video, CourseResource
from courses.serializers import CourseSerializer, LessonSerializer, VideoSerializer, CourseResourceSerializer


class CourseViewSet(viewsets.ModelViewSet):
serializer_class = CourseSerializer
queryset = Course.objects.all()


class LessonViewSet(viewsets.ModelViewSet):
serializer_class = LessonSerializer
queryset = Lesson.objects.all()


class VideoViewSet(viewsets.ModelViewSet):
serializer_class = VideoSerializer
queryset = Video.objects.all()


class CourseResourceViewSet(viewsets.ModelViewSet):
serializer_class = CourseResourceSerializer
queryset = CourseResource.objects.all()
27 changes: 27 additions & 0 deletions apps/courses/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from rest_framework import serializers

from courses.models import Course, Lesson, Video, CourseResource


class CourseSerializer(serializers.ModelSerializer):
class Meta:
model = Course
fields = "__all__"


class LessonSerializer(serializers.ModelSerializer):
class Meta:
model = Lesson
fields = "__all__"


class VideoSerializer(serializers.ModelSerializer):
class Meta:
model = Video
fields = "__all__"


class CourseResourceSerializer(serializers.ModelSerializer):
class Meta:
model = CourseResource
fields = "__all__"
21 changes: 21 additions & 0 deletions apps/organization/api_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

from rest_framework import viewsets

from organization.models import CityDict, CourseOrg, Teacher
from organization.serializers import CityDictSerializer, CourseOrgSerializer, TeacherSerializer


class CityDictViewSet(viewsets.ModelViewSet):
serializer_class = CityDictSerializer
queryset = CityDict.objects.all()


class CourseOrgViewSet(viewsets.ModelViewSet):
serializer_class = CourseOrgSerializer
queryset = CourseOrg.objects.all()


class TeacherViewSet(viewsets.ModelViewSet):
serializer_class = TeacherSerializer
queryset = Teacher.objects.all()

23 changes: 23 additions & 0 deletions apps/organization/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

from rest_framework import serializers

from organization.models import CityDict, CourseOrg, Teacher


class CityDictSerializer(serializers.ModelSerializer):
class Meta:
model = CityDict
fields = "__all__"


class CourseOrgSerializer(serializers.ModelSerializer):
class Meta:
model = CourseOrg
fields = "__all__"


class TeacherSerializer(serializers.ModelSerializer):
class Meta:
model = Teacher
fields = "__all__"

1 change: 1 addition & 0 deletions apps/users/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@
# 我收藏的课程
path('my_message/', MyMessageView.as_view(), name="my_message"),


]
24 changes: 24 additions & 0 deletions apps/users/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# encoding: utf-8
import json

from captcha.helpers import captcha_image_url
from captcha.models import CaptchaStore
from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse
from pure_pagination import Paginator, EmptyPage, PageNotAnInteger
Expand All @@ -9,6 +11,8 @@
# Django自带的用户验证,login
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.backends import ModelBackend
from rest_framework import views
from rest_framework.response import Response

from courses.models import Course
from operation.models import UserCourse, UserFavorite, UserMessage
Expand Down Expand Up @@ -554,3 +558,23 @@ def get(self, request):
"banner_courses": banner_courses,
"course_orgs": course_orgs,
})


class CaptchaView(views.APIView):
permission_classes = ()

def get(self, request, *args, **kwargs):
new_key = CaptchaStore.pick()
response = {
'key': new_key,
'image_url': request.build_absolute_uri(location=captcha_image_url(new_key)),
}
return Response(response)


class AdminIndexView(View):
# 直接调用get方法免去判断
def get(self, request):
# render就是渲染html返回用户
# render三变量: request 模板名称 一个字典写明传给前端的值
return render(request, "admin/index.html")
19 changes: 19 additions & 0 deletions copy_static.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# TODO copy static
import os
import shutil
import glob

for one in glob.glob('./templates/admin/*'):
if one != './templates/admin/index.html':
if os.path.isdir(one):
for file_one in glob.glob(f'{one}/*'):
print(file_one)
to_file_one = file_one.replace('/templates/admin', '/static')
print(to_file_one)
shutil.move(file_one, to_file_one)
else:
print(one)
to_one = one.replace('/templates/admin', '/static')
print(to_one)
shutil.move(one, to_one)
#
Loading

0 comments on commit 179c04b

Please sign in to comment.