def retrieve(self, *args, **kwargs): with session_manager() as session: if self.get_permission(session): instance = self.get_object(session, *args, **kwargs) schema = self.get_schema(session) return {'code': 0, 'data': schema.dump(instance).data} else: return {'code': 1, 'msg': u'无此权限'}
def destroy(self, *args, **kwargs): with session_manager() as session: if self.delete_permission(session): instance = self.get_object(session) self.perform_destroy(session, instance) return {'code': 0, 'msg': u'成功'} else: return {'code': 1, 'msg': u'无此权限'}
def update(self, *args, **kwargs): with session_manager() as session: if self.put_permission(session): if self.schema: try: data = self.schema(self.body) except: return {'code': 1, 'msg': u'失败'} instance = self.perform_update( session, self.get_object(session, *args, **kwargs), data) return {'code': 0, 'data': self.schema.dump(instance).data} else: return {'code': 1, 'msg': u'无此权限'}
def create(self, *args, **kwargs): with session_manager() as session: if self.post_permission(session): if self.schema: try: data = self.schema(self.body) except: return {'code': 1, 'msg': u'失败'} obj = self.perform_create(session, data) if isinstance(obj, self.model): return { 'code': 0, 'msg': u'成功' } else: return obj else: return {'code': 1, 'msg': u'无此权限'}
def list(self, *args, **kwargs): with session_manager() as session: if self.get_permission(session): queryset = self.get_queryset(session) total = self.get_queryset_total(session, queryset) schema = self.get_schema(session, many=True) if self.pagination: page = self.paginate_queryset(queryset) if page: schema = self.get_schema(session, many=True) return { 'code': 0, 'total': total, 'data': schema.dump(page).data } return { 'code': 0, 'total': total, 'data': schema.dump(queryset).data } else: return {'code': 1, 'msg': u'无此权限'}
def destroy(self, *args, **kwargs): with session_manager() as session: if self.delete_permission(session): return self.remove(session, *args, **kwargs) else: return {'code': 1, 'msg': u'无此权限'}