from django.shortcuts import render, redirect, HttpResponse
# Create your views here.
from .models import *
import datetime
import json
from django.http import JsonResponse
from django.contrib import auth
def index(request):
current_date = datetime.datetime.now().date() # 取到当前时间
book_date = request.GET.get("book_date", current_date.strftime("%Y-%m-%d")) # 取到预定时间
book_date = datetime.datetime.strptime(book_date, "%Y-%m-%d") # 格式化时间
time_choices = Book.time_choices # 取到model里定义的时间
room_list = Room.objects.all() # 取到所有房间
book_list = Book.objects.filter(date=book_date) # 筛选当前时间的所有预定信息
html = ""
for room in room_list: # 取到每一个的房间信息
s = '<tr><td>{0}({1})</td>'.format(room.caption, room.num)
for item in time_choices: # 取到每一个可以预定的时间段
flag = False
for book in book_list: # 取到每一个预定信息book
if book.room.caption == room.caption and book.time_id == item[0]: # 如果每一个预定信息的房间名称和时间段匹配成功
flag = True
break
if flag: # 如果预定信息存在
if request.user.pk != book.user.pk: # 如果当前登录用户不是预定用户
s += '<td class="active_other item" room_id="{0}" time_id="{1}">{2}</td>'.format(room.pk, item[0],
book.user.username)
else: # 如果当前登录用户就是预定用户
s += '<td class="active item" room_id="{0}" time_id="{1}">{2}</td>'.format(room.pk, item[0],
book.user.username)
else: # 如果当前房间当前时段没人预定,显示空白
s += '<td class="item" room_id="{0}" time_id="{1}"></td>'.format(room.pk, item[0])
s += "</tr>"
html += s # 拼出tr单元格
return render(request, 'index.html', locals())
def book(request):
print(request.POST)
response = {'status': True, 'msg': None, 'data': None}
try:
choice_date = request.POST.get('date') # 取出从前端get的数据
choice_date = datetime.datetime.strptime(choice_date, '%Y-%m-%d').date() # 转换日期成指定格式
post_data = json.loads(request.POST.get('data')) # 取出ajax传来的数据,并反序列化
# 优化 既在添加字典又在取消字典中 == 不添加 也不取消
for room_id, time_list in post_data['DEL'].items(): # 循环字典中每一个删除信息
if room_id not in post_data['ADD']: # 如果循环到的删除房间 没在 添加房间的字典中
continue # 结束当前循环进入下一个循环
for time_id in list(time_list): # 循环删除字典中每个房间的时间段
if time_id in post_data['ADD'][room_id]: # 如果这个时间段在当前循环房间 添加预订 的字典中
post_data['ADD'][room_id].remove(time_id) # 从添加预订的字典中移除 这个时间段
post_data['DEL'][room_id].remove(time_id) # 从取消预订的字典中移除 这个时间段
# 增加预定
book_obj_list = []
for room_id, time_list in post_data['ADD'].items():
for time_id in time_list:
obj = Book(room_id=room_id, time_id=time_id, user_id=request.user.pk, date=choice_date)
book_obj_list.append(obj) # 添加到预订房间的 列表中
Book.objects.bulk_create(book_obj_list) # 批量创建到数据库
# 删除会议室预定信息
print(post_data['DEL'])
from django.db.models import Q
remove_booking = Q()
for room_id, time_id_list in post_data['DEL'].items():
for time_id in time_id_list:
temp = Q()
temp.connector = 'AND'
temp.children.append(('user_id', request.user.pk,))
temp.children.append(('date', choice_date))
temp.children.append(('room_id', room_id,))
temp.children.append(('time_id', time_id,))
remove_booking.add(temp, 'OR')
if remove_booking:
Book.objects.filter(remove_booking).delete()
except Exception as e:
response['status'] = False
response['msg'] = str(e)
return JsonResponse(response)
def login(request):
if request.method == "POST":
user = request.POST.get("user")
pwd = request.POST.get("pwd")
user = auth.authenticate(username=user, password=pwd) # 用auth 模块对当前登录用户进行验证
if user:
auth.login(request, user)
return redirect("/index/")
return render(request, "login.html")
没有合适的资源?快使用搜索试试~ 我知道了~
Python会议室预定
共74个文件
pyc:20个
py:14个
xml:12个
4星 · 超过85%的资源 需积分: 34 90 下载量 100 浏览量
2018-07-04
22:56:40
上传
评论 3
收藏 411KB RAR 举报
温馨提示
一个基于Python的会议室预定系统, 主要实现会议室的合理分配。
资源推荐
资源详情
资源评论
收起资源包目录
MRBS会议室预订系统.rar (74个子文件)
MRBS
db.sqlite3 46KB
manage.py 802B
app01
admin.py 170B
models.py 1KB
migrations
__pycache__
__init__.cpython-36.pyc 137B
0001_initial.cpython-36.pyc 3KB
0001_initial.cpython-35.pyc 4KB
__init__.cpython-35.pyc 139B
__init__.py 0B
0001_initial.py 4KB
static
datetimepicker
less
datetimepicker.less 13KB
bootstrap-datetimepicker.min.css 11KB
bootstrap-datetimepicker.zh-CN.js 813B
bootstrap-datetimepicker.min.js 38KB
bootstrap
css
bootstrap.min.css 118KB
bootstrap.css.map 380KB
bootstrap-theme.min.css.map 25KB
bootstrap.min.css.map 529KB
bootstrap-theme.css.map 47KB
bootstrap-theme.css 26KB
bootstrap.css 143KB
bootstrap-theme.min.css 23KB
fonts
glyphicons-halflings-regular.svg 106KB
glyphicons-halflings-regular.woff 23KB
glyphicons-halflings-regular.ttf 44KB
glyphicons-halflings-regular.woff2 18KB
glyphicons-halflings-regular.eot 20KB
js
npm.js 484B
bootstrap.js 68KB
bootstrap.min.js 36KB
__init__.py 0B
js
jquery-1.12.4.min.js 95KB
jquery.cookie.js 3KB
tests.py 60B
__pycache__
__init__.cpython-36.pyc 126B
views.cpython-36.pyc 3KB
models.cpython-35.pyc 2KB
__init__.cpython-35.pyc 128B
models.cpython-36.pyc 2KB
admin.cpython-35.pyc 295B
views.cpython-35.pyc 3KB
admin.cpython-36.pyc 275B
__init__.py 0B
apps.py 85B
views.py 5KB
.idea
dataSources
8a6985a8-caf4-485e-98f3-c62fcdfcc4c1.xml 15KB
d1ee922b-b241-4321-a898-a5faac4cde92.xml 15KB
workspace.xml 45KB
encodings.xml 200B
dictionaries
yuan.xml 83B
yuanhao.xml 86B
dataSources.ids 23KB
misc.xml 997B
modules.xml 260B
.name 4B
MRBS.iml 1KB
inspectionProfiles
Project_Default.xml 504B
profiles_settings.xml 235B
dataSources.local.xml 840B
dataSources.xml 3KB
MRBS
settings.py 3KB
urls.py 887B
__pycache__
__init__.cpython-36.pyc 125B
wsgi.cpython-35.pyc 543B
__init__.cpython-35.pyc 127B
urls.cpython-36.pyc 1KB
wsgi.cpython-36.pyc 523B
settings.cpython-35.pyc 2KB
urls.cpython-35.pyc 1KB
settings.cpython-36.pyc 2KB
__init__.py 0B
wsgi.py 386B
templates
login.html 314B
index.html 5KB
共 74 条
- 1
资源评论
- 洛丹伦的雪2020-09-11根本没法用
- muyang80802019-10-28下载学习一下,感谢分享
- jluqcq2020-06-30还不错 学习了
weixin_40023361
- 粉丝: 4
- 资源: 13
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功