Exemplo n.º 1
0
  def get(self):
    # Get logged in user.
    user = users.get_current_user()
    
    # If the user is not logged in, ask them to.
    if not user:
      html = '<a href="%s">Sign In</a>' % users.create_login_url('/')
      # html = '<a href="%s">Sign In</a>' % users.create_login_url('/')
      self.response.write(html)
      return

    # As an example, create and save one slot that starts now.
    time = datetime.datetime.now()
    date = time.date()
    midnight = datetime.datetime.combine(date, datetime.time())
    seconds = (time - midnight).seconds
    slot = seconds / 60 / 30
    name = user.nickname().replace(".", " ").title()
    slot = Slot(owner = name, date = date, slot = slot)
    slot.put()
    
    # Load last 10 slots.
    slots = Slot.query().order(-Slot.slot).fetch(10)
    
    # Set values to pass to HTML template.
    # added 'days' & 'hours' values - Jun21
    # Pass username in the same format as it is stored in DB - Jun24
    template_values = {
      'user': user,
      'slots': slots,
      'days': range(0,7),
      'hours': range(0, 24),
      'username': name,
    }
    template = JINJA_ENVIRONMENT.get_template('index.html')
    self.response.write(template.render(template_values))
Exemplo n.º 2
0
  def post(self):
    if not self._check_authentication():
      return
    
    params = self._get_parameters("slot", "date", "room")
    if not params:
      return
    slot = int(params[0])
    date = make_date(params[1])
    room = params[2]

    # Make sure they're not booking too far in advance.
    advance = date - datetime.datetime.now().date()
    advance = advance.days
    if advance > self.advance_booking:
      self._exit_handler("User booked too far in advance.")
      return

    # Make sure we're not double-booking.
    if Slot.query(ndb.AND(Slot.date == date, Slot.room == room,
        Slot.slot == slot)).get():
      self._exit_handler("User double-booked slot %d." % (slot))
      return

    name = self._get_name()
       
    # Slots reserved by the same user must be at least 2 hours apart.
    empty = self.empty_time / self.slot_length
    slots = Slot.query(ndb.AND(Slot.date == date, Slot.owner == name,
        Slot.room == room))

    block_edges = self._find_block_edges(slots)

    # Make sure we're not booking too large a block.
    failed = False
    for i in range(0, len(block_edges)):
      edge = block_edges[i]
      if (edge - slot == 1 and i % 2 == 0):
        # Adding to beginning.
        if block_edges[i + 1] - slot >= self.max_slots:
          failed = True
      elif (slot - edge == 1 and i % 2 == 1):
        # Adding to end.
        if slot - block_edges[i - 1] >= self.max_slots:
          failed = True

    if failed:
      self._exit_handler("User cannot book this many consecutive slots.")
      return

    # Only worth doing this if there are other slots booked.
    if len(block_edges) != 0:
      # Find the edges that are closest to our slot and get rid of everything
      # else.
      block_edges.append(slot)
      block_edges.sort()
      position = block_edges.index(slot)
      if position == 0:
        # This is a special case.
        block_edges = [block_edges[1]]
      else:
        block_edges = block_edges[(position - 1):]
        block_edges = block_edges[:3]
        block_edges.remove(slot)
      
      for booked_slot in block_edges:
        logging.info("Booked slot: %d." % (booked_slot))
        if (abs(int(booked_slot) - slot) != 1 and \
            abs(int(booked_slot) - slot) <= empty):
          self._exit_handler("User did not leave enough space between blocks.")
          return

    slot = Slot(owner = name, slot = slot, date = date, room = room)
    slot.put()
    logging.info("Saved slot.")

    self._exit_handler()