Google App Engine 中的验证逻辑

发布于 2024-10-19 01:06:22 字数 1194 浏览 1 评论 0原文

我正在编码的应用程序的一部分的结构大致如下:

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

现在,从我读到的有关 MVC 的内容来看,此验证逻辑应该位于模型中,对吧?那么,我应该做这样的事情吗?

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)

这是好的 MVC 形式吗?

Part of the app I'm coding is structured roughly 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 good MVC form?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥抱没勇气 2024-10-26 01:06:22

有几种方法可以做到这一点。某些表单库将完成大部分基本验证,但当您拥有更复杂的数据时,不可避免地会遗漏一些内容。

我认为将值字典传递给模型的 @classmethod 并让它验证数据是个好主意。我通常有一个像 save_to_datastore() 这样的类方法,基本上用于验证和组装要保存的实体,而不是在处理程序中执行它。我不想在处理程序数据存储中包含特定的东西。例如:使用模型类方法进行查询,而不是直接在处理程序中创建查询。它让你认为模型作为一个 API,更容易维护和跟踪索引等。

There are a few ways of doing it. Some form libraries will do most of the basic validation, but some things are inevitably left out when you have more complex data.

I think it is a good idea to pass a dictionary of values to a @classmethod of the model, and let it validate the data. I usually have a class method like your save_to_datastore(), basically used to validate and assemble an entity to be saved instead of doing it in the handler. I prefer to not have in a handler datastore specific things. For example: use model class methods for queries instead of creating queries directly in the handler. It makes you think the model as an api, is easier to maintain and keep track of indexes etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文