Mercurial > kallithea
comparison rhodecode/model/user.py @ 629:7e536d1af60d beta
Code refactoring,models renames
cleaned up sqlalchemy sessions,
added cache support to most queries in models
fixed test.ini file
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Thu, 21 Oct 2010 01:38:14 +0200 |
parents | rhodecode/model/user_model.py@1e757ac98988 |
children | dd532af216d9 |
comparison
equal
deleted
inserted
replaced
628:dd06bdf974c8 | 629:7e536d1af60d |
---|---|
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 Created on April 9, 2010 | |
22 Model for users | |
23 :author: marcink | |
24 """ | |
25 | |
26 from pylons.i18n.translation import _ | |
27 from rhodecode.model.caching_query import FromCache | |
28 from rhodecode.model.db import User | |
29 from rhodecode.model.meta import Session | |
30 import logging | |
31 import traceback | |
32 | |
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(self, user_id, cache=False): | |
46 user = self.sa.query(User) | |
47 if cache: | |
48 user = user.options(FromCache("sql_cache_short", | |
49 "get_user_%s" % user_id)) | |
50 return user.get(user_id) | |
51 | |
52 | |
53 def get_by_username(self, username, cache=False): | |
54 user = self.sa.query(User)\ | |
55 .filter(User.username == username) | |
56 if cache: | |
57 user = user.options(FromCache("sql_cache_short", | |
58 "get_user_%s" % username)) | |
59 return user.scalar() | |
60 | |
61 def create(self, form_data): | |
62 try: | |
63 new_user = User() | |
64 for k, v in form_data.items(): | |
65 setattr(new_user, k, v) | |
66 | |
67 self.sa.add(new_user) | |
68 self.sa.commit() | |
69 except: | |
70 log.error(traceback.format_exc()) | |
71 self.sa.rollback() | |
72 raise | |
73 | |
74 def create_registration(self, form_data): | |
75 try: | |
76 new_user = User() | |
77 for k, v in form_data.items(): | |
78 if k != 'admin': | |
79 setattr(new_user, k, v) | |
80 | |
81 self.sa.add(new_user) | |
82 self.sa.commit() | |
83 except: | |
84 log.error(traceback.format_exc()) | |
85 self.sa.rollback() | |
86 raise | |
87 | |
88 def update(self, user_id, form_data): | |
89 try: | |
90 new_user = self.get(user_id, cache=False) | |
91 if new_user.username == 'default': | |
92 raise DefaultUserException( | |
93 _("You can't Edit this user since it's" | |
94 " crucial for entire application")) | |
95 for k, v in form_data.items(): | |
96 if k == 'new_password' and v != '': | |
97 new_user.password = v | |
98 else: | |
99 setattr(new_user, k, v) | |
100 | |
101 self.sa.add(new_user) | |
102 self.sa.commit() | |
103 except: | |
104 log.error(traceback.format_exc()) | |
105 self.sa.rollback() | |
106 raise | |
107 | |
108 def update_my_account(self, user_id, form_data): | |
109 try: | |
110 new_user = self.get(user_id, cache=False) | |
111 if new_user.username == 'default': | |
112 raise DefaultUserException( | |
113 _("You can't Edit this user since it's" | |
114 " crucial for entire application")) | |
115 for k, v in form_data.items(): | |
116 if k == 'new_password' and v != '': | |
117 new_user.password = v | |
118 else: | |
119 if k not in ['admin', 'active']: | |
120 setattr(new_user, k, v) | |
121 | |
122 self.sa.add(new_user) | |
123 self.sa.commit() | |
124 except: | |
125 log.error(traceback.format_exc()) | |
126 self.sa.rollback() | |
127 raise | |
128 | |
129 def delete(self, user_id): | |
130 try: | |
131 user = self.get(user_id, cache=False) | |
132 if user.username == 'default': | |
133 raise DefaultUserException( | |
134 _("You can't remove this user since it's" | |
135 " crucial for entire application")) | |
136 self.sa.delete(user) | |
137 self.sa.commit() | |
138 except: | |
139 log.error(traceback.format_exc()) | |
140 self.sa.rollback() | |
141 raise | |
142 | |
143 def reset_password(self, data): | |
144 from rhodecode.lib.celerylib import tasks, run_task | |
145 run_task(tasks.reset_user_password, data['email']) |