Mercurial > kallithea
comparison pylons_app/model/forms.py @ 265:0e5455fda8fd
Implemented basic repository managment. Implemented repo2db mappings, model, helpers updates and code cleanups
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Mon, 07 Jun 2010 00:18:33 +0200 |
parents | 5da4ef115006 |
children | 29370bb76fa6 |
comparison
equal
deleted
inserted
replaced
264:0d68a749db33 | 265:0e5455fda8fd |
---|---|
23 from formencode.validators import UnicodeString, OneOf, Int, Number, Regex, \ | 23 from formencode.validators import UnicodeString, OneOf, Int, Number, Regex, \ |
24 Email, Bool, StringBoolean | 24 Email, Bool, StringBoolean |
25 from pylons import session | 25 from pylons import session |
26 from pylons.i18n.translation import _ | 26 from pylons.i18n.translation import _ |
27 from pylons_app.lib.auth import get_crypt_password | 27 from pylons_app.lib.auth import get_crypt_password |
28 import pylons_app.lib.helpers as h | |
28 from pylons_app.model import meta | 29 from pylons_app.model import meta |
29 from pylons_app.model.db import User | 30 from pylons_app.model.db import User, Repository |
30 from sqlalchemy.exc import OperationalError | 31 from sqlalchemy.exc import OperationalError |
31 from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound | 32 from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound |
32 from webhelpers.pylonslib.secure_form import authentication_token | 33 from webhelpers.pylonslib.secure_form import authentication_token |
33 import datetime | 34 import datetime |
34 import formencode | 35 import formencode |
91 from pylons_app.lib.auth import AuthUser | 92 from pylons_app.lib.auth import AuthUser |
92 auth_user = AuthUser() | 93 auth_user = AuthUser() |
93 auth_user.username = username | 94 auth_user.username = username |
94 auth_user.is_authenticated = True | 95 auth_user.is_authenticated = True |
95 auth_user.is_admin = user.admin | 96 auth_user.is_admin = user.admin |
97 auth_user.user_id = user.user_id | |
96 session['hg_app_user'] = auth_user | 98 session['hg_app_user'] = auth_user |
97 session.save() | 99 session.save() |
98 log.info('user %s is now authenticated', username) | 100 log.info('user %s is now authenticated', username) |
99 | 101 |
100 try: | 102 try: |
117 state=State_obj), | 119 state=State_obj), |
118 value, state, | 120 value, state, |
119 error_dict=self.e_dict_disable) | 121 error_dict=self.e_dict_disable) |
120 | 122 |
121 | 123 |
122 | 124 class ValidRepoUser(formencode.validators.FancyValidator): |
125 | |
126 def to_python(self, value, state): | |
127 sa = meta.Session | |
128 try: | |
129 self.user_db = sa.query(User).filter(User.username == value).one() | |
130 except Exception: | |
131 raise formencode.Invalid(_('This username is not valid'), | |
132 value, state) | |
133 return self.user_db.user_id | |
134 | |
135 def ValidRepoName(edit=False): | |
136 class _ValidRepoName(formencode.validators.FancyValidator): | |
137 | |
138 def to_python(self, value, state): | |
139 slug = h.repo_name_slug(value) | |
140 | |
141 sa = meta.Session | |
142 if sa.query(Repository).get(slug) and not edit: | |
143 raise formencode.Invalid(_('This repository already exists'), | |
144 value, state) | |
145 | |
146 return slug | |
147 return _ValidRepoName | |
123 #=============================================================================== | 148 #=============================================================================== |
124 # FORMS | 149 # FORMS |
125 #=============================================================================== | 150 #=============================================================================== |
126 class LoginForm(formencode.Schema): | 151 class LoginForm(formencode.Schema): |
127 allow_extra_fields = True | 152 allow_extra_fields = True |
161 name = UnicodeString(strip=True, min=3, not_empty=True) | 186 name = UnicodeString(strip=True, min=3, not_empty=True) |
162 lastname = UnicodeString(strip=True, min=3, not_empty=True) | 187 lastname = UnicodeString(strip=True, min=3, not_empty=True) |
163 email = Email(not_empty=True) | 188 email = Email(not_empty=True) |
164 | 189 |
165 return _UserForm | 190 return _UserForm |
191 | |
192 def RepoForm(edit=False): | |
193 class _RepoForm(formencode.Schema): | |
194 allow_extra_fields = True | |
195 filter_extra_fields = True | |
196 repo_name = All(UnicodeString(strip=True, min=1, not_empty=True), ValidRepoName(edit)) | |
197 description = UnicodeString(strip=True, min=3, not_empty=True) | |
198 private = StringBoolean(if_missing=False) | |
199 | |
200 if edit: | |
201 user = All(Int(not_empty=True), ValidRepoUser) | |
202 | |
203 return _RepoForm |