def post(self):
    user = users.GetCurrentUser()

    jsonObj = json.loads(self.request.body)
    names = jsonObj["names"]
    events = jsonObj["events"]
    cycle_id = jsonObj["cycleId"]
    cycle_title = jsonObj["title"]

    cycle = None
    user_event_map = {}
    # Get the cycle and any existing users in the cycle.
    if (cycle_id):
      logging.info("getting cycle with id: " + cycle_id)
      cycle = db.get(db.Key.from_path(Cycle.kind(), cycle_id))
      if (cycle):
        logging.info("got cycle: " + str(cycle))
        user_event_map = util.getEmailToEventId(cycle)
    if (not cycle):
      # Create the cycle db entry
      if (not cycle_id):
        os_rand = os.urandom(30)
        cycle_id = id = hashlib.sha1(user.nickname() + '-' +
                   os_rand).hexdigest()

      cycle = Cycle(key_name = cycle_id, initiator = user, title = cycle_title)
      cycle.put()
      logging.info("Cycle with id \"" + cycle_id + "\" created by " +
          user.nickname())

    # Create the user db entries
    for event in events:
      logging.info("event: " + str(event))
      creator = event["owner"]
      if (not creator in names):
        logging.info("skipped due to creator being unselected: " + creator)
        continue
      if (not creator in user_event_map):
        user_event_map[creator] = []
      # Check to make sure we haven't already processed the event.
      if (not event["eventId"] in user_event_map[creator]):
        # Create a new event and put it in the map.
        user_event_map[creator].append(event)
        # Save the event for later.
        db_event = CalendarEvent(
            parent = cycle,
            owner = creator,
            calendar_id = event["calendarId"],
            event_id = event["eventId"],
            summary = event["summary"],
            event_location = event["location"],
            recurrence = event["recurrence"],
            start_time = event["startTime"],
            link = event["link"],
            state = "Pending")
        db_event.put()
        logging.info("created event: " + event["eventId"])
  def post(self):
    user = users.GetCurrentUser()
    # TODO: Validate cycle owned by or shared with current user.

    cycle_id = self.request.get("id")

    cycle = db.get(db.Key.from_path(Cycle.kind(), cycle_id))
    if (not cycle):
      self.response.set_status(404, 'Cycle with id ' + cycle_id + ' not found.')
      return

    page_token = self.request.get("page")
    if (not page_token):
      page_token = 0
    user_event_map_result = util.getEmailToEventObject(cycle, int(page_token))
    user_event_map = user_event_map_result["events"]

    response = {
        "title": cycle.title,
        "events": user_event_map,
        "more_to_come": user_event_map_result["more_to_come"],
        "next_page": user_event_map_result["next_page"]
      }

    self.response.out.write(json.dumps(response))
  def post(self):
    user = users.GetCurrentUser()
    # TODO: Validate cycle owned by or shared with current user.

    cycle_id = self.request.get("id")
    cycle = db.get(db.Key.from_path(Cycle.kind(), cycle_id))
    if not cycle:
      self.response.set_status(404, 'Cycle with id ' + cycle_id + ' not found.')

    cycle.title = self.request.get("title")
    cycle.put()
  def post(self):
    user = users.GetCurrentUser()
    # TODO: Validate user is owner of current cycle.

    cycle_id = self.request.get('id')
    cycle = db.get(db.Key.from_path(Cycle.kind(), cycle_id))
    if not cycle:
      self.response.set_status(404, 'Cycle with id ' + cycle_id + ' not found.')
      return

    events_query = CalendarEvent.all(keys_only=True)
    events_query.ancestor(cycle)
    db.delete(events_query.run())
    db.delete(cycle)
    logging.info('Deleted cycle with id ' + cycle_id)
    def post(self):
        user = users.GetCurrentUser()
        # TODO: Validate user is owner of the cycle containing the event.

        event_id = self.request.get("event_id")
        cycle_id = self.request.get("cycle_id")
        event = None

        if event_id and cycle_id:
            cycle_key = db.Key.from_path(Cycle.kind(), cycle_id)
            event = CalendarEvent.gql("WHERE event_id = :1 AND ANCESTOR IS :2", event_id, cycle_key).get()

        if event is not None:
            event.delete()
            logging.info("Deleted event with id " + event_id)
        else:
            self.response.set_status(404, "Event with id %s not found in " "cycle with id %s" % (event_id, cycle_id))
    def get(self):
        user = users.GetCurrentUser()
        if not user:
            url = users.create_login_url(self.request.uri)
            self.redirect(url)
            return

        cycles = []
        for cycle in Cycle.gql("WHERE initiator = :1", user).run(batch_size=10):
            cycle.cycle_id = cycle.key().name()
            cycles.append(cycle)

        cycles.sort(key=lambda cycle: cycle.start_date, reverse=True)
        logging.info("Got cycles: " + str(cycles))

        template_values = {"name": user.nickname(), "cycles": cycles}
        html = template.render("django/dashboard.html", template_values)
        self.response.out.write(html)