Reloading HTML table in Django when db changes - html

So very briefly, I have an html table implemented with a Django template that displays some information that I have stored in my database. I want this (html) table to stay current and to automatically update if and when the database changes. I have looked around, but haven't found much literature on the subject. Would the best way of doing this be making an Ajax call every so often to update the table? If so, what would a standard amount of time between calls be?
Thanks a lot, and let me know if there is any more information that is needed.

Basically the solution you're proposing yourself is called long polling. Django can't do this out of the box, but there are several add-on solutions, i.e. here. It wouldn't be too hard to whip it up yourself with some clever $.ajax calls on the client side.
A more thorough solution would not be to use long polling directly, but to only use it as a fall back for other server-side initiated events, like WebSockets and Server-Sent Events (SSE). These are however are harder to implement, because the WSGI specs don't offer these. You will have to have either a webserver that handles this outside of WSGI (Tornado comes to mind) or have another webserver (probably node.js) or an event handler like gevent that handles the messages the server sends through WebSockets/SSE.
Socket.io provides a fall-back from WebSockets to long polling and you would need something like this to get things done on the Django side.
There's a rather good blog post about socket.io, django and gevent here.

You should probably use django signals:
https://docs.djangoproject.com/en/1.7/ref/signals/
So the template can be updated each time the db change.
You can use for example post_save signal sent each time the save() method is called.
A simpler approach is to refresh the template at a choosen rate though it's not optimized.

Related

How would I structure a single page application that takes an input, fetches data from the back end, then renders it to the front end with routers?

Example: https://redditmetis.com/
Issue
I've been having trouble trying to structure a recent SPA I started. Like the above example, I need to accept an input, make a few API calls in the back-end, manipulate the data then render it the front-end. I'm currently going for a Django + React stack, since I'm pretty familiar with them. I can't really imagine how this would look like from a surface view, I've worked with API's before but I can't wrap my head around how the client and the server would interact with each other to make it all connect.
What I have so far
After looking into it, I think I need React Routers, similar to the example website provided. In my Django server, I plan on making separate API calls and running an algorithm to organize and sift through the received response, then pushing the product to the client. I'm still figuring out how to set that up, since most API calls are made on componentdidmount which only executes at the start of the DOM. This isn't much, but its a start.
If anyone has pointers on how to start, I'd appreciate it, thanks.
Each class object you make can have a componentdidmount method.
You can also use the fetch method within objects that update dynamically during state changes.

Sending functions rather than data

Nowadays, we always think like "send your data to a server, it computes it for you, then send you back the response".
But imagine something else : i want my client to compute the data itself.
The question is : is there something like a universal protocol to send actions rather than data through http ? So that the server can send the action to the client, whatever system it uses. If it does not exist, what are the technical difficulties you can face creating this kind of system ?
I'm talking about "static" actions, like mathematical functions for example.
You're unfortunately going to run into a problem pretty quick because, technically speaking, a universal language is impossible. Systems are going to have different architecture, different languages available, and different storage means. I believe what you intend (correct me if I'm wrong) is a "widespread" protocol. One way or another, you're going to have to drill down based on your personal use-case.
For a widespread example, you could keep a set of JavaScript files with functions server-side, and refer a web client to the one they need to run it by loading a javascript file during some event. Pass the location of the file and the function name, load it using the link above, then call the JavaScript function by name to run it. I could see this being an admitedly somewhat roundabout solution. This also may work in Java due to its built in JavaScript engine, although I haven't tested it.
Beyond that, I am unaware of anything particularly widespread. Most applications limit what they accept as instructions quite strictly to prevent security breaches (Imagine a SQL Injection that can run free on a client's machine). In fact, JavaScript limits itself quite severely, perhaps most notably in regards to local file reading.
Hopefully this helps with your ideas. Let me know in a comment if you have any questions/issues about what I've said.

Sending continuous data over HTTP with Go

I am currently working on a web service in Go that essentially takes a request and sends back JSON, rather typical. However, this particular JSON takes 10+ seconds to actually complete and return. Because I am also making a website that depends on the JSON, and the JSON contents are subject to change, I implemented a route that quickly generates and returns (potentially updated or new) names as placeholders that would get replaced later by real values that correspond to the names. The whole idea behind that is the website would connect to the service, get back JSON almost immediately to populate a table, then wait until the actual data to fill in came back from the service.
This is where I encounter an issue, potentially because I am newish to Go and don't understand its vast libraries completely. The previous method that I used to send JSON back through the HTTP requests was ResponseWriter.Write(theJSON). However, Write() terminates the response, so the website would have to continually ping the service which could now and will be disastrous in the future
So, I am seeking some industry knowledge into my issue. Can HTTP connections be continuous like that, where data is sent piecewise through the same http request? Is that even a computationally or security smart feature, or are there better ways to do what I am proposing? Finally, does Go even support a feature like that, and how would I asynchronously handle it for performance optimization?
For the record, my website is using React.js.
i would use https websockets to achieve this effect rather than a long persisting tcp.con or even in addition to this. see the golang.org/x/net/websocket package from the go developers or the excellent http://www.gorillatoolkit.org/pkg/websocket from gorilla web toolkit for use details. You might use padding and smaller subunits to allow interruption and restart of submission // or a kind of diff protocol to rewrite previously submitted JSON. i found websocket pretty stable even with small connection breakdowns.
Go does have a keep alive ability net.TCPConn's SetKeepAlive
kaConn, _ := tcpkeepalive.EnableKeepAlive(conn)
kaConn.SetKeepAliveIdle(30*time.Second)
kaConn.SetKeepAliveCount(4)
kaConn.SetKeepAliveInterval(5*time.Second)
Code from felixqe
You can use restapi as webservice and can sent data as a json.SO you can continously sent data over a communication channel.

When to implement logic in the front-end vs the back-end?

So I've gotten very familiar with Javascript MVC's over the past few weeks. Now I'm looking to learn the how to program a backend(specifically using asp.net MVC's implementation). I'm learning about the razor's view engine etc.
One learning block I'm running into when reading up on examples and tutorials is that I am thinking to myself "well..can't I just do that in the front-end with javascriptMVC" for most of the logic, and If I need to talk to a database I can just use a JSON call. There must be some value in back-end coding but right now I don't see it(hoping to get that solved).
The client is always exposed to attackers, hence you can never trust the code.
In other words: Any security-related things, verification and validation logic belongs to the server, all authentication and authorization stuff, … and: when you need to make sure that there is one reliable instance to decide some things, e.g. on prices, discounts, and so on.
There is a saying in web programming, and that is: All input is evil.
So whatever comes from your frontend (which basically is your JavaScript application) should be handled with care. Always black- or whitelist input, encode it, transform it, check it, and so on … and the only place where you can do this reliably, as it's the only place that is under YOUR control is the server.
Moreover: Never put secrets into the client, such as credentials (for your database, e.g.).
Hope this helps.

How to Incorporate logic in Gapps for client side handlers?

I have written a server side Gapps system, but the UI responsiveness is terrible.
Now (unsuccessfully) trying to switch the basic UI handling to client side handlers.
I have discovered the object model (?) documentation for clientside handlers at:
https://developers.google.com/apps-script/class_clienthandler
The first problem seems to be the limited capabilities in Google Script to define event handlers.
I can find no way to incorporate "if" logic in my client side event handler.
Second problem is the (apparent) in-ability to provide client side (java) global variables.
Is there any alternative to storing client side data other than as text in a label or textbox?
I must be missing some basic concept here as I can find no way to provide a real client side Java module to be available in my Gapps delivered page source.
Any suggestions would be greatly appreciated. The UI for this application is quite complex.
PS - the application demands server side processing for several UI triggers.
Client handlers are meant to do trivial tasks such as notifying the user of some progress while all the hard work is done in server handlers. Please post some code of your attempt so that better suggestions can be offered.
Regarding your second question, I prefer to use CacheService to store variable across handlers. For objects, I convert them to JSON and store them using CacheService
I think client handlers maybe confusing, but they make sense. Perhaps think of the handlers as doing the logic for you already. Instead of writing if-then logic, the client handlers will do the work for you. For example, if you want verify that a user has entered in numbers, and make it possible for the user to submit data if the entry is numbers only, then there is a client handler for that. All you have to do is, for example, have a button activated if the entry is numbers.
As you have seen in the documentation, there are many handlers to handle many situations where if-then statements may have been used.
I will be learning the how to handle data storage over the next few days, but there are a few ways to store data like Script and User Properties, scriptDB, spreadsheetd or using JDBC. Here is the documentation referring to the above storage options (in addition to the cacheservice mentioned by Srik) So, I can't offer any expertise there. Try this link for documentation on data storage:
https://developers.google.com/apps-script/script_user_properties
Sorry for the vague answer, but I hope that gives you a least a hint of the direction you need to go for creating the client handlers.