Browse Source

app without data

master
Walter Hupfeld 2 years ago
parent
commit
6eaa21e467
  1. 44
      backend/migrations/0001_initial.py
  2. 167
      backend/models.py
  3. 64
      backend/templates/backend/calculate.html
  4. 47
      backend/templates/backend/counties_upload.html
  5. 34
      backend/templates/backend/details.html
  6. 36
      backend/templates/backend/factsheet.html
  7. 50
      backend/templates/backend/index.html
  8. 217
      backend/templates/backend/output.html
  9. 12
      backend/templates/backend/search.html
  10. 19
      backend/templates/backend/search_results.html
  11. 26
      backend/urls.py
  12. 128
      backend/views.py
  13. 6
      co2budget/settings.py
  14. 4
      co2budget/urls.py
  15. BIN
      db.sqlite3
  16. 43
      docker-compose.yml
  17. 0
      frontend/__init__.py
  18. 3
      frontend/admin.py
  19. 5
      frontend/apps.py
  20. 0
      frontend/migrations/__init__.py
  21. 3
      frontend/models.py
  22. 8
      frontend/templates/frontend/index.html
  23. 3
      frontend/tests.py
  24. 8
      frontend/urls.py
  25. 7
      frontend/views.py
  26. 2
      requirements.txt
  27. 57
      templates/base.html
  28. 43
      templates/frontend.html

44
backend/migrations/0001_initial.py

@ -0,0 +1,44 @@
# Generated by Django 3.1.7 on 2021-03-16 08:41
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='County',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('gemeindeId', models.CharField(max_length=10)),
('kennziffer', models.CharField(max_length=10)),
('Name', models.CharField(max_length=100)),
('Anzahl', models.IntegerField()),
('männlich', models.IntegerField()),
('weiblich', models.IntegerField()),
],
options={
'verbose_name_plural': 'counties',
},
),
migrations.CreateModel(
name='Factsheet',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('code', models.CharField(max_length=20)),
('fact', models.CharField(max_length=100)),
('value', models.IntegerField()),
('reason', models.CharField(blank=True, max_length=80, null=True)),
('source', models.CharField(blank=True, max_length=200, null=True)),
('link', models.CharField(blank=True, max_length=255, null=True)),
('effort', models.CharField(blank=True, max_length=8, null=True)),
('researcher', models.CharField(blank=True, max_length=50, null=True)),
('date', models.CharField(blank=True, max_length=10, null=True)),
],
),
]

167
backend/models.py

@ -1,3 +1,170 @@
from django.db import models
from django.urls import reverse
import datetime
import math
# Create your models here.
class County(models.Model):
gemeindeId= models.CharField(max_length=10)
kennziffer = models.CharField(max_length=10)
Name = models.CharField(max_length=100)
Anzahl = models.IntegerField()
männlich = models.IntegerField()
weiblich = models.IntegerField()
class Meta:
verbose_name_plural = "counties"
def __str__(self):
return self.Name
def get_absolute_url(self):
return reverse('gemeindeId', args=[str(self.id)])
# Some usefull tools
# expand gemeindeId to string with 8 chars
def mkFull(number):
while len(number)<8:
number = number+"0"
return number
# manage - in csv convert to 0
def mkInt(number):
if number=="-":
return 0
else:
return int(number)
class Factsheet(models.Model):
code = models.CharField(max_length=20)
fact = models.CharField(max_length=100)
value = models.IntegerField()
reason = models.CharField(max_length=80,blank=True,null=True)
source = models.CharField(max_length=200,blank=True,null=True)
link = models.CharField(max_length=255,blank=True,null=True)
effort = models.CharField(max_length=8,blank=True,null=True)
researcher = models.CharField(max_length=50,blank=True,null=True)
date = models.CharField(max_length=10,blank=True,null=True)
def __str__(self):
return self.code
def get_absolute_url(self):
return reverse('code', args=[str(self.id)])
class Calc:
def __init__(self,gemeindeId='03152012',targetyear=2030):
self.gemeindeId=gemeindeId
self.zieljahr=targetyear
def get(self,code):
model=Factsheet
return model.objects.get(code=code).value
def round(self, number):
if number>1e6:
t1 = ("%s%s" % ( round(number/1e6,2) , " Mio"))
return t1.replace(".",",")+"."
elif number>1000:
t1 = math.trunc(number/1000)
t2 = round(number-(t1*1000))
return ("%s.%s" % ( t1, t2))
elif number>0:
return round(number)
elif number<0:
return "-"+self.round(-number)
else:
return number
def calculate(self):
now = datetime.datetime.now()
kalkulationszeitraum = self.zieljahr-now.year
populationDE = County.objects.get(gemeindeId="DG").Anzahl
populationDistrict = County.objects.get(gemeindeId=self.gemeindeId).Anzahl
nameDistrict = County.objects.get(gemeindeId=self.gemeindeId).Name
budgetKommune2016 = self.get('CO2bud2016') * populationDistrict / self.get('population_world')
bilanzEW = self.get('CO2e2018_ew') * populationDistrict / populationDE
bilanzGB = self.get('CO2e2018_gb') * populationDistrict / populationDE
bilanzVK = self.get('CO2e2018_vk') * populationDistrict / populationDE
bilanzLW = self.get('CO2e2018_lw') * populationDistrict / populationDE
bilanzSO = self.get('CO2e2018_so') * populationDistrict / populationDE
bilanzIN = self.get('CO2e2018_in') * populationDistrict / populationDE
bilanzGES = bilanzEW + bilanzGB + bilanzVK + bilanzLW + bilanzSO + bilanzIN
emissionKomm = {
"2016" : bilanzGES * self.get('CO2emissionDE2016') / self.get('CO2emissionDE2018'),
"2017" : bilanzGES * self.get('CO2emissionDE2017') / self.get('CO2emissionDE2018'),
"2018" : bilanzGES,
"2019" : bilanzGES * self.get('CO2emissionDE2019') / self.get('CO2emissionDE2018'),
"2020" : bilanzGES * self.get('CO2emissionDE2020') / self.get('CO2emissionDE2018'),
"2021" : bilanzGES * self.get('CO2emissionDE2021') / self.get('CO2emissionDE2018'),
}
for year in range(2022, self.zieljahr+2):
yearStr = str(year)
emissionKomm[yearStr] = emissionKomm["2021"]-emissionKomm["2021"]/(kalkulationszeitraum+1)*(year-2021)
gesamtEmission = 0
for value in emissionKomm:
gesamtEmission = gesamtEmission + emissionKomm[value]
kostenGES = self.get('kosten_gesamt')*populationDistrict
kostenAntKommune = self.get('kosten_komm')*populationDistrict
kostenProJahr = kostenGES/kalkulationszeitraum
kostenAnteilKommuneJahr = kostenAntKommune/kalkulationszeitraum
arbeitsplGES = self.get('arbeitsplaetze')*populationDistrict/1000000
arbeitsplAntKommune = self.get('arbeitsplaetze_komm')*populationDistrict/1000000
emissionKommData = {}
for value in emissionKomm:
emissionKommData[value] = round(emissionKomm[value])
for value in emissionKomm:
emissionKomm[value] = self.round(emissionKomm[value])
context = {
'populationDistrict' : self.round(populationDistrict),
'nameDistrict' : nameDistrict,
'zieljahr' : self.zieljahr,
'kalkulationszeitraum' : kalkulationszeitraum,
'budgetKommune2016': self.round(budgetKommune2016),
'gesamtBisKN': self.round(gesamtEmission),
'verbleibendesBudget': self.round(budgetKommune2016 - gesamtEmission),
'emissionKommune': emissionKomm,
'emissionKommuneData': emissionKommData,
'bilanzGES' :self.round(bilanzGES),
'bilanzEW' :self.round(bilanzEW),
'bilanzGB' :self.round(bilanzGB),
'bilanzVK' :self.round(bilanzVK),
'bilanzLW' :self.round(bilanzLW),
'bilanzSO' :self.round(bilanzSO),
'bilanzIN' :self.round(bilanzIN),
'kostenGES' : self.round(kostenGES),
'kostenAntKommune' : self.round(kostenAntKommune),
'kostenProJahr' : self.round(kostenProJahr),
'kostenAnteilKommuneJahr' : self.round(kostenAnteilKommuneJahr),
'arbeitsplGES': self.round(arbeitsplGES),
'arbeitsplAntKommune':self.round(arbeitsplAntKommune),
}
return context

64
backend/templates/backend/calculate.html

@ -0,0 +1,64 @@
{% extends 'base.html' %}
{% block content %}
<style>
td {text-align:right;}
</style>
<div class="row">
<div class="col-sm-6">
<h1>Kalkulation</h1>
<h2>Budget</h2>
<table class="table table-striped table-sm">
<tr><th>Budget Kommune 2016</th><td>{{ budgetKommune2016 }} t</sub></td></tr>
<tr><th>Gesamtemissionen bis KN</th><td>{{ gesamtBisKN }} t</td></tr>
<tr><th>Verbleibendes Budget der Kommune</th><td>{{ verbleibendesBudget }} t</td></tr>
</table>
<h2>Emission Kommune</h2>
<table class="table table-striped table-sm">
{% for key,val in emissionKommune.items %}
<tr><th>{{ key }}</th><td>{{ val }} t</td></tr>
{% endfor %}
<tr><th></th><td></td></tr>
</table>
<h2>THG Bilanz 2018</h2>
<table class="table table-striped table-sm">
<tr><th>Gesamt</th><td>{{ bilanzGES }} t</td></tr>
<tr><th>Energiewirtschaft</th><td>{{ bilanzEW }} t</td></tr>
<tr><th>Industrie</th><td>{{ bilanzIN }} t</td></tr>
<tr><th>Gebäude</th><td>{{ bilanzGB }} t</td></tr>
<tr><th>Verkehr</th><td>{{ bilanzVK }} t</td></tr>
<tr><th>Landwirtschaft</th><td>{{ bilanzLW }} t</td></tr>
<tr><th>Sonstige</th><td>{{ bilanzSO }} t</td></tr>
</table>
<h2>Kosten Klimaneutralität</h2>
<table class="table table-striped table-sm">
<tr><th>Gesamtkosten</th><td>{{ kostenGES }} €</td></tr>
<tr><th>Anteil der Kommune</th><td> {{ kostenAntKommune }} €</td></tr>
<tr><th>Gesamtkosten pro Jahr bis Zieljahr</th><td>{{kostenProJahr}} €</td></tr>
<tr><th>Anteil der Kommune bis Zieljahr</th><td>{{ kostenAnteilKommuneJahr }} €</td></tr>
</table>
<h2>Arbeitsplätze Klimaneutralität</h2>
<table class="table table-striped table-sm">
<tr><th>Arbeitsplätze gesamt</th><td>{{ arbeitsplGES }}</td></tr>
<tr><th>kommunale Arbeitsplätze</th><td>{{ arbeitsplAntKommune }}</td></tr>
</table>
</div>
</div>
{% endblock %}

47
backend/templates/backend/counties_upload.html

@ -0,0 +1,47 @@
{% extends 'base.html' %}
{% block content %}
{% if messages %}
{% for message in messages %}
<div class="alert alert-danger" role="alert">
<!-- | means OR operator-->
<strong>{{message|safe}}</strong>
</div>
{% endfor %}
{% else %}
<h2>CSV-Upload</h2>
<div class="alert alert-secondary" role="alert">
{{order}}
</div>
<div class="row">
<div class="col col-md-4">
<div class="card">
<div class="card-body">
<form class="form" action="" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<label for="file1"><h5>CSV hochladen</h5></label>
<input type="file" id="file1" name="file">
<br>
<small>Nur CSV-Dateien erlaubt</small>
<br>
<button class="btn btn-primary" type="submit">Upload</button>
</form>
</div>
</div>
</div>
</div>
{% endif %}
{% for profile in profiles %}
{{profile.name}}
{% endfor %}
{% endblock content %}

34
backend/templates/backend/details.html

@ -0,0 +1,34 @@
{% extends 'base.html' %}
{% load l10n %}
{% block content %}
<style>
.left {text-align:right;}
</style>
<h2>Gemeindeansicht</h2>
<div class="row">
<div class="col-md-6">
<table class="table table-bordered table-sm">
<tr><th>Gemeindekennzahl</th><th>Name</th><th>Bevölkerungszahl</th></tr>
<tr><td>{{ county.gemeindeId }}</td><td>{{ county.Name }}</td><td class="left">{{ county.Anzahl }}</td></tr>
{% if district %}
<tr><td>{{ district.gemeindeId }}</td><td>{{ district.Name }}</td><td class="left">{{ district.Anzahl }}</td></tr>
{% endif %}
<tr><td>{{ state.gemeindeId }}</td><td>{{ state.Name }}</td><td class="left">{{ state.Anzahl }}</td></tr>
<tr><td>{{ de.gemeindeId }}</td><td>{{ de.Name }}</td><td class="left"> {{ de.Anzahl|localize }} </td></tr>
</table>
<h3>Klimaneutral bis zum Jahr:</h3>
<form action="{% url 'output' %}" method="get">
<input type="hidden" name="county" value="{{ county.gemeindeId }}" >
<input name="targetyear" type="text" placeholder="Jahreszahl eingeben ..." required='required'><br><br>
</form>
</div>
</div>
{% endblock %}

36
backend/templates/backend/factsheet.html

@ -0,0 +1,36 @@
<!-- population/templates/opulation/factsheet.html -->
{% extends 'base.html' %}
{% block content %}
<h2>Faktentabelle</h2>
<table class="table table-striped table-bordered table-sm">
<thead>
<tr><th>code</th><th>Faktum</th><th>Wert</th><th>Grund</th><th>Quelle</th><th>Link</th></tr>
</thead>
<tbody>
{% for line in object_list %}
<tr><td>{{line.code}}</a></td>
<td>{{line.fact}}</td>
<td style="text-align:right">{{ line.value }}</td>
<td>
{% if line.reason %}
{{line.reason}}
{% endif %}
</td>
<td>
{% if line.source %}
{{line.source}}
{% endif %}
</td>
<td>
{% if line.link %}
<a href="{{line.link}}" target="_blank">Quelle</a>
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock content %}

50
backend/templates/backend/index.html

@ -0,0 +1,50 @@
<!-- adressen/templates/adressen/adress_home.html -->
{% extends 'base.html' %}
{% block content %}
<h2>Einwohnertabelle</h2>
<table class="table table-striped table-bordered table-sm">
<thead>
<tr><th>Gemeinde ID</th><th>Kennziffer</th><th>Name</th><th>Bevölkerungszahl</th><th>männlich</th><th>weiblich</th></tr>
</thead>
<tbody>
{% for line in object_list %}
<tr><td><a href="{% url 'details' %}{{ line.pk }}/">{{line.gemeindeId}}</a></td>
<td>{{line.kennziffer}}</td>
<td>{{ line.Name }}</td>
<td>{{line.Anzahl}}</td>
<td>{{line.männlich}}</td>
<td>{{line.weiblich}}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% if is_paginated %}
<nav>
<ul class="pagination">
{% if page_obj.has_previous %}
<li class="page-item" ><a class="page-link" href="?page={{ page_obj.previous_page_number }}">&laquo;</a></li>
{% else %}
<li class="page-item disabled"><span>&laquo;</span></li>
{% endif %}
{% for i in paginator.page_range %}
{% if page_obj.number == i %}
<li class="page-item active"><span class="page-link" >{{ i }} <span class="sr-only">(current)</span></span></li>
{% else %}
<li class="page-item" ><a class="page-link" href="?page={{ i }}">{{ i }}</a></li>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<li class="page-item" ><a class="page-link" href="?page={{ page_obj.next_page_number }}">&raquo;</a></li>
{% else %}
<li class="page-item disabled"><span>&raquo;</span></li>
{% endif %}
</ul>
</nav>
{% endif %}
{% endblock content %}

217
backend/templates/backend/output.html

@ -0,0 +1,217 @@
<!-- population/templates/population/output.html -->
{% extends 'base.html' %}
{% block content %}
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>
<script src="https://code.highcharts.com/modules/accessibility.js"></script>
<script src="https://code.highcharts.com/modules/series-label.js"></script>
<style>
td {text-align:right;}
.highcharts-figure, .highcharts-data-table table {
min-width: 320px;
max-width: 800px;
margin: 1em auto;
}
.highcharts-data-table table {
font-family: Verdana, sans-serif;
border-collapse: collapse;
border: 1px solid #EBEBEB;
margin: 10px auto;
text-align: center;
width: 100%;
max-width: 500px;
}
.highcharts-data-table caption {
padding: 1em 0;
font-size: 1.2em;
color: #555;
}
.highcharts-data-table th {
font-weight: 600;
padding: 0.5em;
}
.highcharts-data-table td, .highcharts-data-table th, .highcharts-data-table caption {
padding: 0.5em;
}
.highcharts-data-table thead tr, .highcharts-data-table tr:nth-child(even) {
background: #f8f8f8;
}
.highcharts-data-table tr:hover {
background: #f1f7ff;
}
input[type="number"] {
min-width: 50px;
}
</style>
<h1>Ausgabe</h1>
<div class="row">
<div class="col-md-6">
<h2>Angaben zur Kommune</h2>
<table class="table table-striped table-sm">
<tr><th>Name</th><td>{{ nameDistrict }}</sub></td></tr>
<tr><th>Einwohnerzahl</th><td>{{ populationDistrict }}</sub></td></tr>
</table>
<h2>Die wichtigen Zahlen</h2>
<table class="table table-striped">
<tr><th>Zieljahr der Klimaneutralität</th><td>{{ zieljahr }}</sub></td></tr>
<tr><th>Budget Kommune 2016</th><td>{{ budgetKommune2016 }} t</td></tr>
<tr><th>Verbleibendes Budget nach Zieljahr</th><td>{{ verbleibendesBudget }} t</td></tr>
<tr><th>THG-Bilanz 2018</th><td>{{ bilanzGES }} t</td></tr>
<tr><th>Gesamtkosten</th><td>{{ kostenGES }} €</td></tr>
<tr><th>Anteil der Kommune</th><td>{{ kostenAntKommune }} €</td></tr>
<tr><th>Gesamtkosten pro Jahr bis Zieljahr</th><td>{{kostenProJahr}} €</td></tr>
<tr><th>Anteil der Kommune bis Zieljahr</th><td>{{ kostenAnteilKommuneJahr }} €</td></tr>
<tr><th>Arbeitsplätze gesamt</th><td>{{ arbeitsplGES }}</td></tr>
<tr><th>kommunale Arbeitsplätze</th><td>{{ arbeitsplAntKommune }}</td></tr>
</table>
</div>
<div class="col-md-6">
<h2>CO<sub>2</sub> Emission 2018</h2>
<figure class="highcharts-figure">
<div id="container1"></div>
Gesamtmenge {{bilanzGES}} t
</figure>
<h3>Reduktionspfad CO<sub>2</sub>2 Emissionen 2016-2036</h3>
<figure class="highcharts-figure">
<div id="container2"></div>
</figure>
</div>
</div>
<div class="row">
</div>
<script>
Highcharts.chart('container1', {
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false,
type: 'pie'
},
title: {
text: 'CO2-Emission 2018'
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
},
accessibility: {
point: {
valueSuffix: '%'
}
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
format: '<b>{point.name}</b>: {point.percentage:.1f} %'
}
}
},
series: [{
name: 'Anteile',
colorByPoint: true,
data: [{
name: 'Energiewirtschaft',
y: {{ bilanzEW }},
//sliced: true,
//selected: true
}, {
name: 'Industrie',
y: {{ bilanzIN }}
}, {
name: 'Gebäude',
y: {{ bilanzGB }}
}, {
name: 'Verkehr',
y: {{ bilanzGB }}
}, {
name: 'Landwirtschaft',
y: {{ bilanzLW }}
}, {
name: 'Sonstige',
y: {{ bilanzSO }}
}, ]
}]
});
Highcharts.chart('container2', {
chart: {
type: 'spline'
},
title: {
text: 'Reduktionspfad der Kommune'
},
subtitle: {
text: '{{ nameDistrict }}'
},
xAxis: {
categories: [
{% for key,val in emissionKommuneData.items %}
'{{ key }}',
{% endfor %}
]
},
yAxis: {
title: {
text: 'Temperature'
},
labels: {
formatter: function () {
return this.value + '°';
}
}
},
tooltip: {
crosshairs: true,
shared: true
},
plotOptions: {
spline: {
marker: {
radius: 4,
lineColor: '#666666',
lineWidth: 1
}
}
},
series: [{
name: 'CO2-Budget',
marker: {
symbol: 'square'
},
data: [
{% for key,val in emissionKommuneData.items %}
{{ val }},
{% endfor %}
]
}]
});
</script>
{% endblock content %}

12
backend/templates/backend/search.html

@ -0,0 +1,12 @@
{% extends 'base.html' %}
{% block content %}
<h1></h1>Suche</h1>
<p>Suche Deine Stadt, Landkreis oder Kommune</p>
<form action="{% url 'search_results' %}" method="get">
<input name="q" type="text" placeholder="Suche..."><br><br>
</form>
{% endblock %}

19
backend/templates/backend/search_results.html

@ -0,0 +1,19 @@
{% extends 'base.html' %}
{% block content %}
<style>
.gemeinde-id {display:block; float:left; width: 7em;}
</style>
<h1>Suchergebnis</h1>
<ul>
{% for county in object_list %}
<li>
<span class="gemeinde-id">{{ county.gemeindeId }}</span>
<a href="{% url 'details' %}{{ county.pk }}/">{{ county.Name }}</a>
</li>
{% endfor %}
</ul>
{% endblock %}

26
backend/urls.py

@ -0,0 +1,26 @@
from django.urls import path
from .views import CountyHome
from .views import population_upload
from .views import SearchResultsView
from .views import search
from .views import details
from .views import FactsheetHome
from .views import output
from .views import showcalculation
urlpatterns = [
path('', CountyHome.as_view(), name='population_start'),
path('upload/', population_upload, name='population_upload'),
path('search_results/', SearchResultsView.as_view(), name='search_results'),
path('search/', search , name='search'),
path('details/', details, name='details'),
path('calculate/', showcalculation, name='calculate'),
path('details/<int:id>/', details, name='details'),
path('factsheet/', FactsheetHome.as_view(), name='factsheet'),
path('output/', output, name='output'),
]

128
backend/views.py

@ -1,3 +1,131 @@
import io
import csv
import math
from django.contrib import messages
from django.http import Http404
from django.shortcuts import render
from django.views.generic import ListView
from django.db.models import Q
from .models import County
from .models import Factsheet
from .models import Calc
#from django.urls import reverse_lazy
# Create your views here.
class CountyHome(ListView):
model = County
template_name ="backend/index.html"
#fields = '__all__'
#context_object_name = 'users' # Default: object_list
paginate_by = 500
#queryset = User.objects.all() # Default: Model.objects.all()
class SearchResultsView(ListView):
model = County
template_name = 'backend/search_results.html'
def get_queryset(self): # new
query = self.request.GET.get('q')
object_list = County.objects.filter(
Q(Name__icontains=query) | Q(gemeindeId__icontains=query)
)
return object_list
class FactsheetHome(ListView):
model = Factsheet
template_name="backend/factsheet.html"
def showcalculation(request):
template="backend/calculate.html"
c = Calc()
return render(request, template, c.calculate())
def output(request):
template="backend/output.html"
try:
gemeindeId = request.GET.get('county')
targetyear = int(request.GET.get('targetyear'))
if targetyear>2021 and targetyear<2040:
c=Calc(gemeindeId,targetyear)
return render(request, template, c.calculate())
else:
c=Calc()
return render(request, template, c.calculate())
except:
c=Calc()
return render(request, template, c.calculate())
def search(request):
template="backend/search.html"
return render(request, template)
def details(request,id):
template="backend/details.html"
try:
county = County.objects.get(pk=id)
kennziffer = county.gemeindeId
print(kennziffer)
if len(kennziffer)>5 and kennziffer[:2]!='11': # Ausnahme Berlin 11 - da gibt es keine Zwischenebene
kennziffer5 = kennziffer[:5]
district = County.objects.get(gemeindeId=kennziffer5)
else:
district = None
kennziffer2 = kennziffer[:2]
state = County.objects.get(gemeindeId=kennziffer2)
de = County.objects.get(gemeindeId='DG')
except County.DoesNotExist:
raise Http404("Gemeinde nicht gefunden")
context = {'county':county, 'district':district, 'state':state, 'de':de}
return render(request, template, context)
# Create your views here.# one parameter named request
def population_upload(request): # declaring template
template = "backend/counties_upload.html"
data = County.objects.all()# prompt is a context variable that can have different values depending on their context
prompt = {
'order': 'Reihenfolge der CSV sollte seien: gemeindeId, Name, Anzahl, männlich, weiblich',
'profiles': data
}
# GET request returns the value of the data with the specified key.
if request.method == "GET":
return render(request, template, prompt)
csv_file = request.FILES['file']
if not csv_file.name.endswith('.csv'):
messages.error(request, 'Das ist keine CSV-Datei')
data_set = csv_file.read().decode('UTF-8')
io_string = io.StringIO(data_set)
next(io_string)
line_count=0
County.objects.all().delete()
for column in csv.reader(io_string, delimiter=';', quotechar="|"):
print (column)
if line_count>8:
id = County.mkFull(column[0])
name = column[1].lstrip()
_, created = County.objects.update_or_create(
gemeindeId=column[0],
kennziffer=id,
Name=name,
Anzahl=County.mkInt(column[2]),
männlich=County.mkInt(column[3]),
weiblich=County.mkInt(column[4])
)
line_count += 1
context = {}
return render(request, template, context)

6
co2budget/settings.py

@ -9,7 +9,7 @@ https://docs.djangoproject.com/en/3.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""
import os
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
@ -37,6 +37,8 @@ INSTALLED_APPS = [
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'backend',
'frontend'
]
MIDDLEWARE = [
@ -54,7 +56,7 @@ ROOT_URLCONF = 'co2budget.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [

4
co2budget/urls.py

@ -14,8 +14,10 @@ Including another URLconf
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('backend/',include('backend.urls')),
path('',include('frontend.urls'))
]

BIN
db.sqlite3

Binary file not shown.

43
docker-compose.yml

@ -0,0 +1,43 @@
# docker run -d -p 8000:8000 --name django django:1
version: '3.7'
services:
# db:
# image: "postgres:11"
# environment:
# - "POSTGRES_HOST_AUTH_METHOD=trust"
# volumes:
# - postgres_data:/var/lib/postgresql/data/
co2-budget:
image: co2-budget
# volumes:
# - /var/docker/django/co2-budget/code/:/code/
# depends_on:
# - db
container_name: co2-budget
labels:
- "traefik.enable=true"
- "traefik.http.routers.co2-budget.entrypoints=http"
- "traefik.http.routers.co2-budget.rule=Host(`co2-budget.hpadm.de`)"
- "traefik.http.middlewares.co2-budget-https-redirect.redirectscheme.scheme=https"
- "traefik.http.routers.co2-budget.middlewares=co2-budget-https-redirect"
- "traefik.http.routers.co2-budget-secure.entrypoints=https"
- "traefik.http.routers.co2-budget-secure.rule=Host(`co2-budget.hpadm.de`)"
- "traefik.http.routers.co2-budget-secure.tls=true"
- "traefik.http.routers.co2-budget-secure.tls.certresolver=http"
- "traefik.http.routers.co2-budget-secure.service=co2-budget"
- "traefik.http.services.co2-budget.loadbalancer.server.port=8000"
- "traefik.docker.network=proxy"
networks:
- default
- proxy
restart: always
#volumes:
# postgres_data:
networks:
proxy:
external: true

0
frontend/__init__.py

3
frontend/admin.py

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

5
frontend/apps.py

@ -0,0 +1,5 @@
from django.apps import AppConfig
class FrontendConfig(AppConfig):
name = 'frontend'

0
frontend/migrations/__init__.py

3
frontend/models.py

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

8
frontend/templates/frontend/index.html

@ -0,0 +1,8 @@
{% extends 'frontend.html' %}
{% block content %}
<h1>CO2-Budget ihrer Kommune</h1>
{% endblock content %}

3
frontend/tests.py

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

8
frontend/urls.py

@ -0,0 +1,8 @@
from django.urls import path
from .views import index
urlpatterns = [
path('', index, name='forntend_index'),
]

7
frontend/views.py

@ -0,0 +1,7 @@
from django.shortcuts import render
# Create your views here.
def index(request):
template="frontend/index.html"
return render(request, template)

2
requirements.txt

@ -0,0 +1,2 @@
django
django-tinymce

57
templates/base.html

@ -0,0 +1,57 @@
<!DOCTYPE html>
<html>
<meta charset="UTF-8">
{% load static %}
<head>
<title>knut</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
<link rel="stylesheet" href="{% static 'population/style.css' %}">
</head>
<body>
<div class="container">
<header>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<a class="navbar-brand" href="/">Home</a>
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="/knut">Eingabeformular</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/population/search/">Suche</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/population/output/">Ausgabe</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/population/calculate/">Berechnung</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/population/factsheet/">Daten</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/population">Einwohnertabelle</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/population/upload/">Upload</a>
</li>
</ul>
</nav>
</header>
<!--
<div class="jumbotron">
<h1>Django Adressverwaltung</h1>
</div>
-->
{% block content %}
{% endblock content %}
</div>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
</body>
</html>

43
templates/frontend.html

@ -0,0 +1,43 @@
<!DOCTYPE html>
<html>
<meta charset="UTF-8">
{% load static %}
<head>
<title>knut</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
<link rel="stylesheet" href="{% static 'population/style.css' %}">
</head>
<body>
<div class="container">
<header>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<a class="navbar-brand" href="/">Home</a>
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="/eingabe">Eingabeformular</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/backend/">Backend</a>
</li>
</ul>
</nav>
</header>
<!--
<div class="jumbotron">
<h1>Django Adressverwaltung</h1>
</div>
-->
{% block content %}
{% endblock content %}
</div>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
</body>
</html>
Loading…
Cancel
Save