import json
import datetime
from django.shortcuts import render, HttpResponse, redirect
from django.http import JsonResponse
from . import models
from django.forms import Form
from django.forms import fields
from django.forms import widgets
from django.db.models import Q
from django.db.utils import IntegrityError
class LoginForm(Form):
name = fields.CharField(
required=True,
error_messages={'required': '用户名不能为空'},
widget=widgets.TextInput(attrs={'class': 'form-control', 'placeholder': '用户名', 'id': 'name'})
)
password = fields.CharField(
required=True,
error_messages={'required': '密码不能为空'},
widget=widgets.PasswordInput(attrs={'class': 'form-control', 'placeholder': '密码', 'id': 'password'})
)
rmb = fields.BooleanField(required=False, widget=widgets.CheckboxInput(attrs={'value': 1}))
def md5(val):
import hashlib
m = hashlib.md5()
m.update(val.encode('utf-8'))
return m.hexdigest()
def auth(func):
def inner(request, *args, **kwargs):
user_info = request.session.get('user_info')
if not user_info:
return redirect('/login/')
return func(request, *args, **kwargs)
return inner
def auth_json(func):
def inner(request, *args, **kwargs):
user_info = request.session.get('user_info')
if not user_info:
return JsonResponse({'status': False, 'msg': '用户未登录'})
return func(request, *args, **kwargs)
return inner
def login(request):
"""
用户登录
"""
if request.method == "GET":
form = LoginForm()
return render(request, 'login.html', {'form': form})
else:
form = LoginForm(request.POST)
if form.is_valid():
rmb = form.cleaned_data.pop('rmb')
form.cleaned_data['password'] = md5(form.cleaned_data['password'])
user = models.UserInfo.objects.filter(**form.cleaned_data).first()
if user:
request.session['user_info'] = {'id': user.id, 'name': user.name}
if rmb:
request.session.set_expiry(60 * 60 * 24 * 30)
return redirect('/index/')
else:
form.add_error('password', '密码错误')
return render(request, 'login.html', {'form': form})
else:
return render(request, 'login.html', {'form': form})
@auth
def index(request):
"""
会议室预定首页
:param request:
:return:
"""
time_choices = models.Booking.time_choices
return render(request, 'index.html', {'time_choices': time_choices})
@auth_json
def booking(request):
"""
获取会议室预定情况以及预定会议室
:param request:
:param date:
:return:
"""
ret = {'code': 1000, 'msg': None, 'data': None}
current_date = datetime.datetime.now().date()
if request.method == "GET":
try:
fetch_date = request.GET.get('date')
fetch_date = datetime.datetime.strptime(fetch_date, '%Y-%m-%d').date()
if fetch_date < current_date:
raise Exception('查询时间不能是以前的时间')
booking_list = models.Booking.objects.filter(booking_date=fetch_date).select_related('user',
'room').order_by(
'booking_time')
booking_dict = {}
for item in booking_list:
if item.room_id not in booking_dict:
booking_dict[item.room_id] = {item.booking_time: {'name': item.user.name, 'id': item.user.id}}
else:
if item.booking_time not in booking_dict[item.room_id]:
booking_dict[item.room_id][item.booking_time] = {'name': item.user.name, 'id': item.user.id}
"""
{
room_id:{
time_id:{''},
time_id:{''},
time_id:{''},
}
}
"""
room_list = models.MeetingRoom.objects.all()
booking_info = []
for room in room_list:
temp = [{'text': room.title, 'attrs': {'rid': room.id}, 'chosen': False}]
for choice in models.Booking.time_choices:
v = {'text': '', 'attrs': {'time-id': choice[0], 'room-id': room.id}, 'chosen': False}
if room.id in booking_dict and choice[0] in booking_dict[room.id]:
v['text'] = booking_dict[room.id][choice[0]]['name']
v['chosen'] = True
if booking_dict[room.id][choice[0]]['id'] != request.session['user_info']['id']:
v['attrs']['disable'] = 'true'
temp.append(v)
booking_info.append(temp)
ret['data'] = booking_info
except Exception as e:
ret['code'] = 1001
ret['msg'] = str(e)
return JsonResponse(ret)
else:
try:
booking_date = request.POST.get('date')
booking_date = datetime.datetime.strptime(booking_date, '%Y-%m-%d').date()
if booking_date < current_date:
raise Exception('查询时间不能是以前的时间')
booking_info = json.loads(request.POST.get('data'))
for room_id, time_id_list in booking_info['add'].items():
if room_id not in booking_info['del']:
continue
for time_id in list(time_id_list):
if time_id in booking_info['del'][room_id]:
booking_info['del'][room_id].remove(time_id)
booking_info['add'][room_id].remove(time_id)
add_booking_list = []
for room_id, time_id_list in booking_info['add'].items():
for time_id in time_id_list:
obj = models.Booking(
user_id=request.session['user_info']['id'],
room_id=room_id,
booking_time=time_id,
booking_date=booking_date
)
add_booking_list.append(obj)
models.Booking.objects.bulk_create(add_booking_list)
remove_booking = Q()
for room_id, time_id_list in booking_info['del'].items():
for time_id in time_id_list:
temp = Q()
temp.connector = 'AND'
temp.children.append(('user_id', request.session['user_info']['id'],))
temp.children.append(('booking_date', booking_date,))
temp.children.append(('room_id', room_id,))
temp.children.append(('booking_time', time_id,))
remove_booking.add(temp, 'OR')
if remove_booking:
models.Booking.objects.filter(remove_booking).delete()
except IntegrityError as e:
ret['code'] = 1011
ret['msg'] = '会议室已被预定'
except Exception as e:
ret['code'] = 1012
ret['msg'] = '预定失败:%s' % str(e)
return JsonResponse(ret)
没有合适的资源?快使用搜索试试~ 我知道了~
会议室预订代码
共73个文件
py:14个
pyc:12个
xml:10个
5星 · 超过95%的资源 需积分: 50 60 下载量 6 浏览量
2018-05-22
16:11:56
上传
评论 1
收藏 471KB ZIP 举报
温馨提示
一个整体的会议室预订,使用的django框架写的,带前端
资源推荐
资源详情
资源评论
收起资源包目录
meeting.zip (73个子文件)
meeting
.DS_Store 10KB
manage.py 805B
.idea
misc.xml 4KB
dataSources.local.xml 487B
workspace.xml 40KB
meeting.iml 1KB
dataSources.xml 2KB
dictionaries
wupeiqi.xml 86B
encodings.xml 144B
inspectionProfiles
profiles_settings.xml 228B
modules.xml 266B
dataSources
35a50536-5240-4bb8-ba3d-ffc9b731c65f.xml 17KB
35a50536-5240-4bb8-ba3d-ffc9b731c65f
_metadata_
metadata.len 8B
metadata.keystream 4KB
metadata_i 32KB
metadata_i.len 8B
metadata.values.at 77B
metadata.keystream.len 8B
metadata 64KB
storage.xml 124B
_src_
_staging_
storage_v2
_src_
schema
main.uQUzAA.meta 39B
web
apps.py 81B
tests.py 60B
migrations
0001_initial.py 2KB
__init__.py 0B
0002_auto_20171206_1400.py 811B
__pycache__
__init__.cpython-35.pyc 148B
0002_auto_20171206_1400.cpython-35.pyc 1KB
0001_initial.cpython-35.pyc 2KB
admin.py 63B
__init__.py 0B
models.py 1KB
__pycache__
__init__.cpython-35.pyc 137B
models.cpython-35.pyc 2KB
apps.cpython-35.pyc 363B
admin.cpython-35.pyc 182B
views.cpython-35.pyc 6KB
views.py 7KB
db.sqlite3 47KB
meeting
settings.py 3KB
urls.py 892B
__init__.py 0B
__pycache__
__init__.cpython-35.pyc 141B
urls.cpython-35.pyc 1KB
wsgi.cpython-35.pyc 563B
settings.cpython-35.pyc 2KB
wsgi.py 392B
templates
index.html 9KB
login.html 1KB
index.html.bak 4KB
static
img
loading.gif 4KB
js
jquery-1.12.4.min.js 95KB
jquery.cookie.js 3KB
plugins
bootstrap
js
npm.js 484B
bootstrap.js 68KB
bootstrap.min.js 36KB
fonts
glyphicons-halflings-regular.ttf 44KB
glyphicons-halflings-regular.woff2 18KB
glyphicons-halflings-regular.eot 20KB
glyphicons-halflings-regular.svg 106KB
glyphicons-halflings-regular.woff 23KB
css
bootstrap-theme.min.css.map 25KB
bootstrap-theme.css.map 47KB
bootstrap.min.css.map 529KB
bootstrap.min.css 118KB
bootstrap-theme.min.css 23KB
bootstrap-theme.css 26KB
bootstrap.css.map 380KB
bootstrap.css 143KB
datetimepicker
bootstrap-datetimepicker.min.js 38KB
locales
less
datetimepicker.less 13KB
bootstrap-datetimepicker.zh-CN.js 813B
bootstrap-datetimepicker.min.css 11KB
css
共 73 条
- 1
资源评论
- Frank__CMY2020-09-24下载了,初始登录密码是什么呀
- jluqcq2020-06-30还不错 学习了
hiddenhong
- 粉丝: 1
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功