python项目篇-酒店(会议室,电影)预定

1、views.py中

from django.shortcuts import render,redirect,HttpResponse

# Create your views here.

from django.contrib import auth


def login(request):

    if request.method=="POST":
        user=request.POST.get("user")
        pwd=request.POST.get("pwd")

        user=auth.authenticate(username=user,password=pwd)
        if user:
            auth.login(request,user)  # request.user
            return redirect("/index/")


    return render(request,"login.html")


from .models import *
import datetime
def index(request):
    date=datetime.datetime.now().date()
    book_date=request.GET.get("book_date",date)
    print("book_date",book_date)

    time_choices=Book.time_choices
    room_list=Room.objects.all()
    book_list=Book.objects.filter(date=book_date)
    print("time_choices",time_choices)


    htmls=""
    for room in room_list:
        htmls+="{}({})".format(room.caption,room.num)

        for time_choice in time_choices:
            book=None
            flag=False
            for book in book_list:
                if book.room.pk==room.pk and book.time_id==time_choice[0]:
                    #意味这个单元格已被预定
                    flag=True
                    break

            if flag:
                if request.user.pk==book.user.pk:
                     htmls += "{}".format(room.pk, time_choice[0],book.user.username)
                else:
                     htmls += "{}".format(room.pk, time_choice[0],
                                                                                        book.user.username)
            else:
                 htmls+="".format(room.pk,time_choice[0])

        htmls+=""




    # print(htmls)


    return render(request,"index.html",locals())

import json


def book(request):

    # print("request.POST",request.POST)

    post_data=json.loads(request.POST.get("post_data")) # {"ADD":{"1":["5"],"2":["5","6"]},"DEL":{"3":["9","10"]}}
    # print("post_data", post_data)
    choose_date=request.POST.get("choose_date")

    res={"state":True,"msg":None}
    try:
        # 添加预定
        #post_data["ADD"] : {"1":["5"],"2":["5","6"]}

        book_list=[]
        for room_id,time_id_list in post_data["ADD"].items():

            for time_id in time_id_list:
                book_obj=Book(user=request.user,room_id=room_id,time_id=time_id,date=choose_date)
                book_list.append(book_obj)

        Book.objects.bulk_create(book_list)


        # 删除预定
        from django.db.models import Q
        # post_data["DEL"]: {"2":["2","3"]}

        # print("post_data['DEL']:", post_data["DEL"].items())
        remove_book = Q()
        for room_id,time_id_list in post_data["DEL"].items():

            temp = Q()
            for time_id in time_id_list:
                temp.children.append(("room_id",room_id))
                temp.children.append(("time_id",time_id))
                temp.children.append(("user_id",request.user.pk))
                temp.children.append(("date",choose_date))
                remove_book.add(temp,"OR")
        if remove_book:
             Book.objects.filter(remove_book).delete()



    except Exception as e:
        res["state"]=False
        res["msg"]=str(e)

    return HttpResponse(json.dumps(res))

2、HTML




    
    Index
    

 
 

 
 


    


酒店房间预定

{% for time_choice in time_choices %} {% endfor %} {{ htmls|safe }}
房间/时间{{ time_choice.1 }}

3、model.py中

from django.db import models

# Create your models here.
from django.db import models
from django.contrib.auth.models import AbstractUser


class UserInfo(AbstractUser):
    tel=models.CharField(max_length=32)

class Room(models.Model):
    """
    会议室表
    """
    caption = models.CharField(max_length=32)
    num = models.IntegerField()  # 容纳人数
    def __str__(self):
        return self.caption


class Book(models.Model):
    """
    会议室预定信息
    """
    user = models.ForeignKey('UserInfo',on_delete=models.CASCADE)
    room = models.ForeignKey('Room',on_delete=models.CASCADE)
    date = models.DateField()
    time_choices = (
        (1, '8:00'),
        (2, '9:00'),
        (3, '10:00'),
        (4, '11:00'),
        (5, '12:00'),
        (6, '13:00'),
        (7, '14:00'),
        (8, '15:00'),
        (9, '16:00'),
        (10, '17:00'),
        (11, '18:00'),
        (12, '19:00'),
        (13, '20:00'),
    )


    time_id = models.IntegerField(choices=time_choices)

    class Meta:
        unique_together = (
            ('room','date','time_id'),
        )


    def __str__(self):
        return str(self.user)+"预定了"+str(self.room)

里面用到一个日历插件datetimepicker

效果图
python项目篇-酒店(会议室,电影)预定_第1张图片

你可能感兴趣的:(Python,Django,项目)