fork download
  1. def leaveapplication():
  2. form = LeaveApplicationForm()
  3. if form.validate_on_submit():
  4. leave = LeaveApplication(user_id=current_user.id ,leavetype_id=form.leavetype.data,
  5. date_created=datetime.utcnow(), date_from=form.date_from.data, date_to=form.date_to.data,
  6. description=form.description.data)
  7.  
  8. db.session.add(leave)
  9. db.session.commit()
  10. flash('Your application has been sent.')
  11.  
  12. # Calculate the difference between two dates
  13. leave_period = leave.date_to - leave.date_from
  14.  
  15. # Get the current user LeaveBalance
  16. current_leavebalance = LeaveBalance.query.filter_by(user_id=current_user.user_id, leavetype_id=leave.leavetype_id).first()
  17.  
  18. # Perform Calculation to deduct the leave_period from the current balance
  19. updated_leavebalance = current_leavebalance.credits - leave_period.days
  20. update = LeaveBalance.query.filter_by(user_id=leave.user_id)
  21. update.credits = updated_leavebalance
  22. db.session.add(update)
  23. db.session.commit()
Success #stdin #stdout 0.04s 63244KB
stdin
Standard input is empty
stdout
Standard output is empty