How to implement shortcut keys in Django using jquery? - python

I have been working for a accounting based project using Django-2.0.6 and python-3.6.
I want to implement shortcut keys in my project using jquery.
I have tried a library known as django-keyboard-shortcuts but it doesnot supports python3.
So I want to do it using jquery or any other option(if is there).
For example:
If I press Cntrl + R or any other combination from my keyboard it will redirect me to the desired url given in the combination.
Update
I have tried the following:
$(document).ready(function() {
$(document).keypress(function(event) {
if (event.which === 99) { window.location = '{% url 'accounting_double_entry:groupcreate' pk=company_details.pk pk3=selectdatefield_details.pk %}'; }
});
});
But got one problem that when I try to put combination of keys like ctrl+q or something like that it does not works.
Any idea how to do it?
Thank you

Related

my script for google's footer doesn't work correctly

I'm trying to automate google's footer and it runs the script but it doesn't update anything. Does anyone know how to solve it?
Sounds to me like it's an authentication thing.
Someone did something similar?
Description
Here is a simple example of adding a new paragraph to a footer.
Script
function editFooter() {
try {
let doc = DocumentApp.getActiveDocument();
let footer = doc.getFooter();
if( !footer ) footer = doc.addFooter();
footer.appendParagraph("This is the footer");
}
catch(err) {
console.log(err);
}
}
Reference
Document.addFooter()
FooterSection.appendParagraph()

Flask and Jinja2 url_for error - concatenating json object into url_for

I am using Flask with Jinja2 and MapBox on a project which involves plotting data on a map using GeoJSON derived from model data. Example of how this is loaded:
$.getJSON("{{ url_for(".geojson") }}", function(data) {
var geojson = L.geoJson(data, {
onEachFeature: function (feature, layer) {
//do stuff
}
});
markers.addLayer(geojson);
var map = L.map('map', {maxZoom: 9, minZoom: 3}).fitBounds(markers.getBounds());
baseLayer.addTo(map);
markers.addTo(map);
An example of using this JSON data within my JS:
var feature = e.layer.feature;
//print item name
console.log(feature.properties.name)
//print item latitude
console.log(feature.properties.latitude)
//print item category info
console.log(feature.properties.category.name)
This works great. My dataset has now extended to include image urls (example 09379_580_360.jpg), and the images themselves are hosted in a static/images/eol folder. I'd like to include these as an image within a DIV, of which I am setting dynamically via JS like so...
var commoncontent = '<div class="panel-heading"><h3>'+feature.properties.name+'</h3></div>'
$('#common').html(commoncontent)
However, when I attempt to concatenate my image data into jinja's url_for...
var commoncontent = '<div><img src="{{ url_for("static", filename="images/eol/thumbs/big/'+feature.properties.category.localimageurl.jpg+'") }}"></div>'
... I get this error in my console
GET http://127.0.0.1:5000/static/images/eol/thumbs/big/feature.properties.category.localimageurl.jpg 404 (NOT FOUND)
I know that feature.properties.category.localimageurl is correct as it prints to my console when I console.log() it. However, I have no idea why the interpreter is taking it directly as a string and not concatenating it?
feature is a JavaScript object. Jinja doesn't have access to those; it runs on the server, whereas your JavaScript runs in the client. feature doesn't exist when your template is rendered. You will need to handle the concatenation with JavaScript.
var commoncontent = '<div><img src="{{ url_for("static", filename="images/eol/thumbs/big/") }}' + feature.properties.category.localimageurl.jpg + '"></div>'

Calling function on unlike event of facebook

I have been calling a function to run a query in to the database on a like event of facebook like button on my python site .And it is working absolutely fine .
FB.Event.subscribe('edge.create', function(href, widget)
But the problem is I need to use a another query of update on unlike event of the facebook button.
<script>
FB.Event.subscribe('edge.create', function(href, widget) {
top.window.location = 'http://pydev.abc.com/surpriseme/';
});
</script>
<div class="suprise">
<div class="suprise_con">
<div class="fb-like" style="overflow:hidden;" data-href="https://www.facebook.com/VivaAviesta" data-send="false" data-layout="button_count" data-width="100" data-show-faces="false"></div>
<span style="margin-left:5px;"> Like Us to Reveal</span>
</div>
</div>
For the url surprise me I have added a functionin views :
def surpriseme(request):
if request.user.is_authenticated():
cursor = connection.cursor()
cursor.execute("INSERT "......." (name,facebook_id,category,user_id) VALUES ('Abcd',1,'',%s)",[request.user.id])
#return HttpResponse("success")
return redirect("/showroom/")
Can anyone help in calling another function at the unlike event?
Actually the thing is that the person who has not liked the face book button on our site will not be able to see few of the products and once he/she hits like button they can see the products.
Now what I need is if anyone again unlike the button will should not be able to see products , which is not working at the moment.
calll like this
<script type="text/javascript">
window.fbAsyncInit=function() {
FB.Event.subscribe('edge.remove', function(href, widget) {
top.window.location = '{{baseurl}}/surprisemeunlike/';
});
FB.Event.subscribe('edge.create', function(href, widget) {
top.window.location = '{{baseurl}}/surpriseme/';
var url = '{{baseurl}}/surpriseme/';
});
}
</script>
Ok I got the answer, there is another event fron facebook :
FB.Event.unsubscribe('edge.create', handleResponse);
We can call another function on this event .
<script>
FB.Event.unsubscribe('edge.create', function(href, widget) {
top.window.location = 'http://pydev.abc.com/surpriseunlike/';
});
</script>
and it is working fine for me now .
"Like Gating" isn't against the terms of service and is used by many brands. It worked well under FBML, but it isn't reliable for an actual lockdown on the current API options. Facebook only prohibits requiring a like for contest entries (unless you use an approved vendor to run the contest).
If you actually want to require people to "Like" , you should be using their Graph API and 'realtime updates' https://developers.facebook.com/docs/reference/api/realtime/ Your javascript function will only work if someone likes or unlikes on that same page. It won't handle people liking or unliking in other browser windows or different time periods, which undermines your intent.
But if someone Likes your page and then unlikes it - so what? They already saw your special content.

Generating pdf using web2py-appreport (xhtmltopdf) in Python Web2py webapp

I am from a non coding background so python, web2py is very new to me.
My app needs to export textarea content (using RTE redactor) to pdf. I get html content from textarea (redactor), can you please advice me on how to use pyfpdf to generate a pdf file on button click.
I don't know how to get the html content (images and text) on button click in view to generate pdf using appreport.
I was able to use app-report to generate a pdf (using PISA, PYPDF does not work) from an existing html file (without css) if html file has css it throws an error,
***<class 'sx.w3c.cssParser.CSSParseError'> Terminal function expression expected closing ')':: (u'Alpha(Opacity', u'=0); }\n\n\n\n.ui-state-')***
This might be due to a mistake in the controller code:
def myreport():
html = response.render('myreport.html', dict())
return plugin_appreport.REPORTPISA(html = html)
Another thing I tried was passing the html from my view to the controller using ajax post (in Javascript). Redactor is the textarea RTE I am using and alert gives me the desired html result.
View:
function getContent() {
var t= jQuery('#redactor_content').getCode();
alert(t);
jQuery.ajax({
type: "POST",
url: "http://127.0.0.1:8000/Test50/default/myreport2",
data: "{g : 'jQuery('#redactor_content').getCode()'}"
});
}
Controller:
def myreport2():
g = request.get_vars
html = response.render(g)
return plugin_appreport.REPORTPISA(html = html)
Due to my less knowledge in coding , I am not able to figure out and correct my mistake. I will be thankful if anybody can help me with this problem.
Regards,
Akash
Could it be this post request:
jQuery.ajax({
type: "POST",
url: "http://127.0.0.1:8000/Test50/default/myreport2",
data: "{g : 'jQuery('#redactor_content').getCode()'}"
});
}
I think you should have the 'data' parameter be a literal dictionary, not a string. Change this line like this (remove all but one set of quotes):
data: {g : jQuery('#redactor_content').getCode() }
This should properly send the request. The jQuery documentation says that the data parameter should be key-value pairs, not a string.

drop down with onchange events

I have a dropdown with onchange events. When ever user change the drop down box then ajax called and result displayed. All this working fine.
I have following list in template.
Apple
Pineapple
When ever user changed the drop down value. Then result merged
Orange
Graps
Apple
Pineapple
But output should be like this.
Orange
Graps
I have no idea what to do. Please help me. Here is my base. Here is my movie_list template. Here is my movie_sort template. And here is my view . Thanks :-)
UPDATE: Here is ajax code.
function sortMovie(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","/moviesort/?q="+str,true);
xmlhttp.send();
}
DROP DOWN
<select name="category" onchange="sortMovie(this.value)">
<option value="">Choose</option>
{% for category in categories %}
<option value="{{ category.id}}">{{category.name}}</option>
{% endfor %}
</select>
if you are using jQuery to call ajax, I would recommend you use the .empty() straight after the .change() function.
I have no idea what yours looks like, although it should be something like this.
$('#id_category').change(function() {
/* Set your string to nothing */
var str = "";
/* once your dropdown is changed, it performs a look and gets the new data */
$("#id_category option:selected").each(function () {
/* Before inserting the new data, empty out all the old items */
$('#id_sub_category').empty();
str += $(this).val();
$.get('{% url gear_category %}', { category: str }, function(data){
$(data).appendTo("#id_sub_category");
});
});
});
Hope this helps.
I agree with ApPel on this.
However, since you aren't using jQuery...
You'll either have to loop through each option in the select and remove as you go, or I suppose if pure javascript supports you might be able to mimic the jQuery code, but with the javascript syntax.
Either way, you'll have to remove the old values before appending the new ones. That's the problem.

Categories