0001-csv_import-remove-modify-import-form-53463.patch
src/authentic2/manager/forms.py | ||
---|---|---|
737 | 737 |
return new_import |
738 | 738 | |
739 | 739 | |
740 |
class UserEditImportForm(UserImportForm): |
|
741 |
def __init__(self, *args, **kwargs): |
|
742 |
self.user_import = kwargs.pop('user_import') |
|
743 |
initial = kwargs.setdefault('initial', {}) |
|
744 |
initial['encoding'] = self.user_import.encoding |
|
745 |
initial['ou'] = self.user_import.ou |
|
746 |
super(UserEditImportForm, self).__init__(*args, **kwargs) |
|
747 |
del self.fields['import_file'] |
|
748 | ||
749 |
def clean(self): |
|
750 |
from authentic2.csv_import import CsvImporter |
|
751 | ||
752 |
encoding = self.cleaned_data['encoding'] |
|
753 |
with self.user_import.import_file as fd: |
|
754 |
importer = CsvImporter() |
|
755 |
if not importer.run(fd, encoding): |
|
756 |
self.raise_validation_error(importer.error.description or importer.error.code) |
|
757 |
self.cleaned_data['rows_count'] = len(importer.rows) |
|
758 | ||
759 |
def save(self): |
|
760 |
with self.user_import.meta_update as meta: |
|
761 |
meta['ou'] = self.cleaned_data['ou'] |
|
762 |
meta['encoding'] = self.cleaned_data['encoding'] |
|
763 | ||
764 | ||
765 | 740 |
class RolesCsvImportForm(LimitQuerysetFormMixin, forms.Form): |
766 | 741 |
import_file = forms.FileField( |
767 | 742 |
label=_('Roles file'), |
src/authentic2/manager/templates/authentic2/manager/user_import.html | ||
---|---|---|
20 | 20 |
{% block sidebar %} |
21 | 21 |
<aside id="sidebar"> |
22 | 22 |
<div> |
23 |
<h3>{% trans "Modify import" %}</h3>
|
|
23 |
<h3>{% trans "Actions" %}</h3>
|
|
24 | 24 |
<form method="post" id="action-form"> |
25 | 25 |
{% csrf_token %} |
26 |
{{ form|with_template }} |
|
27 | 26 |
<div class="buttons"> |
28 |
<button name="modify">{% trans "Modify" %}</button> |
|
29 |
<button name="simulate">{% trans "Simulate" %}</button> |
|
30 |
<button name="execute">{% trans "Execute" %}</button> |
|
27 |
<button name="simulate">{% trans "Simulate import" %}</button> |
|
28 |
</div> |
|
29 |
<div class="buttons"> |
|
30 |
<button name="execute">{% trans "Execute import" %}</button> |
|
31 | 31 |
</div> |
32 | 32 |
</form> |
33 | 33 |
</div> |
34 | 34 |
<div> |
35 |
<h3>{% trans "Download" %}</h3> |
|
36 | 35 |
<form action="download/{{ user_import.filename }}" id="download-form"> |
37 | 36 |
<div class="buttons"> |
38 |
<button>{% trans "Download" %}</button> |
|
37 |
<button>{% trans "Download CSV file" %}</button>
|
|
39 | 38 |
</div> |
40 | 39 |
</form> |
41 | 40 |
</div> |
... | ... | |
43 | 42 |
{% endblock %} |
44 | 43 | |
45 | 44 |
{% block main %} |
45 |
<p>{% trans "Encoding:" %} {{ encoding }}</p> |
|
46 | 46 |
<p>{% trans "Rows count:" %} {{ user_import.rows_count }}</p> |
47 |
<p>{% trans "Target Organizational Unit:" %} {{ user_import.ou }}</p> |
|
47 | 48 |
<h2>{% trans "Reports" %}</h2> |
48 | 49 |
<table class="main"> |
49 | 50 |
<thead> |
src/authentic2/manager/user_views.py | ||
---|---|---|
55 | 55 | |
56 | 56 |
from . import app_settings |
57 | 57 |
from .forms import ( |
58 |
ENCODINGS, |
|
58 | 59 |
ChooseUserAuthorizationsForm, |
59 | 60 |
ChooseUserRoleForm, |
60 | 61 |
UserAddChooseOUForm, |
... | ... | |
62 | 63 |
UserChangeEmailForm, |
63 | 64 |
UserChangePasswordForm, |
64 | 65 |
UserEditForm, |
65 |
UserEditImportForm, |
|
66 | 66 |
UserNewImportForm, |
67 | 67 |
UserRoleSearchForm, |
68 | 68 |
UserSearchForm, |
... | ... | |
836 | 836 |
user_imports = UserImportsView.as_view() |
837 | 837 | |
838 | 838 | |
839 |
class UserImportView(MediaMixin, PermissionMixin, FormView): |
|
840 |
form_class = UserEditImportForm |
|
839 |
class UserImportView(MediaMixin, PermissionMixin, TemplateView): |
|
841 | 840 |
permissions = ['custom_user.admin_user'] |
842 | 841 |
permissions_global = True |
843 | 842 |
template_name = 'authentic2/manager/user_import.html' |
... | ... | |
855 | 854 |
return FileResponse(self.user_import.import_file, content_type='text/csv') |
856 | 855 |
return super(UserImportView, self).get(request, uuid=uuid, filename=filename) |
857 | 856 | |
858 |
def get_form_kwargs(self): |
|
859 |
kwargs = super(UserImportView, self).get_form_kwargs() |
|
860 |
kwargs['user_import'] = self.user_import |
|
861 |
return kwargs |
|
862 | ||
863 | 857 |
def post(self, request, *args, **kwargs): |
864 | 858 |
from authentic2.manager import user_import |
865 | 859 | |
... | ... | |
873 | 867 |
report.delete() |
874 | 868 |
return redirect(request, 'a2-manager-users-import', kwargs={'uuid': self.user_import.uuid}) |
875 | 869 | |
876 |
simulate = 'simulate' in request.POST |
|
877 |
execute = 'execute' in request.POST |
|
878 |
if simulate or execute: |
|
879 |
report = user_import.Report.new(self.user_import) |
|
880 |
report.run(simulate=simulate) |
|
881 |
return redirect(request, 'a2-manager-users-import', kwargs={'uuid': self.user_import.uuid}) |
|
882 |
return super(UserImportView, self).post(request, *args, **kwargs) |
|
883 | ||
884 |
def form_valid(self, form): |
|
885 |
form.save() |
|
886 |
return super(UserImportView, self).form_valid(form) |
|
887 | ||
888 |
def get_success_url(self): |
|
889 |
return reverse('a2-manager-users-import', kwargs={'uuid': self.user_import.uuid}) |
|
870 |
report = user_import.Report.new(self.user_import) |
|
871 |
report.run(simulate=bool('simulate' in request.POST)) |
|
872 |
return redirect(request, 'a2-manager-users-import', kwargs={'uuid': self.user_import.uuid}) |
|
890 | 873 | |
891 | 874 |
def get_context_data(self, **kwargs): |
892 | 875 |
ctx = super(UserImportView, self).get_context_data(**kwargs) |
876 |
ctx['encoding'] = [encoding for id, encoding in ENCODINGS if id == self.user_import.encoding][0] |
|
893 | 877 |
ctx['user_import'] = self.user_import |
894 | 878 |
ctx['reports'] = sorted(self.user_import.reports, key=operator.attrgetter('created'), reverse=True) |
895 | 879 |
return ctx |
... | ... | |
899 | 883 | |
900 | 884 | |
901 | 885 |
class UserImportReportView(MediaMixin, PermissionMixin, TemplateView): |
902 |
form_class = UserEditImportForm |
|
903 | 886 |
permissions = ['custom_user.admin_user'] |
904 | 887 |
permissions_global = True |
905 | 888 |
tests/test_user_manager.py | ||
---|---|---|
490 | 490 | |
491 | 491 |
response = response.follow() |
492 | 492 | |
493 |
response = response.forms['action-form'].submit(name='modify').follow() |
|
494 | ||
495 | 493 |
response = response.forms['action-form'].submit(name='simulate') |
496 | 494 | |
497 | 495 |
reports = list(_import.reports) |
... | ... | |
634 | 632 | |
635 | 633 |
start = time.time() |
636 | 634 |
response = response.click('Users Import') |
635 |
assert 'Encoding: Unicode (UTF-8)' in response.text |
|
636 |
assert 'Target Organizational Unit: Default organizational unit' in response.text |
|
637 | ||
637 | 638 |
while 'Running' in response.text: |
638 | 639 |
response = response.click('Users Import') |
639 | 640 |
assert time.time() - start < 2 |
640 |
- |