<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def schedule():
 """HTTP endpoint for scheduling tasks If a task with the same code already exists, the one with the shorter interval will be made active. """

  code = request.form['code']
  interval = int(request.form['interval'])

  task_id = binascii.b2a_hex(os.urandom(5))
  new_task = Task(id=task_id)
  new_task.active = True
  new_task.code = code
  new_task.interval = interval

  # TODO(derek): Assert there is only one other_task
  other_task = Task.query.filter_by(code=code, active=True).first()

  if other_task:
    if other_task.interval <= new_task.interval:
      new_task.active = False
    else:
      other_task.active = False
      other_task.save()
      current_app.scheduler.cancel(other_task.id)

  if new_task.active:
    print current_app.scheduler.schedule
    current_app.scheduler.schedule({
      'id': task_id,
      'code': new_task.code,
      'interval': new_task.interval
    })

  new_task.save()

  return json.dumps({
    'status': 'success',
    'id': task_id,
  })