通过项目来深入理解tornado(十):小组详情页完成
通过项目来深入理解tornado(十):小组详情页完成
前言
小组详情页主要就是数据的提取
首先需要验证用户是否登录,以及需要验证该用户是否在小组内
import os import json from playhouse.shortcuts import model_to_dict from tornado_bbs.handler import BaseHandler from tools.auth_dec import authenticated from .forms import CommunityGroupForm, GroupApplyForm import aiofiles import uuid from .models import CommunityGroup, CommunityGroupMember from tools.help_func import json_time class GroupDetailHandler(BaseHandler): @authenticated async def get(self, group_id,*args, **kwargs): re_data = {} group = CommunityGroup.extend().filter(CommunityGroup.id == group_id) groups = await self.application.objects.execute(group) try: for group in groups: await self.application.objects.get(CommunityGroupMember, community=group.id, status='agree', user=self.current_user) re_data = model_to_dict(group) except CommunityGroupMember.DoesNotExist: self.set_status(400) re_data['non_fields'] = '该用户不在组内' self.finish(json.dumps(re_data, default=json_time))
这里涉及到几个问题
1.如果取出小组的数据会涉及到外键,需要用之前写过的extend来构造sql语句,外键需要自己处理
2.返回的json数据中有datetime类型,需要额外用iosformat处理
from datetime import datetime, date def json_time(obj): if isinstance(obj , (date,datetime)): return obj.isoformat() else: raise TypeError