
这里主要是处理和数据库连接的异常
import inspectfrom ..MySQL import loggerfrom ...models import schedulefrom django.db.models import Sumfrom django.core.paginator import Paginator,PageNotAnInteger,EmptyPageclass schedule_M():def __init__(self):self.class_name = "schedule_M()"def to_json(self, data, info="转成json"):try:return_data = {"id":data.id,"name":data.name,"content":data.content,"date":str(data.date)[0:10]}return return_dataexcept Exception as e:logger.error(info+'数据库操作失败: '+ self.class_name + inspect.currentframe().f_code.co_name + str(e))raise# 分页查日程def select_schedule(self, page, page_size, info):try:infoList=schedule.objects.filter(is_deleted = 0).order_by('-date')paginator=Paginator(infoList,page_size)try:pageInfo=paginator.page(page)except PageNotAnInteger:pageInfo=paginator.page(1)except EmptyPage:pageInfo=paginator.page(paginator.num_pages)else:return_data = list(map(self.to_json, pageInfo))return_data = {"data":return_data,"page":page,"num_pages":paginator.num_pages,"count":paginator.count}return return_data# data = [{'user':'xxx1','content':'xxxxxxxxx','date':'2023-11-03 00:00:00'},# {'user':'xxx2','content':'xxxxxxxxx','date':'2023-11-04 00:00:00'}]except Exception as e:logger.error(info+'数据库操作失败: '+ self.class_name + inspect.currentframe().f_code.co_name + str(e))raise# 新增一条日程def add_schedule(self, data, info):try:today = data['date']date_data = self.date_schedule(today, info)if date_data:return today+"已有日程"else:# data = {'name':'xxx', 'content':'xxxxxxxxx', 'date':'2023-11-03 00:00:00', }schedule.objects.create(**data)return "ok"except Exception as e:logger.error(info+'数据库操作失败: '+ self.class_name + inspect.currentframe().f_code.co_name + str(e))raise# 修改一条日程def update_schedule(self, id, data, info):try:# data = {'name':'xxx', 'content':'xxxxxxxxx', 'date':'2023-11-03 00:00:00', }schedule.objects.filter(id=id).update(**data)return "ok"except Exception as e:logger.error(info+'数据库操作失败: '+ self.class_name + inspect.currentframe().f_code.co_name + str(e))raise# 都删除一条日程def delete_schedule(self, id, info):try:# data = {'name':'xxx', 'content':'xxxxxxxxx', 'date':'2023-11-03 00:00:00', }schedule.objects.filter(id=id).update(is_deleted = 1)return "ok"except Exception as e:logger.error(info+'数据库操作失败: '+ self.class_name + inspect.currentframe().f_code.co_name + str(e))raise# 指定日期的日程def date_schedule(self, date, info):try:data = schedule.objects.get(date=date, is_deleted = 0)# data = [{'name':'xxx', 'content':'xxxxxxxxx', 'date':'2023-11-03 00:00:00', }]return_data = self.to_json(data)return return_dataexcept Exception as e:return {}