How to trigger a function of class via button in view - django

I'm setting up an application working with Django and Keras. It takes a lot of time to take my uploaded document, prepare, train and to show my results. That's why I'm trying to split my code into different phases. But I don't know how to trigger a function using my created object before. Do you know how to connect a button to trigger a process via using created object?
Thanks in advance!

To trigger an action from a template view you need to set up an endpoint and use needed class/func there. You can call it with ajax if that's you want.
path('action/', app.views.action, 'action')
def action(request):
my_util_class.my_function()
return HttpResponse('')
Button

Related

Sitecore Scheduled Task: Cannot access any items in run class

I configured a scheduled task via database, which is triggered as expected and calls a method "Execute" in a C# class. My problem is that in my Execute method no Sitecore items can be accessed. I even get a NullPointerExecption when calling Sitecore.Context.Database.
I guess this must be a security problem, which makes sense. So I tried to get the permissions using UserSwitcher and SecurityDisabler, but that did not help.
Can anyone point me to what I'm missing?
You cannot use Sitecore.Context in a scheduled task. It does not exist there. Sitecore.Context is tied with HttpRequest, and there is no request in a scheduled task. And as John W writes in his article (The Sitecore Context):
The Sitecore context, exposed by the static Sitecore.Context class, contains information about the Sitecore installation and the current HTTP request. Processors in the httpRequestBegin pipeline defined in the web.config file are largely responsible for defining the Sitecore context. After Sitecore determines the context database, it determines the context item in that database.
In a schedule job you should get the database by its name, e.g.:
var masterDb = Sitecore.Data.Database.GetDatabase("master");
var webDb = Sitecore.Data.Database.GetDatabase("web");

Django async update a single page template

I have a django view with this function to get the data for a template:
def get_context_data(self, **kwargs):
context = super(MyView, self).get_context_data(**kwargs)
context['extra_data'] = a_long_running_function()
return context
The extra_data is displayed in a table. As the above function indicates, the page takes a long time to load due to calculation of extra_data.
So how can I show the page straight away, and then update the tablewhen extra_data is computed?
I understand how I can use celery to make a_long_running_function execute asynchronously, but I dont know how to then make the page (which is now loaded, but missing data for the table), get that data and update automatically?
If you plan in going ahead with celery, you will need 2 views:
1.viewA that loads the main page (without the extra_data - maybe a spinning gif animation in it's place in the HTML, to convey to the user that there is still data to be loaded in the page). This view will also start the celery task (but will not wait for it to complete). It would look similar to:
def viewA(request):
task = a_long_running_function.delay()
return render_to_response('viewA.html', {'task_id': task.id})
2.viewB that will be accessed via AJAX after the user's browser loads viewA (it's purpose will be to provide the extra_data which was not loaded by viewA). It would look similar to:
def viewB(request, task_id):
extra_data = a_long_running_function.AsyncResult(task_id)
if extra_data.ready():
return render_to_response('viewB.html', {'extra_data': extra_data.get()})
return HttpResponse('')
Once the user's browser finishes loading viewA, you will need a bit of javascript to start running AJAX requests every X seconds/minutes to viewB to attempt to retrieve the celery task result (based on the celery task id that is available). Once the AJAX request successfully retrieves the task result from viewB, it can make it visible to the user.
Anybody interested in asynchronous updating a template using AJAX can use django-async-include (GitHub repository).
This project makes it easy changing an static block inclusion to a asynchronous one. That's perfect for inclusion of computational-heavy template block.
Disclaimer: I'm the developer of this project.

How can I write data about process assignees to database

I use camunda 7.2.0 and i'm not very experienced with it. I'm trying to write data about users, who had done something with process instance to database (i'm using rest services) to get some kind of reports later. The problem is that i don't know how to trigger my rest(that sends information to datebase about current user and assignee) when user assignes task to somebody else or claims task to himself. I see that camunda engine sends request like
link: engine/engine/default/task/5f965ab7-e74b-11e4-a710-0050568b5c8a/assignee
post: {"userId":"Tom"}
As partial solution I can think about creating a global variable "currentUser" and on form load check if user is different from current, and if he is - run the rest and change variable. But this solution don't looks correct to me. So is there any better way to do it? Thanks in advance
You could use a task listener which updates your data when the assignee of a task is changed. If you want this behavior for every task you could define a global task listener.

How to get all the Open Graph Beta actions generated by an app?

It is already possible to get all actions for custom app actions and objects:
https://graph.facebook.com/me/{appNameSpace}:{action}/{object}
This will list all the actions generated by an app, but only for a given user...
How do I get all the actions generated by an app (for all its users)?
I've tried this request with an app access token :
https://graph.facebook.com/{appId}/{appNameSpace}:{action}/{object}
But it does not work...
I was also looking for an activities or actions FQL table, since each action has an id, they should be accessible somewhere.
Any suggestions?
AFAIK there is no way to get all actions published by application via OpenGraph, but your application creates those actions, why just not record results of this operation?
Then you create action you should get response like this:
{
id: “{action-instance-id}”
}
Later you may read this action:
GET https://graph.facebook.com/{action-instance-id}
You for sure may benefit from doing request to get multiple actions like this:
GET https://graph.facebook.com/?ids={action-id1},{action-id2},{action-idn}
And even batch those requests

Call Custom Code Upon Item Deletion in Sitecore

Is there a way to call custom code when an item is deleted within Sitecore? Or can I somehow attach code to a publish action?
You can subscribe to item:deleted event. Check this article: Using events.
Attaching to item:deleted will work only if you have a single server solution. If you have separated the content delivery and the content edit servers then it will be a bit more involved.