Using django to run python scripts with password arguments
Question: I need to create a web interface that will prompt for a username, password,
and "record id" to run a separate python script using those credentials, and
then spit back a generated file.

The separate script I wrote takes a username/password (and ID) to connect to
an Oracle database (using cx_Oracle), gathers the relevant information,
generates a PDF and saves it to a preset location. It spits out the location
of the filename when it's done.

My thought was to create a django app that takes the following 3 parameters:

  * Username
  * Password
  * ID (this will be used to determine which records to fetch for the PDF)

Django will feed the parameters to the script by calling subprocess. I would
put this into my view like so:

    
    
    # ... 
    import subprocess
    
    # ...
    def login_view(request):
        username = password = ''
        if request.POST:
            username = request.POST.get('username')
            password = request.POST.get('password')
            record_id = request.POST.get('record_id')
    
            output = subprocess.check_output([
                "python", 
                "myscript.py", 
                "-id", 
                record_id, 
                "-u", 
                username, 
                "-p",
                password
            ])
            # The output will look like:
            # File /path/to/1234567.pdf saved
            fname = output.split(' ')[1]
    
            if fname.endswith('.pdf'):
                # Get the filename without the /path/to junk in front
                fname_nopath = fname.split('/')[-1]
    
                file = open(fname, 'rb')
                content = file.read()
                file.close
    
                response = HttpResponse(content, mimetype='application/pdf')
                response['Content-Disposition'] = 'attachment; filename=%s' % fname_nopath
    
                return response
            else:
                response = HttpResponse(output)
                return response
    

I think this method is a little "clunky", and I would think there's a more
elegant approach to take.

Another way I was thinking about would be to import the external script
directly into my views.py file, then call its main function using the
username, password, and record ID passed from django. This way I can avoid the
subprocess machinery. If needed I can create a separate main() function in the
script, have it return a filestring, and use that for both opening/serving the
file through django.

The only problem is this interface is going to be used pretty frequently by
users. To require them to login every time to generate a PDF report for each
record_id is a bit of a pain. Ideally I would like to keep the users logged in
(I think I would have to maintain a cx_Oracle cursor separately, and pass that
into the script).

My feeling is that this is overcomplicating things. Is there a better way to
do this or did I miss something?


Answer: 1) You should use django forms for handling user passed parameters.

2) You could save username/password in user's session- this way users won't
need to enter them again.

3) This really depends on how much record id's you have, but you could show a
list of available record id's by querying the database.

