Exemplo n.º 1
0
  def get(self):
    list, list_url = get_list_from_url(self.request.get('list'))
    msg = None
    if list is None:
      render(self, 'error.html', msg = 'The specified list does not exist.')
      return
    # If the last time the list was updated was over 12 hours ago, schedule an
    # update
    if list.last_fetched_time < datetime.now() - timedelta(hours=12):
      schedule_list_update(list)
      msg = 'Fetching new messages...'

    threads = Thread.all().filter('list_url =', list_url).\
                     order('-last_message_time')
    formatted_threads = []
    for t in threads:
      if len(t.participants) > 3:
        t.participants = [t.participants[0], '...'] + t.participants[-2:]
      t.participants = [', '.join(p.split()[0] for p in  t.participants)]
      t.subject = strip_tags(t.subject)
      t.last_message_body = strip_tags(t.last_message_body)[:100]
      one_day = timedelta(days=1)
      if t.last_message_time > datetime.now() - timedelta(days=1):
        t.short_date = datetime.strftime(t.last_message_time, '%H:%M%p')
      elif t.last_message_time > datetime.now() - timedelta(days=365):
        t.short_date = datetime.strftime(t.last_message_time, '%b %d')
      else:
        t.short_date = datetime.strftime(t.last_message_time, '%D')
      t.read = False
      formatted_threads.append(t)

    user = users.get_current_user()
    if user:
      def lookup():
        if len(lookup_pool) == 0:
          return
        for ut in UserThread.all().filter('thread_id IN', lookup_pool):
          last_viewed[ut.thread_id] = ut.last_viewed
        del lookup_pool[:]

      last_viewed = {} # maps from thread_ids to last viewed
      lookup_pool = []
      for t in formatted_threads:
        lookup_pool.append(t.thread_id)
        if len(lookup_pool) == 30:
          lookup()
      lookup()

      for t in formatted_threads:
        if t.thread_id in last_viewed:
          if t.last_message_time <= last_viewed[t.thread_id]:
            t.read = True

    render(self, 'view.html', list = list, threads = formatted_threads,
                              msg = msg)
Exemplo n.º 2
0
  def get(self):
    thread_id = self.request.get('thread')
    thread = gql_limit1(Thread, thread_id = thread_id)
    messages = Message.all().filter('thread_id = ', thread_id)

    formatted_messages = []
    for message in messages:
      if message.content_type == 'text/plain':
        message.body = strip_tags(message.body).strip().\
                                                replace(' ', '&nbsp;').\
                                                replace('\n\n', '</p><p>').\
                                                replace('\n', '<br>')
        message.body = '<p>' + message.body + '</p>'
      formatted_messages.append(message)
    
    if len(formatted_messages) == 0:
      render(self, 'info.html', error_message = "No messages in this thread.")
      return

    user = users.get_current_user()
    already_seen = None
    if user:
      # If user is logged in, mark these messages as viewed.
      max_viewed = max(msg.date for msg in formatted_messages)
      userthread = gql_limit1(UserThread, thread_id = thread_id, user = user)
      if not userthread:
        userthread = UserThread(user = user, thread_id = thread_id,
                                last_viewed = max_viewed)
      else:
        already_seen = userthread.last_viewed
        for message in formatted_messages:
          if message.date <= already_seen:
            message.read = True
      userthread.last_viewed = max_viewed
      userthread.put()


    render(self, 'thread.html', thread=thread, already_seen=already_seen,
           messages=formatted_messages)