Ask Your Question
0

Why "must add revision before saving the answer"?

asked 2013-06-15 20:14:52 -0500

kate_r gravatar image

Hi

In models.post, there's the following:

    #must add revision before saving the answer
    self.add_revision(
        author = edited_by,
        revised_at = edited_at,
        text = text,
        comment = comment,
        by_email = by_email
    )

How come the revision has to be saved before the answer?

Thanks

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2013-06-15 20:19:24 -0500

Evgeny gravatar image

updated 2013-06-15 20:21:54 -0500

Not sure what your intention is. The safest approach is to use function user.post_answer(). There are also post_question() and post_comment() - those will take care of all the underlying records and will test the permissions.

If you want to follow the underlying mechanics - basically any time a foreign key relation to another object - that one has to be created first. In this case - answer object is created first, then we create the revision, because revision requires the foreign key to the answer post.

edit flag offensive delete link more

Comments

I might not be reading the code right. Isn't the revision created before the answer here? self.add_revision(..) is called before self.parse_save(..), which saves the answer object. It only doesn't crash here because the post object itself would have already been created (because it's an edit function). My point is mostly that the comment "must add revision before saving the answer" might perhaps be misleading?

kate_r gravatar imagekate_r ( 2013-06-15 20:44:26 -0500 )edit

That's probably in the edit function - in that case we already have the post id to use for the foreign key. In the create answer function we do this in the opposite order, see PostManager.create_new.

Evgeny gravatar imageEvgeny ( 2013-06-15 20:47:46 -0500 )edit

If you use functions added to the user object, you can bypass all those details (and there are more, actually).

Evgeny gravatar imageEvgeny ( 2013-06-15 20:50:12 -0500 )edit

Sure. Thanks for the pointers. So does that mean that comment in the edit function doesn't actually hold?

kate_r gravatar imagekate_r ( 2013-06-15 20:54:36 -0500 )edit

No it all works, just in the edit function we have the post, so we can safely create a revision. When we make a new post - we first make the post and then add the revision. See functions available in the askbot/models/__init__.py, added via User.add_to_class - they start at about 75% down the file.

Evgeny gravatar imageEvgeny ( 2013-06-15 20:58:03 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2013-06-15 20:14:52 -0500

Seen: 55 times

Last updated: Jun 15 '13