Skip to content

Commit

Permalink
issue #483, #485 and SAP date format fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
ayeshamk committed Sep 6, 2021
1 parent e2c72e3 commit a471d52
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 146 deletions.
12 changes: 11 additions & 1 deletion server_py/flatgov/bills/templatetags/bill_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from common import constants
from typing import Union
import re
from datetime import datetime


# BILL_NUMBER_REGEX = r'(?P<congress>[1-9][0-9]*)(?P<stage>[a-z]+)(?P<number>[0-9]+)(?P<version>[a-z]+)?$'
Expand Down Expand Up @@ -145,4 +146,13 @@ def normalize_score(score: float, total: float) -> int:
if not score:
return 0
else:
return round(score/total * 100)
return round(score/total * 100)


@register.filter
def custom_date(date):
try:
date = datetime.strptime(date, '%Y-%m-%d')
except:
pass
return date
24 changes: 24 additions & 0 deletions server_py/flatgov/home/propublica_api_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from django.http import JsonResponse
from django.views import View
import json
from django.conf import settings
import requests


class GetPressStatementsAPIView(View):
"""Getting the data from propublica and returning for datatable serverside rendering"""
def get(self, request, congress, bill_type, bill_number):
offset = request.GET['start']
url = f'https://api.propublica.org/congress/v1/{congress}/bills/{bill_type}{bill_number}/statements.json?offset={offset}'
propublica_api_key = getattr(settings, "PROPUBLICA_CONGRESS_API_KEY", None)
headers = {
'x-api-key': propublica_api_key
}
response = requests.get(url, headers=headers)
data = json.loads(response.content)
processed_data = {
"recordsTotal": data.get('num_results', 0),
"recordsFiltered": data.get('num_results', 0),
'data': data.get('results', [])
}
return JsonResponse(processed_data)
2 changes: 2 additions & 0 deletions server_py/flatgov/home/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.views.decorators.cache import never_cache

from . import views
from .propublica_api_views import GetPressStatementsAPIView

app_name = 'home'

Expand All @@ -20,6 +21,7 @@
path('home/bill-list/', never_cache(views.BillListAPIView.as_view()), name='bill-list'),
path('home/bill-titles/<congressnumber>', never_cache(views.BillListTitleAPIView.as_view()), name='bill-titles'),
path('home/bill-title/<bill>/', never_cache(views.GetBillTitleAPIView.as_view()), name='bill-title'),
path('press-statements/<congress>/<bill_type>/<bill_number>/', never_cache(GetPressStatementsAPIView.as_view()), name='press-statements'),
]

urlpatterns += api_urlpatterns
224 changes: 81 additions & 143 deletions server_py/flatgov/templates/bills/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ <h6 class="pt-3">
<td>{{ statement.bill_number }}</td>
<td style="width: 300px;">{{ statement.bill_title|truncatechars:120 }}</td>
<td>{{ statement.congress }}</td>
<td>{{ statement.date_issued }}</td>
<td>{{ statement.date_issued|custom_date|date:"M d, Y" }}</td>
<td>
{% if statement.get_permanent_pdf_link %}
<a href='{{ statement.get_permanent_pdf_link }}' target="_blank" class='btn btn-primary'>
Expand All @@ -697,8 +697,7 @@ <h6 class="pt-3">
</div>
<div class="tab-pane fade" id="press-statements" role="tabpanel" aria-labelledby="press-statements-tab">
<div class="">
<table v-if="pressStatements.length" class="table mt-3 h-50" id='press-statements-table' style="height: 50vh; overflow: scroll">

<table class="table mt-3 h-50" id='press-statements-table'>
<thead>
<tr>
<th scope="col">Title</th>
Expand All @@ -709,58 +708,8 @@ <h6 class="pt-3">
<th scope="col">Link</th>
</tr>
</thead>

<tbody>

<tr v-for='(pressStatement, index) in pressStatements' :key="index">
<td style="width: 550px;">$[ pressStatement.title ]</td>
<td style="width: 200px;">$[ pressStatement.name ]</td>
<td>$[ pressStatement.chamber ]</td>
<td>$[ pressStatement.party ]</td>
<td style="min-width:120px" >$[ pressStatement.date|formatDate ]</td>
<td style="min-width:300px"><a v-bind:href="pressStatement.url" target="_blank" class='btn btn-primary'>
View the press statement
</a></td>
</tr>
</tbody>
</table>
<table v-else class="table mt-3" id="press-statements-table">
<thead>
<tr>
<th scope="col">Title</th>
<th scope="col">Issued By</th>
<th scope="col">Chamber</th>
<th scope="col">Party</th>
<th scope="col">Date</th>
<th scope="col">Link</th>
</tr>
</thead>
<tbody class="" >

<tr class="text-center" style="text-align: center">
<td style='text-align: center;' colspan="6">No relevant press statements are available</td>

</tr>
</tbody>

</table>
<nav aria-label="..." class="d-flex justify-content-end dataTables_paginate">
<ul class="pagination">
<li @click="changePage(false)" class="page-item ">
<button class="page-link "><</button>
</li>
<li v-for="(page, index) in getPages"
:key="index"
@click="getPressStatements(page)"
v-bind:class="[(offset == page)? 'active page-item':'page-item']"><button class="page-link">$[ index+1 ]</button></li>
<li @click="changePage(true)" class="page-item">
<button class="page-link">></button>
</li>
</ul>
</nav>
</div>


</div>
<div class="tab-pane fade" id="cbo-score" role="tabpanel" aria-labelledby="cbo-score-tab">
<h6 class="pt-3">
Expand Down Expand Up @@ -824,19 +773,9 @@ <h6 class="pt-3">
</tr>

{% endfor %}
<!-- {% if not committees %}
<tr>
<td valign="top" colspan="5" class="empty_table">
{{ no_data_message }}
</td>
</tr>
{% endif %}-->
</tbody>
</table>
</div>
<!--div class="tab-pane fade" id="floor-proceedings" role="tabpanel" aria-labelledby="floor-proceedings-tab">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Laudantium magnam nisi reprehenderit animi iste rem ut corrupti, vel libero fuga dicta asperiores, consectetur error nam hic quasi vitae temporibus eaque?
</div-->
</div>
</div>
<hr>
Expand Down Expand Up @@ -979,86 +918,6 @@ <h6 class="pt-2">

{% block extra_js %}
<script src="{% static 'js/bill.js' %}"></script>
<script>
// var table = new Tabulator("#committee-jurisdiction-table", {
// // rowFormatter:function(row){
// });
</script>

<!-- vue.js files -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>



<script type="text/javascript">
Vue.http.headers.common['x-api-key'] = "{{propublica_api_key}}";
new Vue({
el: '#app',
components: {
},
delimiters: ['$[', ']'],
data: {
pressStatements: [],
errors: [],
offset: 0,
numResults: 0,
status: null,
height:0,
pages: [],

showModal: false,
},
created: function () {
this.getPressStatements(0)
},
computed: {
getPages() {
this.pages = []
var current_offset = 0
while (current_offset < this.numResults) {
this.pages.push(current_offset)
current_offset += 20
}
return this.pages;

}
},
methods: {
getPressStatements: async function (offset) {
this.offset = offset;
await this.$http.get(`https://api.propublica.org/congress/v1/{{bill.congress}}/bills/{{bill.type}}{{bill.number}}/statements.json?offset=${offset}`)
.then((response) => {
this.pressStatements = response.data.results;
this.numResults = response.data.num_results;
this.status = response.data.status;
})
.catch((err) => {
console.warn(err)
});
},
changePage(isIncrease) {
if (isIncrease) {
if (this.offset + 20 <= this.numResults) {
this.getPressStatements(this.offset+20)
}
} else {
if (this.offset - 20 >= 0) {
this.getPressStatements(this.offset-20)
}
}
}

}
});

Vue.filter('formatDate', function(value) {
if (value) {
return moment(String(value)).format('MMM DD, YYYY');
}
}
)
</script>

<script>
const showMoreText = $('.bill-summary-text').text();
Expand Down Expand Up @@ -1121,6 +980,70 @@ <h6 class="pt-2">
$(event.currentTarget).find('#tool').css('display', 'none')
})
</script>
<script>
var press_statements = $('#press-statements-table').DataTable({
"processing": true,
"serverSide": true,
"ajax": {
url: `/press-statements/{{bill.congress}}/{{bill.type}}/{{bill.number}}/`,

},
pageLength : 20,
lengthChange : false,
info : true,
searching : true,
bPaginate: true,
autoWidth : true,
scrollY: '50vh',
scrollCollapse: true,

columns: [
{
"data" : "title",
"orderable": true,
render: function ( data, type, row, meta ) {
title = `<div style="width: 550px;">${data}</div>`;
return title
}
},
{
"data" : "name",
"orderable": true
},
{
"data" : "chamber",
"orderable" : true,
},
{
"data" : "party",
"orderable": true
},
{
"data" : "date",
"orderable": true,
render: function (data) {
return moment(String(data)).format('MMM DD, YYYY')
}
},
{
"data" : "url",
"orderable": false,
render: function ( data, type, row, meta ) {
button = '<a role="button" target="_blank" class="btn btn-primary" href="'+data+'">View the press statement</a>';
return button
}
}
],
language: {
emptyTable: "No press statements are available",
paginate: {
"previous": "<",
"next": ">",
},
info: "_START_ to _END_ of _TOTAL_ Press statements"
},
});
</script>
<style>
div.dt-buttons {
float: none;
Expand All @@ -1132,6 +1055,21 @@ <h6 class="pt-2">
background-color: red;

}

#press-statements-table_paginate {
margin-top: 30px;
}

#press-statements-table_processing {
color: #2D4059;
}

.dataTables_scrollBody {
position: relative;
overflow: auto;
max-height: 50vh;
width: 100%;
}

.show-more-summary {
text-decoration: none;
Expand Down
4 changes: 2 additions & 2 deletions server_py/flatgov/uscongress/tasks.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
from typing import Optional
from server_py.flatgov.common.billdata import updateBillModelFields
from server_py.flatgov.common import constants
from common.billdata import updateBillModelFields
from common import constants
import subprocess
import shutil
from celery import shared_task, current_app
Expand Down

0 comments on commit a471d52

Please sign in to comment.