Back to index

gcompris  8.2.2
user_edit.py
Go to the documentation of this file.
00001 #  gcompris - user_edit.py
00002 # 
00003 # Copyright (C) 2005 Bruno Coudoin
00004 # 
00005 #   This program is free software; you can redistribute it and/or modify
00006 #   it under the terms of the GNU General Public License as published by
00007 #   the Free Software Foundation; either version 2 of the License, or
00008 #   (at your option) any later version.
00009 # 
00010 #   This program is distributed in the hope that it will be useful,
00011 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 #   GNU General Public License for more details.
00014 # 
00015 #   You should have received a copy of the GNU General Public License
00016 #   along with this program; if not, write to the Free Software
00017 #   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 # 
00019 
00020 
00021 import gtk
00022 import gobject
00023 from gettext import gettext as _
00024 
00025 # Database
00026 from pysqlite2 import dbapi2 as sqlite
00027 
00028 import user_list
00029 
00030 import constants
00031 
00032 class UserEdit(gtk.Window):
00033 
00034     def __init__(self, db_connect, db_cursor,
00035                  user_id, login, firstname, lastname, birthdate, class_id,
00036                  list_user):
00037         # Create the toplevel window
00038         gtk.Window.__init__(self)
00039 
00040         self.cur = db_cursor
00041         self.con = db_connect
00042 
00043         self.user_id  = user_id
00044         self.class_id = class_id
00045         
00046         # A pointer to the user_list class
00047         # Will be called to refresh the list when edit is done
00048         self.list_user = list_user
00049         
00050         self.set_title(_("Editing a User"))
00051         self.set_border_width(8)
00052         self.set_default_size(320, 250)
00053 
00054         if(firstname and lastname):
00055             frame = gtk.Frame(_("Editing a User ") +
00056                               lastname + " " +
00057                               firstname)
00058             self.new_user = False
00059             self.old_login = login
00060         else:
00061             frame = gtk.Frame(_("Editing a new user"))
00062             self.new_user = True
00063 
00064         self.add(frame)
00065 
00066         # Main VBOX
00067         vbox = gtk.VBox(False, 8)
00068         vbox.set_border_width(8)
00069         frame.add(vbox)
00070 
00071         # Label and Entry for the user name
00072         table = gtk.Table(2, 5, homogeneous=False)
00073         table.set_border_width(0)
00074         table.set_row_spacings(0)
00075         table.set_col_spacings(20)
00076         vbox.pack_start(table, True, True, 0)
00077         
00078         label = gtk.Label(_('Login:'))
00079         label.set_alignment(0, 0)
00080         table.attach(label, 0, 1, 0, 1, xoptions=gtk.SHRINK, yoptions=gtk.EXPAND)
00081         self.entry_login = gtk.Entry()
00082         self.entry_login.set_max_length(20)
00083         self.entry_login.insert_text(login, position=0)
00084         table.attach(self.entry_login, 1, 2, 0, 1, xoptions=gtk.SHRINK, yoptions=gtk.EXPAND)
00085 
00086         # FIXME: How to remove the selection
00087         
00088         # Label and Entry for the first name
00089         label = gtk.Label(_('First name:'))
00090         label.set_alignment(0, 0)
00091         table.attach(label, 0, 1, 1, 2, xoptions=gtk.SHRINK, yoptions=gtk.EXPAND)
00092         self.entry_firstname = gtk.Entry()
00093         self.entry_firstname.set_max_length(30)
00094         self.entry_firstname.insert_text(firstname, position=0)
00095         table.attach(self.entry_firstname, 1, 2, 1, 2, xoptions=gtk.SHRINK, yoptions=gtk.EXPAND)
00096 
00097         # Label and Entry for the last name
00098         label = gtk.Label(_('Last name:'))
00099         label.set_alignment(0, 0)
00100         table.attach(label, 0, 1, 2, 3, xoptions=gtk.SHRINK, yoptions=gtk.EXPAND)
00101         self.entry_lastname = gtk.Entry()
00102         self.entry_lastname.set_max_length(30)
00103         self.entry_lastname.insert_text(lastname, position=0)
00104         table.attach(self.entry_lastname, 1, 2, 2, 3, xoptions=gtk.SHRINK, yoptions=gtk.EXPAND)
00105 
00106         # Label and Entry for the birth date
00107         label = gtk.Label(_('Birth date:'))
00108         label.set_alignment(0, 0)
00109         table.attach(label, 0, 1, 3, 4, xoptions=gtk.SHRINK, yoptions=gtk.EXPAND)
00110         self.entry_birthdate = gtk.Entry()
00111         self.entry_birthdate.set_max_length(30)
00112         self.entry_birthdate.insert_text(birthdate, position=0)
00113         table.attach(self.entry_birthdate, 1, 2, 3, 4, xoptions=gtk.SHRINK, yoptions=gtk.EXPAND)
00114 
00115         # Confirmation Buttons
00116         # --------------------
00117         vbox.pack_start(gtk.HSeparator(), False, False, 0)
00118 
00119         bbox = gtk.HBox(homogeneous=False, spacing=8)
00120         
00121         button = gtk.Button(stock='gtk-help')
00122         bbox.pack_start(button, expand=False, fill=False, padding=0)
00123 
00124         button = gtk.Button(stock='gtk-ok')
00125         bbox.pack_end(button, expand=False, fill=False, padding=0)
00126         button.connect("clicked", self.ok)
00127 
00128         button = gtk.Button(stock='gtk-close')
00129         bbox.pack_end(button, expand=False, fill=False, padding=0)
00130         button.connect("clicked", self.close)
00131 
00132         vbox.pack_start(bbox, False, False, 0)
00133         
00134         # Ready GO
00135         self.show_all()
00136 
00137 
00138     # -------------------
00139     # User Management
00140     # -------------------
00141 
00142 
00143     # Done, can quit this dialog (without saving)
00144     #
00145     def close(self, button):
00146         self.destroy()
00147         
00148     # Done, can quit this dialog with saving
00149     #
00150     def ok(self, button):
00151 
00152         # Tell the user he must provide enough information
00153         if(self.entry_login.get_text() == "" or
00154            self.entry_firstname.get_text() == "" or
00155            self.entry_lastname.get_text() == ""):
00156             dialog = gtk.MessageDialog(None,
00157                                        gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
00158                                        gtk.MESSAGE_INFO, gtk.BUTTONS_OK,
00159                                        _("You need to provide at least a login, first name and last name for your users"))
00160             dialog.run()
00161             dialog.destroy()
00162             return
00163 
00164         if(self.new_user):
00165             # Check the login do not exist already
00166             self.cur.execute('SELECT login FROM users')
00167         else:
00168             self.cur.execute('SELECT login FROM users WHERE login != \'%s\'' % self.old_login)
00169         logins = [x[0].upper() for x in self.cur.fetchall()]
00170         login = self.entry_login.get_text().decode('utf-8').upper()
00171 
00172         if login in logins:
00173             dialog = gtk.MessageDialog(None,
00174                                        gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
00175                                        gtk.MESSAGE_INFO, gtk.BUTTONS_OK,
00176                                        _("There is already a user with this login"))
00177             dialog.run()
00178             dialog.destroy()
00179             return
00180 
00181         #
00182         # Now everything is correct, create the user
00183         #
00184         user_data = (self.user_id,
00185                      self.entry_login.get_text(),
00186                      self.entry_firstname.get_text(),
00187                      self.entry_lastname.get_text(),
00188                      self.entry_birthdate.get_text(),
00189                      self.class_id
00190                      )
00191         # Save the changes in the base
00192         self.cur.execute('insert or replace into users (user_id, login, firstname, lastname, birthdate, class_id) values (?, ?, ?, ?, ?, ?)', user_data)
00193         self.con.commit()
00194 
00195         # Close the dialog window now
00196         print "user_edit calling reload"
00197         self.list_user.reload(self.class_id)
00198 
00199         self.destroy()
00200