How to make query in an NLP Api? - python

I am Making an API in which I want help is how to make query?.
I can display result using 127.0.0.1:8000/Naturallanguageprocessing/
It is getting All attributes that i included parts of speech,lemmatization etc.but i have to display in this way 127.0.0.1:8000/?q = "querytext"&all = 1
It should display all parts of speech ,lemmatization etc. and if i call 127.0.0.1:8000/?q = "querytext" & partsofspeech = 1 & lemmatization = 1.
It should display parts of speech and lemmatization.
Below is my views.py.
from django.shortcuts import render,redirect,HttpResponse
from django.views.generic.edit import FormView
from rest_framework.decorators import api_view
from django.http import JsonResponse
from django.conf import settings
from rest_framework.response import Response
from django.views import View
import json
import spacy
import pdb
nlp = spacy.load('en_core_web_sm')
#api_view(["POST"])
def Naturallanguageprocessing(requestdata):
try:
text = str(requestdata.body)
stopwordsremoval = []
partsofspeech= []
nounphrases = []
word_lemma = []
tokenization = []
nameentityrecognization = []
for word in (nlp(text)):
a = (word.is_stop, word.text)
b = (word.text, word.pos_, word.tag_)
d = (word.lemma_)
e = (word.text)
tokenization.append(e)
word_lemma.append(d)
stopwordsremoval.append(a)
partsofspeech.append(b)
for word in (nlp(text).noun_chunks):
c = (word.text)
nounphrases.append(c)
for word in (nlp(text).ents):
f = (word.text,word.label_)
nameentityrecognization.append(f)
output = {"stopwordremoval": stopwordsremoval,
"partsofspeech": partsofspeech,
"nounphrases" : nounphrases,
"word_lemma":word_lemma,
"tokenization":tokenization,
"nameentityrecognization":nameentityrecognization
}
return JsonResponse(output,safe = False)
except ValueError as e:
return Response(e.args[0],status.HTTP_400_BAD_REQUEST)

Related

Can I use the values from a form in other form with Django?

I have a Django app with two forms where the views.py looks like this:
import pandas as pd
import numpy as np
from django.contrib.auth.decorators import login_required
from django.shortcuts import render
from django.template.response import TemplateResponse
from pbmcalculator.models import PbmCalculator, simulator
from django.core.cache import cache
import json
#login_required
def subs_calc(request):
# this is from the first form #
selection = request.GET.get("id", None)
customer = request.GET.get("customer", None)
initial_date = request.GET.get("initial_date", None)
final_date = request.GET.get("final_date", None)
if selection is not None and 'initial_date' in request.GET:
calculation_target = PbmCalculator(customer, initial_date, final_date)
html = TemplateResponse(
request,
"pbmcalculator/table.html",
{
some operations
},
)
return html
if 'generico_sim' in request.GET:
# this is from the second form #
brand_sim = request.GET.get("brand_sim",None)
generico_sim = request.GET.get("generico_sim",None)
marca_sim = request.GET.get("marca_sim",None)
initial_date_sim = '2021-08-08'
final_date_sim = '2021-08-15'
calculation_sim = simulator(brand_sim, initial_date_sim, final_date_sim)
html = TemplateResponse(
request,
"pbmcalculator/table1.html",
{
some operations
},
)
return html
return render(request, "pbmcalculator/subs_calc.html", {"selected": False})
So I want to use the variables initial_date and final_date in the second if statement to fill the initial_date_sim and final_date_sim. I tried several ways but none worked. Some idea?
I solved this issue by taking the values through the ajax of each form, there it doesn't matter if the value comes from one or the other. then the second form will be filtered by the dates of the first as I wanted. Thank you Jelle for your time.

In Django no other function getting called except one rest showing error 403

This is our code for url
from django.conf.urls import url,include
from django.urls import path,re_path
from . import views
app_name = 'challengeide'
urlpatterns = [
url('challengeide', views.qtn,name='test'),
url('challenge/', views.challengeide, name='challengeide'),
url('challenge/', views.dataentry, name='dataentry'),
#url('challengeide', views.dataentry,name='dataentry'),
url('challenge/challengeide/', views.send_challenge_request, name='send_challenge_request'),
]
this is code for views
from django.shortcuts import render
from django.http import JsonResponse, HttpResponseForbidden
import requests
from challengeide.models import Challenge_Detail
from challengeide.models import ChallengeDemo
import json
from django.utils.datastructures import MultiValueDictKeyError
from users.models import Profile
from django.shortcuts import redirect, get_object_or_404
from django.http.response import HttpResponseRedirect
from django.contrib.auth import get_user_model
def qtn(request):
try:
#print("If loop")
data = request.POST['opponent']
data1 = request.POST['qts']
opponent_row = Profile.objects.get(slug = data)
global to_id
to_id = opponent_row.user_id
print(data,data1)
global a
a = int(data1)
question_desc = Challenge_Detail.objects.get(id = a)
#print(a)
#dataentry(request)
return render(request,"challengeide.html",{'ChallengeDetails':question_desc})
except MultiValueDictKeyError :
#print("in else")
question_desc = Challenge_Detail.objects.get(id = a)
e1 = question_desc.expout
e2 = question_desc.expout2
e3 = question_desc.expout3
e4 = question_desc.expout4
e5 = question_desc.expout5
i1 = question_desc.expin
i2 = question_desc.expin2
i3 = question_desc.expin3
i4 = question_desc.expin4
i5 = question_desc.expin5
for i in range(1,6):
if (i==1):
if request.is_ajax():
source = request.POST['source']
lang = request.POST['lang']
data = {
'client_secret': '' ,
'async': 0,
'source': source,
'lang': lang,
'time_limit': 5,
'memory_limit': 262144,
}
x = str(i1)
i1 = x.replace("qwe","\n")
data['input'] = i1
'''
if 'input' in request.POST:
data['input'] = request.POST['input']'''
r = requests.post(RUN_URL, data=data)
temp =(r.json().copy())
print(temp)
print(data)
global out_returned
if(temp["run_status"]["status"]!="CE"):
out_returned = temp['run_status']["output"]
e = temp['compile_status']
print(e)
compare(out_returned,e1)
print(e1)
print(out_returned)
else:
error = (temp["compile_status"])
print(error)
ex = {"Problem" :error}
print(type(ex))
print(ex)
return JsonResponse(r.json(), safe=False))
else:
return HttpResponseForbidden()
def send_challenge_request(request):
print("dataintial")
user = get_object_or_404(User, id=to_id)
print(request.user)
print(user)
print("dataentry")
entry = ChallengeDemo(
from_id=request.user,
to_id=user,
score1 = 10.5,
score2 = 12.5,
qtn_id = 1
)
entry.save()
print("dataexit")
return render(request, 'challenge.html')
this is where we are calling the function send_challenge_request in html
<a
class="btn btn-primary mr-2"
href="{% url 'challengeide:send_challenge_request' %}"
>Submit</a
>
the function send_challenge_request is not getting called and error 403 Access to localhost was denied is been shown and this is the anchor tag which we using to call the function present in views.py.

Iterate in django views

I have problem with iteration all over the list in django views.py file
from django.shortcuts import render, redirect, HttpResponse
from django.views.decorators.http import condition
import time
import roundrobin
STATES = [
"hello",
"world"
]
get_roundrobin = roundrobin.basic(STATES)
def test(request):
for i in get_roundrobin():
return HttpResponse(i)
Django answers me only first symbol of the each element of the STATES list, I mean django return "h" then "w" instead "hello" then "world", how to fix it?
Upd: By the way inside of the roundrobin library, there the next code:
from itertools import cycle
def basic(dataset):
iterator = cycle(dataset)
def get_next():
a = next(iterator)
print(a)
return a
return get_next
All is correct, it answers like "hello" then "world".
Because get_roundrobin returns a string and then you are iterating through a list which returns only first character at a time, Try this
from django.shortcuts import render, redirect, HttpResponse
from django.views.decorators.http import condition
import time
import roundrobin
STATES = [
"hello",
"world"
]
get_roundrobin = roundrobin.basic(STATES)
def test(request):
return HttpResponse(get_roundrobin())
Now test will return 'hello' then 'world' then 'hello' and so on...

How to render output of cartridge API's on custom HTML page?

I am working on a cartridge project. I have created custom html templates for better visual and now I want to render all data which is coming through cartridge's built in APIs on my custom html pages. For.ex. I have a product.html, on which I want to show all products stored in db (category wise).
Actually, I tried to explore url,
url("^shop/", include("cartridge.shop.urls")),
I am not getting that on which API or Function, this url is hitting.
urls.py file of shop app looks like this, I tested it, none of those url get called,
from __future__ import unicode_literals
from django.conf.urls import url
from mezzanine.conf import settings
from cartridge.shop import views
_slash = "/" if settings.APPEND_SLASH else ""
urlpatterns = [
url("^product/(?P<slug>.*)%s$" % _slash, views.product,
name="shop_product"),
url("^wishlist%s$" % _slash, views.wishlist, name="shop_wishlist"),
url("^cart%s$" % _slash, views.cart, name="shop_cart"),
url("^checkout%s$" % _slash, views.checkout_steps, name="shop_checkout"),
url("^checkout/complete%s$" % _slash, views.complete,
name="shop_complete"),
url("^invoice/(?P<order_id>\d+)%s$" % _slash, views.invoice,
name="shop_invoice"),
url("^invoice/(?P<order_id>\d+)/resend%s$" % _slash,
views.invoice_resend_email, name="shop_invoice_resend"),
]
These are cartridge's views for '/shop/product', '/shop/wishlist' and '/shop/cart'
from __future__ import unicode_literals
from future.builtins import int, str
from json import dumps
from django.contrib.auth.decorators import login_required
from django.contrib.messages import info
from django.core.urlresolvers import reverse
from django.db.models import Sum
from django.http import Http404, HttpResponse
from django.shortcuts import get_object_or_404, redirect
from django.template import RequestContext
from django.template.defaultfilters import slugify
from django.template.loader import get_template
from django.template.response import TemplateResponse
from django.utils.translation import ugettext as _
from django.views.decorators.cache import never_cache
from mezzanine.conf import settings
from mezzanine.utils.importing import import_dotted_path
from mezzanine.utils.views import set_cookie, paginate
from mezzanine.utils.urls import next_url
from cartridge.shop import checkout
from cartridge.shop.forms import (AddProductForm, CartItemFormSet,
DiscountForm, OrderForm)
from cartridge.shop.models import Product, ProductVariation, Order
from cartridge.shop.models import DiscountCode
from cartridge.shop.utils import recalculate_cart, sign
try:
from xhtml2pdf import pisa
except (ImportError, SyntaxError):
pisa = None
HAS_PDF = pisa is not None
# Set up checkout handlers.
handler = lambda s: import_dotted_path(s) if s else lambda *args: None
billship_handler = handler(settings.SHOP_HANDLER_BILLING_SHIPPING)
tax_handler = handler(settings.SHOP_HANDLER_TAX)
payment_handler = handler(settings.SHOP_HANDLER_PAYMENT)
order_handler = handler(settings.SHOP_HANDLER_ORDER)
def product(request, slug, template="shop/product.html",
form_class=AddProductForm, extra_context=None):
"""
Display a product - convert the product variations to JSON as well as
handling adding the product to either the cart or the wishlist.
"""
published_products = Product.objects.published(for_user=request.user)
product = get_object_or_404(published_products, slug=slug)
fields = [f.name for f in ProductVariation.option_fields()]
variations = product.variations.all()
variations_json = dumps([dict([(f, getattr(v, f))
for f in fields + ["sku", "image_id"]]) for v in variations])
to_cart = (request.method == "POST" and
request.POST.get("add_wishlist") is None)
initial_data = {}
if variations:
initial_data = dict([(f, getattr(variations[0], f)) for f in fields])
initial_data["quantity"] = 1
add_product_form = form_class(request.POST or None, product=product,
initial=initial_data, to_cart=to_cart)
if request.method == "POST":
if add_product_form.is_valid():
if to_cart:
quantity = add_product_form.cleaned_data["quantity"]
request.cart.add_item(add_product_form.variation, quantity)
recalculate_cart(request)
info(request, _("Item added to cart"))
return redirect("shop_cart")
else:
skus = request.wishlist
sku = add_product_form.variation.sku
if sku not in skus:
skus.append(sku)
info(request, _("Item added to wishlist"))
response = redirect("shop_wishlist")
set_cookie(response, "wishlist", ",".join(skus))
return response
related = []
if settings.SHOP_USE_RELATED_PRODUCTS:
related = product.related_products.published(for_user=request.user)
context = {
"product": product,
"editable_obj": product,
"images": product.images.all(),
"variations": variations,
"variations_json": variations_json,
"has_available_variations": any([v.has_price() for v in variations]),
"related_products": related,
"add_product_form": add_product_form
}
context.update(extra_context or {})
templates = [u"shop/%s.html" % str(product.slug), template]
return TemplateResponse(request, templates, context)
#never_cache
def wishlist(request, template="shop/wishlist.html",
form_class=AddProductForm, extra_context=None):
"""
Display the wishlist and handle removing items from the wishlist and
adding them to the cart.
"""
if not settings.SHOP_USE_WISHLIST:
raise Http404
skus = request.wishlist
error = None
if request.method == "POST":
to_cart = request.POST.get("add_cart")
add_product_form = form_class(request.POST or None,
to_cart=to_cart)
if to_cart:
if add_product_form.is_valid():
request.cart.add_item(add_product_form.variation, 1)
recalculate_cart(request)
message = _("Item added to cart")
url = "shop_cart"
else:
error = list(add_product_form.errors.values())[0]
else:
message = _("Item removed from wishlist")
url = "shop_wishlist"
sku = request.POST.get("sku")
if sku in skus:
skus.remove(sku)
if not error:
info(request, message)
response = redirect(url)
set_cookie(response, "wishlist", ",".join(skus))
return response
# Remove skus from the cookie that no longer exist.
published_products = Product.objects.published(for_user=request.user)
f = {"product__in": published_products, "sku__in": skus}
wishlist = ProductVariation.objects.filter(**f).select_related("product")
wishlist = sorted(wishlist, key=lambda v: skus.index(v.sku))
context = {"wishlist_items": wishlist, "error": error}
context.update(extra_context or {})
response = TemplateResponse(request, template, context)
if len(wishlist) < len(skus):
skus = [variation.sku for variation in wishlist]
set_cookie(response, "wishlist", ",".join(skus))
return response
#never_cache
def cart(request, template="shop/cart.html",
cart_formset_class=CartItemFormSet,
discount_form_class=DiscountForm,
extra_context=None):
"""
Display cart and handle removing items from the cart.
"""
cart_formset = cart_formset_class(instance=request.cart)
discount_form = discount_form_class(request, request.POST or None)
if request.method == "POST":
valid = True
if request.POST.get("update_cart"):
valid = request.cart.has_items()
if not valid:
# Session timed out.
info(request, _("Your cart has expired"))
else:
cart_formset = cart_formset_class(request.POST,
instance=request.cart)
valid = cart_formset.is_valid()
if valid:
cart_formset.save()
recalculate_cart(request)
info(request, _("Cart updated"))
else:
# Reset the cart formset so that the cart
# always indicates the correct quantities.
# The user is shown their invalid quantity
# via the error message, which we need to
# copy over to the new formset here.
errors = cart_formset._errors
cart_formset = cart_formset_class(instance=request.cart)
cart_formset._errors = errors
else:
valid = discount_form.is_valid()
if valid:
discount_form.set_discount()
# Potentially need to set shipping if a discount code
# was previously entered with free shipping, and then
# another was entered (replacing the old) without
# free shipping, *and* the user has already progressed
# to the final checkout step, which they'd go straight
# to when returning to checkout, bypassing billing and
# shipping details step where shipping is normally set.
recalculate_cart(request)
if valid:
return redirect("shop_cart")
context = {"cart_formset": cart_formset}
context.update(extra_context or {})
settings.use_editable()
if (settings.SHOP_DISCOUNT_FIELD_IN_CART and
DiscountCode.objects.active().exists()):
context["discount_form"] = discount_form
return TemplateResponse(request, template, context)
When you hit shop url, your application will try to use an empty url from your cartridge.shop.urls file. So basically when you would like to check which API / view is called, go to this file and look for something similar to this:
url(r'^$', 'your-view', name='your-view'),
ok after posting your second urls file you have following options:
you call:
/shop/wishlist/ - you are executing a view named wishlist
/shop/cart/ - you are executing a view named cart
/shop/checkout/complete/ - you are executing a view named complete
so just find your views.py file, and all those views are going to be there

memcache doesn't work as expected

We have a small application for Google App Engine in Python, and we are using memcache. But memcache keys remain even after memcache.delete, and also memcache returns a number (0) when I expect it to return a string ("undefined"). Here is my code:
check_feature.py:
import sys
sys.path.insert(0, 'libs')
import webapp2
import json
from google.appengine.api import memcache
from models.shard_counter import GeneralCounterShard
from models.check_feature_limit import CheckFeatureLimit
class CheckFeatureHandler(webapp2.RequestHandler):
def get_number_of_users_enabled(self, feature_name):
"""
Get the number of users enabled for the given feature name.
"""
number_of_users_enabled_undefined = "undefined"
number_of_users_enabled = memcache.get(key=feature_name)
if (number_of_users_enabled is None):
check_feature_limit = None
check_feature_limits = CheckFeatureLimit.gql("WHERE feature_name=:1 ORDER BY last_modified DESC LIMIT 1", feature_name)
if (check_feature_limits.count() > 0):
check_feature_limit = check_feature_limits.get()
if (check_feature_limit):
number_of_users_enabled = check_feature_limit.number_of_users_enabled
if (number_of_users_enabled is None):
number_of_users_enabled = number_of_users_enabled_undefined
memcache.add(key=feature_name, value=number_of_users_enabled, time=3600)
if (number_of_users_enabled == number_of_users_enabled_undefined):
number_of_users_enabled = None
return number_of_users_enabled
admin.py:
import sys
sys.path.insert(0, 'libs')
import webapp2
import json
import requests
from google.appengine.ext.webapp import template
from google.appengine.api import memcache
from models.shard_counter import GeneralCounterShard
from models.check_feature_limit import CheckFeatureLimit
template.register_template_library("tags.tags")
class AdminHandler(webapp2.RequestHandler):
def get(self):
self.post()
def post(self):
params = {}
number_of_users_enabled_dict = {}
number_of_users_dict = {}
r = requests.get(url="http://jsons.[part_of_link_suppressed].com.s3.amazonaws.com/flags.json")
flags = json.loads(r.text)
if ((flags) and ("depending_on_counter" in flags) and (len(flags["depending_on_counter"]) > 0)):
for feature_name in flags["depending_on_counter"]:
check_feature_limit = None
check_feature_limits = CheckFeatureLimit.gql("WHERE feature_name=:1 ORDER BY last_modified DESC LIMIT 1", feature_name)
if (check_feature_limits.count() > 0):
check_feature_limit = check_feature_limits.get()
number_of_users_enabled = self.request.get(feature_name + "_number_of_users_enabled")
if (number_of_users_enabled):
number_of_users_enabled = int(number_of_users_enabled)
if (not(check_feature_limit)):
check_feature_limit = CheckFeatureLimit(feature_name=feature_name)
check_feature_limit.number_of_users_enabled = number_of_users_enabled
check_feature_limit.put()
memcache.delete(key=feature_name) # I don't think it works.
number_of_users_enabled = None
if (check_feature_limit):
number_of_users_enabled = check_feature_limit.number_of_users_enabled
if (not(number_of_users_enabled is None)):
number_of_users_enabled_dict[feature_name] = number_of_users_enabled
number_of_users = GeneralCounterShard.get_count(feature_name)
number_of_users_dict[feature_name] = number_of_users
params["depending_on_counter"] = flags["depending_on_counter"]
params["number_of_users_enabled_dict"] = number_of_users_enabled_dict
params["number_of_users_dict"] = number_of_users_dict
html = template.render("admin/admin.html", params)
self.response.out.write(html)
app = webapp2.WSGIApplication([
("/admin", AdminHandler)
], debug=True)
The values of test_counter_feature_1 (Number of users to enable) is 2, test_counter_feature_2 is 4 and test_counter_feature_3 is undefined (there is no object), but in the memcache the values are 2, 3 and 0 respectively, even after I save the form (and therefore the memcache should be deleted). What is the problem? I expect the value of test_counter_feature_3 to be "undefined", not 0. And the two other values should be deleted after saving the form.
OK, I found the problem. GeneralCounterShard also saved the same key to memcache, so I renamed the key and everything works now. The new key is feature_name + "_number_of_users_enabled", instead of feature_name like it was before. So I replaced all the calls to memcache with the new key, and now it works. Thank you!

Categories