CSV Export

Fixes #2
This commit is contained in:
gw3000 2023-08-08 16:23:33 +02:00
parent 10ca0cd7f1
commit 6be7f3c3cd
10 changed files with 128 additions and 0 deletions

0
app/export/__init__.py Normal file
View file

3
app/export/admin.py Normal file
View file

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

6
app/export/apps.py Normal file
View file

@ -0,0 +1,6 @@
from django.apps import AppConfig
class ExportConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "export"

View file

3
app/export/models.py Normal file
View file

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

View file

@ -0,0 +1,19 @@
{% extends "base.html" %}
{% load static %}
{% block content %}
<div class="row">
<div class="col-lg-8 mb-3">
<h3>Datenexport</h3>
<p>
Anbei finden Sie mehrere Möglichkeiten die Daten der Anwendung zu
exportieren.
</p>
<ul>
<li><a href="{% url 'export_birds' %}">alle Patienten</a></li>
<li>alle Finder</li>
<li>alle Patienten Finder</li>
<li><a href="{% url 'export_costs' %}">alle Kosten</a></li>
</ul>
</div>
</div>
{% endblock content %}

3
app/export/tests.py Normal file
View file

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

13
app/export/urls.py Normal file
View file

@ -0,0 +1,13 @@
from django.urls import path
from .views import (
export_birds,
export_costs,
site_exports,
)
urlpatterns = [
path("", site_exports, name="site_exports"),
path("birds/", export_birds, name="export_birds"),
path("costs/", export_costs, name="export_costs"),
]

77
app/export/views.py Normal file
View file

@ -0,0 +1,77 @@
import csv
from costs.models import Costs
from bird.models import FallenBird
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.shortcuts import render
@login_required(login_url="account_login")
def site_exports(request):
return render(
request,
"export/export_site.html",
)
@login_required(login_url="account_login")
def export_costs(request):
costs = Costs.objects.all().values_list(
"id_bird__bird_identifier",
"costs",
"created",
"comment",
"user__username")
response = HttpResponse(content_type="text/csv")
response["Content-Disposition"] = "attachment, filename=fbf_costs.csv"
writer = csv.writer(response)
writer.writerow(
["Vogel", "Betrag in Euro", "Gebucht am", "Kommentar", "Gebucht von"]
)
for single_costs in costs:
writer.writerow(single_costs)
return response
@login_required(login_url="account_login")
def export_birds(request):
birds = FallenBird.objects.all().values_list(
"bird_identifier",
"bird__name",
"age",
"sex",
"date_found",
"place",
"created",
"updated",
"find_circumstances__description",
"diagnostic_finding",
"rescuer__last_name",
"user__username",
"status__description",
"aviary__description",
"sent_to"
)
response = HttpResponse(content_type="text/csv")
response["Content-Disposition"] = "attachment, filename=fbf_birds.csv"
writer = csv.writer(response)
writer.writerow(["Vogel",
"Patienten Alias",
"Alter",
"Geschlecht",
"gefunden am",
"Fundort",
"Pateient angelegt am",
"Pateient aktualisiert am",
"Fundumstände",
"Diagnose bei Fund",
"Finder (Nachname)",
"Benutzer",
"Status",
"Voliere",
"Übersandt"
])
for bird in birds:
writer.writerow(bird)
return response

View file

@ -30,6 +30,10 @@
<a class="nav-link {% if '/costs/all' in request.path %} active {% endif %}" <a class="nav-link {% if '/costs/all' in request.path %} active {% endif %}"
href="{% url 'costs_all' %}">alle Kosten</a> href="{% url 'costs_all' %}">alle Kosten</a>
</li> </li>
<li class="nav-item">
<a class="nav-link {% if '/export' in request.path %} active {% endif %}"
href="{% url 'site_exports' %}">Export</a>
</li>
{% endif %} {% endif %}
</ul> </ul>