##// END OF EJS Templates
#56 fixed found bugs, implemented adding of new group + forms+validators...
marcink -
r959:fff21c9b beta
parent child Browse files
Show More
@@ -80,6 +80,10 def make_map(config):
80 m.connect('repo_cache', "/repos_cache/{repo_name:.*}",
80 m.connect('repo_cache', "/repos_cache/{repo_name:.*}",
81 action="repo_cache", conditions=dict(method=["DELETE"],
81 action="repo_cache", conditions=dict(method=["DELETE"],
82 function=check_repo))
82 function=check_repo))
83
84 #ADMIN USER REST ROUTES
85 map.resource('user', 'users', controller='admin/users', path_prefix='/_admin')
86
83 #ADMIN USER REST ROUTES
87 #ADMIN USER REST ROUTES
84 map.resource('users_group', 'users_groups', controller='admin/users_groups', path_prefix='/_admin')
88 map.resource('users_group', 'users_groups', controller='admin/users_groups', path_prefix='/_admin')
85
89
@@ -41,8 +41,9 from rhodecode.lib.auth import LoginRequ
41 from rhodecode.lib.base import BaseController, render
41 from rhodecode.lib.base import BaseController, render
42
42
43 from rhodecode.model.db import User, UsersGroup
43 from rhodecode.model.db import User, UsersGroup
44 from rhodecode.model.forms import UserForm
44 from rhodecode.model.forms import UserForm, UsersGroupForm
45 from rhodecode.model.user import UserModel
45 from rhodecode.model.user import UserModel
46 from rhodecode.model.users_group import UsersGroupModel
46
47
47 log = logging.getLogger(__name__)
48 log = logging.getLogger(__name__)
48
49
@@ -63,16 +64,38 class UsersGroupsController(BaseControll
63 def index(self, format='html'):
64 def index(self, format='html'):
64 """GET /users_groups: All items in the collection"""
65 """GET /users_groups: All items in the collection"""
65 # url('users_groups')
66 # url('users_groups')
66 c.users_groups_list = []
67 c.users_groups_list = self.sa.query(UsersGroup).all()
67 return render('admin/users_groups/users_groups.html')
68 return render('admin/users_groups/users_groups.html')
68
69
69 def create(self):
70 def create(self):
70 """POST /users_groups: Create a new item"""
71 """POST /users_groups: Create a new item"""
71 # url('users_groups')
72 # url('users_groups')
73 users_group_model = UsersGroupModel()
74 users_group_form = UsersGroupForm()()
75 try:
76 form_result = users_group_form.to_python(dict(request.POST))
77 users_group_model.create(form_result)
78 h.flash(_('created users group %s') % form_result['users_group_name'],
79 category='success')
80 #action_logger(self.rhodecode_user, 'new_user', '', '', self.sa)
81 except formencode.Invalid, errors:
82 return htmlfill.render(
83 render('admin/users_groups/users_group_add.html'),
84 defaults=errors.value,
85 errors=errors.error_dict or {},
86 prefix_error=False,
87 encoding="UTF-8")
88 except Exception:
89 log.error(traceback.format_exc())
90 h.flash(_('error occurred during creation of users group %s') \
91 % request.POST.get('users_group_name'), category='error')
92
93 return redirect(url('users_groups'))
72
94
73 def new(self, format='html'):
95 def new(self, format='html'):
74 """GET /users_groups/new: Form to create a new item"""
96 """GET /users_groups/new: Form to create a new item"""
75 # url('new_users_group')
97 # url('new_users_group')
98 return render('admin/users_groups/users_group_add.html')
76
99
77 def update(self, id):
100 def update(self, id):
78 """PUT /users_groups/id: Update an existing item"""
101 """PUT /users_groups/id: Update an existing item"""
@@ -158,19 +158,18 class UsersGroup(Base, BaseModel):
158 __tablename__ = 'users_groups'
158 __tablename__ = 'users_groups'
159 __table_args__ = {'useexisting':True}
159 __table_args__ = {'useexisting':True}
160
160
161 user_group_id = Column("users_groups_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
161 users_group_id = Column("users_group_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
162 user_group_name = Column("user_group_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None)
162 users_group_name = Column("users_group_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None)
163
163 users_group_active = Column("users_group_active", Boolean(), nullable=True, unique=None, default=None)
164
164
165 members = relation('UsersGroupMember')
165 members = relation('UsersGroupMember')
166
166
167
168 class UsersGroupMember(Base, BaseModel):
167 class UsersGroupMember(Base, BaseModel):
169 __tablename__ = 'users_groups_members'
168 __tablename__ = 'users_groups_members'
170 __table_args__ = {'useexisting':True}
169 __table_args__ = {'useexisting':True}
171
170
172 user_groups_members_id = Column("user_groups_members_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
171 users_group_member_id = Column("users_group_member_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
173 user_group_id = Column("user_group_id", Integer(), ForeignKey('users_groups.users_groups_id'), nullable=False, unique=None, default=None)
172 users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
174 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
173 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
175
174
176 user = relation('User')
175 user = relation('User')
@@ -257,7 +256,7 class UsersGroupToPerm(Base, BaseModel):
257 __tablename__ = 'users_group_to_perm'
256 __tablename__ = 'users_group_to_perm'
258 __table_args__ = (UniqueConstraint('users_group_id', 'permission_id'), {'useexisting':True})
257 __table_args__ = (UniqueConstraint('users_group_id', 'permission_id'), {'useexisting':True})
259 users_group_to_perm_id = Column("users_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
258 users_group_to_perm_id = Column("users_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
260 users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_groups_id'), nullable=False, unique=None, default=None)
259 users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
261 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
260 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
262
261
263 users_group = relation('UsersGroup')
262 users_group = relation('UsersGroup')
@@ -36,6 +36,7 from rhodecode.lib.exceptions import Lda
36 from rhodecode.model import meta
36 from rhodecode.model import meta
37 from rhodecode.model.user import UserModel
37 from rhodecode.model.user import UserModel
38 from rhodecode.model.repo import RepoModel
38 from rhodecode.model.repo import RepoModel
39 from rhodecode.model.users_group import UsersGroupModel
39 from rhodecode.model.db import User
40 from rhodecode.model.db import User
40 from rhodecode import BACKENDS
41 from rhodecode import BACKENDS
41
42
@@ -88,6 +89,38 def ValidUsername(edit, old_data):
88
89
89 return _ValidUsername
90 return _ValidUsername
90
91
92
93
94 def ValidUsersGroup(edit, old_data):
95
96 class _ValidUsersGroup(formencode.validators.FancyValidator):
97
98 def validate_python(self, value, state):
99 if value in ['default']:
100 raise formencode.Invalid(_('Invalid group name'), value, state)
101 #check if group is unique
102 old_un = None
103 if edit:
104 old_un = UserModel().get(old_data.get('users_group_id')).username
105
106 if old_un != value or not edit:
107 if UsersGroupModel().get_by_groupname(value, cache=False,
108 case_insensitive=True):
109 raise formencode.Invalid(_('This users group already exists') ,
110 value, state)
111
112
113 if re.match(r'^[a-zA-Z0-9]{1}[a-zA-Z0-9\-\_\.]+$', value) is None:
114 raise formencode.Invalid(_('Group name may only contain '
115 'alphanumeric characters underscores, '
116 'periods or dashes and must begin with '
117 'alphanumeric character'),
118 value, state)
119
120 return _ValidUsersGroup
121
122
123
91 class ValidPassword(formencode.validators.FancyValidator):
124 class ValidPassword(formencode.validators.FancyValidator):
92
125
93 def to_python(self, value, state):
126 def to_python(self, value, state):
@@ -368,6 +401,19 def UserForm(edit=False, old_data={}):
368
401
369 return _UserForm
402 return _UserForm
370
403
404
405 def UsersGroupForm(edit=False, old_data={}):
406 class _UsersGroupForm(formencode.Schema):
407 allow_extra_fields = True
408 filter_extra_fields = True
409
410 users_group_name = All(UnicodeString(strip=True, min=1, not_empty=True),
411 ValidUsersGroup(edit, old_data))
412
413 users_group_active = StringBoolean(if_missing=False)
414
415 return _UsersGroupForm
416
371 def RegisterForm(edit=False, old_data={}):
417 def RegisterForm(edit=False, old_data={}):
372 class _RegisterForm(formencode.Schema):
418 class _RegisterForm(formencode.Schema):
373 allow_extra_fields = True
419 allow_extra_fields = True
@@ -24,3 +24,56
24 # along with this program; if not, write to the Free Software
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26 # MA 02110-1301, USA.
26 # MA 02110-1301, USA.
27
28 import logging
29 import traceback
30
31 from pylons.i18n.translation import _
32
33 from rhodecode.model import BaseModel
34 from rhodecode.model.caching_query import FromCache
35 from rhodecode.model.db import UsersGroup
36
37 from rhodecode.lib.exceptions import DefaultUserException, UserOwnsReposException
38
39 from sqlalchemy.exc import DatabaseError
40
41 log = logging.getLogger(__name__)
42
43
44 class UsersGroupModel(BaseModel):
45
46 def get(self, users_group_id, cache=False):
47 users_group = self.sa.query(UsersGroup)
48 if cache:
49 users_group = users_group.options(FromCache("sql_cache_short",
50 "get_users_group_%s" % users_group_id))
51 return users_group.get(users_group_id)
52
53
54 def get_by_groupname(self, users_group_name, cache=False, case_insensitive=False):
55
56 if case_insensitive:
57 user = self.sa.query(UsersGroup)\
58 .filter(UsersGroup.users_group_name.ilike(users_group_name))
59 else:
60 user = self.sa.query(UsersGroup)\
61 .filter(UsersGroup.users_group_name == users_group_name)
62 if cache:
63 user = user.options(FromCache("sql_cache_short",
64 "get_user_%s" % users_group_name))
65 return user.scalar()
66
67 def create(self, form_data):
68 try:
69 new_users_group = UsersGroup()
70 for k, v in form_data.items():
71 setattr(new_users_group, k, v)
72
73 self.sa.add(new_users_group)
74 self.sa.commit()
75 except:
76 log.error(traceback.format_exc())
77 self.sa.rollback()
78 raise
79
@@ -0,0 +1,55
1 ## -*- coding: utf-8 -*-
2 <%inherit file="/base/base.html"/>
3
4 <%def name="title()">
5 ${_('Add users group')} - ${c.rhodecode_name}
6 </%def>
7 <%def name="breadcrumbs_links()">
8 ${h.link_to(_('Admin'),h.url('admin_home'))}
9 &raquo;
10 ${h.link_to(_('Users groups'),h.url('users_groups'))}
11 &raquo;
12 ${_('add new users group')}
13 </%def>
14
15 <%def name="page_nav()">
16 ${self.menu('admin')}
17 </%def>
18
19 <%def name="main()">
20 <div class="box">
21 <!-- box / title -->
22 <div class="title">
23 ${self.breadcrumbs()}
24 </div>
25 <!-- end box / title -->
26 ${h.form(url('users_groups'))}
27 <div class="form">
28 <!-- fields -->
29 <div class="fields">
30 <div class="field">
31 <div class="label">
32 <label for="users_group_name">${_('Group name')}:</label>
33 </div>
34 <div class="input">
35 ${h.text('users_group_name',class_='small')}
36 </div>
37 </div>
38
39 <div class="field">
40 <div class="label label-checkbox">
41 <label for="users_group_active">${_('Active')}:</label>
42 </div>
43 <div class="checkboxes">
44 ${h.checkbox('users_group_active',value=True)}
45 </div>
46 </div>
47
48 <div class="buttons">
49 ${h.submit('save','save',class_="ui-button")}
50 </div>
51 </div>
52 </div>
53 ${h.end_form()}
54 </div>
55 </%def> No newline at end of file
@@ -29,7 +29,6
29 <div class="table">
29 <div class="table">
30 <table class="table_disp">
30 <table class="table_disp">
31 <tr class="header">
31 <tr class="header">
32 <th></th>
33 <th class="left">${_('group name')}</th>
32 <th class="left">${_('group name')}</th>
34 <th class="left">${_('members')}</th>
33 <th class="left">${_('members')}</th>
35 <th class="left">${_('active')}</th>
34 <th class="left">${_('active')}</th>
@@ -37,13 +36,13
37 </tr>
36 </tr>
38 %for cnt,u_group in enumerate(c.users_groups_list):
37 %for cnt,u_group in enumerate(c.users_groups_list):
39 <tr class="parity${cnt%2}">
38 <tr class="parity${cnt%2}">
40 <td>${h.link_to(u_group.groupname,h.url('edit_user_group', id=u_group.group_id))}</td>
39 <td>${h.link_to(u_group.users_group_name,h.url('edit_users_group', id=u_group.users_group_id))}</td>
41 <td>${u_group.members}</td>
40 <td>${len(u_group.members)}</td>
42 <td>${h.bool2icon(u_group.active)}</td>
41 <td>${h.bool2icon(u_group.users_group_active)}</td>
43 <td>
42 <td>
44 ${h.form(url('users_group', id=group.group_id),method='delete')}
43 ${h.form(url('users_group', id=u_group.users_group_id),method='delete')}
45 ${h.submit('remove_','delete',id="remove_group_%s" % group.group_id,
44 ${h.submit('remove_','delete',id="remove_group_%s" % u_group.users_group_id,
46 class_="delete_icon action_button",onclick="return confirm('Confirm to delete this user group');")}
45 class_="delete_icon action_button",onclick="return confirm('Confirm to delete this users group');")}
47 ${h.end_form()}
46 ${h.end_form()}
48 </td>
47 </td>
49 </tr>
48 </tr>
General Comments 0
You need to be logged in to leave comments. Login now