def __init__(self, id_game, id_user, *args, **kw):
        super(VistaGameUser, self).__init__(*args, **kw)
        self.res = xrc.XmlResource('../view/vistas_game_user.xrc')
        self.frame = self.res.LoadFrame(None, 'VistaGameUser')
        self.panel = xrc.XRCCTRL(self.frame, 'panelData')

        self.id_game = id_game
        self.id_user = id_user
        self.game = VideoGame()
        self.conect = Conection()
        self.rents = Rentas()

        self.text_name_game = xrc.XRCCTRL(self.panel, 'staticTextName')
        self.text_company = xrc.XRCCTRL(self.panel, 'staticTextCompany')
        self.text_gender_game = xrc.XRCCTRL(self.panel, 'staticTextGenero')
        self.text_duration = xrc.XRCCTRL(self.panel, 'staticTextDuration')
        self.text_estate_game = xrc.XRCCTRL(self.panel, 'staticTextEstado')

        self.button_rent = xrc.XRCCTRL(self.panel, 'buttonRent')
        self.frame.Bind(wx.EVT_BUTTON, self.rent_selected, self.button_rent)

        self.button_exit = xrc.XRCCTRL(self.panel, 'buttonCancel')
        self.frame.Bind(wx.EVT_BUTTON, self.close_frame, self.button_exit)

        self.get_datos_game()

        self.frame.Show()
    def __init__(self, id_serie, id_user, *args, **kw):
        super(VistaUserSerie, self).__init__(*args, **kw)
        self.res = xrc.XmlResource('../view/vistas_serie_user.xrc')
        self.frame = self.res.LoadFrame(None, 'VistaUserSerie')
        self.panel = xrc.XRCCTRL(self.frame, 'panelOptions')

        self.id_serie = id_serie
        self.id_user = id_user
        self.rents = Rentas()
        self.series = Series()
        self.conect = Conection()

        self.text_name_serie = xrc.XRCCTRL(self.panel, 'staticTextName')
        self.text_creator = xrc.XRCCTRL(self.panel, 'staticTextCreator')
        self.text_gender_serie = xrc.XRCCTRL(self.panel, 'staticTextGenero')
        self.text_temporates_serie = xrc.XRCCTRL(self.panel, 'staticTextTemporates')
        self.text_estate_serie = xrc.XRCCTRL(self.panel, 'staticTextEstado')

        self.button_rent = xrc.XRCCTRL(self.panel, 'rent')
        self.frame.Bind(wx.EVT_BUTTON, self.rent_selected, self.button_rent)
        self.button_exit = xrc.XRCCTRL(self.panel, 'cancel')
        self.frame.Bind(wx.EVT_BUTTON, self.close_frame, self.button_exit)

        self.get_datos_serie()

        self.frame.Show()
Beispiel #3
0
    def __init__(self, frame_father=None, id_serie=None):
        super(RegistroSerie, self).__init__()
        self.res = xrc.XmlResource('../view/series.xrc')
        self.series = Series()
        self.conection = Conection()
        self.id_serie = id_serie
        self.frame = self.res.LoadFrame(None, 'RegistroSerie')
        self.panel = xrc.XRCCTRL(self.frame, 'm_panel6')
        self.titulo_serie = xrc.XRCCTRL(self.panel, 'textCtrlName')
        self.creador = xrc.XRCCTRL(self.panel, 'textCtrlCreator')
        self.genero = xrc.XRCCTRL(self.panel, 'textCtrlGender')
        self.no_temporadas = xrc.XRCCTRL(self.panel, 'textCtrlTemporates')
        self.estado = xrc.XRCCTRL(self.panel, 'textCtrlSate')
        self.imagen_url = xrc.XRCCTRL(self.panel, 'fileLocationSerie')
        self.button_save = xrc.XRCCTRL(self.panel, 'wxID_OK')
        self.button_cancel = xrc.XRCCTRL(self.panel, 'wxID_CANCEL')
        self.frame.Bind(wx.EVT_BUTTON, self.close_frame, self.button_cancel)

        self.frame.Bind(wx.EVT_BUTTON, self.close_frame, self.button_cancel)
        self.frame.Bind(wx.EVT_BUTTON, self.create_serie, self.button_save)

        self.frame.Show()

        if frame_father is not None:
            self.frame_father = frame_father

        if self.id_serie is not None:
            self.load_data_serie()
class VistaUserSerie(wx.Frame):

    def __init__(self, id_serie, id_user, *args, **kw):
        super(VistaUserSerie, self).__init__(*args, **kw)
        self.res = xrc.XmlResource('../view/vistas_serie_user.xrc')
        self.frame = self.res.LoadFrame(None, 'VistaUserSerie')
        self.panel = xrc.XRCCTRL(self.frame, 'panelOptions')

        self.id_serie = id_serie
        self.id_user = id_user
        self.rents = Rentas()
        self.series = Series()
        self.conect = Conection()

        self.text_name_serie = xrc.XRCCTRL(self.panel, 'staticTextName')
        self.text_creator = xrc.XRCCTRL(self.panel, 'staticTextCreator')
        self.text_gender_serie = xrc.XRCCTRL(self.panel, 'staticTextGenero')
        self.text_temporates_serie = xrc.XRCCTRL(self.panel, 'staticTextTemporates')
        self.text_estate_serie = xrc.XRCCTRL(self.panel, 'staticTextEstado')

        self.button_rent = xrc.XRCCTRL(self.panel, 'rent')
        self.frame.Bind(wx.EVT_BUTTON, self.rent_selected, self.button_rent)
        self.button_exit = xrc.XRCCTRL(self.panel, 'cancel')
        self.frame.Bind(wx.EVT_BUTTON, self.close_frame, self.button_exit)

        self.get_datos_serie()

        self.frame.Show()

    def close_frame(self, evt):
        self.frame.Close()

    # Corregir los errores a la hora de eliminar las series rentadas.
    def rent_selected(self, evt):
        result = wx.MessageBox('Do you really want to rent this serie?', 'Information', wx.YES_NO | wx.ICON_EXCLAMATION)
        self.result = self.series.get_serie(id_serie=self.id_serie)

        if result == wx.YES:
            session = self.conect.connect_database()
            new_state = "Prestado"

            if self.result.estado == "Entregado":
                self.result.estado = new_state
                rent = Rentas(id_usuario=self.id_user, id_renta=self.id_serie, estado_renta=self.result.estado)
                session.add(rent)
                session.commit()
                session.close()
                wx.MessageBox('The rent has been rented correctly', 'Information', wx.OK | wx.ICON_INFORMATION)
                self.frame.Close()

                if self.id_serie is not None:
                    data = {'estado': self.result.estado}
                    self.series.edit_state(self.id_serie, data)

                else:
                    return None

            elif self.result.estado == "Prestado":
                rent = session.query(Rentas).filter_by(id_usuario=self.id_user).one_or_none()

                if rent is not None:
                    self.rents.id_usuario = rent.id_usuario

                else:
                    self.rents.id_usuario = rent

                if self.id_user == self.rents.id_usuario:
                    result = wx.MessageBox('You have already rented this series. Do you want to return it now?', 'Information', wx.YES_NO | wx.ICON_EXCLAMATION)

                    if result == wx.YES:
                        session = self.conect.connect_database()
                        rent_deliver = session.query(Rentas).filter_by(id_renta=self.id_serie).one()
                        session.delete(rent_deliver)

                        self.result.estado = 'Entregado'
                        data = {'estado': self.result.estado}
                        self.series.edit_state(self.id_serie, data)
                        wx.MessageBox('The rent has been delivered correctly', 'Information', wx.OK | wx.ICON_INFORMATION)
                        self.frame.Close()
                        session.close()
                else:
                    wx.MessageBox('You have not been the person who has rented this serie, therefore you cannot return or lend it until it is returned.', 'Information', wx.OK | wx.ICON_INFORMATION)

            session.close()

    def get_datos_serie(self):
        self.result = self.series.get_serie(id_serie=self.id_serie)
        self.text_name_serie.SetLabel(self.result.titulo_serie)
        self.text_creator.SetLabel(self.result.creador)
        self.text_gender_serie.SetLabel(self.result.genero)
        self.text_temporates_serie.SetLabel(str(self.result.no_temporadas))
        self.text_estate_serie.SetLabel(self.result.estado)
Beispiel #5
0
class VideoGame():

    def __init__(self, *args):
        self.conect = Conection()

    def create_game(self, titulo_videojuego, compania, genero, horas_duracion, estado, imagen_url):
        game = Videojuego(titulo_videojuego=titulo_videojuego, compania=compania, genero=genero, horas_duracion=horas_duracion, estado=estado, imagen_url=imagen_url)
        session = self.conect.connect_database()
        session.add(game)
        session.commit()
        session.close()
    def edit_state(self, id_game, data):
        session = self.conect.connect_database()
        game = session.query(Videojuego).filter_by(id=id_game).one()
        game.estado = data['estado']
        session.add(game)
        session.commit()
        session.close()

    def edit_game(self, id_game, data):
        session = self.conect.connect_database()
        game = session.query(Videojuego).filter_by(id=id_game).one()
        game.titulo_videojuego = data['titulo_videojuego']
        game.compania = data['compania']
        game.genero = data['genero']
        game.horas_duracion = data['horas_duracion']
        game.estado = data['estado']
        game.imagen_url = data['imagen_url']
        session.add(game)
        session.commit()
        session.close()

    def get_game(self, id_game):
        session = self.conect.connect_database()

        try:
            game = session.query(Videojuego).filter_by(id=id_game).one()
            session.close()
            return game

        except (MultipleResultsFound, NoResultFound):
            return None

    def get_all_games(self):
        session = self.conect.connect_database()
        games = session.query(Videojuego).all()
        session.close()
        return games

    def get_all_images_games(self):
        session = self.conect.connect_database()

        try:
            game = session.query(Videojuego).all()
            list_games = []
            for games in game:
                list_games.append(games)
            session.close()
            return list_games

        except (MultipleResultsFound, NoResultFound):
            return None

    def delete_game(self, id_game):
        session = self.conect.connect_database()
        game = session.query(Videojuego).filter_by(id=id_game).one()
        session.delete(game)
        session.commit()
        session.close()
Beispiel #6
0
 def __init__(self, *args):
     self.conect = Conection()
 def __init__(self):
     self.conect = Conection()
class Series():
    def __init__(self):
        self.conect = Conection()

    def create_serie(self, titulo_serie, creador, genero, no_temporadas, estado, imagen_url):
        serie = Serie(titulo_serie=titulo_serie, creador=creador, genero=genero, no_temporadas=no_temporadas, estado=estado, imagen_url=imagen_url)
        session = self.conect.connect_database()
        session.add(serie)
        session.commit()
        session.close()

    def edit_state(self, id_serie, data):
        session = self.conect.connect_database()
        serie = session.query(Serie).filter_by(id=id_serie).one()
        serie.estado = data['estado']
        session.add(serie)
        session.commit()
        session.close()

    def edit_series(self, id_serie, data):
        session = self.conect.connect_database()
        serie = session.query(Serie).filter_by(id=id_serie).one()
        serie.titulo_serie = data['titulo_serie']
        serie.creador = data['creador']
        serie.genero = data['genero']
        serie.no_temporadas = data['no_temporadas']
        serie.estado = data['estado']
        serie.imagen_url = data['imagen_url']
        session.add(serie)
        session.commit()
        session.close()

    def get_serie(self, id_serie):
        session = self.conect.connect_database()

        try:
            serie = session.query(Serie).filter_by(id=id_serie).one()
            session.close()
            return serie

        except (MultipleResultsFound, NoResultFound):
            return None

    def get_all_series(self):
        session = self.conect.connect_database()
        series = session.query(Serie).all()
        session.close()
        return series

    def get_all_images(self):
        session = self.conect.connect_database()

        try:
            serie = session.query(Serie).all()
            list_series = []
            for series in serie:
                list_series.append(series)
            session.close()
            return list_series

        except (MultipleResultsFound, NoResultFound):
            return None

    def get_image(self, text_serie):
        session = self.conect.connect_database()

        try:
            serie = session.query(Serie).filter_by(titulo_serie=text_serie).all()
            print(serie)
            list_serie = []
            for series in serie:
                list_serie.append(series)
            session.close()
            return list_serie

        except (MultipleResultsFound, NoResultFound):
            return None

    def delete_serie(self, id_serie):
        session = self.conect.connect_database()
        serie = session.query(Serie).filter_by(id=id_serie).one()
        session.delete(serie)
        session.commit()
        session.close()

    def get_name_serie(self, titulo_serie):
        session = self.conect.connect_database()

        try:
            serie = session.query(Serie).filter_by(titulo_serie=titulo_serie).all()
            session.close()
            return serie

        except NoResultFound:
            return None
    def __init__(self):
        super(InicioFrame, self).__init__()
        self.res = xrc.XmlResource('../view/inicio.xrc')
        self.user_controller = UserController()
        self.conect = Conection()
        self.series = Series()
        self.games = VideoGame()
        self.grid_sizer = None
        self.add_user_frame = None
        self.update_users = None
        self.user_selected = None
        self.add_series = None
        self.add_games = None
        self.list_user = []
        self.list_serie = []
        self.frame = self.res.LoadFrame(None, 'InicioFrame')
        self.panel = xrc.XRCCTRL(self.frame, 'InicioPanel')
        self.listbook_menu = xrc.XRCCTRL(self.panel, 'listbook')
        self.panel_users = xrc.XRCCTRL(self.listbook_menu, 'panel2')
        self.listctrl_users = xrc.XRCCTRL(self.panel_users, 'listControl')
        self.button_add_user = xrc.XRCCTRL(self.panel_users, 'addUser')
        self.load_columns_listcrtl_user()
        self.load_data_listctrl_user()

        #Users
        self.frame.Bind(wx.EVT_BUTTON, self.create_user, self.button_add_user)
        self.frame.Bind(wx.EVT_LIST_ITEM_SELECTED, self.list_user_selected,
                        self.listctrl_users)
        self.text_search = xrc.XRCCTRL(self.panel_users, 'textCtrlSearch')
        self.button_search = xrc.XRCCTRL(self.panel_users, 'search')
        self.frame.Bind(wx.EVT_BUTTON, self.search_user, self.button_search)
        # Exit Buttons
        self.button_exit = xrc.XRCCTRL(self.panel_users, 'logOut')
        self.frame.Bind(wx.EVT_BUTTON, self.close_frames, self.button_exit)

        #Series
        self.panel_serie = xrc.XRCCTRL(self.listbook_menu, 'panelSeries')
        self.grid_serie = xrc.XRCCTRL(self.panel_serie, 'scrolledWindowserie')
        self.grid_sizer_series()

        self.text_search_serie = xrc.XRCCTRL(self.panel_serie, 'textCtrlSerie')
        self.button_search_serie = xrc.XRCCTRL(self.panel_serie, 'searchSerie')
        self.frame.Bind(wx.EVT_BUTTON, self.search_serie,
                        self.button_search_serie)
        self.button_exit_series = xrc.XRCCTRL(self.panel_users, 'logOut')
        self.Bind(wx.EVT_BUTTON, self.close_frames, self.button_exit_series)
        # Button Add series
        self.button_add_serie = xrc.XRCCTRL(self.panel_serie, 'addSerie')
        self.frame.Bind(wx.EVT_BUTTON, self.open_add_serie,
                        self.button_add_serie)

        #Games
        self.panel_game = xrc.XRCCTRL(self.listbook_menu, 'panelGames')
        self.grid_game = xrc.XRCCTRL(self.panel_game, 'scrolledWindowGame')
        self.grid_sizer_games()

        self.text_search_game = xrc.XRCCTRL(self.panel_game, 'textCtrlGame')
        self.button_search_game = xrc.XRCCTRL(self.panel_game, 'searchGame')
        self.frame.Bind(wx.EVT_BUTTON, self.search_game,
                        self.button_search_game)
        self.button_exit_games = xrc.XRCCTRL(self.panel_users, 'logOut')
        self.Bind(wx.EVT_BUTTON, self.close_frames, self.button_exit_games)
        #Button Add Games
        self.button_add_game = xrc.XRCCTRL(self.panel_game, 'addGame')
        self.frame.Bind(wx.EVT_BUTTON, self.open_add_game,
                        self.button_add_game)

        self.frame.Show()
class InicioFrame(wx.Frame):
    def __init__(self):
        super(InicioFrame, self).__init__()
        self.res = xrc.XmlResource('../view/inicio.xrc')
        self.user_controller = UserController()
        self.conect = Conection()
        self.series = Series()
        self.games = VideoGame()
        self.grid_sizer = None
        self.add_user_frame = None
        self.update_users = None
        self.user_selected = None
        self.add_series = None
        self.add_games = None
        self.list_user = []
        self.list_serie = []
        self.frame = self.res.LoadFrame(None, 'InicioFrame')
        self.panel = xrc.XRCCTRL(self.frame, 'InicioPanel')
        self.listbook_menu = xrc.XRCCTRL(self.panel, 'listbook')
        self.panel_users = xrc.XRCCTRL(self.listbook_menu, 'panel2')
        self.listctrl_users = xrc.XRCCTRL(self.panel_users, 'listControl')
        self.button_add_user = xrc.XRCCTRL(self.panel_users, 'addUser')
        self.load_columns_listcrtl_user()
        self.load_data_listctrl_user()

        #Users
        self.frame.Bind(wx.EVT_BUTTON, self.create_user, self.button_add_user)
        self.frame.Bind(wx.EVT_LIST_ITEM_SELECTED, self.list_user_selected,
                        self.listctrl_users)
        self.text_search = xrc.XRCCTRL(self.panel_users, 'textCtrlSearch')
        self.button_search = xrc.XRCCTRL(self.panel_users, 'search')
        self.frame.Bind(wx.EVT_BUTTON, self.search_user, self.button_search)
        # Exit Buttons
        self.button_exit = xrc.XRCCTRL(self.panel_users, 'logOut')
        self.frame.Bind(wx.EVT_BUTTON, self.close_frames, self.button_exit)

        #Series
        self.panel_serie = xrc.XRCCTRL(self.listbook_menu, 'panelSeries')
        self.grid_serie = xrc.XRCCTRL(self.panel_serie, 'scrolledWindowserie')
        self.grid_sizer_series()

        self.text_search_serie = xrc.XRCCTRL(self.panel_serie, 'textCtrlSerie')
        self.button_search_serie = xrc.XRCCTRL(self.panel_serie, 'searchSerie')
        self.frame.Bind(wx.EVT_BUTTON, self.search_serie,
                        self.button_search_serie)
        self.button_exit_series = xrc.XRCCTRL(self.panel_users, 'logOut')
        self.Bind(wx.EVT_BUTTON, self.close_frames, self.button_exit_series)
        # Button Add series
        self.button_add_serie = xrc.XRCCTRL(self.panel_serie, 'addSerie')
        self.frame.Bind(wx.EVT_BUTTON, self.open_add_serie,
                        self.button_add_serie)

        #Games
        self.panel_game = xrc.XRCCTRL(self.listbook_menu, 'panelGames')
        self.grid_game = xrc.XRCCTRL(self.panel_game, 'scrolledWindowGame')
        self.grid_sizer_games()

        self.text_search_game = xrc.XRCCTRL(self.panel_game, 'textCtrlGame')
        self.button_search_game = xrc.XRCCTRL(self.panel_game, 'searchGame')
        self.frame.Bind(wx.EVT_BUTTON, self.search_game,
                        self.button_search_game)
        self.button_exit_games = xrc.XRCCTRL(self.panel_users, 'logOut')
        self.Bind(wx.EVT_BUTTON, self.close_frames, self.button_exit_games)
        #Button Add Games
        self.button_add_game = xrc.XRCCTRL(self.panel_game, 'addGame')
        self.frame.Bind(wx.EVT_BUTTON, self.open_add_game,
                        self.button_add_game)

        self.frame.Show()

    # Se puede mejorar la funcion de buscar series de manera que retorne el boton de la serie mas no un mensaje solamente.
    def search_serie(self, evt):
        text_serie = str(self.text_search_serie.GetValue())
        session = self.conect.connect_database()

        try:
            serie = session.query(Serie).filter_by(
                titulo_serie=text_serie).all()
            if text_serie != "":
                if serie:

                    wx.MessageBox('The serie exist in this aplication.')

                else:
                    wx.MessageBox(
                        'The serie does not exist in this aplication.')

            else:
                wx.MessageBox('Fields no entered.')

            session.close()

        except MultipleResultsFound:
            session.close()
            return False

    # Se puede mejorar la funcion de buscar juegos de manera que retorne el boton del juego mas no un mensaje solamente.
    def search_game(self, evt):
        text_game = str(self.text_search_game.GetValue())
        session = self.conect.connect_database()

        try:
            game = session.query(Videojuego).filter_by(
                titulo_videojuego=text_game).all()
            if text_game != "":
                if game:
                    wx.MessageBox('The videogame exist in this aplication.')

                else:
                    wx.MessageBox(
                        'The videogame does not exist in this aplication.')

            else:
                wx.MessageBox('Fields no entered')

            session.close()

        except MultipleResultsFound:
            session.close()
            return False

    def grid_sizer_games(self):
        cols = 2
        images_games = self.games.get_all_images_games()
        cantidad = len(images_games)
        filas = cantidad // cols

        if cantidad % cols != 0:
            filas += 1

        grid_sizer = wx.GridSizer(filas, cols, 5, 5)
        indicador = 0
        for i in range(grid_sizer.GetRows()):
            for j in range(grid_sizer.GetCols()):
                if indicador < len(images_games):
                    button = wx.BitmapButton(
                        self.grid_game,
                        name=str(images_games[indicador].id),
                        bitmap=wx.Bitmap(images_games[indicador].imagen_url))
                    grid_sizer.Add(button, 0, wx.EXPAND)
                    self.grid_game.Bind(wx.EVT_BUTTON, self.click_images_game,
                                        button)
                    self.grid_game.SetSizer(grid_sizer)
                    indicador += 1

    def grid_sizer_series(self):
        cols = 2
        images_series = self.series.get_all_images()
        cantidad = len(images_series)
        filas = cantidad // cols

        if cantidad % cols != 0:
            filas += 1

        grid_sizer = wx.GridSizer(filas, cols, 5, 5)
        indicador = 0
        for i in range(grid_sizer.GetRows()):
            for j in range(grid_sizer.GetCols()):
                if indicador < len(images_series):
                    button = wx.BitmapButton(
                        self.grid_serie,
                        name=str(images_series[indicador].id),
                        bitmap=wx.Bitmap(images_series[indicador].imagen_url))
                    grid_sizer.Add(button, 0, wx.EXPAND)
                    self.grid_serie.Bind(wx.EVT_BUTTON,
                                         self.click_images_serie, button)
                    self.grid_serie.SetSizer(grid_sizer)
                    indicador += 1

    def click_images_serie(self, evt):
        id_serie = evt.GetEventObject().GetName()
        VistaSerie(id_serie=id_serie)

    def click_images_game(self, evt):
        id_game = evt.GetEventObject().GetName()
        vistaGame(id_game=id_game)

    def open_add_serie(self, evt):
        self.add_series = RegistroSerie(self)

    def open_add_game(self, evt):
        self.add_games = RegistroGame(self)

    def search_user(self, evt):
        text = str(self.text_search.GetValue())
        session = self.conect.connect_database()

        try:
            user = session.query(User).filter_by(first_name=text).all()
            if text != "":
                if user:
                    self.load_data_listctrl_name(text)
                    return True

                else:
                    wx.MessageBox('User does not exist in the database')
                    return self.load_data_listctrl_user()

            else:
                wx.MessageBox('Fields no entered')
                self.load_data_listctrl_user()

            session.close()

        except MultipleResultsFound:
            session.close()
            return False

    def close_frames(self, evt):
        self.frame.Destroy()

    def load_columns_listcrtl_user(self):
        self.listctrl_users.InsertColumn(0,
                                         "Id",
                                         format=wx.LIST_FORMAT_CENTER,
                                         width=wx.LIST_AUTOSIZE)
        self.listctrl_users.InsertColumn(1,
                                         "First Name",
                                         format=wx.LIST_FORMAT_CENTER,
                                         width=wx.LIST_AUTOSIZE)
        self.listctrl_users.InsertColumn(2,
                                         "Last Name",
                                         format=wx.LIST_FORMAT_CENTER,
                                         width=wx.LIST_AUTOSIZE)
        self.listctrl_users.InsertColumn(3,
                                         "UserName",
                                         format=wx.LIST_FORMAT_CENTER,
                                         width=wx.LIST_AUTOSIZE)
        self.listctrl_users.InsertColumn(4,
                                         "Password",
                                         format=wx.LIST_FORMAT_CENTER,
                                         width=wx.LIST_AUTOSIZE)

    def load_data_listctrl_user(self):
        self.list_user = self.user_controller.get_all_users()
        self.listctrl_users.DeleteAllItems()

        for user in self.list_user:
            if len(self.list_user) > 1:
                self.listctrl_users.Append([
                    user.id, user.first_name, user.last_name, user.username,
                    user.password
                ])

    def load_data_listctrl_name(self, name):
        user = self.user_controller.get_first_name(name)
        self.listctrl_users.DeleteAllItems()

        for item in user:
            self.listctrl_users.Append([
                item.id, item.first_name, item.last_name, item.username,
                item.password
            ])

    def create_user(self, evt):
        self.add_user_frame = RegistroFrame(self)

    def list_user_selected(self, evt):
        current_item = evt.GetIndex()
        self.user_selected = self.list_user[current_item]
        menu = wx.Menu()
        id_item_menu_update = wx.NewId()
        id_item_menu_delete = wx.NewId()
        menu.Append(id_item_menu_update, UPDATE_USER)
        menu.Append(id_item_menu_delete, DELETE_USER)
        self.frame.Bind(wx.EVT_MENU,
                        self.popup_item_selected,
                        id=id_item_menu_update)
        self.frame.Bind(wx.EVT_MENU,
                        self.popup_item_selected,
                        id=id_item_menu_delete)
        self.frame.PopupMenu(menu)
        menu.Destroy()

    def popup_item_selected(self, evt):
        id_item = evt.GetId()
        menu = evt.GetEventObject()
        menu_item = menu.FindItemById(id_item)

        if menu_item.GetLabel() == UPDATE_USER:
            self.add_user_frame = ModificFrame(self, self.user_selected.id)

        elif menu_item.GetLabel() == DELETE_USER:
            msg = "Is you sure to delete the User %s %s?" % (
                self.user_selected.first_name, self.user_selected.last_name)
            result = wx.MessageBox(msg, "Delete User",
                                   wx.YES_NO | wx.ICON_EXCLAMATION)

            if result == wx.YES:
                self.user_controller.delete_user(self.user_selected.id)
                self.load_data_listctrl_user()
class UserController():
    def __init__(self):
        self.conect = Conection()

    def create_user(self, first_name, last_name, username, password):
        user = User(first_name=first_name,
                    last_name=last_name,
                    username=username,
                    password=password)
        session = self.conect.connect_database()
        session.add(user)
        session.commit()
        session.close()

    def get_user(self, id_user):
        session = self.conect.connect_database()

        try:
            user = session.query(User).filter_by(id=id_user).one()
            session.close()
            return user

        except (MultipleResultsFound, NoResultFound):
            return None

    def get_all_users(self):
        session = self.conect.connect_database()
        users = session.query(User).all()
        session.close()
        return users

    def search_user(self, username, password):
        session = self.conect.connect_database()

        try:
            user = session.query(User).filter_by(
                username=username, password=password).one_or_none()
            session.close()

            if user is not None:
                return True
            return False

        except MultipleResultsFound:
            session.close()
            return False

    def search_username(self, username):
        session = self.conect.connect_database()

        try:
            user = session.query(User).filter_by(
                username=username).one_or_none()
            session.close()

            if user is not None:
                return user
            return None

        except MultipleResultsFound:
            session.close()
            return False

    def edit_user(self, id_user, data):
        session = self.conect.connect_database()
        user = session.query(User).filter_by(id=id_user).one()
        user.first_name = data['first_name']
        user.last_name = data['last_name']
        user.username = data['username']
        user.password = data['password']
        session.add(user)
        session.commit()
        session.close()

    def delete_user(self, id_user):
        session = self.conect.connect_database()
        user = session.query(User).filter_by(id=id_user).one()
        session.delete(user)
        session.commit()
        session.close()

    def get_first_name_user(self, first_name):
        session = self.conect.connect_database()

        try:
            user = session.query(User).filter_by(
                first_name=first_name).one_or_none()
            session.close()
            return user.first_name

        except (MultipleResultsFound, NoResultFound):
            return first_name

    def get_first_name(self, first_name):
        session = self.conect.connect_database()

        try:
            user = session.query(User).filter_by(first_name=first_name).all()
            session.close()
            return user

        except (MultipleResultsFound, NoResultFound):
            return None