Skip to content

Instantly share code, notes, and snippets.

import json
from django.http import HttpResponse
def SearchView(request):
project = request.GET.get('project', '')
role = request.GET.get('role', '')
#query = MODEL.objects.filter()
query = None
if query:
result = [{'is_already_approved':True}]
else:
@jayapal
jayapal / get_celery_task_status.py
Created June 7, 2017 17:00
how to get celery task state or status
@task
def hello():
print "hello world"
task = hello.delay()
task_id = task.id
from celery.result import AsyncResult
state = AsyncResult(task_id).state
print state # you can get the task status
import requests
from bs4 import BeautifulSoup
def get_image_url(purchase_url):
"""
Method to get HTML content from Lowes store and return product image
@params purchase_url : product url
@outpout : img url
"""
html = requests.get(purchase_url).text
@jayapal
jayapal / get_color_code.py
Created May 27, 2016 07:37
get_color_code.py
from sklearn.cluster import KMeans
from sklearn import metrics
import cv2
# By Adrian Rosebrock
import numpy as np
import cv2
# Load the image
image = cv2.imread("red.png")
def myview(request)
customer = request.session['Customer']
template = preppy.getModule(settings.REPORTS_DIR + '/report_templates/prepfiles/hello.prep')
rmlText = template.get(customer)
response = HttpResponse(mimetype='application/pdf')
response['Content-Disposition'] = 'attachment; filename=hello.pdf'
pdf = rml2pdf.parseString(rmlText)
response.write(pdf.read())
return response
@jayapal
jayapal / gist:93f783d3550eaf1555ab
Created March 22, 2015 16:53
Django listview as JSON and Jquery getJSON example
class JsonResponseMixin(object):
"""
Return json
"""
def render_to_response(self, context):
queryset = self.model.objects.all()
data = serializers.serialize('json', queryset)
return HttpResponse(data, content_type='application/json')
JSON parsing using getJSON
// js where you add header for the table dynamically
field = "user_type"
field_split = field.split("_")
field_text = field_split.join(" ")
console.log(field_text) // user type
var th = $('<th>', {
text : field_text,
class: "text-capitalize",
})
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="data.csv"'
writer = csv.writer(response)
result = [{"user": 'jai', "email": "jai@gmail.com"}, {"user": 'jai', "email": "jai@gmail.com"}.............]
if result:
header= result[0].keys()
writer.writerow(header)
for row in result:
data = []
for key in header:
<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Style id="s1">
<LineStyle>
<color>7f0000ff</color>
<width>4</width>
</LineStyle>
<PolyStyle>
<color>7f0000ff</color>
@jayapal
jayapal / new.kml
Last active August 29, 2015 14:05
<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Style id="s1">
<LineStyle>
<color>7f0000ff</color>
<width>4</width>
</LineStyle>
<PolyStyle>
<color>7f0000ff</color>