-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
37 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,13 @@ | ||
from django.urls import re_path | ||
|
||
from . import views | ||
from app.views import post, product | ||
from app.views import post, product, event | ||
|
||
urlpatterns = [ | ||
re_path(r'^posts$', post.post_list), | ||
re_path(r'^post/(?P<post_id>\d+)$', post.post_detail), | ||
|
||
re_path(r'products$', product.product_list), | ||
re_path(r'product/(?P<product_id>\d+)$', product.product_detail), | ||
# | ||
# re_path(r'events$', views.query_events) | ||
|
||
re_path(r'events$', event.event_list) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +0,0 @@ | ||
from django.shortcuts import render | ||
from app.serializers.event import EventListSerializer | ||
from app.service.event import index as event_index | ||
|
||
from rest_framework import viewsets, status, permissions | ||
from rest_framework.parsers import JSONParser | ||
from rest_framework.response import Response | ||
from rest_framework.decorators import action, api_view, permission_classes, parser_classes | ||
|
||
# from app.core import post_crawl | ||
|
||
# Create your views here. | ||
|
||
|
||
# @api_view(['GET']) | ||
# @permission_classes((permissions.AllowAny, )) | ||
# @parser_classes((JSONParser,)) | ||
# def crawl_post(request): | ||
# res = post_crawl.executor_post() | ||
# post_crawl.bulk_update(res) | ||
# return Response(render_success(res), status=status.HTTP_200_OK) | ||
|
||
@api_view(['GET']) | ||
@permission_classes((permissions.AllowAny, )) | ||
@parser_classes((JSONParser,)) | ||
def query_events(request): | ||
page = int(request.GET.get('page', 1)) | ||
limit = int(request.GET.get('size', 10)) | ||
|
||
events, total = event_index(page=page, limit=limit) | ||
serializer = EventListSerializer(events, many=True) | ||
|
||
resp_data = render_page_resp(page, limit, total, serializer.data) | ||
return Response(render_success(resp_data), status=status.HTTP_200_OK) | ||
|
||
|
||
def render_page_resp(page, limit, total, items): | ||
return { | ||
'pageBean': { | ||
'page': page, | ||
'size': limit, | ||
'total': total, | ||
}, | ||
'items': items, | ||
} | ||
|
||
|
||
def render_success(data): | ||
return { | ||
'success': True, | ||
'message': '', | ||
'results': data | ||
} | ||
|
||
|
||
def render_failure(reason): | ||
return { | ||
'success': False, | ||
'message': reason, | ||
'results': None | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from app.serializers.event import EventListSerializer | ||
|
||
from rest_framework import permissions | ||
from rest_framework.parsers import JSONParser | ||
from rest_framework.decorators import api_view, permission_classes, parser_classes | ||
|
||
from django.core.paginator import Paginator | ||
|
||
from app.views import render | ||
from app.models import Event, PageBean | ||
|
||
|
||
@api_view(['GET']) | ||
@permission_classes((permissions.AllowAny, )) | ||
@parser_classes((JSONParser,)) | ||
def event_list(request): | ||
try: | ||
page = int(request.GET.get('page', 1)) | ||
limit = int(request.GET.get('size', 10)) | ||
except ValueError: | ||
return render.parameters_illegal() | ||
|
||
all_events = Event.objects.all().order_by('-register_end_date').filter(is_available=True) | ||
total = len(all_events) | ||
paginator = Paginator(all_events, limit) | ||
page_bean = PageBean(page, limit, total) | ||
|
||
if paginator.num_pages < page: | ||
return render.page_success(page_bean, []) | ||
|
||
events = paginator.page(page) | ||
|
||
serializer = EventListSerializer(events, many=True) | ||
return render.page_success(page_bean, serializer.data) |