Advertisement
amr_aly

Print_django-tables2

Aug 6th, 2021 (edited)
1,443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.71 KB | None | 0 0
  1. ############### views.py
  2. from .tables import VisitsTable
  3. from datetime import date
  4.  
  5. def calculate_year_income(request):
  6.     today = date.today()
  7.    
  8.     year_income = Visits.objects.filter(visitdate__year=today.year).order_by('-id')
  9.     year_table = VisitsTable(year_income, exclude='addpresent')
  10.     year_table.paginate(page=request.GET.get("page", 1), per_page=10)
  11.    
  12.     print_table = VisitsTable(year_income, exclude='addpresent, addlab, addrevis')
  13.     print_table.paginate(page=request.GET.get("page", 1), per_page=10)
  14.  
  15.     context = {
  16.         'year_table': year_table,
  17.         'print_table': print_table,
  18.     }
  19.     return render(request, 'reports/year_table.html', context)
  20.  
  21.  
  22. ############### year_table.html
  23.  
  24. {% extends 'index.html' %}
  25.  
  26. {% load render_table from django_tables2 %}
  27.  
  28.  
  29.  
  30. {% block content %}
  31.     <div>
  32.         <p class="float-left">
  33.             <h1><strong>Yearly Report</strong></h1>
  34.         </p>
  35.         <a type="button" class="btn btn-outline-primary" href="{% url 'reports:print_html' %}">
  36.             Print Preview
  37.         </a>
  38.         <button class="btn btn-outline-success" id="print" onclick="printData()">
  39.             Print
  40.         </button>
  41.  
  42.     </div>
  43.     <br><br>
  44.     <div class="container">
  45.         {% render_table year_table %}
  46.     </div>
  47.     <div id="pdata" hidden>
  48.         {% render_table print_table %}
  49.     </div>
  50. {% endblock %}
  51.  
  52.  
  53.  
  54. {% block scripts %}
  55.  
  56. <script type="text/javascript">
  57.  
  58.     function printData() {
  59.     var divToPrint = document.getElementById('pdata');
  60.     var htmlToPrint = '' +
  61.         '<style type="text/css">' +
  62.         'table th, table td {' +
  63.         'border:1px solid #000;' +
  64.         'padding:0.5em;' +
  65.         'width:100%;' +
  66.         'white-space:nowrap;' + // this line to make the cells flixable
  67.         '}' +
  68.         'p {' +
  69.         'text-align: right' +
  70.         'margin-bottom:0;' +
  71.         'padding:0;' +
  72.         'line-height:12px;' +
  73.         'font-size:10px;' +
  74.         '}' +
  75.         '#pdata {display: block;}' +
  76.         '@media print {' +
  77.         'nav { visibility: hidden; }' + // for remove pagination from    
  78.         // printing
  79.         '}' +
  80.         '</style>';
  81.     htmlToPrint += divToPrint.outerHTML;
  82.     newWin = window.open("");
  83.     newWin.document.write(htmlToPrint);
  84.     newWin.print();
  85.     newWin.close();
  86. }
  87.  
  88. </script>
  89. {% endblock  %}
  90.  
  91.  
  92. ################# tables.py
  93. import django_tables2 as tables
  94. from .models import Visits
  95.  
  96.  
  97. def render_footer(bound_column, table):
  98.     return sum(bound_column.accessor.resolve(row) for row in table.data)
  99.  
  100.  
  101. class VisitsTable(tables.Table):
  102.    
  103.     amount = tables.TemplateColumn(
  104.         '<a href="{% url \'visits:visits_patient_id\' record.id record.patient_id %}">{{ record.amount }}</a>',
  105.         verbose_name=u'Amount',
  106.         footer=render_footer)  
  107.  
  108.     id = tables.TemplateColumn(
  109.         '<a href="{% url \'visits:visits_patient_id\' record.id record.patient_id %}">{{ record.id }}</a>',
  110.         verbose_name=u'Visits ID',
  111.         template_name='django_tables2/bootstrap4.html',
  112.     )
  113.     patient = tables.TemplateColumn(
  114.         '<a href="{% url \'visits:visits_patient_id\' record.id record.patient_id %}">{{ record.patient }}</a>',
  115.         verbose_name=u'Patient Name',
  116.         template_name='django_tables2/bootstrap4.html',
  117.     )
  118.  
  119.     visitdate = tables.TemplateColumn(
  120.         '<a href="{% url \'visits:visits_patient_id\' record.id record.patient_id %}">{{ record.visitdate }}</a>',
  121.         verbose_name=u'Visit Date',
  122.     )
  123.  
  124.     diagnosis = tables.TemplateColumn(
  125.         '<a href="{% url \'visits:visits_patient_id\' record.id record.patient_id %}">{{ record.diagnosis }}</a>',
  126.         verbose_name=u'Daignosis',  #footer=len(tables.rows)
  127.     )
  128.  
  129.     addlab = tables.TemplateColumn(
  130.         '<a class="btn btn-outline-dark" href="{% url \'labs:add_lab_visit\' record.patient_id record.id %}">Add Analysis</a>',
  131.         verbose_name=u'Add Analysis',
  132.         footer="")
  133.  
  134.     addpresent = tables.TemplateColumn(
  135.         '<a class="btn btn-outline-primary" href="{% url \'presenthistory:save_present_hist\' record.patient_id record.id %}">Add Present History</a>',
  136.         verbose_name=u'Add Present History')
  137.    
  138.     addrevis = tables.TemplateColumn(
  139.         '<a class="btn btn-outline-dark" href="{% url \'revisits:save_revisit\' record.patient_id record.id %}">Add Revisit</a>',
  140.         verbose_name=u'Add Revisit')
  141.  
  142.     class Meta:
  143.         model = Visits
  144.         attrs = {"id": "visits-table",
  145.                 'class': 'table table-striped table-bordered table-hover'}
  146.         template_name = 'django_tables2/bootstrap4.html'
  147.         fields = ('id', 'patient', 'visitdate', 'diagnosis', 'amount',
  148.                 'addrevis', 'addlab')
  149.  
  150.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement