Validation logic in Google App Engine

The part of the application that I am coding is structured like this:

class PostModel(db.Model):
    some_property = db.WhateverProperty()
    some_other_property = db.WhateverProperty()

class PostHandler(webapp.RequestHandler):
    def get(self):
        #code to generate form
    def post(self):
        #code to validate input from form
        #create entity and put() it to datastore if input passes the validation

Now, from what I read about MVC, this validation logic should be in the model, right? So should I do something like this instead?

class PostModel(db.Model):
    some_property = db.WhateverProperty()
    some_other_property = db.WhateverProperty()
    @staticmethod
    def validation_logic(form_input):
        #throw exceptions if validation fails
    @staticmethod
    def save_to_datastore(form_input):
        #this would assume data already passed validation
        #create entity and save it

class PostHandler(webapp.RequestHandler):
    def get(self):
        #code to generate form
    def post(self):
        try:
            PostModel.validation_logic(form_input)
        except CustomException,e:
            self.redirect('/errorpage?msg='+e.msg)
        PostModel.save_to_datastore(form_input)

Is this a good form of MVC?

+3
source share
1 answer

There are several ways to do this. Some form libraries will do most of the basic validation, but some things inevitably go unnoticed when you have more complex data.

, @classmethod . , save_to_datastore(), , , , . . : . , api, ..

+3

Source: https://habr.com/ru/post/1794775/


All Articles