Server IP : 85.214.239.14 / Your IP : 3.145.186.132 Web Server : Apache/2.4.62 (Debian) System : Linux h2886529.stratoserver.net 4.9.0 #1 SMP Tue Jan 9 19:45:01 MSK 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.18 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : OFF | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /proc/3/cwd/proc/3/cwd/srv/modoboa/env/lib64/python3.5/site-packages/modoboa/admin/views/ |
Upload File : |
"""Export related views.""" import csv from io import StringIO from django.contrib.auth.decorators import ( login_required, permission_required, user_passes_test ) from django.http import HttpResponse from django.shortcuts import render from django.urls import reverse from django.utils.translation import ugettext as _ from ..forms import ExportDataForm from ..lib import get_domains, get_identities def _export(content, filename): """Export a csv file's content :param content: the content to export (string) :param filename: the name that will appear into the response :return: an ``HttpResponse`` object """ resp = HttpResponse(content) resp["Content-Type"] = "text/csv" resp["Content-Length"] = len(content) resp["Content-Disposition"] = 'attachment; filename="{}"'.format(filename) return resp @login_required @user_passes_test( lambda u: u.has_perm("core.add_user") or u.has_perm("admin.add_alias") ) def export_identities(request): ctx = { "title": _("Export identities"), "action_label": _("Export"), "action_classes": "submit", "formid": "exportform", "action": reverse("admin:identity_export"), } if request.method == "POST": form = ExportDataForm(request.POST) form.is_valid() fp = StringIO() csvwriter = csv.writer(fp, delimiter=form.cleaned_data["sepchar"]) identities = get_identities( request.user, **request.session["identities_filters"]) for ident in identities: ident.to_csv(csvwriter) content = fp.getvalue() fp.close() return _export(content, "modoboa-identities.csv") ctx["form"] = ExportDataForm() return render(request, "common/generic_modal_form.html", ctx) @login_required @permission_required("admin.add_domain") def export_domains(request): ctx = { "title": _("Export domains"), "action_label": _("Export"), "action_classes": "submit", "formid": "exportform", "action": reverse("admin:domain_export"), } if request.method == "POST": form = ExportDataForm(request.POST) form.is_valid() fp = StringIO() csvwriter = csv.writer(fp, delimiter=form.cleaned_data["sepchar"]) for dom in get_domains(request.user, **request.session["domains_filters"]): dom.to_csv(csvwriter) content = fp.getvalue() fp.close() return _export(content, "modoboa-domains.csv") ctx["form"] = ExportDataForm() return render(request, "common/generic_modal_form.html", ctx)