def test_permission_needed(app, admin, demomodels, action): permission_mapping = { 'export_as_csv': 'adminactions_export', 'export_as_fixture': 'adminactions_export', 'export_as_xls': 'adminactions_export', 'export_delete_tree': 'adminactions_export', 'mass_update': 'adminactions_massupdate', 'merge': 'adminactions_merge', 'graph_queryset': 'adminactions_chart', } perm = "demo.{}_demomodel".format(permission_mapping[action]) url = reverse('admin:demo_demomodel_changelist') pks = [demomodels[0].pk, demomodels[1].pk] with user_grant_permission(admin, ['demo.change_demomodel']): res = app.post(url, params=[('action', action), ('_selected_action', pks)], extra_environ={'wsgi.url_scheme': 'https'}, user=admin.username, expect_errors=True) assert res.status_code == 302 res = res.follow() assert 'Sorry you do not have rights to execute this action' in [ str(m) for m in res.context['messages'] ] with user_grant_permission(admin, [perm]): res = app.post(url, params=[('action', action), ('_selected_action', pks)], extra_environ={'wsgi.url_scheme': 'https'}, user=admin.username, expect_errors=True) assert res.status_code == 200
def test_permission_needed(app, admin, demomodels, action): permission_mapping = {'export_as_csv': 'adminactions_export', 'export_as_fixture': 'adminactions_export', 'export_as_xls': 'adminactions_export', 'export_delete_tree': 'adminactions_export', 'mass_update': 'adminactions_massupdate', 'merge': 'adminactions_merge', 'graph_queryset': 'adminactions_chart', } perm = "demo.{}_demomodel".format(permission_mapping[action]) url = reverse('admin:demo_demomodel_changelist') pks = [demomodels[0].pk, demomodels[1].pk] with user_grant_permission(admin, ['demo.change_demomodel']): res = app.post(url, [('action', action), ('_selected_action', pks)], extra_environ={'wsgi.url_scheme': 'https'}, user=admin.username, expect_errors=True) assert res.status_code == 302 res = res.follow() assert 'Sorry you do not have rights to execute this action' in [str(m) for m in res.context['messages']] with user_grant_permission(admin, [perm]): res = app.post(url, [('action', action), ('_selected_action', pks)], extra_environ={'wsgi.url_scheme': 'https'}, user=admin.username, expect_errors=True) assert res.status_code == 200
def _run_action(self, steps=3, page_start=None): with user_grant_permission( self.user, ['auth.change_user', 'auth.adminactions_merge_user']): if isinstance(steps, int): steps = list(range(1, steps + 1)) res = self.app.get('/', user='******') res = res.click('Users') else: res = page_start if 1 in steps: form = res.forms['changelist-form'] form['action'] = 'merge' self._select_rows(form) res = form.submit() assert not hasattr(res.form, 'errors') if 2 in steps: res.form['username'] = res.form['form-1-username'].value res.form['email'] = res.form['form-1-email'].value res.form['last_login'] = res.form['form-1-last_login'].value res.form['date_joined'] = res.form['form-1-date_joined'].value res = res.form.submit('preview') assert not hasattr(res.form, 'errors') if 3 in steps: res = res.form.submit('apply') return res
def test_issue_93(self): # default date(time) format in XLS export doesn't import well on excel with user_grant_permission(self.user, ['demo.change_demomodel', 'demo.adminactions_export_demomodel']): res = self.app.get('/', user='******') res = res.click('Demo models') form = res.forms['changelist-form'] form['action'] = self.action_name self._select_rows(form) res = form.submit() res.form['header'] = 1 res.form['columns'] = ['date', ] res = res.form.submit('apply') io = six.BytesIO(res.body) io.seek(0) w = xlrd.open_workbook(file_contents=io.read(), formatting_info=True) sheet = w.sheet_by_index(0) cell = sheet.cell(1, 1) fmt = w.xf_list[cell.xf_index] format_key = fmt.format_key format = w.format_map[format_key] # gets a Format object self.assertEqual(cell.value, 41303.0) self.assertEqual(cell.ctype, 3) self.assertEqual(format.format_str, u'd/m/Y')
def _run_action(self, steps=3, page_start=None): with user_grant_permission(self.user, ['auth.change_user', 'auth.adminactions_merge_user']): if isinstance(steps, int): steps = list(range(1, steps + 1)) res = self.app.get('/', user='******') res = res.click('Users') else: res = page_start if 1 in steps: form = res.forms['changelist-form'] form['action'] = 'merge' self._select_rows(form) res = form.submit() assert not hasattr(res.form, 'errors') if 2 in steps: res.form['username'] = res.form['form-1-username'].value res.form['email'] = res.form['form-1-email'].value res.form['last_login'] = res.form['form-1-last_login'].value res.form['date_joined'] = res.form['form-1-date_joined'].value res = res.form.submit('preview') assert not hasattr(res.form, 'errors') if 3 in steps: res = res.form.submit('apply') return res
def test_form_rows_fields_exists(self): """ Check model fields appear in action form for each selected models """ with user_grant_permission(self.user, [ 'demo.change_demomodel', 'demo.adminactions_byrowsupdate_demomodel' ]): res = self._get_changelist_form_response() form = res.forms['changelist-form'] self._select_rows(form, selected_rows=self._selected_rows) res = self._get_action_form_response(change_list_response=res) byrows_update_get_fields(ModelAdmin(DemoModel, self.site)) for r, value in enumerate(self._selected_values): for fname in byrows_update_get_fields( ModelAdmin(DemoModel, self.site)): fname = 'form-%d-%s' % (r, fname) try: # Attempt split (admin datetime widget) fields first assert res.form[fname + '_0'] except AssertionError: # assert for non-split fields to return the regular # field name upon errors assert res.form[fname]
def test_permissions(admin): assert Permission.objects.filter( codename__startswith='adminactions').count() == 45 with user_grant_permission(admin, ['demo.adminactions_export_demomodel']): assert admin.get_all_permissions() == set( [u'demo.adminactions_export_demomodel'])
def test_faster_export(self): # generate 3k users start = time.time() user_count = User.objects.count() User.objects.bulk_create([User( username='******' % i) for i in range(3000)]) # print('created 3k users in %.1f seconds' % (time.time() - start)) self.assertEqual(User.objects.count(), 3000 + user_count) start = time.time() with user_grant_permission(self.user, [ 'auth.change_user', 'auth.adminactions_export_user']): res = self.app.get('/', user='******') res = res.click('Users') form = res.forms['changelist-form'] form['action'] = self.action_name form['select_across'] = 1 self._select_rows(form) res = form.submit() res.form['header'] = 1 res = res.form.submit('apply') res_time = (time.time() - start) # print('Response returned in %.1f seconds' % res_time) io = six.BytesIO(res.body) io.seek(0) w = xlrd.open_workbook(file_contents=io.read()) sheet = w.sheet_by_index(0) self.assertEqual(sheet.nrows, 3000 + user_count + 1) self.assertLessEqual(res_time, 6.5, "Response should return under 6.5 " "seconds, was %.2f" % res_time)
def _run_action(self, steps=3, page_start=None): with user_grant_permission( self.user, ['demo.change_demomodel', 'demo.adminactions_merge_demomodel']): if isinstance(steps, int): steps = list(range(1, steps + 1)) res = self.app.get('/', user='******') res = res.click('Demo models') else: res = page_start if 1 in steps: form = res.forms['changelist-form'] form['action'] = 'merge' self._select_rows(form) res = form.submit() assert not hasattr(res.form, 'errors') if 2 in steps: res.form['image'] = res.form['form-1-image'].value res.form['field_names'] = 'image,subclassed_image' res = res.form.submit('preview') assert not hasattr(res.form, 'errors') if 3 in steps: res = res.form.submit('apply') return res
def test_issue_93(self): # default date(time) format in XLS export doesn't import well on excel with user_grant_permission( self.user, ['demo.change_demomodel', 'demo.adminactions_export_demomodel']): res = self.app.get('/', user='******') res = res.click('Demo models') form = res.forms['changelist-form'] form['action'] = self.action_name self._select_rows(form) res = form.submit() res.form['header'] = 1 res.form['columns'] = [ 'date', ] res = res.form.submit('apply') io = six.BytesIO(res.body) io.seek(0) w = xlrd.open_workbook(file_contents=io.read(), formatting_info=True) sheet = w.sheet_by_index(0) cell = sheet.cell(1, 1) fmt = w.xf_list[cell.xf_index] format_key = fmt.format_key format = w.format_map[format_key] # gets a Format object self.assertEqual(cell.value, 41303.0) self.assertEqual(cell.ctype, 3) self.assertEqual(format.format_str, u'd/m/Y')
def test_use_display_ko(self): with user_grant_permission( self.user, ['demo.change_demomodel', 'demo.adminactions_export_demomodel']): res = self.app.get('/', user='******') res = res.click('Demo models') form = res.forms['changelist-form'] form['action'] = self.action_name self._select_rows(form) res = form.submit() res.form['header'] = 1 res.form['columns'] = ['char', 'text', 'bigint', 'choices' ''] res = res.form.submit('apply') io = six.BytesIO(res.body) io.seek(0) w = xlrd.open_workbook(file_contents=io.read()) sheet = w.sheet_by_index(0) self.assertEquals(sheet.cell_value(0, 1), u'Chäř') self.assertEquals(sheet.cell_value(0, 2), u'bigint') self.assertEquals(sheet.cell_value(0, 3), u'text') self.assertEquals(sheet.cell_value(0, 4), u'choices') self.assertEquals(sheet.cell_value(1, 1), u'Pizzä ïs Gööd') self.assertEquals(sheet.cell_value(1, 2), 333333333.0) self.assertEquals(sheet.cell_value(1, 3), u'lorem ipsum') self.assertEquals(sheet.cell_value(1, 4), 2.0)
def test_success(self): with user_grant_permission( self.user, ['auth.change_user', 'auth.adminactions_export_user']): res = self.app.get('/', user='******') res = res.click('Users') form = res.forms['changelist-form'] form['action'] = self.action_name # form.set('_selected_action', True, 0) # form.set('_selected_action', True, 1) # form.set('_selected_action', True, 2) self._select_rows(form) res = form.submit() res.form['header'] = 1 res.form['columns'] = ['id', 'username', 'first_name' ''] res = res.form.submit('apply') io = six.BytesIO(res.body) io.seek(0) w = xlrd.open_workbook(file_contents=io.read()) sheet = w.sheet_by_index(0) self.assertEquals(sheet.cell_value(0, 0), u'#') self.assertEquals(sheet.cell_value(0, 1), u'ID') self.assertEquals(sheet.cell_value(0, 2), u'username') self.assertEquals(sheet.cell_value(0, 3), u'first name') self.assertEquals(sheet.cell_value(1, 1), 1.0) self.assertEquals(sheet.cell_value(1, 2), u'sax') self.assertEquals(sheet.cell_value(2, 2), u'user')
def _run_action(self, steps=3, page_start=None): with user_grant_permission(self.user, ['demo.change_demomodel', 'demo.adminactions_merge_demomodel']): if isinstance(steps, int): steps = list(range(1, steps + 1)) res = self.app.get('/', user='******') res = res.click('Demo models') else: res = page_start if 1 in steps: form = res.forms['changelist-form'] form['action'] = 'merge' self._select_rows(form) res = form.submit() assert not hasattr(res.form, 'errors') if 2 in steps: res.form['image'] = res.form['form-1-image'].value res = res.form.submit('preview') assert not hasattr(res.form, 'errors') if 3 in steps: res = res.form.submit('apply') return res
def test_merge_delete_detail(self): from adminactions.merge import MergeForm with user_grant_permission(self.user, ['auth.change_user', 'auth.adminactions_merge_user']): # removed = User.objects.get(pk=self._selected_rows[0]) # preserved = User.objects.get(pk=self._selected_rows[1]) res = self.app.get('/', user='******') res = res.click('Users') form = res.forms['changelist-form'] form['action'] = 'merge' self._select_rows(form, [1, 2]) res = form.submit() removed = User.objects.get(pk=self._selected_values[0]) preserved = User.objects.get(pk=self._selected_values[1]) removed.userdetail_set.create(note='1') preserved.userdetail_set.create(note='2') # steps = 2: res.form['master_pk'] = self._selected_values[1] res.form['other_pk'] = self._selected_values[0] res.form['username'] = res.form['form-0-username'].value res.form['email'] = res.form['form-0-email'].value res.form['last_login'] = res.form['form-1-last_login'].value res.form['date_joined'] = res.form['form-1-date_joined'].value res.form['dependencies'] = MergeForm.DEP_DELETE res = res.form.submit('preview') # steps = 3: res = res.form.submit('apply') preserved_after = User.objects.get(pk=self._selected_values[1]) self.assertEqual(preserved_after.userdetail_set.count(), 1) self.assertFalse(User.objects.filter(pk=removed.pk).exists())
def test_swap(self): with user_grant_permission(self.user, ['auth.change_user', 'auth.adminactions_merge_user']): # removed = User.objects.get(pk=self._selected_rows[0]) # preserved = User.objects.get(pk=self._selected_rows[1]) res = self.app.get('/', user='******') res = res.click('Users') form = res.forms['changelist-form'] form['action'] = 'merge' self._select_rows(form, [1, 2]) res = form.submit() removed = User.objects.get(pk=self._selected_values[0]) preserved = User.objects.get(pk=self._selected_values[1]) # steps = 2 (swap): res.form['master_pk'] = self._selected_values[1] res.form['other_pk'] = self._selected_values[0] res.form['username'] = res.form['form-0-username'].value res.form['email'] = res.form['form-0-email'].value res.form['last_login'] = res.form['form-1-last_login'].value res.form['date_joined'] = res.form['form-1-date_joined'].value # res.form['field_names'] = 'username,email' res = res.form.submit('preview') # steps = 3: res = res.form.submit('apply') preserved_after = User.objects.get(pk=self._selected_values[1]) self.assertFalse(User.objects.filter(pk=removed.pk).exists()) self.assertTrue(User.objects.filter(pk=preserved.pk).exists()) self.assertEqual(preserved_after.email, removed.email) self.assertFalse(LogEntry.objects.filter(pk=removed.pk).exists())
def test_use_display_ko(self): with user_grant_permission(self.user, ['demo.change_demomodel', 'demo.adminactions_export_demomodel']): res = self.app.get('/', user='******') res = res.click('Demo models') form = res.forms['changelist-form'] form['action'] = self.action_name self._select_rows(form) res = form.submit() res.form['header'] = 1 res.form['columns'] = ['char', 'text', 'bigint', 'choices' ''] res = res.form.submit('apply') io = six.BytesIO(res.body) io.seek(0) w = xlrd.open_workbook(file_contents=io.read()) sheet = w.sheet_by_index(0) self.assertEquals(sheet.cell_value(0, 1), u'Chäř') self.assertEquals(sheet.cell_value(0, 2), u'bigint') self.assertEquals(sheet.cell_value(0, 3), u'text') self.assertEquals(sheet.cell_value(0, 4), u'choices') self.assertEquals(sheet.cell_value(1, 1), u'Pizzä ïs Gööd') self.assertEquals(sheet.cell_value(1, 2), 333333333.0) self.assertEquals(sheet.cell_value(1, 3), u'lorem ipsum') self.assertEquals(sheet.cell_value(1, 4), 2.0)
def test_success(self): with user_grant_permission(self.user, ['auth.change_user', 'auth.adminactions_export_user']): res = self.app.get('/', user='******') res = res.click('Users') form = res.forms['changelist-form'] form['action'] = self.action_name # form.set('_selected_action', True, 0) # form.set('_selected_action', True, 1) # form.set('_selected_action', True, 2) self._select_rows(form) res = form.submit() res.form['header'] = 1 res.form['columns'] = ['id', 'username', 'first_name' ''] res = res.form.submit('apply') io = six.BytesIO(res.body) io.seek(0) w = xlrd.open_workbook(file_contents=io.read()) sheet = w.sheet_by_index(0) self.assertEquals(sheet.cell_value(0, 0), u'#') self.assertEquals(sheet.cell_value(0, 1), u'ID') self.assertEquals(sheet.cell_value(0, 2), u'username') self.assertEquals(sheet.cell_value(0, 3), u'first name') self.assertEquals(sheet.cell_value(1, 1), 1.0) self.assertEquals(sheet.cell_value(1, 2), u'sax') self.assertEquals(sheet.cell_value(2, 2), u'user')
def test_error_if_too_many_records(self): with user_grant_permission(self.user, ['auth.change_user', 'auth.adminactions_merge_user']): res = self.app.get('/', user='******') res = res.click('Users') form = res.forms['changelist-form'] form['action'] = 'merge' self._select_rows(form, [1, 2, 3]) res = form.submit().follow() self.assertContains(res, 'Please select exactly 2 records')
def test_no_permission(self): with user_grant_permission(self.user, ['auth.change_user']): res = self.app.get('/', user='******') res = res.click('Users') form = res.forms['changelist-form'] form['action'] = 'merge' self._select_rows(form) res = form.submit().follow() assert 'Sorry you do not have rights to execute this action' in str(res.body)
def test_no_permission(self): with user_grant_permission(self.user, ['auth.change_user']): res = self.app.get('/', user='******') res = res.click('Users') form = res.forms['changelist-form'] form['action'] = self.action_name form.set('_selected_action', True, 0) res = form.submit().follow() assert b'Sorry you do not have rights to execute this action' in res.body
def test_no_permission(self): with user_grant_permission(self.user, ['demo.change_demomodel']): res = self._get_changelist_form_response() form = res.forms['changelist-form'] form['action'] = 'byrows_update' self._select_rows(form, selected_rows=self._selected_rows) res = form.submit().follow() assert six.b('Sorry you do not have rights to execute this action') in res.body
def test_no_permission(self): with user_grant_permission(self.user, ['demo.change_demomodel']): res = self.app.get('/', user='******') res = res.click('Demo models') form = res.forms['changelist-form'] form['action'] = 'mass_update' form.set('_selected_action', True, 0) res = form.submit().follow() assert six.b('Sorry you do not have rights to execute this action') in res.body
def test_no_permission(self): with user_grant_permission(self.user, ['demo.change_demomodel']): res = self.app.get('/', user='******') res = res.click('Demo models') form = res.forms['changelist-form'] form['action'] = 'mass_update' form.set('_selected_action', True, 0) res = form.submit().follow() assert six.b('Sorry you do not have rights to execute this action' ) in res.body
def test_no_permission(self): with user_grant_permission(self.user, ['demo.change_demomodel']): res = self._get_changelist_form_response() form = res.forms['changelist-form'] form['action'] = 'byrows_update' self._select_rows(form, selected_rows=self._selected_rows) res = form.submit().follow() assert 'Sorry you do not have rights to execute this action' in str( res.body)
def test_no_permission(self): with user_grant_permission(self.user, ['auth.change_user']): res = self.app.get('/', user='******') res = res.click('Users') form = res.forms['changelist-form'] form['action'] = 'export_as_csv' form.set('_selected_action', True, 0) res = form.submit().follow() assert six.b( 'Sorry you do not have rights to execute this action') in res.body
def test_messages(self): with user_grant_permission(self.user, ['demo.change_demomodel', 'demo.adminactions_massupdate_demomodel']): res = self._run_action(**{'_clean': 1}).follow() messages = [m.message for m in list(res.context['messages'])] self.assertTrue(messages) self.assertEqual('Updated 2 records', messages[0]) res = self._run_action(selected_rows=[1]).follow() messages = [m.message for m in list(res.context['messages'])] self.assertTrue(messages) self.assertEqual('Updated 1 records', messages[0])
def test_form_rows_count(self): """ Count the selected items appear in the action form """ with user_grant_permission(self.user, ['demo.change_demomodel', 'demo.adminactions_byrowsupdate_demomodel']): res = self._get_changelist_form_response() form = res.forms['changelist-form'] self._select_rows(form, selected_rows=self._selected_rows) res = self._get_action_form_response(change_list_response=res) self.assertEqual(len(res.html.find(id="formset").find_all(class_="row")), len(self._selected_rows))
def test_success(self): with user_grant_permission(self.user, ['auth.change_user', 'auth.adminactions_export_user']): res = self.app.get('/', user='******') res = res.click('Users') form = res.forms['changelist-form'] form['action'] = self.action_name self._select_rows(form, [0, 1]) res = form.submit() res.form['use_natural_key'] = True res = res.form.submit('apply') assert res.json[0]['pk'] == 1
def _run_action(self, steps=2): with user_grant_permission(self.user, ['auth.change_user', 'auth.adminactions_export_user']): res = self.app.get('/', user='******') res = res.click('Users') if steps >= 1: form = res.forms['changelist-form'] form['action'] = self.action_name self._select_rows(form) res = form.submit() if steps >= 2: res = res.form.submit('apply') return res
def test_success(self): with user_grant_permission( self.user, ['auth.change_user', 'auth.adminactions_export_user']): res = self.app.get('/', user='******') res = res.click('Users') form = res.forms['changelist-form'] form['action'] = self.action_name self._select_rows(form, [0, 1]) res = form.submit() res.form['use_natural_key'] = True res = res.form.submit('apply') assert res.json[0]['pk'] == 1
def _run_action(self, steps=2): with user_grant_permission( self.user, ['auth.change_user', 'auth.adminactions_export_user']): res = self.app.get('/', user='******') res = res.click('Users') if steps >= 1: form = res.forms['changelist-form'] form['action'] = self.action_name self._select_rows(form) res = form.submit() if steps >= 2: res = res.form.submit('apply') return res
def test_form_rows_fields_exists(self): """ Check model fields appear in action form for each selected models """ with user_grant_permission(self.user, ['demo.change_demomodel', 'demo.adminactions_byrowsupdate_demomodel']): res = self._get_changelist_form_response() form = res.forms['changelist-form'] self._select_rows(form, selected_rows=self._selected_rows) res = self._get_action_form_response(change_list_response=res) byrows_update_get_fields(ModelAdmin(DemoModel, self.site)) for r, value in enumerate(self._selected_values): for fname in byrows_update_get_fields(ModelAdmin(DemoModel, self.site)): assert res.form["form-%d-%s" % (r, fname)]
def test_add_foreign_keys(self): with user_grant_permission(self.user, ['auth.change_user', 'auth.adminactions_export_user']): res = self.app.get('/', user='******') res = res.click('Users') form = res.forms['changelist-form'] form['action'] = self.action_name form.set('_selected_action', True, 0) form.set('_selected_action', True, 1) res = form.submit() res.form['use_natural_key'] = True res.form['add_foreign_keys'] = True res = res.form.submit('apply') assert res.json[0]['pk'] == 1
def test_messages(self): with user_grant_permission(self.user, [ 'demo.change_demomodel', 'demo.adminactions_massupdate_demomodel' ]): res = self._run_action(**{'_clean': 1}).follow() messages = [m.message for m in list(res.context['messages'])] self.assertTrue(messages) self.assertEqual('Updated 2 records', messages[0]) res = self._run_action(selected_rows=[1]).follow() messages = [m.message for m in list(res.context['messages'])] self.assertTrue(messages) self.assertEqual('Updated 1 records', messages[0])
def test_add_foreign_keys(self): with user_grant_permission( self.user, ['auth.change_user', 'auth.adminactions_export_user']): res = self.app.get('/', user='******') res = res.click('Users') form = res.forms['changelist-form'] form['action'] = self.action_name form.set('_selected_action', True, 0) form.set('_selected_action', True, 1) res = form.submit() res.form['use_natural_key'] = True res.form['add_foreign_keys'] = True res = res.form.submit('apply') assert res.json[0]['pk'] == 1
def test_success(self): with user_grant_permission( self.user, ['demo.change_demomodel', 'demo.adminactions_export_demomodel']): res = self.app.get('/', user='******') res = res.click('Demo models') form = res.forms['changelist-form'] form['action'] = self.action_name # form.set('_selected_action', True, 1) self._select_rows(form) res = form.submit() res = res.form.submit('apply') buff = io.StringIO(smart_text(res.body)) csv_reader = csv.reader(buff) self.assertEqual(len(list(csv_reader)), 2)
def test_form_rows_count(self): """ Count the selected items appear in the action form """ with user_grant_permission(self.user, [ 'demo.change_demomodel', 'demo.adminactions_byrowsupdate_demomodel' ]): res = self._get_changelist_form_response() form = res.forms['changelist-form'] self._select_rows(form, selected_rows=self._selected_rows) res = self._get_action_form_response(change_list_response=res) self.assertEqual( len(res.html.find(id="formset").find_all(class_="row")), len(self._selected_rows))
def test_form_rows_fields_exists(self): """ Check model fields appear in action form for each selected models """ with user_grant_permission(self.user, [ 'demo.change_demomodel', 'demo.adminactions_byrowsupdate_demomodel' ]): res = self._get_changelist_form_response() form = res.forms['changelist-form'] self._select_rows(form, selected_rows=self._selected_rows) res = self._get_action_form_response(change_list_response=res) byrows_update_get_fields(ModelAdmin(DemoModel, self.site)) for r, value in enumerate(self._selected_values): for fname in byrows_update_get_fields( ModelAdmin(DemoModel, self.site)): assert res.form["form-%d-%s" % (r, fname)]
def test_success(self): with user_grant_permission(self.user, ['demo.change_demomodel', 'demo.adminactions_export_demomodel']): res = self.app.get('/', user='******') res = res.click('Demo models') form = res.forms['changelist-form'] form['action'] = self.action_name # form.set('_selected_action', True, 1) self._select_rows(form) res = form.submit() res = res.form.submit('apply') if six.PY2: buff = io.BytesIO(res.body) else: buff = io.StringIO(smart_text(res.body)) csv_reader = csv.reader(buff) self.assertEqual(len(list(csv_reader)), 2)
def _run_action(self, steps=2): # with user_grant_permission(self.user, ['demo.change_demomodel', 'demo.adminactions_massupdate_demomodel']): with user_grant_permission(self.user, ['auth.change_user', 'auth.adminactions_chart_user']): res = self.app.get('/', user='******') res = res.click('Users') if steps >= 1: form = res.forms['changelist-form'] form['action'] = 'graph_queryset' self._select_rows(form) # form.set('_selected_action', True, 0) # form.set('_selected_action', True, 1) res = form.submit() if steps >= 2: res.form['axes_x'] = 'username' res = res.form.submit('apply') return res
def _run_action(self, steps=2): with user_grant_permission( self.user, ['auth.change_user', 'auth.adminactions_chart_user']): res = self.app.get('/', user='******') res = res.click('Users') if steps >= 1: form = res.forms['changelist-form'] form['action'] = 'graph_queryset' self._select_rows(form) # form.set('_selected_action', True, 0) # form.set('_selected_action', True, 1) res = form.submit() if steps >= 2: res.form['axes_x'] = 'username' res = res.form.submit('apply') return res
def test_custom_filename(self): """ if the ModelAdmin has `get_export_as_csv_filename()` use that method to get the attachment filename """ with user_grant_permission(self.user, ['auth.change_user', 'auth.adminactions_export_user']): res = self.app.get('/', user='******') with admin_register(User) as md: with mock.patch.object(md, 'get_export_as_csv_filename', lambda r, q: 'new.test', create=True): res = res.click('Users') form = res.forms['changelist-form'] form['action'] = 'export_as_csv' form.set('_selected_action', True, 0) form['select_across'] = 1 res = form.submit() res = res.form.submit('apply') self.assertEqual(res.content_disposition, u'attachment;filename="new.test"')
def _run_action(self, steps=2, **kwargs): selected_rows = kwargs.pop('selected_rows', self._selected_rows) with user_grant_permission(self.user, ['demo.change_demomodel', 'demo.adminactions_massupdate_demomodel']): res = self.app.get('/', user='******') res = res.click('Demo models') if steps >= 1: form = res.forms['changelist-form'] form['action'] = 'mass_update' self._select_rows(form, selected_rows) res = form.submit() if steps >= 2: for k, v in kwargs.items(): res.form[k] = v res.form['chk_id_char'].checked = True res.form['func_id_char'] = 'upper' res.form['chk_id_choices'].checked = True res.form['func_id_choices'] = 'set' res.form['choices'] = '1' res = res.form.submit('apply') return res
def test_form_rows_edit(self): """ Modify a value in action form and see if its stored upon form submit """ with user_grant_permission(self.user, ['demo.change_demomodel', 'demo.adminactions_byrowsupdate_demomodel']): res = self._get_changelist_form_response() form = res.forms['changelist-form'] self._select_rows(form, selected_rows=self._selected_rows) res = self._get_action_form_response(change_list_response=res) row_to_modify = 0 new_values = { 'char': 'Bob Marley' } for k, v in new_values.items(): res.form["form-%d-%s" % (row_to_modify, k)] = v res.form.submit('apply') obj = DemoModel.objects.get(id=self._selected_values[row_to_modify]) for k, v in new_values.items(): self.assertEquals(v, getattr(obj, k))
def test_unicode(self): with user_grant_permission( self.user, ['demo.change_demomodel', 'demo.adminactions_export_demomodel']): res = self.app.get('/', user='******') res = res.click('Demo models') form = res.forms['changelist-form'] form['action'] = self.action_name self._select_rows(form) res = form.submit() res.form['header'] = 1 res.form['columns'] = [ 'char', ] res = res.form.submit('apply') buff = io.BytesIO(res.body) buff.seek(0) w = xlrd.open_workbook(file_contents=buff.read()) sheet = w.sheet_by_index(0) self.assertEqual(sheet.cell_value(0, 1), u'Chäř') self.assertEqual(sheet.cell_value(1, 1), u'Pizzä ïs Gööd')
def _run_action(self, steps=2, **kwargs): selected_rows = kwargs.pop('selected_rows', self._selected_rows) with user_grant_permission(self.user, [ 'demo.change_demomodel', 'demo.adminactions_massupdate_demomodel' ]): res = self.app.get('/', user='******') res = res.click('Demo models') if steps >= 1: form = res.forms['changelist-form'] form['action'] = 'mass_update' self._select_rows(form, selected_rows) res = form.submit() if steps >= 2: for k, v in kwargs.items(): res.form[k] = v res.form['chk_id_char'].checked = True res.form['func_id_char'] = 'upper' res.form['chk_id_choices'].checked = True res.form['func_id_choices'] = 'set' res.form['choices'] = '1' res = res.form.submit('apply') return res
def test_custom_filename(self): """ if the ModelAdmin has `get_export_as_csv_filename()` use that method to get the attachment filename """ with user_grant_permission( self.user, ['auth.change_user', 'auth.adminactions_export_user']): res = self.app.get('/', user='******') with admin_register(User) as md: with mock.patch.object(md, 'get_export_as_csv_filename', lambda r, q: 'new.test', create=True): res = res.click('Users') form = res.forms['changelist-form'] form['action'] = 'export_as_csv' form.set('_selected_action', True, 0) form['select_across'] = 1 res = form.submit() res = res.form.submit('apply') self.assertEqual(res.content_disposition, u'attachment;filename="new.test"')
def test_form_rows_edit(self): """ Modify a value in action form and see if its stored upon form submit """ with user_grant_permission(self.user, [ 'demo.change_demomodel', 'demo.adminactions_byrowsupdate_demomodel' ]): res = self._get_changelist_form_response() form = res.forms['changelist-form'] self._select_rows(form, selected_rows=self._selected_rows) res = self._get_action_form_response(change_list_response=res) row_to_modify = 0 new_values = {'char': 'Bob Marley'} for k, v in new_values.items(): res.form["form-%d-%s" % (row_to_modify, k)] = v res.form.submit('apply') obj = DemoModel.objects.get( id=self._selected_values[row_to_modify]) for k, v in new_values.items(): self.assertEqual(v, getattr(obj, k))
def test_faster_export(self): # generate 3k users start = time.time() user_count = User.objects.count() User.objects.bulk_create( [User(username='******' % i) for i in range(3000)]) # print('created 3k users in %.1f seconds' % (time.time() - start)) self.assertEqual(User.objects.count(), 3000 + user_count) start = time.time() with user_grant_permission( self.user, ['auth.change_user', 'auth.adminactions_export_user']): res = self.app.get('/', user='******') res = res.click('Users') form = res.forms['changelist-form'] form['action'] = self.action_name form['select_across'] = 1 self._select_rows(form) res = form.submit() res.form['header'] = 1 res = res.form.submit('apply') res_time = (time.time() - start) # print('Response returned in %.1f seconds' % res_time) io = six.BytesIO(res.body) io.seek(0) w = xlrd.open_workbook(file_contents=io.read()) sheet = w.sheet_by_index(0) self.assertEqual(sheet.nrows, 3000 + user_count + 1) self.assertLessEqual( res_time, 6.5, "Response should return under 6.5 " "seconds, was %.2f" % res_time)
def test_permissions(admin): assert Permission.objects.filter(codename__startswith='adminactions').count() == 40 with user_grant_permission(admin, ['demo.adminactions_export_demomodel']): admin.get_all_permissions() == set([u'demo.adminactions_export_demomodel'])