Mercurial > kallithea
comparison rhodecode/model/user_model.py @ 547:1e757ac98988
renamed project to rhodecode
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Wed, 06 Oct 2010 03:18:16 +0200 |
parents | pylons_app/model/user_model.py@a08f610e545e |
children |
comparison
equal
deleted
inserted
replaced
546:7c2f5e4d7bbf | 547:1e757ac98988 |
---|---|
1 #!/usr/bin/env python | |
2 # encoding: utf-8 | |
3 # Model for users | |
4 # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com> | |
5 # | |
6 # This program is free software; you can redistribute it and/or | |
7 # modify it under the terms of the GNU General Public License | |
8 # as published by the Free Software Foundation; version 2 | |
9 # of the License or (at your opinion) any later version of the license. | |
10 # | |
11 # This program is distributed in the hope that it will be useful, | |
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 # GNU General Public License for more details. | |
15 # | |
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 | |
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | |
19 # MA 02110-1301, USA. | |
20 | |
21 """ | |
22 Created on April 9, 2010 | |
23 Model for users | |
24 @author: marcink | |
25 """ | |
26 from rhodecode.lib import auth | |
27 from pylons.i18n.translation import _ | |
28 from rhodecode.lib.celerylib import tasks, run_task | |
29 from rhodecode.model.db import User | |
30 from rhodecode.model.meta import Session | |
31 import traceback | |
32 import logging | |
33 log = logging.getLogger(__name__) | |
34 | |
35 class DefaultUserException(Exception):pass | |
36 | |
37 class UserModel(object): | |
38 | |
39 def __init__(self, sa=None): | |
40 if not sa: | |
41 self.sa = Session() | |
42 else: | |
43 self.sa = sa | |
44 | |
45 def get_default(self): | |
46 return self.sa.query(User).filter(User.username == 'default').scalar() | |
47 | |
48 def get_user(self, id): | |
49 return self.sa.query(User).get(id) | |
50 | |
51 def get_user_by_name(self, name): | |
52 return self.sa.query(User).filter(User.username == name).scalar() | |
53 | |
54 def create(self, form_data): | |
55 try: | |
56 new_user = User() | |
57 for k, v in form_data.items(): | |
58 setattr(new_user, k, v) | |
59 | |
60 self.sa.add(new_user) | |
61 self.sa.commit() | |
62 except: | |
63 log.error(traceback.format_exc()) | |
64 self.sa.rollback() | |
65 raise | |
66 | |
67 def create_registration(self, form_data): | |
68 try: | |
69 new_user = User() | |
70 for k, v in form_data.items(): | |
71 if k != 'admin': | |
72 setattr(new_user, k, v) | |
73 | |
74 self.sa.add(new_user) | |
75 self.sa.commit() | |
76 except: | |
77 log.error(traceback.format_exc()) | |
78 self.sa.rollback() | |
79 raise | |
80 | |
81 def update(self, uid, form_data): | |
82 try: | |
83 new_user = self.sa.query(User).get(uid) | |
84 if new_user.username == 'default': | |
85 raise DefaultUserException( | |
86 _("You can't Edit this user since it's" | |
87 " crucial for entire application")) | |
88 for k, v in form_data.items(): | |
89 if k == 'new_password' and v != '': | |
90 new_user.password = v | |
91 else: | |
92 setattr(new_user, k, v) | |
93 | |
94 self.sa.add(new_user) | |
95 self.sa.commit() | |
96 except: | |
97 log.error(traceback.format_exc()) | |
98 self.sa.rollback() | |
99 raise | |
100 | |
101 def update_my_account(self, uid, form_data): | |
102 try: | |
103 new_user = self.sa.query(User).get(uid) | |
104 if new_user.username == 'default': | |
105 raise DefaultUserException( | |
106 _("You can't Edit this user since it's" | |
107 " crucial for entire application")) | |
108 for k, v in form_data.items(): | |
109 if k == 'new_password' and v != '': | |
110 new_user.password = v | |
111 else: | |
112 if k not in ['admin', 'active']: | |
113 setattr(new_user, k, v) | |
114 | |
115 self.sa.add(new_user) | |
116 self.sa.commit() | |
117 except: | |
118 log.error(traceback.format_exc()) | |
119 self.sa.rollback() | |
120 raise | |
121 | |
122 def delete(self, id): | |
123 try: | |
124 | |
125 user = self.sa.query(User).get(id) | |
126 if user.username == 'default': | |
127 raise DefaultUserException( | |
128 _("You can't remove this user since it's" | |
129 " crucial for entire application")) | |
130 self.sa.delete(user) | |
131 self.sa.commit() | |
132 except: | |
133 log.error(traceback.format_exc()) | |
134 self.sa.rollback() | |
135 raise | |
136 | |
137 def reset_password(self, data): | |
138 run_task(tasks.reset_user_password, data['email']) |