def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            x, y = event.x(), event.y()
            x -= 10
            y -= 60
            y //= 18

            self.lodgers = LodgerModel(db.get_connection()).get_all(self.o[7])
            if x <= 20:
                lod = LodgerModel(db.get_connection()).get(self.lodgers[y][0])
                if lod[9] == None or lod[9] == 0:
                    self.delete_lodger(self.lodgers[y][0])
                else:
                    self.change_status(
                        "Нельзя удалить постояльца, который проживает в номере."
                    )
            else:
                self.choose_lodger = self.lodgers[y][0]

            if x >= 290:
                x -= 290
                self.rooms = RoomModel(db.get_connection()).get_all(self.o[7])
                if x <= 20:
                    lod = RoomModel(db.get_connection()).get(self.rooms[y][0])
                    if lod[4] == 0:
                        self.delete_room(self.rooms[y][0])
                    else:
                        self.change_status(
                            "Нельзя удалить комнату, в которой проживает постоялец"
                        )
                else:
                    self.choose_room = self.rooms[y][0]
 def out_lodger(self):
     self.lodgers = LodgerModel(db.get_connection())
     self.lodgers.check_out(self.choose_lodger)
     self.room = RoomModel(db.get_connection())
     self.room.check_out(self.choose_lodger)
     HotelModel(db.get_connection()).out_lodger(self.o[7])
     self.change()
 def change(self):
     self.rooms = RoomModel(db.get_connection()).get_all(self.o[7])
     self.room_session.setText('\n'.join(
         ['[x] ' + str(x[3]) for x in self.rooms]))
     self.lodgers = LodgerModel(db.get_connection()).get_all(self.o[7])
     self.lodger_session.setText('\n'.join([
         '[x] ' + x[1] + ' ' + str(x[9]) if x[9] else '[x] ' + x[1]
         for x in self.lodgers
     ]))
 def delete_room(self, id):
     self.rooms = RoomModel(db.get_connection())
     room = self.rooms.get(id)
     if room[4] != 0:
         self.change_status(
             "Эта камната заселена. Вы не можете её удалить.")
     else:
         self.rooms.delete(id)
         self.change()
def add_room(cinema_id, user_id):
    form = AddRoomForm()
    if form.validate_on_submit():
        title = form.roomname.data
        content = form.roomcount.data
        nm = RoomModel(db.get_connection())
        nm.insert(title, content, session['user_id'], cinema_id)
        return redirect("/rooms/{}/{}".format(str(cinema_id), str(user_id)))
    return render_template('add_room.html',
                           title='Добавление комнаты',
                           form=form,
                           username=session['username'],
                           cinema_id=cinema_id)
def rooms(cinema_id, user_id):
    rooms = RoomModel(db.get_connection()).get_cinema(cinema_id)
    return render_template('rooms.html',
                           username=session['username'],
                           rooms=rooms,
                           cinema_id=cinema_id,
                           user_id=user_id)
 def in_lodger(self):
     self.lodger = LodgerModel(db.get_connection())
     lod = self.lodger.get(self.choose_lodger)
     if lod[9] != None and lod[9] != 0:
         self.change_status(
             "Этот постоялец заселен. Его нельзя заселить заново.")
     else:
         self.room = RoomModel(db.get_connection())
         room = self.room.get(self.choose_room)
         if room[4] != 0:
             self.change_status("Эта комната занята.")
             #print(room)
         else:
             self.lodger.check_in(self.choose_lodger, self.choose_room,
                                  self.o[7])
             self.room.check_in(self.choose_lodger, self.choose_room)
             HotelModel(db.get_connection()).in_lodger(self.o[7])
             self.change()
 def initUI(self):
     self.btnadd_hostel.clicked.connect(self.add_room)
     if self.type == -1:
         self.title.setText("Добавление постояльца")
     else:
         lod = RoomModel(db.get_connection()).get(self.type)
         self.nlevel.setText(str(lod[1]))
         self.nlevel.setReadOnly(True)
         self.nroom.setText(str(lod[3]))
         self.country.setText(str(lod[2]))
         self.title.setText("Редактирование постояльца")
    def mouseDoubleClickEvent(self, event):
        x, y = event.x(), event.y()
        try:
            if 10 <= x <= 201 and 60 <= y <= 191:
                x -= 10
                y -= 60
                x //= 6
                y //= 18
                self.lodgers = LodgerModel(db.get_connection()).get_all(
                    self.o[7])
                self.edit_lodger(self.lodgers[y][0])

            if 300 <= x <= 491 and 60 <= y <= 191:
                x -= 300
                y -= 60
                x //= 6
                y //= 18
                self.rooms = RoomModel(db.get_connection()).get_all(self.o[7])
                self.edit_room(self.rooms[y][0])
        except Exception as e:
            print(e)
 def add_room(self):
     if self.type == -1:
         nlevel = self.nlevel.text()
         nroom = self.nroom.text()
         square = self.country.text()
         lodger = RoomModel(db.get_connection())
         if nlevel in lodger.get_ids(self.adm[7]) or int(nlevel) == 0:
             self.change_status(
                 "Комната с таким номером существует или номер не является натуральным числом"
             )
         elif nroom == '':
             self.change_status("Вы не указали колличество комнат")
         elif square == '':
             self.change_status("Вы не указали площадь")
         else:
             lodger.insert(nlevel, nroom, square, self.adm[7])
             self.close()
     else:
         nroom = self.nroom.text()
         square = self.country.text()
         lodger = RoomModel(db.get_connection())
         lodger.change(self.type, nroom, square)
         self.close()
def delete_room(room_id, cinema_id, user_id):
    nm = RoomModel(db.get_connection())
    films = [x[0] for x in FilmModel(db.get_connection()).get_all(room_id)]
    nm.delete(room_id)
    films = [delete_film(x, room_id) for x in films]
    return redirect("/rooms/{}/{}".format(cinema_id, user_id))
def delete_cinema(cinema_id):
    nm = CinemaModel(db.get_connection())
    rooms = [x[0] for x in RoomModel(db.get_connection()).get_all(cinema_id)]
    nm.delete(cinema_id)
    rooms = [delete_room(x, cinema_id) for x in rooms]
    return redirect("/chain/{}".format(session['user_id']))
class AdminM(QWidget, Ui_Qadmin):
    def __init__(self, obj):
        super(AdminM, self).__init__()
        self.setupUi(self)
        self.o = obj
        self.choose_lodger = -1
        self.choose_room = -1
        self.initUI()

    def initUI(self):
        self.btnin_lodger.clicked.connect(self.in_lodger)
        self.btnout_lodger.clicked.connect(self.out_lodger)
        self.btnadd_lodger.clicked.connect(self.add_lodger)
        self.btnadd_room.clicked.connect(self.add_room)
        self.btnto_csv.clicked.connect(self.to_csv)
        self.change()

    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            x, y = event.x(), event.y()
            x -= 10
            y -= 60
            y //= 18

            self.lodgers = LodgerModel(db.get_connection()).get_all(self.o[7])
            if x <= 20:
                lod = LodgerModel(db.get_connection()).get(self.lodgers[y][0])
                if lod[9] == None or lod[9] == 0:
                    self.delete_lodger(self.lodgers[y][0])
                else:
                    self.change_status(
                        "Нельзя удалить постояльца, который проживает в номере."
                    )
            else:
                self.choose_lodger = self.lodgers[y][0]

            if x >= 290:
                x -= 290
                self.rooms = RoomModel(db.get_connection()).get_all(self.o[7])
                if x <= 20:
                    lod = RoomModel(db.get_connection()).get(self.rooms[y][0])
                    if lod[4] == 0:
                        self.delete_room(self.rooms[y][0])
                    else:
                        self.change_status(
                            "Нельзя удалить комнату, в которой проживает постоялец"
                        )
                else:
                    self.choose_room = self.rooms[y][0]

    def mouseDoubleClickEvent(self, event):
        x, y = event.x(), event.y()
        try:
            if 10 <= x <= 201 and 60 <= y <= 191:
                x -= 10
                y -= 60
                x //= 6
                y //= 18
                self.lodgers = LodgerModel(db.get_connection()).get_all(
                    self.o[7])
                self.edit_lodger(self.lodgers[y][0])

            if 300 <= x <= 491 and 60 <= y <= 191:
                x -= 300
                y -= 60
                x //= 6
                y //= 18
                self.rooms = RoomModel(db.get_connection()).get_all(self.o[7])
                self.edit_room(self.rooms[y][0])
        except Exception as e:
            print(e)

    def add_lodger(self):
        self.w1 = AddLodgerM(self.o, -1)
        self.w1.show()
        self.change()

    def delete_lodger(self, id):
        self.lodgers = LodgerModel(db.get_connection())
        lod = self.lodgers.get(id)
        if lod[9] != None:
            self.change_status("Этот постоялец заселен. Его нельзя удалить.")
        else:
            self.lodgers.delete(id)
            self.change()

    def in_lodger(self):
        self.lodger = LodgerModel(db.get_connection())
        lod = self.lodger.get(self.choose_lodger)
        if lod[9] != None and lod[9] != 0:
            self.change_status(
                "Этот постоялец заселен. Его нельзя заселить заново.")
        else:
            self.room = RoomModel(db.get_connection())
            room = self.room.get(self.choose_room)
            if room[4] != 0:
                self.change_status("Эта комната занята.")
                #print(room)
            else:
                self.lodger.check_in(self.choose_lodger, self.choose_room,
                                     self.o[7])
                self.room.check_in(self.choose_lodger, self.choose_room)
                HotelModel(db.get_connection()).in_lodger(self.o[7])
                self.change()

    def out_lodger(self):
        self.lodgers = LodgerModel(db.get_connection())
        self.lodgers.check_out(self.choose_lodger)
        self.room = RoomModel(db.get_connection())
        self.room.check_out(self.choose_lodger)
        HotelModel(db.get_connection()).out_lodger(self.o[7])
        self.change()

    def add_room(self):
        self.w1 = Add_roomM(self.o, -1)
        self.w1.show()
        self.change()

    def delete_room(self, id):
        self.rooms = RoomModel(db.get_connection())
        room = self.rooms.get(id)
        if room[4] != 0:
            self.change_status(
                "Эта камната заселена. Вы не можете её удалить.")
        else:
            self.rooms.delete(id)
            self.change()

    def edit_lodger(self, id):
        self.w1 = AddLodgerM(self.o, id)
        self.w1.show()

    def edit_room(self, id):
        self.w1 = Add_roomM(self.o, id)
        self.w1.show()

    def to_csv(self):
        self.w1 = ToCSVM(self.o)
        self.w1.show()

    def change_status(self, text):
        self.status.clear()
        self.status.append(text)

    def change(self):
        self.rooms = RoomModel(db.get_connection()).get_all(self.o[7])
        self.room_session.setText('\n'.join(
            ['[x] ' + str(x[3]) for x in self.rooms]))
        self.lodgers = LodgerModel(db.get_connection()).get_all(self.o[7])
        self.lodger_session.setText('\n'.join([
            '[x] ' + x[1] + ' ' + str(x[9]) if x[9] else '[x] ' + x[1]
            for x in self.lodgers
        ]))