hand
_1_5_111
4
返回栏目
1k
9k
1k
1k
5k
1k
1k
1k
1k
3k
2k
1k
0.8k
2k
3k
1k
1k
0.7k
0.9k
1k
0.6k
0.4k
0.4k
0.3k
3k
2k
9k
0.4k
0.4k
0.8k
0.5k
3k
5k
1k
2k
2k
3k
5k
1k
1k
0.4k
0.5k
0.4k
0.6k
0.7k
1k
0.4k
0.3k
4k
0.5k
0k
0.3k
0k
0.2k
0.2k
0.3k
0.9k
0.9k
0.1k
0.9k
0.9k
1k
0.5k
6k
0.3k
0.4k
0.7k
0.6k
8k
3k
1k
1k
1k
1k
0k
2k
1k
1k
0.2k
5k
4k
5k
0.4k
0.8k
1k
1k
1k
0.1k
2k
1k
2k
6k
0k
2k
7k
1k
5k
2k
3k
1k
0k
1k
0.9k
0.4k
0.2k
1k
3k
4k
1k
1k
1k
2k
3k
0.7k
0.3k
0.5k
0.6k
1k
0.9k
3k
0.3k
4k
返回python栏目
作者:
贺及楼
成为作者
更新日期:2023-12-08 15:00:34
这里主要是处理和数据库连接的异常
import inspect
from ..MySQL import logger
from ...models import schedule
from django.db.models import Sum
from django.core.paginator import Paginator,PageNotAnInteger,EmptyPage
class 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_data
except 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_data
except Exception as e:
return {}
python
整章节共122节
快分享给你的小伙伴吧 ~