I am working in a flask app that pass some arguments to tryton.
I imported Transaction in flask routes file. Something like described here:
from trytond.transaction import Transaction
@bp.route('/course/',methods=['GET', 'POST'])
@tryton.transaction(readonly=False,user=1)
def course():
with Transaction().set_context(courses=courses):
# do something
But it doesn’t pass the arguments to tryton. Do I need to use @tryton.transaction() or something similar?
I don’t know how to use it correctly to pass context with @tryton.transaction() because it is used like a decorator before define the function with the route.
How it is used inside the function and update the context?
I have the follow route in flask. Every lesson has a course, and need to pass it to tryton.
#from trytond.transaction import Transaction #doesn't workd
from flask_tryton import Transaction #doesn't work neither
@bp.errorhandler(404)
@bp.route('/<base>/<slug>/',methods=['GET', 'POST'])
@tryton.transaction(readonly=False,user=1)
@login_required
def lesson(base, slug):
if base and slug:
lessons = Lesson.search([
('course.slug','=',base),
('slug','=',slug)
])
if len(lessons)==1:
user = User(current_user)
defaults = {}
defaults['lessons'] = lessons
defaults['course'] = base
defaults['user'] = user
defaults['form'] = form
if len(comments)>0:
defaults['comments'] = comments
else:
defaults['comments'] = []
courses = []
courses.append(lessons[0].course.service.id)
#with Transaction().set_context(context={'courses':courses}) #doesn't work neither
with Transaction().set_context(courses=courses):
if lessons[0].membership_type == 'free':
return render_template('course/lesson.html', **defaults)
elif lessons[0].membership_type == 'premium' and user.active_membership:
return render_template('course/lesson.html', **defaults)
else:
return redirect(url_for('course.enroll',
course=lessons[0].course.id))
return render_template('404.html',user=user), 404
In tryton, I need to determine if an user can access or not the course.
@classmethod
def get_active_membership(cls, users, name):
pool = Pool()
User = pool.get('web.user')
Subscription = pool.get('sale.subscription')
transaction = Transaction().context
result = {}
for user in users:
if not user.party:
result[user.id] = False
return result
courses = transaction.get('courses') # These always is None
An Model instance keep a copy of the context that was used when it was instantiate. So if you want to change the context of an instance, you must re-instantiate below the new context.