views:

155

answers:

1

I am adding MetaWeblog API support to a Django CMS, and am not quite sure how to layer the application.

I am using django_xmlrpc, which allows me to map to parameterised functions for each request. It is just a case of what level do I hook in calls to the django application from the service functions (AddPage, EditPage etc)

For django-page-cms, and I suppose many django apps, the business logic and validation is contained within the forms. In this case there is PageForm(forms.ModelForm) and PageAdmin(ModelAdmin), which both contain a lot of logic and validation.

If I am to build an API to allow maintenance of pages and content, does this mean I should be programmatically creating and filling a PageAdmin instance? Then catching any exceptions, and converting to their api equivalent? Or would this be a bad idea - misusing what forms are intended for?

The other option is refactoring the code so that business and logic is kept outside of the form classes. Then I would have the form and api, both go through the separate business logic.

Any other alternatives?

What would be the best solution?

+2  A: 

Web services API's are just more URL's.

These WS API URL's map to view functions.

The WS view functions handle GET and POST (and possibly PUT and DELETE).

The WS view functions use Forms as well as the Models to make things happen.

It is, in a way, like an admin interface. Except, there's no HTML.

The WS view functions respond with JSON messages or XML messages.

S.Lott
I already have the Web Service up and running. Its a case of which level in my app do I hook into.So for my service function AddPage() for example, you would then generate the admin form programmatically populate it and save?
Andrew Rimmer
I don't know what an "Admin Form" is. I do know what a model form is (http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#modelform) If you mean ModelForm, then yes. You use the Model Form to validate inputs, create model instances and save the objects to the database.
S.Lott
The layers of the cms go PageAdmin(ModelAdmin) > PageForm(ModelForm) Page(Model). At the moment the only way to update content is via the admin area. It looks like the best solution will be for my web service to use the PageForm, and then to refactor a little so that PageAdmin doesn't repeat the same business logic and validation.
Andrew Rimmer
@Andrew Rimmer. You can write your own view functions that do updates for web services. The built-in admin application returns HTML pages, which aren't really a good choice for web services requests. Write your own view functions -- it's much simpler.
S.Lott