Is it possible to use web2py language in javascript file (.js)? - python

I'm trying to use AJAX with web2py language but I have a problem
My code is:
javascript
$(document).ready(function(){
$(".className").click(function(){
jQuery.ajax({
type:'POST',
url:'getName',
data:{
itemName:'a'
},
timeout: 20000,
success: function(msg) {
alert(msg);
},
error: function(objAJAXRequest, strError){
alert( "Error:" + strError );
}
});
});
default.py
def getName():
itemName=request.vars.itemName
return "Name: " + itemName
The thing is I want to use the data from the database, but is it possible to use
{{for item in tableName:}}
var name={{=item.name}}
like this?
I'm not sure how to extract data from DB in javascript.
Can you help me a bit?
Cheers

The short answer is that you can't directly extract data from the db in javascript using web2py. You have to query the db with web2py, and then use web2py to send your query data to the javascript (or more accurately since you're using ajax, use jquery/javascript to pull your query data down from web2py). Make sure that you actually need to perform logic on the client (javascript) side here, because the easiest thing to do would be to perform all your logic in python in the web2py controller.
However, if you do for some reason need to perform logic on your data on the client side, then you are on the right track. The easiest thing for you to do would be fetch the records you need from the db in the web2py controller, then package the records up as a json object in your web2py controller ("import simplejson" or you can do it with the standard library with the latest version of python), then return that json object to make it available for your js to fetch using the ajax request you've included above. Only at that point should you loop through the json object using the javascript to get the data you need.
That said, if you're just trying to get a field from a single record from the database, the easiest thing to do would be just to query the database correctly to get that record, and return it in a web2py controller for your ajax script to get.

Related

How to close a OTRS ticket using pyotrs in python?

I am new to ticketing system - OTRS. I can successfully create a OTRS ticket using pyotrs, also access ticket attributes like ticket number etc. However, I dont have any idea how to close a ticket using otrs ticket-id or ticket number. On close inspection in OTRS documentation I got the following -
"Attention: PyOTRS can only retrieve Ticket data at the moment!".
So any suggestion as to how to close an OTRS ticket with or without python (non-GUI)?
I assume you already have a webservice named "GenericTicketConnectorREST" (because of the pyotrs example webservice). In there you need to have an operation called TicketUpdate (last entry in the image).
Under "Configuration" you should add/change the entry for TicketUpdate (i had to change it):
Using Postman (or any tool to send your rest request) you can send the following JSON to close a Ticket. Remember to set the Postman request to Patch, as it is set in the configuration.
{
"UserLogin":"root#localhost",
"Password":"root",
"TicketID":"1",
"Ticket":{
"State": "closed successful"
},
"Article":{
"ContentType":"text/plain; charset=utf8",
"Subject":"Ticket closed",
"Body":"Ticket closed"
}
}
The URL Depends on the name of your webservice and how you set the routing (TicketUpdate). http://localhost/otrs/nph-genericinterface.pl/Webservice/GenericTicketConnectorREST/TicketUpdate.
You can change anything listed in the api for the operation "TicketUpdate". Eveything under the key Data is something you can add to change.
https://doc.znuny.org/doc/api/otrs/6.0/Perl/Kernel/GenericInterface/Operation/Ticket/TicketUpdate.pm.html

parsing exception: Expected [START_OBJECT] but foud [START_ARRAY]

I'm trying to convert my python ETLs to airflow.
I have an ETL written in python to copy data from Elastic to MSSQL.
I've build a DAG with 3 tasks.
task 1- get the latest date from the table in MSSQL
task 2- generate an elastic query based on that date retrieved from the previous task plus some filters (must not and sould) taken from a different table in MSSQL (less relevant).
eventually generating a body like so:
{ "query": {
"bool": {
"filter": {
"range": {
"#timestamp": { "gt": latest_timestamp }
}
},
"must_not": [],
"should": [],
"minimum_should_match": 1
}
}
}
task 3- scroll the elastic index using the body generated in the previous task and write the data to mssql.
My DAG fails on the 3rd task with the error:
parsing exception: Expected [START_OBJECT] but foud [START_ARRAY]
I've taken the generated body and ran it on elastic in dev tools and it is working fine.
So I have no idea what is the problem and how to debug it.
Any ideas?
I've found the problem.
I use XCOM to pass the body between Task2 and Task3.
Apparently something in the XCOM is messing with the body (I don't see it in the UI anyway).
When I put the logic of the body (task2) and call the search with the same body without passing it via XCOM everything is working as expected.
So beware of using XCOM cause it has side effects apparently.
I'm not a big ELK guy, but I would assume a different format is required there.
When you are doing "scroll the elastic index", you most probably use some API that expects one query format, while in dev console another query format is expected.
E.g. this thread:
https://discuss.elastic.co/t/unable-to-send-json-data-to-elastic-search/143506/3
Kibana Post Search - Expected [START_OBJECT] but found [VALUE_STRING]
So, check what format is expected by API handle you use to scroll through the data. Or, if still unclear, please share the function you use for scrolling in task 3.
Also,

How to input MySQL database data to a html table using python?

I'm fairly new to using MySQL, HTML, and Python synchronously. I have a website that I create using HTML, CSS, and Javascript. Then I use Python to enter data into a MySQL database. My question is how can I create a table on my HTML side and input MySQL data into the table using Python. I would like the table to grow row-wise dynamically (i.e. I don't want to refresh/reload and the new data should enter the HTML table in a new row so that the table display's all of the data in the MySQL database).
So far I am entering data correctly into the MySQL database and it is being populated. However, I'm stuck on how to:
Create an HTML table that grows dynamically based on amount of data
Use Python to input the data from the MySQL database into the table
I would like a solution WITHOUT using PHP
I'm very new to MySQL, HTML, and Python and therefore, any and all help is greatly appreciated. Thank you in advance!
It seems like you need to use a server to manage the operations of your website smoothly. You should probably check out flask, its extremely user friendly. If you have any amount of experience with Python it shouldn't be difficult to figure it out.
Backend: Flask has the added functionality of handling MySQL databases and handle the operations whenever the server receives requests. Your server can handle this request by responding with a JSON. Check out flask.ext.mysqldb. With Python's json library you can easily convert the query into JSON. Check this answer out for more details
Frontend: You can use a variety of methods with JavaScript to handle HttpRequests, like fetch and XMLHttpRequest. You can call your server and ask your server for the data that is stored in your backend and populate tables with the JSON you receive.
Dynamically growing HTML: this is a dummy function assuming that you receive a JSON response from your server and you have only 2 columns in your MySQL table. The code below is JavaScript.
Taking a dummy HTML like this:
<table id="table">
</table>
With a JavaScript function handling the response of your server.
const response = //JSON response from server
var table = document.getElementById("table");
table.innerHTML = ""; //making sure we don't present corrupt data
response.forEach((result, index) => {
const content = ` <tr>
<td>${result['col1']}</td>
<td>${result['col2']}</td> </tr>}`;
table.innerHTML += content;
})
Side note: You can make use of CSS classes to style these tables!
I know this is a very high level explanation of what is to be done. If you have any more clarifications let me know!

How can I display a message to the user at a certain time Django

I was wondering how I could display a simple message to the user at the top of their screen at a certain time in Django. I haven't been able to find anything online about this, so I was wondering how I would go about doing this.
Would I need to do something with DateTime? What view/model would I include the code in?
Thanks for any help you can give :)
I still don't have enough reputation to comment:
So just tell me when you 'read' this answer and I will delete it.
If you want you might copy / paste the interesting parts into a comment.
I think the exact use case is very important for giving the right answer.It all depends what exactly you mean with 'certain time'.
Notification time is known upfront (javascript alert)
Asking the Django server periodically can determine when to send the message (polling long / polling)
The Django server needs to perform some background task and the message should be sent at the end of this task
If you know already upfront (at the time the user opens the page) when this notification should happen, then Kostas Charitidis is right.
The page can just use javascript to program a timer and it will be the web browser who can at the given time make a request to Django to fetch an display the message.
To 'push' anything from a web server to a web browser at a given time one requires something like continous polling, long polling, web-sockets.
If the notification time is not known upfront, then
a polling approach would for example use a piece of javascript, that periodically (every few seconds / minutes / depending on your use case) queries the Django server whether a message should be fetched.
Long polling is an approach where the web browser performs a get request to the server and the server stalls the answer until the message should be sent. (if the requests times out a new polling request will be initiated)
However to use long polling (efficiently) you'd need some special plugins on the server side to make this efficient / resource friendly.
If your web site does not need to be accessed from behind some old corporate (or paranoid) firewalls, then you might consider using web sockets (but there are still some public services / big companies who do block web sockets)
django-channels ( https://pypi.org/project/channels/ ) can help you with web socket notifications and some background tasks
celery ( https://pypi.org/project/celery/ ) can help you with running periodic / defered / tasks, but does not contain the means to send the message to your browser.
Celery is one of the most recommended solutions, but is (in my opinion) rather painful to set up.
Depending on the project something lighter (like for example django-background-tasks https://pypi.org/project/django-background-tasks/ ) might do the job
You might look at the django message frame work ( https://docs.djangoproject.com/en/2.2/ref/contrib/messages/) . But very probably it is not the right answer in your context.
You don't need any model to do this,
Option 1
Use ajax (Use when you want to refresh certain time on specific interval)
Just create a view like,
import datetime
from django.http import JsonResponse
def get_certain_date(request):
# or get certain date from database or somewhere else.
return JsonResponse({'certain_date':datetime.datetime.now()})
And use ajax call to update html contents.
Option 2 (Use when you dont want to update certain time untill user refreshes page)
render certain time in django view.
from django.shortcuts import render
def home(request):
render(request, "html.html", {'certain_time': certain_time}) # get it from database.
You can do it purely with frontend Javascript. No Django code needed at all. However, it depends on what you intend to do.
<p id="demo" ></p>
<script>
var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var dateTime = date+' '+time;
if (today.getHours() >= 1){
document.getElementById("demo").innerHTML = "Insert your message here!!";
}
else {
document.getElementById("demo").innerHTML = "We don't want you to know the message yet";
}
</script>
If the time when the user loads the page is past 1AM for the day (the time I am using given where I am for testing), it shows the "Insert your message here!!". If it is not yet that time, it shows "We don't want you to know the message yet".
Insert your chosen hour in the if statement and set your chosen message to the innerHTML.
Alternatively, you could use Javascript to show or hide an element. You can use something like this for that.
if (if (today.getHours() >= 1)) {
document.getElementById("demo").style.display = "block";
} else {
document.getElementById("demo").style.display = "none";
}
For the case (proposed by Nishant) where you need it for the server time rather than the user time, you would get the hour from datetime.datetime.now(), and pass as a variable (say perhaps curr_date_hour) and substitute it into the Javascript if statement like so:
if (if (today.getHours() >= {{curr_date_hour}})) {
document.getElementById("demo").style.display = "block";
} else {
document.getElementById("demo").style.display = "none";
}

Dealing with large json file

I have a large json file from a web scraping project I've been doing for a while. Now I'm trying to build a web frontend using the JSON data. I'm having a hard time figuring out the best way to go about building it, though.
The json file looks like this:
{
"_id" : { "$oid" : "55d5c85a96cc6212bdd4ca08" },
"name" : "Example",
"url" : "http://example.com/blahblah",
"ts" : { "$date" : 1073423706824 }
}
I have a couple questions:
The json file would be added to overtime, so would the best solution be to regularly add to a database, or just keep the json file in the cloud somewhere and pull from it when needed?
If I put it in a database, how could I regularly add it to a database, without slowing down the front end of the site? I know I could use something like json_decode, but I've mostly only seen examples with a few lines of json, could it be used for larger json files?
If I put it in a database, would a relational db be faster/more efficient or something like mongodb?
After doing a lot of webscraping myself here's what I would recommend:
Decide between your relational and non, relational database. If your data is constantly changing with an unknown number of parameters I recommend using MongoDB (as it's almost JSON and is fully schemaless so easy to add new facets). If your data is all the same format then using a relational DB is a good step forward. PostgreSQL and MariaDB are good, open source options.
Convert your current JSON data into the DB format chosen and insert it.
Start scraping straight to the DB, try not to use JSON files any more.
Read from the database for your front end. If you're choosing Python, you could look at flask as a good option.
There is also a really interesting question on Store static data in an array or in a database previously posted with some in depth answers as to static files vs. Database.
If you take static files out of the equation and use databases here are the answers to your 3 questions;
Just use the database.
Adding to the database is simple. Once you've got it set up, your scraper can write straight to it with the relevant driver. Again, no need for JSON files.
It all depends on your data

Categories

Resources