How to convert Django datetime to HighCharts format - django

I am trying to display datetime on a chart but having some trouble as it seems to be putting out an odd format to the chart.
Django template:
var employeeLastLoginWithPoints = [
{% for employee in employee_list %}
[{{ employee.last_login|date:"SHORT_DATE_FORMAT" }}, {{ employee.points }}]
{% if not forloop.last %}, {% endif %}{% endfor %}
];
Highcharts snippet:
xAxis: {
type: "datetime",
dateTimeLabelFormats: {
day: '%b/%d/%Y'
},
What the chart looks like:
All the date data is pushed to the left at Jan/01/1970
Sources/Index of the DOM:
var employeeLastLoginWithPoints = [
[01/11/2020, 0]
,
[01/07/2020, 3]
,
[01/11/2020, 14]
,
[01/08/2020, 8]
];
console.log(employeeLastLoginWithPoints):
(4) [Array(2), Array(2), Array(2), Array(2)]
0: (2) [0.000045004500450045, 0]
1: (2) [0.00007072135785007072, 3]
2: (2) [0.000045004500450045, 14]
3: (2) [0.00006188118811881188, 8]
Why is the console log showing up different and how do I get it to display correctly?
Thanks in advance!

You need to wrap the rendered date in quotes,
["{{ employee.last_login|date:"SHORT_DATE_FORMAT" }}", {{ employee.points }}]
At the moment you are rendering 01/11/2020 as one of the values, this is being interpreted as the calculation 01 / 11 / 2020 which returns a float

Related

grouped bar dataset in djanog template loop

i would like to draw a grouped bar from chart js using django which currently works fine. Actually i do manually. but i need to draw a grouped bar through for loop(data,label,background-color).
Actual
label_list=[2019,2020]
color_list=['#e75e72','#3396ff']
data_list=[[282,314,106,502,107,111],[86,350,411,340,635,809]]
datasets: [
{
label: {{label_list.0|safe}},
backgroundColor: '{{color_list.0|safe}}',
data:{{data_list.0|safe}} ,
},
{
label: {{label_list.1|safe}},
backgroundColor: '{{color_list.1|safe}}',
data: {{data_list.1|safe}} ,
},
]
i really do not have any idea to make it dynamically.
i need something like
{% for x in label_list %}
{{
label:label_list.forloop.counter[x],
background-color:color_list.forloop.counter[x],
data:data_list.forloop.counter[x]
}}//forloop.counter0,forloop.counter1
{% endfor %}
thanx in advance.
Rather than processing the values in template, why not you build the dataset in view and send it as context, like this:
datasets = list()
for label, color, data in zip(label_list, color_list, data_list):
value_dict = {
'label': label,
'backgroundColor' : color,
'data': data
}
datasets.append(value_dict)
context = {'datasets': json.dumps(datasets)} # use 'import json' on top of the file
return render('template.html', context)
Then use it directly in your template like this:
<script>
var dataset = {{ datasets }}
// rest of the code
</script>

How to access custom fields in a data.home._children loop

I've added a few custom fields in lib/modules/apostrophe-custom-pages/index.js
Specifically, I've added a field for an excerpt, image Singleton for a cover image and another Singleton that will allow users to define an icon to a page.
This is how these images are defined:
{
name: 'icon',
label: 'Icon',
type: 'singleton',
widgetType: 'apostrophe-images',
options:{
limit: 1,
minSize: [200,200],
aspectRatio: [1,1],
},
controls:{
movable:false
},
image: true
},
{
name: 'coverimg',
label: 'Header Image',
type: 'singleton',
widgetType: 'apostrophe-images',
options:{
limit: 1,
minSize: [400,400],
aspectRatio: [1,1],
},
controls:{
movable:false
},
image: true
}
The cover image and the icon I can retrieve while on the page by using: {% set coverimage = apos.images.first(data.page.coverimg) or null %} ,
however, I can't reach the icon in the navigation under data.home._children like so:
{%- for tab in data.home._children -%}
{%- set iconImg = apos.images.all(tab.icon) %}
{{ apos.log(tab) }}
<li>
<a class="sidebar-main-link
{% if data.page and (apos.pages.isAncestorOf(tab, data.page) or tab._id == data.page._id) %}
active
{% endif %}
" href="{{ tab.slug }}">
<div class="icon" style="
backgroung-image: url('{{ apos.attachments.url(image.attachment, { size: 'full'}) }}') "></div>
{{ tab.title }}
</a>
</li>
{% endfor %}
This returns the standard missing.svg image
There is a new tutorial on the apostrophe documentation site which covers this issue in detail.
The short answer is that you must configure the filters option of the apostrophe-pages module to load related pages with more than just very basic information. For performance reasons, this default behavior is not a bad thing, but asking Apostrophe to load just one widget won't slow things down too much.
// in lib/modules/apostrophe-pages/index.js
module.exports = {
// other configuration, then...
filters: {
ancestors: {
children: {
areas: [ 'thumbnail' ]
}
}
}
};

Ansible and Template file inserting values in to the template

I'm having issues at the moment I have a map of some aws subnets with their routing tables like so ( example output via ansible) :
"subnetwork_route_map": [
{
"route_table_id": "rtb-xxxxxx",
"subnet_id": "subnet-xxxxxx"
},
{
"route_table_id": "rtb-xxxxxx",
"subnet_id": "subnet-xxxxxxx"
},
{
"route_table_id": "rtb-xxxxxx",
"subnet_id": "subnet-xxxxxx"
}
]
I wish to insert these values in to a template file and I thought I could do something like this after seeing a few example:
{% for item in subnetwork_route_map %}
{{ item[1]['subnet_id'] }},{{ item[1]['route_table_id'] }}§
{% endfor %}
how ever i'm getting an error when I try this I get an error from ansible:
fatal: [localhost]: FAILED! => {"failed": true, "msg": "dict object has no element 1"}
subnetwork_route_map is a list of dictionaries, item is an individual dictionary, you don't need the [1] part:
{% for item in subnetwork_route_map %}
{{ item['subnet_id'] }},{{ item['route_table_id'] }}§
{% endfor %}

regex to update symfony render tag signature after update

After symfony update the render tag signature has changed (https://github.com/symfony/symfony/blob/master/UPGRADE-2.2.md):
Before:
{% render 'BlogBundle:Post:list' with { 'limit': 2 }, { 'alt': 'BlogBundle:Post:error' } %}
After:
{% render controller('BlogBundle:Post:list', { 'limit': 2 }), { 'alt': 'BlogBundle:Post:error' } %}
{# Or: #}
{{ render(controller('BlogBundle:Post:list', { 'limit': 2 }), { 'alt': 'BlogBundle:Post:error'}) }}
I'm looking for a way to modify my calls automatically using some regular expression.
Can anyone help?
replace:
\{% render ([^,%]*?), ([^%]*?)%\}
with:
{% render controller(\1), \2%}

Parsing JSON in Django template

I want to populate the data that Im getting from Webservice - JSON response in Template.
My Service integration code:
serviceRequest = requests.get(ServiceSettings.getCitiesURL(),
headers={
"Content-Type":servicesettings.JSON_CONTENT_TYPE,
"Accept":servicesettings.HEADER_ACCEPT
})
dataJson = serviceRequest.json ()
Response that Im getting is
{"cities": [{"latitude": "21.321", "cityIdentifier": "GOOD", "cityName": "NY", "longitude": "23.23432"} , {"latitude": "1.321", "cityIdentifier": "GOOD", "cityName": "CA", "longitude": "3.23432"}
], "statusMessage": "OK", "statusCode": 200}
I'm trying to iterate it in DJango Tempalte (HTML) like below but am not not able to list the cityName
{% for objCities in cityList%}
{% for objCity in objCities.citiess%}
{{objCity.cityName}}
{% endfor%}
{% endfor%}
In your loop, you were trying to access a string with an index. So TypeError: string indices must be integers, not str was raised.
Consider following:
{% for objCity in cityList.cities %}
{{objCity.cityName}}
{% endfor%}
This will work.