def mutate(self, info, **kwargs): if not info.context.user: raise GraphQLError(json.dumps({"error": "Not authorized"})) form = GroupForm(kwargs) if form.is_valid(): form.save() UserGroupLink.objects.create(group=form.instance, user=info.context.user, permission=3) return CreateGroupMutation(group=form.instance, user=info.context.user) raise GraphQLError(json.dumps(form.errors))
def mutate(self, info, **kwargs): if not info.context.user: raise GraphQLError(json.dumps({"error": "Not authorized"})) group = Group.objects.filter(id=kwargs["id"]) if not group: raise GraphQLError('{"group": ["Does not exist"]}') if not groups_run_by_user(info.context.user).filter(id=kwargs["id"]): raise GraphQLError('{"group": ["Not an admin"]}') form = GroupForm(kwargs, instance=group.first()) if form.is_valid(): form.save() return UpdateGroupMutation(group=form.instance, user=info.context.user) raise GraphQLError(json.dumps(form.errors))
def create_group(request): """ 新建权限组 :param request: :return: """ result = {"status": 0, "apiVersion": "1.0", "msg": "", "data": {}} form = GroupForm(request.POST) if form.is_valid(): form.save() else: result['msg'] = u'表单填写错误' return json_response(result) result['status'] = 1 result['msg'] = u'新建成功' return json_response(result)
def manage_group(request, group_id=None): if request.user.is_superuser: if group_id: actual_group = get_object_or_404(Group, pk=group_id) else: actual_group = Group() # POST mode if request.method == 'POST': form = GroupForm(request.POST, instance=actual_group) if form.is_valid(): form.save() return redirect('group-list') else: # Non-POST mode, show only form = GroupForm(instance=actual_group) return render(request, 'core/groups/create_edit_group.html', locals()) else: raise Http404
def test_groups_form(self): # set user as admin01 self.request.user = self.test_user1 # empty form form = GroupForm(request=self.request) self.assertFalse(form.is_valid()) # upload header_logo_image uf = create_dff_image(field_name='header_logo_img') # Test Create # ------------------------------ name = 'Map group test' form_data = { 'title': name, 'name': name, 'description': 'Test', 'srid': self.epsg.pk, 'form_id': uf.form_id, 'lang': 'it', 'mapcontrols': [mp.pk for mp in self.map_controls] } # Test ACL form_data.update({ 'editor_user': self.test_editor1.pk, 'editor2_user': self.test_editor2.pk, 'viewer_users': [self.test_viewer1.pk, self.test_viewer1_2.pk], 'editor_user_groups': [self.test_gu_editor1.pk], 'viewer_user_groups': [self.test_gu_viewer1.pk, self.test_gu_viewer2.pk] }) form = GroupForm(request=self.request, data=form_data) self.assertTrue(form.is_valid()) # Check possible choices values # editor1 editor1.2 editor1.3 self.assertEqual(len(form.fields['editor_user'].queryset), 3) # editor2 editor2.2 editor2.3 self.assertEqual(len(form.fields['editor2_user'].queryset), 3) # viewer1 viewer1.2 viewer1.3 anonymous self.assertEqual(len(form.fields['viewer_users'].queryset), 4) # GU_EDITOR1 GU_EDITOR2 GU_EDITOR1_E1_2 self.assertEqual(len(form.fields['editor_user_groups'].queryset), 3) # GU_VIEWER1 GU_VIEWER2 GU_VIEWER_E1_2 self.assertEqual(len(form.fields['viewer_user_groups'].queryset), 3) form.save() # check is it saved into db mg = Group.objects.get(name='Map group test') self.assertEqual(mg.name, name) self.assertEqual(mg.srid.srid, self.epsg.srid) # check ACL self.assertTrue(self.test_editor1.has_perm('core.change_group', mg)) self.assertFalse(self.test_editor2.has_perm('core.change_group', mg)) self.assertTrue( self.test_editor1.has_perm('core.add_project_to_group', mg)) self.assertTrue( self.test_editor2.has_perm('core.add_project_to_group', mg)) self.assertTrue(self.test_viewer1.has_perm('core.view_group', mg)) self.assertTrue(self.test_viewer1_2.has_perm('core.view_group', mg)) self.assertTrue( self.test_editor2_3.has_perm('core.add_project_to_group', mg)) self.assertTrue(self.test_viewer1_3.has_perm('core.view_group', mg)) # Test Update # ------------------------------ initial_form_data = copy.copy(form_data) name = 'Map group test update' form_data.update({ 'name': name, }) # Test ACL form_data.update({ 'editor_user': self.test_editor1_2.pk, 'editor2_user': self.test_editor2_2.pk, 'viewer_users': [self.test_viewer1_3.pk], 'editor_user_groups': [], 'viewer_user_groups': [] }) form = GroupForm(request=self.request, data=form_data, instance=mg, initial=initial_form_data) self.assertTrue(form.is_valid()) form.save() # Reload form db mg.refresh_from_db() self.assertEqual(mg.name, name) # check ACL self.assertFalse(self.test_editor1.has_perm('core.change_group', mg)) self.assertTrue(self.test_editor1_2.has_perm('core.change_group', mg)) self.assertFalse(self.test_editor2.has_perm('core.change_group', mg)) self.assertFalse(self.test_editor2_2.has_perm('core.change_group', mg)) self.assertTrue( self.test_editor1_2.has_perm('core.add_project_to_group', mg)) self.assertTrue( self.test_editor2_2.has_perm('core.add_project_to_group', mg)) self.assertTrue(self.test_viewer1_3.has_perm('core.view_group', mg)) self.assertFalse(self.test_viewer1_2.has_perm('core.view_group', mg)) self.assertFalse( self.test_editor2_3.has_perm('core.add_project_to_group', mg)) self.assertTrue(self.test_viewer1_3.has_perm('core.view_group', mg)) # Testing for editor level 1 use # ==================================================== # editor1 # ----------------- self.request.user = self.test_editor1 form = GroupForm(request=self.request) self.assertFalse(form.is_valid()) self.assertCountEqual(form.fields['editor2_user'].queryset, []) # editor1.2 # ----------------- self.request.user = self.test_editor1_2 form = GroupForm(request=self.request) self.assertFalse(form.is_valid()) # editor2.2 and editor2.3 self.assertEqual(len(form.fields['editor2_user'].queryset), 2) self.assertEqual( list(form.fields['editor2_user'].queryset.order_by('pk')), [self.test_editor2_2, self.test_editor2_3]) # Anonymous user always present # Anonymoususer, viewer2.3 self.assertEqual(len(form.fields['viewer_users'].queryset), 2) self.assertEqual( list(form.fields['viewer_users'].queryset.order_by('pk')), [self.anonymoususer, self.test_viewer1_3]) # Only editor and viewer user groups for test_editor1_2 self.assertEqual(len(form.fields['editor_user_groups'].queryset), 1) self.assertEqual(len(form.fields['viewer_user_groups'].queryset), 1)