⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 coursebook.py

📁 python web programming 部分
💻 PY
字号:
#
# $Workfile: CourseBook.py $ $Revision: 3 $
# $Date: 10/07/01 1:35p $ $Author: Sholden $
#
import Courses
from Error import Error
from dbsource import dbsource

conn = dbsource().conn
cursor = conn.cursor()

class Page(Courses.Page):

    def Book(self, btype):
        if not self.path:
            raise Error(400, "Bad Request", errmsg="Class number required")
        if self.path[0] not in [b[0] for b in self.session._bookings]:
            # cannot check database as may not have an email address yet
            request = self.path[0], btype
            self.session._bookings.append(request)
            msg = """
                <P><B>Your reservation has been entered into your shopping cart.</B>
                Please remember to check out before leaving the site!</P>
                """
        else:
            msg = """
            <P><B>You have already reserved a place on this class.</B>
            Please do not attempt multiple reservations: each student
            must make their own bookings, although you can pay for several
            students with a single payment.</P>
            """
        return msg+self.ViewCart(self.session._bookings)

    def Reservation(self, eml, clsnum, tblname):
            cursor.execute("""
                SELECT CrsTitle, LocName
                FROM %s, Class, Location, Course
                WHERE %s.ClsNum=Class.ClsNum
                  AND Class.CrsNum=Course.CrsNum
                  AND Location.LocNum=Class.LocNum
                  AND StdEmail=? AND Class.ClsNum=?""" % (tblname, tblname),
                            (eml, clsnum))
            return cursor.fetchall()

    def Enrollment(self, eml, clsnum):
        return self.Reservation(eml, clsnum, "Enrollment")

    def WaitList(self, eml, clsnum):
        return self.Reservation(eml, clsnum, "Waitlist")

    def ViewCart(self, bookings, final=0):
        result = []
        if bookings:
            result.append("""
            <table cellpadding="2">
              <tr>
                <td>Date</td>
                <td>Class</td>
                <td align="right">Price</td>
                <td>Request</td>""")
            if not final:
                result.append("""
                <td>&nbsp;</td>""")
            result.append("""
              </tr>""")
            total = 0
            color = 0
            for clsnum, btp in bookings:
                color = 1-color
                cursor.execute("""
                    SELECT ClsDate, CrsTitle, CrsPrice, LocName
                    FROM Class, Course, Location
                    WHERE Class.CrsNum=Course.CrsNum
                      AND Class.LocNum=Location.LocNum
                      AND ClsNum=?""", (clsnum, ))
                dt, ttl, prc, loc = cursor.fetchall()[0]
                if btp == "E":
                    b = "Enrol"
                    total += prc
                    prc = "$%d" % prc
                else:
                    b = "Wait"
                    prc = "N/A"
                result.append("""
                <tr bgcolor=%s>
                  <td valign="top">%s</td>
                  <td valign="top">%s<br><font size="-2">%s</font></td>
                  <td valign="top" align="right">%s</td>
                  <td valign="top">%s</td>""" % (("#ccffff", "#33ffff")[color],
                        dt.strftime("%b %d, %Y"),
                        ttl, loc, prc, b))
                if not final:
                    result.append("""
                  <td valign="top"><a href="/CourseDelBook/%s/">Remove</a></td>
                  """ % clsnum)
                result.append("""
                </tr>
                """)
                
            if not final:
                brh = """<a href="/CourseChkOut/">Check Out</a>"""
            else:
                brh = "&nbsp;"
            result.append("""
                <tr>
                  <td>&nbsp;</td>
                  <td>TOTAL COST</td>
                  <td align="right">$%d</td>
                  <td>&nbsp;</td>
                  <td>%s</td>
                </tr>
              </table>""" % (total, brh))
        else:
            result.append("""
                <P><B>Your shopping cart is currently empty.</B></P>""")
        return "\n".join(result)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -