django update page when database field updates? - ajax

I have a results page and I am trying to work out how to auto update the page when an external database field is updated. I have seen quite a few examples but they seem to relate to PHP. I have a test that calls various APIs that can take up to an hour to complete. Once the test has completed, it will enter a success or failed message in a database field.
I already have my results page being rendered by django using template tags. I have a table and I have the field I would like to update. There are multiple fields that need update which correspond to each API test.
I have seen this site.. is this the kind of stuff to use? http://www.dajaxproject.com/ Is this an easy task to do? Does anyone have any ideas on how to do this?
Sorry but I don't know where to start on this one.
Cheers - Oli

I decided not to be so bold and just use the old classic page update for this purpose using javascript..
window.onload = setupRefresh;
function setupRefresh() {
setTimeout("refreshPage();", 30000); // milliseconds
}
function refreshPage() {
window.location = location.href;
}
Still open for options however I'm rather inexperienced in django and maybe this was too much to bite off too quickly..

Related

Laravel PDF generation with Graph and send it with Email

I tried to find it on StackOverflow and also tried to google it, but could not find any relevant answer.
I want to send monthly reports to the user of Laravel application with a PDF that contain a graph/chart.
This is what that is already done
Created a route, lets say
Route::get('/print/', 'PrintController#report');
In printController created a report function that is getting all the necessary data from the DB and returning a view with user data
return view::make('monthly_report', $user_data);
In monthly_report view, get all the user data, show the view and create a chart with the data. The chart is created with Charts.js. it is in a canvas.
Send the generated chart as image to the server with Ajax. For example
var canvas = document.getElementById("myChart"), // Get your canvas
imgdata = canvas.toDataURL();
file_name = "<?php echo $chart_file_name; ?>"; //created with userId and date
//send it to server
$.ajax({
method: "POST",
url: "save",
data: {
data: imgdata,
file_name: file_name,
_token: token,
}
});
Created a save route
Route::post('print/save', 'PrintController#saveChart');
In print controller, saveChart function, save the chart
$data = base64_decode($data);
//save chart on server
file_put_contents("Charts/".$fileName, $data);
Then create a PDF report by using another view monthly_report2, that is also in saveChart function. The view monthly_report2 does not use any JavaScript and use the chart image that was generated by monthly_report, in number 6.
$pdf = \PDF::loadView('monthly_report2', $cll_data);
file_put_contents("reports/".$pdf_name, $pdf->output());
It save the generated PDF on server. So far so good.
To send these PDF reports to the users by e-mail, I will created a schedule/crone job that will be run on a specific date, monthly and will send the e-mails with PDF reports as attachments.
I skip some details for clarity, please ask, if you need more information.
Thank you for reading so far, now I have two questions
The way I am doing is good or it can be improve?
We want all this process automatically (generating reports and sending by email). To generate the PDF's, monthly_report view must be loaded? so that it generate the Chart and send it to the server. Can we schedule it also, so that it generate the pdf reports automatically?, If no, is there any other way to do it?
Kind of a big question, but I'll try to answer
I think it's good. I'm not a big fan of using JavaScript to create charts, but that's me. You obviously know what you're doing and PDF generation is in my experience a "If it works, please don't break it" functionality.
I think this might be more difficult. Since you're using JavaScript to create charts, you need some kind of engine (NodeJS comes to mind) to parse the JavaScript and actually create your charts without opening a browser and doing it manually. (This is why I don't like using JavaScript to create charts). You could take a look at tutorials like this one to get an idea of how to render your charts serverside.
After that, you can take a look at the Laravel task scheduler (provided you're on Laravel 5, a community package exists for Laravel 4). You can schedule existing and custom-made commands to be executed. In pseudo-code, a PDF generation command could look like this:
public function createAndSendCharts() {
// 1. Get necessary user data
// 2. Create your charts
// 3. Save your charts
// 4. Create email with charts
// 5. Send your email
}
You could then add that function to your Task Scheduler
$schedule->command('send:charts')
->monthly();
Hope this was of some help. All in all, you're doing fine, but the choice for ChartJS has some consequences if you want to automate the whole process. Nothing really special, tons of tutorials exist for this situation :)

Accessing dynamic links in the format of domain.com/<dynamic_page_name> in CodeIgniter

I am using code Igniter for my PHP project. I want to give provision in my site such that users can create new pages of their own, and access them directly from domain.com/their_page_name.
But, my developers have raised a concern that, 1000's of dynamic links that are presented in the format of domain.com/ is "not good for site's performance". For some 10-15 pages, it is fine. But, beyond that, it would effect the site's performance.
So, they proposed that the URL format should be like www.domain.com/something/page_name (here, 'something' is the controller name, as they mentioned it)
But, I really can't sacrifice my framework nor my requirement.
Is there any way that I can achieve the format of "www.domain.com/page_name" without effecting the site's performance?
Thanks in advance.
No issues on
Www.domain.com\userpagename.
It's not a framework issues. Codeigniter support this type of URL.you can create n no of URL.
Performance will matter how you are handling that particular controller or that particular function.
If may be 10 may be 100 ,work around same way.
You just have to put route accordingly.
$route[default_controller]=userurl;
$route[userurl/(:any)]=userurl yourfunction/$1`;
What it seems you need is dynamic controller, which can be done using Codeigniter's build in function _remap().
A code example is:
public function _remap($method){
if($method != null){
$this->yourFunction($method);
} else {
// handle the error as you like
}
}
public function yourFunction($key){
// your code logic here
}
All this code block goes inside your controller.
Edit: the performance is exactlu the same as going with domain.com/controller/method. What it matters, as stated above, is how you handle the data.

How to make wp plugin auto-refreshing on the public side of view (auto load page)

First have to say that almost every time when searching on technical solutions.. search engine will direct me to this site here. I admit that i have learned such A LOT here just by searching through for answers..
but also have to say that not being a coder.. so don't understand much here at all ;)
Ok, the Problem:
Want to update wordpress tables or calendar within wp admin. These changes should become updated on the public side automatically and more or less instantly (~ few seconds is perfectly fine).
I found solutions about manipulating header.php, i found some ajax plugins I found -too much to count- info on how to implement ajax into wp
Sadly.. none of these could help me through as the plugins mostly either not work, are outdated or require countless file edits. Manipulating header.php seems odd.. as i don't want to loop-refresh the whole page, post etc.. but only get e.g. table or calendar data updated.
So maybe any of you coders know of something to e.g. get the plugin "All-in-One Event Calendar by Timely" to update the public visible part of the calendar once data edited in wp admin?
Greatly appreciate pointers in any direction to solve this.
P.S.: What would you think of plugins like "ajaxify everything" ? don't know if this really the way to go..
Again, thanks for your help, time and contribution!!
update
additional question:
I now found such plugin to let me chose which parts i want to "ajaxify".
Such plugin would ask me for a specific function name.
Now, where do i start to gather this information?
Below is the instruction from the plugin context inside wordpress admin
Generate Ajaxized DIV for your function
Ajaxize will allow you to ajaxize almost any php function on your
site.
It can be a plugin, a function you wrote, or even a core wordpress
function.
There are some (obvious or less obvious) limitations currently:
Functions must return valid HTML - this will be called in php and returned via the
Ajax call
Functions cannot accept any parameters (at least at the moment)
Enter a function name below.
Function Name: xxx input field xxx what put in here? xxx
The generated div can be inserted to any page/post on the site and
will ajaxize the call to the function automatically.
Please make sure you enter a valid function name, that the function
does not require any parameters, and that it returns valid HTML.
In your context you want to refresh the calendar automaticaly when there is a change in the WP_Admin;
but you can't exactly do this.
You can write a Js method which will check the time of lastest modification of the calendar at the load of the page, stock it in Var.
After you'll have a constant (each 10seconds) Js "daemon" which will get the time of lastest modification of the calendar and compare it to the stocked Var; If change you reload the calendar.
PS: Ajaxify everything is not really a solution due of the developpement you need To Ajaxify and to code for people who has disabled Javascript.
Hope it helps

Load single post using Ajax for WPML based site

I want to load single posts into an index page using Ajax for a WPML based site that I'm working on. Not sure what I'm missing but there seems to be a catch when using multiple languages and fetching the correct posts.
Here's the code I wrote -
function loadProject(reference) {
console.log(window.location.origin + '/references/' + reference);
// Displays "http://mysite.com/references/example-post/", which exists
$('#content').load(window.location.origin + '/references/' + reference, function() {
console.log('Load was performed.');
});
}
I don't reach the JS log after load() but I don't receive any error message either (e.g. "File not found").
"References" is a custom post type that I've created using the Types plugin, and has a file of it's own called single-references.php. I'm not entirely sure how CPTs work with WPML, and that's probably where the problem lies. I have two languages, Swedish and English, and the CPT only has one slug for its kind ("references").
[Solved]
Used the .get() method instead to retrieve the post and now it works.
Well, no console error, no network query :
it is not a WPML issue
did you check #content really exists ?
In chrome dev tool, go to Network, and trigger your function. You'll see what you receive.

Forcing JQuery Datatable to load data via AJAX upon selection of a date range

I have the wonderful jquery data tables currently loading data using ajax.
The code is pretty much the same as the example I took it from which can be found at: http://www.datatables.net/release-datatables/examples/server_side/pipeline.html
I found this the best example because it incoporates pagination, sorting and also the search box into the ajax requests. The search box allows you to type in key words and this triggers the ajax function which includes the search value as a $_GET var to the server script.
This is a small preview of my table with table tools etc. loaded.
http://img828.imageshack.us/img828/9778/previewxjh.png
As you can see the main focus here is the filament groups date range plugin which I have added. I have finished with this now, and have a fail safe for the duplicate event firing problem etc. its ready to go and just needs including in the ajax pipeline - which is where I have been stuck for the last day or so.
fnDataTablesPipeline appears to be just an interim and doesnt reference the search box at all, so I can't figure out how the search box is working, and I am unsure if this is the right place to go including my date range value (everything I have tried just leads me to a dead end)
I want to use my onChange event for the date range filter, and apply it exactly the same way that the search box works. When the value is changed, simply pass it as a get variable so that my php script can deal with it there. I have concerns regarding the paging, and I will probably need to just reset back to page one after the date is changed (not sure how I am going to deal with this just yet, but thats the next step)
I need help telling datatables to refresh from the ajax source, and include the date range as a get parameter to the server side script (like when the search box value changes)
- sounds straight forward, but this is where I am breaking down and not making good use of my time due to the lack of understanding.
Is there anyone that has implemented similar that can help me?
The biggest problem right now is how to force the refresh from my date range onChange event, and of course include the single string value which contains my dates (which I know how to cover server side)
Many Thanks,
Chris
EDIT: I actually managed to get this working before I finished for the day. I'll post my code when I get back in the office tomorrow, it was actually surprisingly easy - I was tackling it completely wrong.
After changing date..
oTable.fnClearTable(0);
oTable.fnDraw();
Include var in pipeline
function fnDataTablesPipeline ( sSource, aoData, fnCallback, dateRange ) {
aoData.push( { "name": "dateRange", "value": $('#dateRangePicker').val(), } );
...
Obtain var in php script
$_GET['dateRange']

Resources