Exemplo n.º 1
0
    def view_index(self, request):
        load_models()

        class ModelForm(forms.Form):
            model = forms.ChoiceField(
                choices=[(get_model_id(m), get_model_id(m))
                         for m in sorted(d_apps.get_models(),
                                         key=lambda x: x._meta.db_table)])
            action = forms.CharField(widget=forms.HiddenInput)

        if request.POST:
            form = ModelForm(request.POST)
            if form.is_valid():
                if form.cleaned_data["action"] == "Export":
                    app, m = form.cleaned_data["model"].split(".", 1)
                    model = d_apps.get_model(app, m)
                    if not model:
                        return self.response_not_found("Invalid model")
                    return self.render_plain_text(
                        csv_export(model),
                        content_type="text/csv; encoding=utf-8")
                else:
                    return self.response_redirect("/main/csv/import/%s/" %
                                                  form.cleaned_data["model"])
        else:
            form = ModelForm()
        return self.render(request, "index.html", form=form)
Exemplo n.º 2
0
 def handle(self, *args, **options):
     if len(args) < 1:
         self._usage()
     r = args[0].split(".")
     if len(r) != 2:
         self._usage()
     app, model = r
     m = models.get_model(app, model)
     if not m:
         return self._usage()
     print(
         csv_export(m,
                    queryset=self.get_queryset(m, args[1:]),
                    first_row_only=options.get("template")), )
Exemplo n.º 3
0
def admin_csv_export(modeladmin, request, queryset):
    return HttpResponse(csv_export(modeladmin.model, queryset),
                        content_type="text/csv; encoding=utf-8")