OLD | NEW |
(Empty) | |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is govered by a BSD-style |
| 3 # license that can be found in the LICENSE file or at |
| 4 # https://developers.google.com/open-source/licenses/bsd |
| 5 |
| 6 """A page for site admins to create a new user group.""" |
| 7 |
| 8 import logging |
| 9 import re |
| 10 |
| 11 from framework import framework_helpers |
| 12 from framework import permissions |
| 13 from framework import servlet |
| 14 from proto import usergroup_pb2 |
| 15 from services import user_svc |
| 16 from sitewide import group_helpers |
| 17 |
| 18 |
| 19 class GroupCreate(servlet.Servlet): |
| 20 """Shows a page with a simple form to create a user group.""" |
| 21 |
| 22 _PAGE_TEMPLATE = 'sitewide/group-create-page.ezt' |
| 23 |
| 24 def AssertBasePermission(self, mr): |
| 25 """Assert that the user has the permissions needed to view this page.""" |
| 26 super(GroupCreate, self).AssertBasePermission(mr) |
| 27 |
| 28 if not permissions.CanCreateGroup(mr.perms): |
| 29 raise permissions.PermissionException( |
| 30 'User is not allowed to create a user group') |
| 31 |
| 32 def GatherPageData(self, _mr): |
| 33 """Build up a dictionary of data values to use when rendering the page.""" |
| 34 visibility_levels = group_helpers.BuildUserGroupVisibilityOptions() |
| 35 initial_visibility = group_helpers.GroupVisibilityView( |
| 36 usergroup_pb2.MemberVisibility.ANYONE) |
| 37 group_types = group_helpers.BuildUserGroupTypeOptions() |
| 38 |
| 39 return { |
| 40 'groupadmin': '', |
| 41 'group_types': group_types, |
| 42 'import_group': '', |
| 43 'initial_friendprojects': '', |
| 44 'initial_group_type': '', |
| 45 'initial_name': '', |
| 46 'initial_visibility': initial_visibility, |
| 47 'visibility_levels': visibility_levels, |
| 48 } |
| 49 |
| 50 def ProcessFormData(self, mr, post_data): |
| 51 """Process the posted form.""" |
| 52 # 1. Gather data from the request. |
| 53 group_name = post_data.get('groupname') |
| 54 try: |
| 55 existing_group_id = self.services.user.LookupUserID(mr.cnxn, group_name) |
| 56 existing_settings = self.services.usergroup.GetGroupSettings( |
| 57 mr.cnxn, existing_group_id) |
| 58 if existing_settings: |
| 59 mr.errors.groupname = 'That user group already exists' |
| 60 except user_svc.NoSuchUserException: |
| 61 pass |
| 62 |
| 63 if post_data.get('import_group'): |
| 64 vis = usergroup_pb2.MemberVisibility.OWNERS |
| 65 ext_group_type = post_data.get('group_type') |
| 66 friend_projects = '' |
| 67 if not ext_group_type: |
| 68 mr.errors.groupimport = 'Please provide external group type' |
| 69 else: |
| 70 ext_group_type = str( |
| 71 usergroup_pb2.GroupType(int(ext_group_type))).lower() |
| 72 else: |
| 73 vis = usergroup_pb2.MemberVisibility(int(post_data['visibility'])) |
| 74 ext_group_type = None |
| 75 friend_projects = post_data.get('friendprojects', '') |
| 76 who_can_view_members = str(vis).lower() |
| 77 |
| 78 if not mr.errors.AnyErrors(): |
| 79 project_ids, error = self.services.usergroup.ValidateFriendProjects( |
| 80 mr.cnxn, self.services, friend_projects) |
| 81 if error: |
| 82 mr.errors.friendprojects = error |
| 83 |
| 84 # 2. Call services layer to save changes. |
| 85 if not mr.errors.AnyErrors(): |
| 86 group_id = self.services.usergroup.CreateGroup( |
| 87 mr.cnxn, self.services, group_name, who_can_view_members, |
| 88 ext_group_type, project_ids) |
| 89 |
| 90 # 3. Determine the next page in the UI flow. |
| 91 if mr.errors.AnyErrors(): |
| 92 self.PleaseCorrect(mr, initial_name=group_name) |
| 93 else: |
| 94 # Go to the new user group's detail page. |
| 95 return framework_helpers.FormatAbsoluteURL( |
| 96 mr, '/g/%s/' % group_id, include_project=False) |
OLD | NEW |