Ajout export pdf des heures supplémetnaires
This commit is contained in:
@@ -2,6 +2,7 @@ from django.contrib import admin
|
||||
from rangefilter.filters import DateRangeFilter
|
||||
from django.contrib.auth.models import User
|
||||
from django.db.models import Q
|
||||
from django.shortcuts import render
|
||||
|
||||
from collabs.models import *
|
||||
|
||||
@@ -37,7 +38,6 @@ class Collabs_hour_Admin(admin.ModelAdmin):
|
||||
list_editable = ['bNoticed']
|
||||
list_filter = [('dtDate', DateRangeFilter), ('user', admin.RelatedOnlyFieldListFilter),'sBases','type']
|
||||
|
||||
|
||||
search_fields = ['userName']
|
||||
|
||||
readonly_fields = ["userName"]
|
||||
|
15
collabs/list_pdf_export.py
Normal file
15
collabs/list_pdf_export.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from django.http import HttpResponse
|
||||
from django.template.loader import get_template
|
||||
from xhtml2pdf import pisa
|
||||
|
||||
|
||||
def render_to_pdf(template_src, context_dict={}):
|
||||
template = get_template(template_src)
|
||||
html = template.render(context_dict)
|
||||
response = HttpResponse(content_type='application/pdf')
|
||||
pdf_status = pisa.CreatePDF(html, dest=response)
|
||||
|
||||
if pdf_status.err:
|
||||
return HttpResponse('Some errors were encountered <pre>' + html + '</pre>')
|
||||
|
||||
return response
|
@@ -10,6 +10,11 @@ from django.contrib import admin
|
||||
|
||||
#test
|
||||
|
||||
BASES_CHOICES = [
|
||||
('1', 'Monthey'),
|
||||
('2', 'Uvrier'),
|
||||
]
|
||||
|
||||
class Collabs_hour_types(models.Model):
|
||||
|
||||
|
||||
@@ -27,10 +32,7 @@ class Collabs_hour_types(models.Model):
|
||||
|
||||
class Collabs_hour(models.Model):
|
||||
|
||||
BASES_CHOICES = [
|
||||
('1', 'Monthey'),
|
||||
('2', 'Uvrier'),
|
||||
]
|
||||
|
||||
|
||||
userName = models.CharField("Auteur", max_length=100)
|
||||
user = models.ForeignKey(settings.AUTH_USER_MODEL, limit_choices_to={'groups__name': "Intervenants"}, verbose_name="Collaborateur", on_delete=models.DO_NOTHING)
|
||||
@@ -50,18 +52,17 @@ class Collabs_hour(models.Model):
|
||||
def get_total_hour_by_user(self):
|
||||
#objs = Collabs_hour.objects.filter(user_id=self.user_id,dtCreated__year=self.dtCreated.year, dtCreated__month=self.dtCreated.month)
|
||||
objs = Collabs_hour.objects.filter(user_id=self.user_id, dtDate__year=self.dtDate.year , dtDate__month=self.dtDate.month)
|
||||
print(f"user {self.user} dtCreated__year {self.dtDate.year} dtCreated__month {self.dtDate.month}")
|
||||
# print(f"user {self.user} dtCreated__year {self.dtDate.year} dtCreated__month {self.dtDate.month}")
|
||||
#print( Collabs_hour.objects.all())
|
||||
|
||||
total = 0.0
|
||||
|
||||
for object in objs:
|
||||
total += object.nHour + object.nMinutes/60
|
||||
print(f"hour = {object.nHour} minutes = {object.nMinutes} = {object.nMinutes/60}")
|
||||
# print(f"hour = {object.nHour} minutes = {object.nMinutes} = {object.nMinutes/60}")
|
||||
|
||||
|
||||
|
||||
print(total)
|
||||
print(objs)
|
||||
if total > 0.0:
|
||||
hours = int(total)
|
||||
minutes = (total*60) % 60
|
||||
@@ -74,6 +75,9 @@ class Collabs_hour(models.Model):
|
||||
|
||||
return ret_string
|
||||
|
||||
def get_bases_name(self):
|
||||
return BASES_CHOICES[int(self.sBases)-1][1]
|
||||
|
||||
@admin.display(description='Présence de remarques', boolean=True)
|
||||
def has_remarques(self):
|
||||
ret = False
|
||||
|
58
collabs/templates/pdf_template.html
Normal file
58
collabs/templates/pdf_template.html
Normal file
@@ -0,0 +1,58 @@
|
||||
<html>
|
||||
<head>
|
||||
<style>
|
||||
@page{
|
||||
size: A4 landscape;
|
||||
margin: 2cm;
|
||||
}
|
||||
* {
|
||||
font-size: 15px;
|
||||
}
|
||||
|
||||
table {
|
||||
table-layout: fixed;
|
||||
width: 100%;
|
||||
border: 1px solid black;
|
||||
}
|
||||
|
||||
table th {
|
||||
text-align: left;
|
||||
padding: 3px;
|
||||
padding-bottom: 0;
|
||||
}
|
||||
|
||||
table td {
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
padding: 3px;
|
||||
padding-bottom: 0;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<table>
|
||||
<tr>
|
||||
<th>N°</th>
|
||||
<th>Date</th>
|
||||
<th>Heures</th>
|
||||
<th>Minutes</th>
|
||||
<th>Collaborateur</th>
|
||||
<th>Types</th>
|
||||
<th>Total du mois</th>
|
||||
</tr>
|
||||
{%for data in record%}
|
||||
<tr>
|
||||
<td style="width: 10%;"> {{forloop.counter}} </td>
|
||||
<td sttyle=""> {{data.dtDate}} </td>
|
||||
<td style=""> {{data.nHour}} </td>
|
||||
<td style=""> {{data.nMinutes}} </td>
|
||||
<td style=""> {{data.user}} ({{data.get_bases_name}}) </td>
|
||||
<td style=""> {{data.type}} </td>
|
||||
<td style=""> {{data.get_total_hour_by_user}} </td>
|
||||
</tr>
|
||||
{%endfor%}
|
||||
</table>
|
||||
</body>
|
||||
</html>
|
7
collabs/templatetags/replace_collabs.py
Normal file
7
collabs/templatetags/replace_collabs.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from django import template
|
||||
|
||||
register = template.Library()
|
||||
|
||||
@register.filter
|
||||
def replace_collabs(value):
|
||||
return value.replace("/admin/collabs/collabs_hour/","")
|
@@ -1,11 +1,13 @@
|
||||
from django.urls import path
|
||||
|
||||
|
||||
|
||||
from . import views
|
||||
|
||||
app_name = "collabs"
|
||||
urlpatterns = [
|
||||
path('export/<year>/<month>', views.ExportView.as_view(), name='detail'),
|
||||
#path('export/<year>/<month>', views.ExportView.as_view(), name='detail'),
|
||||
path('print_pdf', views.ExportPdfView, name='print_pdf'),
|
||||
|
||||
]
|
||||
|
||||
|
@@ -7,10 +7,19 @@ from django.urls import reverse
|
||||
from django.views import generic
|
||||
|
||||
from collabs.models import *
|
||||
from collabs.list_pdf_export import render_to_pdf
|
||||
from datetime import datetime
|
||||
|
||||
# Create your views here.
|
||||
|
||||
|
||||
def ExportPdfView(request):
|
||||
start = request.GET['dtDate__range__gte']
|
||||
template_name = "pdf_template.html"
|
||||
records = Collabs_hour.objects.filter(dtDate__range=[start,request.GET['dtDate__range__lte']])
|
||||
return render_to_pdf(template_name,{"record": records})
|
||||
|
||||
|
||||
class ExportView(generic.ListView):
|
||||
model = Collabs_hour
|
||||
template_name = 'collabs_hour/detail.html'
|
||||
|
Reference in New Issue
Block a user