0

I want to call a python script with paramters from django view. The python script is stored in a subfolder (see attached picture).

Basically I have an input form and want to call crawl.py with the input data from that form. The form data should be stored in variable "production_number" in crawl.py.

view.py

from .forms import CustomerForm, LoginForm
from .models import Orders

from .ERPProgramm.crawl import crawlmain

def newOrders(request):

if request.method == 'POST':
    form = CustomerForm(request.POST)
    if form.is_valid():
        formdata = form.cleaned_data['product_ID']

        # call crawl.py with paramter formdata 

        return HttpResponseRedirect('/customer/newOrders')

crawl.py

import db
import sys
import requests
import json
from datetime import datetime

def query(resource):
    r = requests.get('http://11.111.11.11:8080/webapp/api/v1/' + resource,
        headers={'AuthenticationToken': '11111-11111-1111-1111-11111'}
    )
    return r


costumer_id = 1
production_number = formdata

d = query('productionOrder/?productionOrderNumber-eq={}'.format(production_number)).json()

session = db.Session()

costumer = session.query(db.Costumer).get(costumer_id)

if 'result' in d and len(d['result']) > 0:
    r = d['result'][0]
    order = db.Order()
    try:
        order.article_id = r['articleId']
        order.amount = r['targetQuantity']
        order.create_date = datetime.fromtimestamp(r['createdDate'] / 1000)
        order.start_date = datetime.fromtimestamp(r['targetStartDate'] / 1000)
        order.end_date = datetime.fromtimestamp(r['targetEndDate'] / 1000)
    except NameError as e:
        sys.exit('Error {}'.format(e.what()))

    article_number = r['articleNumber']
    d = query('article/?articleNumber-eq={}'.format(article_number)).json()

    if 'result' in d and len(d['result']) > 0:
        r = d['result'][0]
        article_image_id = r['articleImages'][0]['id']
        order.price_offer = r['articlePrices'][0]['price']

        r = query('article/id/{}/downloadArticleImage?articleImageId={}'.format(order.article_id, article_image_id))

        order.article_image = r.content
    else:
        print('No result for article with number', article_number)

    costumer.orders.append(order)

    session.add(costumer)
    session.commit()
else:
    print('No result for production order with article number', article_number)

How can I call crawl.py from django view?

Directory Overview

pap
  • 33
  • 8
  • 1
    I'd maybe put your crawl script into a function, with the crawl parameters as function arguments, then import the `crawler.py` into your `views.py`, you can the call the crawl function in your `newOrders` view if your form is valid. If the crawler takes a long time, it might be work running it in the background so the user can keep using your app, maybe look into RabbitMQ and celery if this is the case. – RHSmith159 Aug 23 '17 at 10:00
  • @pap: `NameError` exception has no `what()` method. Where do you find that? Why do you catch `NameError`? – Laurent LAPORTE Aug 23 '17 at 10:13
  • Your `crawl.py` script is not functional. Can you fix it and edit? – Laurent LAPORTE Aug 23 '17 at 10:17
  • @RHSmith159: I tried to do exact that. Then I receive an error message "NameError: name 'db' is not defined". db.py is a different script imported in crawl.py. The standalone script crawl.py is working fine without the name error. However, if called from django view, it yields that error. Do you have any idea, why this is happening? – pap Aug 23 '17 at 11:02
  • @pap interesting! It might be a naming conflict between a django module or class called `db` maybe try renaming the `db` script – RHSmith159 Aug 23 '17 at 11:04
  • @RHSmith159: I renamed the db.py script, but the same problem still occurs. I get this error (regardless of the name from db.py): from . import views, admin File "C:\Users\s229988\PycharmProjects\Platform\Website\Customer\views.py", line 12, in from .ERPProgramm.crawl import crawlmain File "C:\Users\s229988\PycharmProjects\Platform\Website\Customer\ERPProgramm\c rawl.py", line 1, in import db ImportError: No module named 'db' – pap Aug 23 '17 at 11:23
  • how about `import ERPProgramm.db` – RHSmith159 Aug 23 '17 at 12:06

1 Answers1

0

you can check this In your case you need to find exact file path for doing this you need to find the path from base dir in settings.py import it in view.py .

settings.py

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
FILE_DIR = os.path.abspath(os.path.join(BASE_DIR,'/Customer/ERPProgram')

views.py

import sys
sys.path.insert(0, os.path.join(settings.FILE_DIR))
import crawl
ketan khandagale
  • 460
  • 6
  • 22