Hi,

I have an existing web application written in the Python web framework Flask (http://flask.pocoo.org/).
I am considering using Drupal as a front end for this application. I.e.: I want to use Drupal on the main site: www.example.com. Drupal will then function as a normal CMS. Some of the pages in drupal will be "special" in that they contain a form that submits data (either directly or through some client side AJAX) to app.example.com on which the existing Flask web application resides. Drupal should then handle the results and display the results in a proper template.

The web application is a shop-like application that allows the user to search for various products using very specific criteria. It includes HTML forms as well as presents search results/products using HTML and images.
I imagine that I will have to rewrite the existing Flask application so that it functions as a RESTful service and instead of returning HTML/pictures for search results, it instead returns a JSON response with all the necessary data that Drupal can then display nicely in a template.

So my question is: What is the best way of using Drupal as a front end to an existing web application?

Is this even possible (or advisable)?

Where do I start? Should this be made as a plugin for Drupal? Or can it be done entirely by making special templates and jQuery?

I have considered using Drupal Services because it sounded like it was an API for this, however, it turned out that Services solves the reverse problem: Makes it possible to use an external app as front end for a Drupal site.

I do not know much about Drupal. The reason I am considering Drupal is that the users are already familiar with using Drupal as a CMS. I do have several years experience with PHP.

Some extra info about the web app:

* Web app is written in Python/Flask
* It is a web shop like application that allows users to search for products based on various criteria.
* Web app is highly modular and cleanly split into views, forms, model, so it would be fairly straight forward to change the web app in order to present data via another protocol/API. I figure it would be possible to create new views that present the data in JSON or something like that and I hope that Drupal can somehow pick that up and display it in its templates.
* Database is PostgreSQL
* Frameworks used: Flask, SQLAlchemy, WTForms

Comments

WorldFallz’s picture

...I will have to rewrite the existing Flask application so that it functions as a RESTful service and instead of returning HTML/pictures for search results...

It would be one thing if you just wanted to interface with an existing rest or soap enabled application the way it is. But to rewrite it for that only to use drupal as the front end strikes me as more trouble than it's worth. Integrations never work as well as a single properly designed app.

Drupal commerce is an amazing powerful, flexible and customizable 'web shop' system built from the ground up by highly proficient and experienced drupal shop developers. If it were me, I wouldn't wast time writing a rest/soap api-- I'd simply build the thing in drupal using drupal commerce.

tdn’s picture

WorldFallz, thanks for your response.

I agree that it would be better if I could have just one properly designed app that did it all. Or to use an existing product like Drupal Commerce that did everything already would be great. Unfortunately, this is not an option. The existing app already does what it is supposed to. It is a non-trivial application with an elaborate back end with a lots of complex scheduled jobs, queue systems, etc. So that is the reason I try to avoid porting it all to Drupal.

I want to use Drupal as CMS for the traditional web content like About us, Contact us, etc. As well as other basic Drupal functionality like blog, comments, etc.

The only interaction between Drupal and Flask app is going to be search and display of products. The existing app has an admin interface for updating product information and so on.

Also, adding REST/JSON interfaces to the current app for search and product display is not a major task. I have already done some POC stuff and seen it work. Now I just need a CMS that will interact with it.
I have looked a bit into Drupal Module Developer's Guide. As I understand, this should be possible to do by creating a custom Drupal module that will create a new custom page type. I then need to make a handler in PHP that will get and parse the JSON data from the existing app and then hand it off to the Drupal template to render it as a web page. Is my understanding correct?

I would love some pointers to basic examples of how a minimal Drupal module can look like that will create a custom page type that will fetch data from somewhere (anywhere really, like a database, a CSV file or something like that will do) and display it. That would be a great starting point.

WorldFallz’s picture

Definitely makes sense then. The first thing you'll want to decide is whether or not you want to cache the data locally for the drupal site to use/search or always access it remotely. Users tend to get pretty impatient with a slow search so, unless your pipe is really really fast, you might want to consider caching it locally and then linking back to the parent app where necessary.

And you probably don't need to write a custom module-- custom code should always be the last option, not the first you attempt.

There are several ways to interact with external data. If caching the data locally is ok, you can use feeds to pull over the necessary data and store it as stub nodes or use it with the data module to place it in dumb tables. Both ways allows you to use the views module with views exposed filters to create an awesome search experience without writing a single line of custom code. There are feeds plugins for just about any data source you might need (xml, xls, csv, web services, dbs)-- depending on the db, you might not even have to create a rest/soap interface as there is a plugin to connect directly to a db.

I use this method for a portal I created that pulls data from various sources (files, dbs, services, etc) into a central repository that then creates displays graphs and reports (using views) for all that different data in one location. I have feeds setup for each of the different data sources that get updated nightly and I reformat the displayed links to link back to the original source of the data rather than the drupal stub node where possible. Not every point of data has a user accessible UI so I also get the added benefit of being able to present a user friendly display of data that would otherwise be inaccessible.

This method works brilliantly and I only had to create one custom plugin for the wsclient_feeds module for a webservice that doesn't use standard authentication-- so maybe 6 - 12 lines of custom code total.

If you decide not to cache the data locally there's also a couple of options, though I haven't had a chance to take these for a test drive myself yet (my network is always an issue so I can't rely on remote access, lol) so I can't speak to it personally. For some good info see:

I would recommend any of those methods before resorting to custom code. However, having said that, creating a minimal module to handle a particular url is pretty simple-- it's basically a .info file and a .module file. In the .module file you'll implement hook_menu in order to define the function that handles the url you specify and then write the code for the function that does what you want to happen at that path.

The real bulk of the work will be writing and maintaining the code required to do what you want to happen at that page.

For more info see: