class Hall(db.Model,Model): h_id = db.Column(db.Integer,primary_key=True) c_id = db.Column(db.Integer) h_name = db.Column(db.String(64),nullable=False) h_screen_type = db.Column(db.String(32)) h_audio_type = db.Column(db.String(32)) h_seats_num = db.Column(db.Integer,default=0,nullable=False) h_status = db.Column(db.Integer, server_default='0', nullable=False, index=True)
class Order(db.Model, Model): __tablename__ = 'orders' # the id of the order o_id = db.Column(db.String(32), primary_key=True) c_id = db.Column(db.Integer, nullable=False) p_id = db.Column(db.Integer, nullable=False) s_id = db.Column(db.Integer, nullable=False) ticket_flag = db.Column(db.String(64)) # the code to get the tickets amount = db.Column(db.Integer, default=0, nullable=False) tickest_num = 0 seller_order_num = db.Column(db.String(32), unique=True) paid_time = db.Column(db.DateTime) printed_time = db.Column(db.DateTime) refund_time = db.Column(db.DateTime) created_time = db.Column(db.DateTime, server_default=text('CURRENT_TIMESTAMP')) updated_time = db.Column(db.DateTime, onupdate=func.now()) status = db.Column(db.Integer, server_default='0', nullable=False) # to create an order @classmethod def create(cls, c_id, p_id, s_id): order = cls() # the order num: create time + 6 random number + p_id order.o_id = '%s%s%s' % (timeset.now(), randint(100000, 999999), p_id) order.c_id = c_id order.p_id = p_id # if there are more than one seats, then use list, and cut the list into string by ',' # the string will be change to parameter list by the 'multi_int' if type(s_id) == list: order.s_id = ','.join(str(i) for i in s_id) else: order.s_id = s_id return order # to seek the order by the order num @classmethod def gen_ticket_flag(self): s = [] for i in range(8): s.append(str(randint(1000, 9999))) self.ticket_flag = ''.join(s) def validate(self, ticket_flag): return self.ticket_flag == ticket_flag @classmethod def getby_orderno(cls, ordernum): return Order.query.filter_by(seller_order_num=ordernum).first() @classmethod def getby_ticket_flag(cls, ticket_flag): return cls.query.filter_by(ticket_flag=ticket_flag).first()
class Seat(db.Model, Model): # every record means one seat s_id = db.Column(db.Integer, primary_key=True) c_id = db.Column(db.Integer, nullable=False) h_id = db.Column(db.Integer, nullable=False) x = db.Column(db.Integer) y = db.Column(db.Integer) row = db.Column(db.String(8)) column = db.Column(db.String(8)) area = db.Column(db.String(8)) love_seats = db.Column(db.String(32)) seat_type = db.Column(db.String(16)) status = db.Column(db.Integer, nullable=False, server_default='0') @classmethod def getby_hid(cls, h_id): return cls.query.filter_by(h_id=h_id).all()
class Movie(db.Model, Model): m_id = db.Column(db.Integer, primary_key=True) # sn is the code that create by The State Administration of Radio Film and Television of China sn = db.Column(db.String(32), unique=True, nullable=False) m_name = db.Column(db.String(64), nullable=False) m_language = db.Column(db.String(32)) m_subtitle = db.Column(db.String(32)) m_show_date = db.Column(db.Date) m_mode = db.Column( db.String(16)) # the type of movies like digital or film m_vision = db.Column(db.String(16)) # 2D or 3D m_screen_size = db.Column(db.String(16)) m_introduction = db.Column(db.Text) m_status = db.Column(db.Integer, server_default='0', nullable=False, index=True) @classmethod def create_test_data(cls, num=10): for i in range(1, num + 1): m = Movie() m.m_id = i m.sn = str(i).zfill(10) m.name = 'the name of movie is %s' % i m.m_language = 'English' m.m_subtitle = 'Mandarin' m.m_mode = 'digital' m.m_vision = '2D' m.m_screen_size = 'IMAX' m.m_introduction = 'hahaha' m.m_status = 1 db.session.add(m) db.session.commit() current_app.logger.info('movie test data done!')
class PlaySeat(db.Model, Model): p_sid = db.Column(db.Integer, primary_key=True) order_num = db.Column(db.String(32), index=True) s_id = db.Column(db.Integer, nullable=False) p_id = db.Column(db.Integer, nullable=False) c_id = db.Column(db.Integer, nullable=False) h_id = db.Column(db.Integer, nullable=False) x = db.Column(db.Integer) y = db.Column(db.Integer) row = db.Column(db.String(8)) column = db.Column(db.String(8)) area = db.Column(db.String(8)) love_seats = db.Column(db.String(32)) seat_type = db.Column(db.String(16)) status = db.Column(db.Integer, nullable=False, server_default='0', index=True) locked_time = db.Column(db.DateTime) created_time = db.Column(db.DateTime, server_default=text('CURRENT_TIMESTAMP')) @classmethod def getby(cls, p_id, s_id): return cls.get('%s-%s' % (p_id, s_id)) def copy(self, seat): # just copy the information of seat to the PlaySeat self.s_id = seat.s_id self.c_id = seat.c_id self.h_id = seat.h_id self.x = seat.x self.y = seat.y self.row = seat.row self.column = seat.column self.area = seat.area self.loev_seats = seat.love_seats self.seat_type = seat.seat_type self.status = seat.status @classmethod def getby_ordernum(cls, ordernum): cls.query.filter_by(ordernum=ordernum).all() return @classmethod def lock(cls, ordernum, p_id, s_id_list): # create the session of database session = db.create_scoped_session() rows = session.query(PlaySeat).filter( PlaySeat.p_id == p_id, PlaySeat.status == SeatStatus.ok.value, PlaySeat.s_id.in_(s_id_list) # change the attribute of the seats: seller order num, status code and the locked time ).update( { 'ordernum': ordernum, 'status': SeatStatus.locked.value, 'locked_time': datetime.now() }, synchronize_session=False ) # means when update the database, other request will not be obstructed # after update, the database will return a value to show how many data are changed, if the value is 0, then the update failed if rows != len(s_id_list): session.rollback() return 0 session.commit() return rows # unlock the seat @classmethod def unlock(cls, ordernum, p_id, s_id_list): session = db.create_scoped_session() # 'rows' is the return value comes from the database # the seat is seeked by order number and status, and change the seller order num to empty, the status change to available rows = session.query(PlaySeat).filter_by( ordernum=ordernum, status=SeatStatus.locked.value).update( { 'ordernum': None, 'status': SeatStatus.ok.value }, synchronize_session=False) if rows != len(s_id_list): session.rollback() return 0 session.commit() return rows @classmethod def print_tickets(cls, ordernum, p_id, s_id_list): session = db.create_scoped_session() rows = session.query(PlaySeat).filter_by( ordernum=ordernum, status=SeatStatus.sold.value).update( {'status': SeatStatus.printed.value}, synchronize_session=False) if rows != len(s_id_list): session.rollback() return 0 session.commit() return rows @classmethod def refund(cls, ordernum, p_id, s_id_list): session = db.create_scoped_session() rows = session.query(PlaySeat).filter_by( ordernum=ordernum, status=SeatStatus.sold.value).update( { 'status': SeatStatus.ok.value, 'ordernum': None }, synchronize_session=False) if rows != len(s_id_list): session.rollback() return 0 session.commit() return rows
class Cinema(db.Model, Model): c_id = db.Column(db.Integer, primary_key=True) c_name = db.Column( db.String(64), unique=True, nullable=False) # the name must be unique and cannot be null c_address = db.Column(db.String(128), nullable=False) c_halls = db.Column(db.Integer, default=0, nullable=False) # how many halls in the cinema c_handle_fee = db.Column(db.Integer, default=0, nullable=False) # the service charge c_buy_limit = db.Column( db.Integer, default=0, nullable=False) # how many tickets can be bought at one time status = db.Column(db.Integer, server_default='0', nullable=False, index=True) @classmethod def create_test_data(cls, cinema_num=10, hall_num=10, play_num=10): from theater.models.hall import Hall from theater.models.play import Play from theater.models.seat import PlaySeat, Seat from theater.models.order import Order f = faker.Faker() screen_type = ['normal', 'IMAX'] audio_type = ['normal', 'Dolby'] start_time = time.time() for i in range(1, cinenam_num + 1): cinema = Cinema() cinema.c_id = i cinema.c_name = '%s cinema' % f.street_name() cinema.c_address = f.address() cinema.status = 1 cinema.save() for n in range(1, hall_num + 1): hall = Hall() hall.c_id = cinema.c_id hall.h_name = 'No %s hall' % n hall.h_screen_type = random.choice(screen_type) hall.h_audio_type = random.choice(audio_type) hall.h_seats_num = 25 hall.status = 1 hall.save() seats = [] for s in range(1, hall.h_seats_num + 1): seat = Seat() seat.c_id = hall.c_id seat.h_id = hall.h_id seat.x = s % 5 or 5 seat.y = math.ceil(s / 5) seat.row = seat.x seat.column = seat.y seat.seat_type = 1 seat.put() seats.append(seat) Seat.commit() for p in range(1, play_num + 1): play = Play() play.c_id = cinema.c_id play.h_id = hall.h_id play.m_id = p play.start_time = datetime.now() play.p_duration = 3600 play.price_type = 1 play.price = 7000 play.market_price = 5000 play.lowest_price = 3000 play.status = 1 play.save() for seat in seats: ps = PlaySeat() ps.p_id = play.p_id ps.copy(seat) ps.put() PlaySeat.commit() current_app.logger.info('create test data done! cost %.2f seconds' % (time.time() - start_time))