Code snapshot 1
db.define_table('board'
Field('name')
)
db.define_table('post'
Field('author', auth.user_id, default=auth.user_id),
Field('board_id', "reference board"),
)
db.post.board_id.on_delete = "SET NULL"
##################
# Generate the URL via URL('default', 'create_post', args=[board.id],
user_signature=True)
@auth.requires_login()
@auth.requires_signature()
def create_post():
"""The board will be arg 0, so this is called as:
/default/create_post/<board_id>
"""
# Not necessary but a good idea.
board = db.board(request.args(0))
if board is None:
session.flash = T("No such board")
redirect(URL('default', 'index'))
form = SQLFORM(db.post)
#1 kind of works
db.post.board_id.default = board.id
#2 even a bit better
form.vars.board_id = board.id
if form.process().accepted:
# Go to view the board again.
Code snapshot 2
db.define_table('board'
Field('name')
)
db.define_table('post'
Field('author', auth.user_id, default=auth.user_id),
Field('board_id', "reference board"),
Field('posting_time', 'datetime', default=datetime.utcnow())
)
db.post.board_id.on_delete = "SET NULL"
##################
# Generate the URL via URL('default', 'create_post', args=[board.id])
@auth.requires_login()
def show_board():
"""The board will be arg 0, so this is called as:
/default/create_post/<board_id>
"""
# Not necessary but a good idea.
db.execute('SET TRANSACTION ISOLATION LEVEL SERIALIZABLE')
board = db.board(request.args(0))
if board is None:
session.flash = T("No such board")
redirect(URL('default', 'index'))
db.post.posting_time.readable = db.post.posting_time.writable = False
form = SQLFORM(db.post)
#1 kind of works
db.post.board_id.default = board.id
#2 even a bit better
form.vars.board_id = board.id
now = datetime.utcnow()
form.vars.posting_time = now
if form.process().accepted:
# Let's update the board time.
board.last_post_time = now
board.update_record()
try:
db.commit()
except Exception, e:
logger.warning("Transaction commit failed while updating board time")
else:
session.flash = T("Added")
redirect(URL('default', 'create_post', args=[board.id]))
# Go to view the board again.
# Read the posts.
posts = db(db.post.board_id == board.id).select()
return dict(form=form, posts=posts)
###
db.define_table('participates',
Field('hw', db.hw),
Field('student', db.student),
)
board_id: (last_post_time, checked_time)