Commits

Marcin Kuzminski  committed 88d13c1

removed users_group controller in replace for model methods,
fixed tests

  • Participants
  • Parent commits a62af7d
  • Branches beta

Comments (0)

Files changed (6)

File rhodecode/controllers/admin/users_groups.py

 
 from rhodecode.model.db import User, UsersGroup, Permission, UsersGroupToPerm
 from rhodecode.model.forms import UserForm, UsersGroupForm
-from rhodecode.model.users_group import UsersGroupModel
 
 log = logging.getLogger(__name__)
 
     def create(self):
         """POST /users_groups: Create a new item"""
         # url('users_groups')
-        users_group_model = UsersGroupModel()
+
         users_group_form = UsersGroupForm()()
         try:
             form_result = users_group_form.to_python(dict(request.POST))
-            users_group_model.create(form_result)
+            UsersGroup.create(form_result)
             h.flash(_('created users group %s') \
                     % form_result['users_group_name'], category='success')
             #action_logger(self.rhodecode_user, 'new_user', '', '', self.sa)
         #           method='put')
         # url('users_group', id=ID)
 
-        users_group_model = UsersGroupModel()
-        c.users_group = users_group_model.get(id)
+        c.users_group = UsersGroup.get(id)
         c.group_members = [(x.user_id, x.user.username) for x in
                            c.users_group.members]
 
 
         try:
             form_result = users_group_form.to_python(request.POST)
-            users_group_model.update(id, form_result)
+            UsersGroup.update(id, form_result)
             h.flash(_('updated users group %s') \
                         % form_result['users_group_name'],
                     category='success')
         #    h.form(url('users_group', id=ID),
         #           method='delete')
         # url('users_group', id=ID)
-        users_group_model = UsersGroupModel()
+
         try:
-            users_group_model.delete(id)
+            UsersGroup.delete(id)
             h.flash(_('successfully deleted users group'), category='success')
         except UsersGroupsAssignedException, e:
             h.flash(e, category='error')

File rhodecode/model/db.py

 from vcs.utils.lazy import LazyProperty
 from vcs.nodes import FileNode
 
+from rhodecode.lib.exceptions import UsersGroupsAssignedException
 from rhodecode.lib import str2bool, json, safe_str
 from rhodecode.model.meta import Base, Session
 from rhodecode.model.caching_query import FromCache
                                           "get_user_%s" % group_name))
         return gr.scalar()
 
+
+    @classmethod
+    def get(cls, users_group_id, cache=False):
+        users_group = Session.query(cls)
+        if cache:
+            users_group = users_group.options(FromCache("sql_cache_short",
+                                    "get_users_group_%s" % users_group_id))
+        return users_group.get(users_group_id)
+
+    @classmethod
+    def create(cls, form_data):
+        try:
+            new_users_group = cls()
+            for k, v in form_data.items():
+                setattr(new_users_group, k, v)
+
+            Session.add(new_users_group)
+            Session.commit()
+        except:
+            log.error(traceback.format_exc())
+            Session.rollback()
+            raise
+
+    @classmethod
+    def update(cls, users_group_id, form_data):
+
+        try:
+            users_group = cls.get(users_group_id, cache=False)
+
+            for k, v in form_data.items():
+                if k == 'users_group_members':
+                    users_group.members = []
+                    Session.flush()
+                    members_list = []
+                    if v:
+                        for u_id in set(v):
+                            members_list.append(UsersGroupMember(
+                                                            users_group_id,
+                                                            u_id))
+                    setattr(users_group, 'members', members_list)
+                setattr(users_group, k, v)
+
+            Session.add(users_group)
+            Session.commit()
+        except:
+            log.error(traceback.format_exc())
+            Session.rollback()
+            raise
+
+    @classmethod
+    def delete(cls, users_group_id):
+        try:
+
+            # check if this group is not assigned to repo
+            assigned_groups = UsersGroupRepoToPerm.query()\
+                .filter(UsersGroupRepoToPerm.users_group_id ==
+                        users_group_id).all()
+
+            if assigned_groups:
+                raise UsersGroupsAssignedException('Group assigned to %s' %
+                                                   assigned_groups)
+
+            users_group = cls.get(users_group_id, cache=False)
+            Session.delete(users_group)
+            Session.commit()
+        except:
+            log.error(traceback.format_exc())
+            Session.rollback()
+            raise
+
+
 class UsersGroupMember(Base, BaseModel):
     __tablename__ = 'users_groups_members'
     __table_args__ = {'extend_existing':True}

File rhodecode/model/users_group.py

-# -*- coding: utf-8 -*-
-"""
-    rhodecode.model.user_group
-    ~~~~~~~~~~~~~~~~~~~~~~~~~~
-
-    users groups model for RhodeCode
-
-    :created_on: Jan 25, 2011
-    :author: marcink
-    :copyright: (C) 2009-2011 Marcin Kuzminski <marcin@python-works.com>
-    :license: GPLv3, see COPYING for more details.
-"""
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program.  If not, see <http://www.gnu.org/licenses/>.
-
-import logging
-import traceback
-
-from pylons.i18n.translation import _
-
-from rhodecode.lib.exceptions import UsersGroupsAssignedException
-from rhodecode.model import BaseModel
-from rhodecode.model.caching_query import FromCache
-from rhodecode.model.db import UsersGroup, UsersGroupMember, \
-    UsersGroupRepoToPerm
-
-log = logging.getLogger(__name__)
-
-
-class UsersGroupModel(BaseModel):
-
-    def get(self, users_group_id, cache=False):
-        users_group = self.sa.query(UsersGroup)
-        if cache:
-            users_group = users_group.options(FromCache("sql_cache_short",
-                                    "get_users_group_%s" % users_group_id))
-        return users_group.get(users_group_id)
-
-    def create(self, form_data):
-        try:
-            new_users_group = UsersGroup()
-            for k, v in form_data.items():
-                setattr(new_users_group, k, v)
-
-            self.sa.add(new_users_group)
-            self.sa.commit()
-        except:
-            log.error(traceback.format_exc())
-            self.sa.rollback()
-            raise
-
-    def update(self, users_group_id, form_data):
-
-        try:
-            users_group = self.get(users_group_id, cache=False)
-
-            for k, v in form_data.items():
-                if k == 'users_group_members':
-                    users_group.members = []
-                    self.sa.flush()
-                    members_list = []
-                    if v:
-                        for u_id in set(v):
-                            members_list.append(UsersGroupMember(
-                                                            users_group_id,
-                                                            u_id))
-                    setattr(users_group, 'members', members_list)
-                setattr(users_group, k, v)
-
-            self.sa.add(users_group)
-            self.sa.commit()
-        except:
-            log.error(traceback.format_exc())
-            self.sa.rollback()
-            raise
-
-    def delete(self, users_group_id):
-        try:
-
-            # check if this group is not assigned to repo
-            assigned_groups = UsersGroupRepoToPerm.query()\
-                .filter(UsersGroupRepoToPerm.users_group_id ==
-                        users_group_id).all()
-
-            if assigned_groups:
-                raise UsersGroupsAssignedException('Group assigned to %s' %
-                                                   assigned_groups)
-
-            users_group = self.get(users_group_id, cache=False)
-            self.sa.delete(users_group)
-            self.sa.commit()
-        except:
-            log.error(traceback.format_exc())
-            self.sa.rollback()
-            raise

File rhodecode/templates/changelog/changelog.html

 						</div>	
 						<div class="right">
 									<div id="${cs.raw_id}_changes_info" class="changes">
-                                        <span id="${cs.raw_id}" class="changed_total tooltip" 
-                                        title="${_('Affected number of files, click to show more details')}">
-                                        ${len(cs.affected_files)}
-                                        </span>
+                                        <span id="${cs.raw_id}" class="changed_total tooltip" title="${_('Affected number of files, click to show more details')}">${len(cs.affected_files)}</span>
 									</div>					
 										%if len(cs.parents)>1:
 										<div class="merge">

File rhodecode/tests/functional/test_admin_users_groups.py

 from rhodecode.tests import *
+from rhodecode.model.db import UsersGroup
 
 TEST_USERS_GROUP = 'admins_test'
 
         response = self.app.put(url('users_group', id=1))
 
     def test_update_browser_fakeout(self):
-        response = self.app.post(url('users_group', id=1), params=dict(_method='put'))
+        response = self.app.post(url('users_group', id=1),
+                                 params=dict(_method='put'))
 
     def test_delete(self):
-        response = self.app.delete(url('users_group', id=1))
+        self.log_user()
+        users_group_name = TEST_USERS_GROUP + 'another'
+        response = self.app.post(url('users_groups'),
+                                 {'users_group_name':users_group_name,
+                                  'active':True})
+        response.follow()
+
+        self.checkSessionFlash(response,
+                               'created users group %s' % users_group_name)
+
+
+        gr = self.sa.query(UsersGroup)\
+                           .filter(UsersGroup.users_group_name ==
+                                   users_group_name).one()
+
+        response = self.app.delete(url('users_group', id=gr.users_group_id))
+
+        gr = self.sa.query(UsersGroup)\
+                           .filter(UsersGroup.users_group_name ==
+                                   users_group_name).scalar()
+
+        self.assertEqual(gr, None)
+
 
     def test_delete_browser_fakeout(self):
-        response = self.app.post(url('users_group', id=1), params=dict(_method='delete'))
+        response = self.app.post(url('users_group', id=1),
+                                 params=dict(_method='delete'))
 
     def test_show(self):
         response = self.app.get(url('users_group', id=1))

File rhodecode/tests/functional/test_changelog.py

 
     def test_index_hg(self):
         self.log_user()
-        response = self.app.get(url(controller='changelog', action='index', repo_name=HG_REPO))
+        response = self.app.get(url(controller='changelog', action='index',
+                                    repo_name=HG_REPO))
 
-        assert """<div id="chg_20" class="container">""" in response.body, 'wrong info about number of changes'
-        assert """<input class="changeset_range" id="5e204e7583b9" name="5e204e7583b9" type="checkbox" value="1" />""" in response.body, 'no checkbox for this commit'
-        assert """<span>commit 154: 5e204e7583b9@2010-08-10 01:18:46</span>""" in response.body , 'no info on this commit'
-        assert """Small update at simplevcs app""" in response.body, 'missing info about commit message'
+        self.assertTrue("""<div id="chg_20" class="container">"""
+                        in response.body)
+        self.assertTrue("""<input class="changeset_range" id="5e204e7583b9" """
+                        """name="5e204e7583b9" type="checkbox" value="1" />"""
+                        in response.body)
+        self.assertTrue("""<span>commit 154: 5e204e7583b9@2010-08-10 """
+                        """01:18:46</span>""" in response.body)
+        self.assertTrue("""Small update at simplevcs app""" in response.body)
 
-        assert """<span class="removed tooltip" title="<b>removed</b>: No Files">0</span>""" in response.body, 'wrong info about removed nodes'
-        assert """<span class="changed tooltip" title="<b>changed</b>: <br/> vcs/backends/hg.py<br/> vcs/web/simplevcs/models.py">2</span>""" in response.body, 'wrong info about changed nodes'
-        assert """<span class="added tooltip" title="<b>added</b>: <br/> vcs/web/simplevcs/managers.py">1</span>""" in response.body, 'wrong info about added nodes'
+
+        self.assertTrue("""<span id="5e204e7583b9c8e7b93a020bd036564b1e"""
+                        """731dae" class="changed_total tooltip" """
+                        """title="Affected number of files, click to """
+                        """show more details">3</span>""" in response.body)
 
         #pagination
 
-        response = self.app.get(url(controller='changelog', action='index', repo_name=HG_REPO), {'page':1})
-        response = self.app.get(url(controller='changelog', action='index', repo_name=HG_REPO), {'page':2})
-        response = self.app.get(url(controller='changelog', action='index', repo_name=HG_REPO), {'page':3})
-        response = self.app.get(url(controller='changelog', action='index', repo_name=HG_REPO), {'page':4})
-        response = self.app.get(url(controller='changelog', action='index', repo_name=HG_REPO), {'page':5})
-        response = self.app.get(url(controller='changelog', action='index', repo_name=HG_REPO), {'page':6})
+        response = self.app.get(url(controller='changelog', action='index',
+                                    repo_name=HG_REPO), {'page':1})
+        response = self.app.get(url(controller='changelog', action='index',
+                                    repo_name=HG_REPO), {'page':2})
+        response = self.app.get(url(controller='changelog', action='index',
+                                    repo_name=HG_REPO), {'page':3})
+        response = self.app.get(url(controller='changelog', action='index',
+                                    repo_name=HG_REPO), {'page':4})
+        response = self.app.get(url(controller='changelog', action='index',
+                                    repo_name=HG_REPO), {'page':5})
+        response = self.app.get(url(controller='changelog', action='index',
+                                    repo_name=HG_REPO), {'page':6})
 
 
         # Test response after pagination...
-        print response.body
-        assert """<input class="changeset_range" id="46ad32a4f974" name="46ad32a4f974" type="checkbox" value="1" />""" in response.body, 'no checkbox for this commit'
-        assert """<span>commit 64: 46ad32a4f974@2010-04-20 00:33:21</span>"""in response.body, 'wrong info about commit 64'
-        assert """<span class="removed tooltip" title="<b>removed</b>: <br/> docs/api.rst">1</span>"""in response.body, 'wrong info about number of removed'
-        assert """<span class="changed tooltip" title="<b>changed</b>: <br/> .hgignore<br/> README.rst<br/> docs/conf.py<br/> docs/index.rst<br/> setup.py<br/> tests/test_hg.py<br/> tests/test_nodes.py<br/> vcs/__init__.py<br/> vcs/backends/__init__.py<br/> vcs/backends/base.py<br/> vcs/backends/hg.py<br/> vcs/nodes.py<br/> vcs/utils/__init__.py">13</span>"""in response.body, 'wrong info about number of changes'
-        assert """<span class="added tooltip" title="<b>added</b>: <br/> docs/api/backends/hg.rst<br/> docs/api/backends/index.rst<br/> docs/api/index.rst<br/> docs/api/nodes.rst<br/> docs/api/web/index.rst<br/> docs/api/web/simplevcs.rst<br/> docs/installation.rst<br/> docs/quickstart.rst<br/> setup.cfg<br/> vcs/utils/baseui_config.py<br/> vcs/utils/web.py<br/> vcs/web/__init__.py<br/> vcs/web/exceptions.py<br/> vcs/web/simplevcs/__init__.py<br/> vcs/web/simplevcs/exceptions.py<br/> vcs/web/simplevcs/middleware.py<br/> vcs/web/simplevcs/models.py<br/> vcs/web/simplevcs/settings.py<br/> vcs/web/simplevcs/utils.py<br/> vcs/web/simplevcs/views.py">20</span>"""in response.body, 'wrong info about number of added'
-        assert """<div class="message"><a href="/%s/changeset/46ad32a4f974e45472a898c6b0acb600320579b1">Merge with 2e6a2bf9356ca56df08807f4ad86d480da72a8f4</a></div>""" % HG_REPO in response.body, 'wrong info about commit 64 is a merge'
+        self.assertTrue("""<input class="changeset_range" id="46ad32a4f974" """
+                        """name="46ad32a4f974" type="checkbox" value="1" />"""
+                        in response.body)
+        self.assertTrue("""<span>commit 64: 46ad32a4f974@2010-04-20"""
+                        """ 00:33:21</span>"""in response.body)
+
+        self.assertTrue("""<span id="46ad32a4f974e45472a898c6b0acb600320"""
+                        """579b1" class="changed_total tooltip" """
+                        """title="Affected number of files, click to """
+                        """show more details">21</span>"""in response.body)
+        self.assertTrue("""<div class="message"><a href="/%s/changeset/"""
+                        """46ad32a4f974e45472a898c6b0acb600320579b1">"""
+                        """Merge with 2e6a2bf9356ca56df08807f4ad86d48"""
+                        """0da72a8f4</a></div>""" % HG_REPO in response.body)