Integrating Django into existing cmd project

Hello,

I am very new to Django so I apologize in advance if there are any misconceptions about the framework within this question. I have developed a log analysis program that currently uses the command-line as the user interface using the cmd2 library. I am attaching an image of the project structure with the different Python files. The program is started from log_analyze.py. The commands are parsed in user_interface.py. Other classes like sddc_log.py, get_logs.py, etc. contain logic that is used in the commands for parsing through logs. We currently run this program from VM that our teammates have access to. We are thinking of also creating a web UI for this program so that team members can connect to the VM from the browser to perform log analysis. My question is what would be the best way to organize a Django project that can leverage the existing logic we have in these other Python files and use that information to populate the web UI. Specifically, what would be the proper file structure? Are there any other aspects that I should pay specific attention to? Would this even be a good use case of Django? Thanks for your help in advance.

I believe about the easiest way to do this would be to create another directory in your project to store these files. They won’t be a “Django app” in the Django sense of the word, but can still be imported and have functions run by your views. (You might have “import” statements that may need to be changed - it’s just something to keep an eye on.)

The more sophisticated way of doing this would be to turn this into an installable package for those files to be installed in your virtual environment.

Either way, keep in mind that when you’re running code on the server, that code is running as the user that the server is running as (e.g. gunicorn or uwsgi) and not the user accessing the site. You may have file & directory permission issues to address as well.

(This type of thing does work - we have similar sites.)

Hey Ken,

Thanks for your reply. In the case of the second more sophisticated way, would the Django project be in a separate project directory altogether or could it still be within another directory in my project similar to the first way you gave?

Also, I had another design question as well: Consider if we would also like to have a REST API that also uses the same logic classes mentioned above. So in practice, there would be three avenues that our core logic Python files would be used: command-line, web, and REST API. Would that throw a wrench in anything? Would it be best practice for the REST API and the web page to have separate apps within one Django project? Thanks for your insight

Yes. You would have two separate projects. One being your general tools and utilities package installed in your virtual environment via pip, the other being your working Django project.

Nope, no problem at all.

Not necessarily. Depending upon the number and complexity of endpoints needed, I’d be tempted to have my views.py file have a general function that calls your utility application, and two views (“web” and “rest”) that accept the appropriate input, build whatever parameters are necessary, and then call that single general function.
Or, depending upon how sophisticated you want the web client to be, you could also construct your web interface such that it calls your REST endpoint rather than creating a separate view. You’ve got all sorts of options, and no single, generalized “right” answer outside the context of your specific requirements.

Hey Ken,

Thanks for your answers. I think I’m leaning towards installing the utility functions as a package into a completely new Django project. I’ll have to do some more consideration on whether I want to go the route of having the web interface call the REST endpoint. I’ll start developing this week and see how far I can get. Thanks for your insight. I’m going to mark you initial response as a solution.