##// END OF EJS Templates
protected againts changing default user.
marcink -
r314:0d26d46b default
parent child Browse files
Show More
@@ -1,77 +1,91 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
2 # encoding: utf-8
3 # Model for users
3 # Model for users
4 # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com>
4 # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com>
5
5
6 # This program is free software; you can redistribute it and/or
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; version 2
8 # as published by the Free Software Foundation; version 2
9 # of the License or (at your opinion) any later version of the license.
9 # of the License or (at your opinion) any later version of the license.
10 #
10 #
11 # This program is distributed in the hope that it will be useful,
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
14 # GNU General Public License for more details.
15 #
15 #
16 # You should have received a copy of the GNU General Public License
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 # MA 02110-1301, USA.
19 # MA 02110-1301, USA.
20
20
21 """
21 """
22 Created on April 9, 2010
22 Created on April 9, 2010
23 Model for users
23 Model for users
24 @author: marcink
24 @author: marcink
25 """
25 """
26
26
27 from pylons_app.model.db import User
27 from pylons_app.model.db import User
28 from pylons_app.model.meta import Session
28 from pylons_app.model.meta import Session
29 from pylons.i18n.translation import _
29 import logging
30 import logging
30 log = logging.getLogger(__name__)
31 log = logging.getLogger(__name__)
31
32
33 class DefaultUserException(Exception):pass
34
32 class UserModel(object):
35 class UserModel(object):
33
36
34 def __init__(self):
37 def __init__(self):
35 self.sa = Session()
38 self.sa = Session()
36
39
37 def get_user(self, id):
40 def get_user(self, id):
38 return self.sa.query(User).get(id)
41 return self.sa.query(User).get(id)
39
42
40 def create(self, form_data):
43 def create(self, form_data):
41 try:
44 try:
42 new_user = User()
45 new_user = User()
43 for k, v in form_data.items():
46 for k, v in form_data.items():
44 setattr(new_user, k, v)
47 setattr(new_user, k, v)
45
48
46 self.sa.add(new_user)
49 self.sa.add(new_user)
47 self.sa.commit()
50 self.sa.commit()
48 except Exception as e:
51 except Exception as e:
49 log.error(e)
52 log.error(e)
50 self.sa.rollback()
53 self.sa.rollback()
51 raise
54 raise
52
55
53 def update(self, id, form_data):
56 def update(self, id, form_data):
54 try:
57 try:
55 new_user = self.sa.query(User).get(id)
58 new_user = self.sa.query(User).get(id)
59 if new_user.username == 'default':
60 raise DefaultUserException(
61 _("You can't Edit this user since it's"
62 " crucial for entire application"))
56 for k, v in form_data.items():
63 for k, v in form_data.items():
57 if k == 'new_password' and v != '':
64 if k == 'new_password' and v != '':
58
65
59 new_user.password = v
66 new_user.password = v
60 else:
67 else:
61 setattr(new_user, k, v)
68 setattr(new_user, k, v)
62
69
63 self.sa.add(new_user)
70 self.sa.add(new_user)
64 self.sa.commit()
71 self.sa.commit()
65 except Exception as e:
72 except Exception as e:
66 log.error(e)
73 log.error(e)
67 self.sa.rollback()
74 self.sa.rollback()
68 raise
75 raise
69
76
70 def delete(self, id):
77 def delete(self, id):
78
71 try:
79 try:
72 self.sa.delete(self.sa.query(User).get(id))
80
81 user = self.sa.query(User).get(id)
82 if user.username == 'default':
83 raise DefaultUserException(
84 _("You can't remove this user since it's"
85 " crucial for entire application"))
86 self.sa.delete(user)
73 self.sa.commit()
87 self.sa.commit()
74 except Exception as e:
88 except Exception as e:
75 log.error(e)
89 log.error(e)
76 self.sa.rollback()
90 self.sa.rollback()
77 raise
91 raise
General Comments 0
You need to be logged in to leave comments. Login now