Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/boards/python/admin/user_edit.py
blob: d3b4a67f0f2e82b17fd32f75d5e3d23fc741290f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#  gcompris - user_edit.py
#
# Copyright (C) 2005 Bruno Coudoin
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, see <http://www.gnu.org/licenses/>.
#


import gtk
import gobject
from gettext import gettext as _

# Database
from pysqlite2 import dbapi2 as sqlite

import user_list

import constants

class UserEdit(gtk.Window):

    def __init__(self, db_connect, db_cursor,
                 user_id, login, firstname, lastname, birthdate, class_id,
                 list_user):
        # Create the toplevel window
        gtk.Window.__init__(self)

        self.cur = db_cursor
        self.con = db_connect

        self.user_id  = user_id
        self.class_id = class_id

        # A pointer to the user_list class
        # Will be called to refresh the list when edit is done
        self.list_user = list_user

        self.set_title(_("Editing a User"))
        self.set_border_width(8)
        self.set_default_size(320, 250)

        if(firstname and lastname):
            frame = gtk.Frame(_("Editing a User ") +
                              lastname + " " +
                              firstname)
            self.new_user = False
            self.old_login = login
        else:
            frame = gtk.Frame(_("Editing a new user"))
            self.new_user = True

        self.add(frame)

        # Main VBOX
        vbox = gtk.VBox(False, 8)
        vbox.set_border_width(8)
        frame.add(vbox)

        # Label and Entry for the user name
        table = gtk.Table(2, 5, homogeneous=False)
        table.set_border_width(0)
        table.set_row_spacings(0)
        table.set_col_spacings(20)
        vbox.pack_start(table, True, True, 0)

        label = gtk.Label(_('Login:'))
        label.set_alignment(0, 0)
        table.attach(label, 0, 1, 0, 1, xoptions=gtk.SHRINK, yoptions=gtk.EXPAND)
        self.entry_login = gtk.Entry()
        self.entry_login.set_max_length(20)
        self.entry_login.insert_text(login, position=0)
        table.attach(self.entry_login, 1, 2, 0, 1, xoptions=gtk.SHRINK, yoptions=gtk.EXPAND)

        # FIXME: How to remove the selection

        # Label and Entry for the first name
        label = gtk.Label(_('First name:'))
        label.set_alignment(0, 0)
        table.attach(label, 0, 1, 1, 2, xoptions=gtk.SHRINK, yoptions=gtk.EXPAND)
        self.entry_firstname = gtk.Entry()
        self.entry_firstname.set_max_length(30)
        self.entry_firstname.insert_text(firstname, position=0)
        table.attach(self.entry_firstname, 1, 2, 1, 2, xoptions=gtk.SHRINK, yoptions=gtk.EXPAND)

        # Label and Entry for the last name
        label = gtk.Label(_('Last name:'))
        label.set_alignment(0, 0)
        table.attach(label, 0, 1, 2, 3, xoptions=gtk.SHRINK, yoptions=gtk.EXPAND)
        self.entry_lastname = gtk.Entry()
        self.entry_lastname.set_max_length(30)
        self.entry_lastname.insert_text(lastname, position=0)
        table.attach(self.entry_lastname, 1, 2, 2, 3, xoptions=gtk.SHRINK, yoptions=gtk.EXPAND)

        # Label and Entry for the birth date
        label = gtk.Label(_('Birth date:'))
        label.set_alignment(0, 0)
        table.attach(label, 0, 1, 3, 4, xoptions=gtk.SHRINK, yoptions=gtk.EXPAND)
        self.entry_birthdate = gtk.Entry()
        self.entry_birthdate.set_max_length(30)
        self.entry_birthdate.insert_text(birthdate, position=0)
        table.attach(self.entry_birthdate, 1, 2, 3, 4, xoptions=gtk.SHRINK, yoptions=gtk.EXPAND)

        # Confirmation Buttons
        # --------------------
        vbox.pack_start(gtk.HSeparator(), False, False, 0)

        bbox = gtk.HBox(homogeneous=False, spacing=8)

        button = gtk.Button(stock='gtk-help')
        bbox.pack_start(button, expand=False, fill=False, padding=0)

        button = gtk.Button(stock='gtk-ok')
        bbox.pack_end(button, expand=False, fill=False, padding=0)
        button.connect("clicked", self.ok)

        button = gtk.Button(stock='gtk-close')
        bbox.pack_end(button, expand=False, fill=False, padding=0)
        button.connect("clicked", self.close)

        vbox.pack_start(bbox, False, False, 0)

        # Ready GO
        self.show_all()


    # -------------------
    # User Management
    # -------------------


    # Done, can quit this dialog (without saving)
    #
    def close(self, button):
        self.destroy()

    # Done, can quit this dialog with saving
    #
    def ok(self, button):

        # Tell the user he must provide enough information
        if(self.entry_login.get_text() == "" or
           self.entry_firstname.get_text() == "" or
           self.entry_lastname.get_text() == ""):
            dialog = gtk.MessageDialog(None,
                                       gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
                                       gtk.MESSAGE_INFO, gtk.BUTTONS_OK,
                                       _("You need to provide at least a login, first name and last name for your users"))
            dialog.run()
            dialog.destroy()
            return

        if(self.new_user):
            # Check the login do not exist already
            self.cur.execute('SELECT login FROM users')
        else:
            self.cur.execute('SELECT login FROM users WHERE login != \'%s\'' % self.old_login)
        logins = [x[0].upper() for x in self.cur.fetchall()]
        login = self.entry_login.get_text().decode('utf-8').upper()

        if login in logins:
            dialog = gtk.MessageDialog(None,
                                       gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
                                       gtk.MESSAGE_INFO, gtk.BUTTONS_OK,
                                       _("There is already a user with this login"))
            dialog.run()
            dialog.destroy()
            return

        #
        # Now everything is correct, create the user
        #
        user_data = (self.user_id,
                     self.entry_login.get_text(),
                     self.entry_firstname.get_text(),
                     self.entry_lastname.get_text(),
                     self.entry_birthdate.get_text(),
                     self.class_id
                     )
        # Save the changes in the base
        self.cur.execute('insert or replace into users (user_id, login, firstname, lastname, birthdate, class_id) values (?, ?, ?, ?, ?, ?)', user_data)
        self.con.commit()

        # Close the dialog window now
        print "user_edit calling reload"
        self.list_user.reload(self.class_id)

        self.destroy()