接口

Posted on 2019-08-09 14:57  大白不白  阅读(180)  评论(0编辑  收藏  举报


@list_route(methods=["POST"])
def activity_list(self, request):
per = get_permisson(request)
if not per:
return Response({"status": 0, "errormsg": "没有权限"})
name_filters = request.DATA.get("name_filters")
type_filter_list = request.DATA.get("type_filters")
status_filter_list = request.DATA.get("status_filters")
try:
type_filter_list = int_string_list(type_filter_list)
status_filter_list = int_string_list(status_filter_list)
except:
return Response({"status": 0, "errormsg": "非法请求"})
joiner_num = request.DATA.get("joiner_num")
create_time = request.DATA.get("create_time")
type_q = Q()
name_q = Q()
status_q = Q()
if per == 1:
per_q = Q(user=request.user)
else:
per_q = Q(center=get_center_for_user(request.user))
if type_filter_list:
type_q = Q(activity_type__in=type_filter_list)
if status_filter_list:
status_q = Q(status__in=status_filter_list)
if name_filters:
name_q = Q(title__icontains=name_filters)
order_condition = ORDER_BY_JOINER.get(joiner_num) or ORDER_BY_TIME.get(create_time)
if not order_condition:
order_condition = "-create_time"
activities = Activity.objects.filter(per_q, type_q, status_q, name_q).select_related("info", "user").order_by(order_condition)
res_data = paging(request, activities)
activities = res_data.pop("objs")
ser = ActivityListSerializer(activities, many=True)
res_data["activity_info"] = ser.data
res_data["permisson"] = PER_DIC.get(per)
return Response({"status": 1, "data": res_data})


def paging(request, objs, num=ONE_PAGE_NUM):
try:
current_page = request.DATA.get('page') or request.GET.get("page")
current_page = int(current_page)
except:
current_page = 1
p = Paginator(objs, num)
page_nums = p.num_pages
if current_page < 0 or current_page > page_nums:
current_page = 1
page = p.page(current_page)
obj = page.object_list
res_data = dict()
res_data["page_nums"] = page_nums
res_data["current_page"] = current_page
res_data["objs"] = obj
return res_data

Copyright © 2024 大白不白
Powered by .NET 9.0 on Kubernetes