0

Python (Flask) web application has a big portion of email/SMS messages generated by templates, translated by Flask-Babel.

To my eye, having literal constants for the message in views, among other view logic and calls to the other layers, is not elegant: Views are there to render the html or serve ajax calls, not render mail/SMS at the same time. Message literals in the models look even uglier, though they may seem to belong there.

However, the alternatives of extracting message-related functionality into some other module or package may also seem too artificial, because it makes it more difficult to compose the message, a lot of parameters may need to be included in the alls, making signatures unmanageable. Event-emitting/handling approach is just a variation of this (not sure whether it can be good?).

What could be a good way to keep the view code clean, and at the same time not making the distinction too artificial?

For simple example, form-controller may need to send a confirmation message (or even more than one) in case form validated and data received and stored in the database. There may be many details involved (names, dates, flags, etc) in the message rendering. How this can be done elegantly?

Personally, I think having a separate package just for messaging is quite similar to the views-templates strata web application has. And it may also have advantages in testability. Less sure about using event mechanisms to cause messages to be sent though. Maybe, I am missing something obvious already invented?

2 Answers 2

1

In the (server-side) MVC pattern, the View part is responsible for getting the model representation to the user. This can take many forms, like an HTML page that the browser can render, or a JSON document that a mobile app can interpret or even an e-mail or SMS message.

You are correct to say that a single View class should not be doing all those things, but a single action in a Controller is allowed to trigger updates to multiple View classes.
Thus, you could have one set of View classes for the HTML pages, a second set for the JSON and a third and fourth set for the e-mail and SMS messages.

Also, there is no prohibition in MVC for a View to push its content to the user, provided that you know how to reach the user.

1
  • Trying to apply this to Flask situation. If I understand correctly, Flask's view is template, and Controller is the function with route attached. So, basically this means I need to call "SMS-view's" controllers, which in their turn access their Views and maybe also access model... Some kind of hierarchical MVC? So, this means separate module(s) for SMS controllers and separate directory for SMS-views (templates). In a web-application backend setting MVC pattern is not that natural, but if understood more broadly, it will possibly do the job.
    – Roman Susi
    Sep 17, 2017 at 16:57
0

While researching more, I've found the following article, tackling almost exactly my question (judging from examples):

http://dev.nando.audio/2014/04/01/large_apps_with_sqlalchemy__architecture.html

Some key moments:

Web-frameworks such as Flask are using something, which can be called MTV (model, template, view, latter can be understood as "controller"). (see laso https://stackoverflow.com/questions/6621653/django-vs-model-view-controller ). While useful in simple cases, the need to send mail/SMS (it is used as example in the article, BTW) and similar non-trivial situations necessitate adding another layer to the architecture, layer called actions. It is where the transaction script for causing messaging should reside, and basically, where business rules are to dwell (the model layer is mainly for persistence, though some business rules can go there when related to single aggregates of entities).

In plain words, it means actions package and services for accessing external services. So, basically in my setup I am missing actions layer functionality.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.