示例#1
0
def _assemble_inbox_data(request, entries, actor_streams, inbox_owner_ref):
  stream_keys = [e.stream for e in entries]
  stream_keys += [s.key().name() for s in actor_streams]
  streams = api.stream_get_streams(request.user, stream_keys)

  contact_nicks = api.actor_get_contacts_safe(request.user,
                                              inbox_owner_ref.nick,
                                              limit=CONTACTS_PER_PAGE)
  channel_nicks = api.actor_get_channels_member_safe(request.user,
                                                     inbox_owner_ref.nick,
                                                     limit=CHANNELS_PER_PAGE)
  actor_nicks = (contact_nicks +
                 channel_nicks +
                 [inbox_owner_ref.nick] +
                 [s.owner for s in streams.values()] +
                 [e.owner for e in entries] +
                 [e.actor for e in entries])
  actors = api.actor_get_actors(request.user, actor_nicks)

  # here comes lots of munging data into shape
  contacts = [actors[x] for x in contact_nicks if actors[x]]
  channels = [actors[x] for x in channel_nicks if actors[x]]
  streams = display.prep_stream_dict(streams, actors)
  entries = display.prep_entry_list(entries, streams, actors)

  return (contacts, channels, streams, entries)
示例#2
0
文件: views.py 项目: webatat/webatat
def _assemble_inbox_data(request, entries, actor_streams, inbox_owner_ref):
  stream_keys = [e.stream for e in entries]
  stream_keys += [s.key().name() for s in actor_streams]
  streams = api.stream_get_streams(request.user, stream_keys)

  contact_nicks = api.actor_get_contacts_safe(request.user,
                                              inbox_owner_ref.nick,
                                              limit=CONTACTS_PER_PAGE)
  channel_nicks = api.actor_get_channels_member_safe(request.user,
                                                     inbox_owner_ref.nick,
                                                     limit=CHANNELS_PER_PAGE)
  actor_nicks = (contact_nicks +
                 channel_nicks +
                 [inbox_owner_ref.nick] +
                 [s.owner for s in streams.values()] +
                 [e.owner for e in entries] +
                 [e.actor for e in entries])
  actors = api.actor_get_actors(request.user, actor_nicks)

  # here comes lots of munging data into shape
  contacts = [actors[x] for x in contact_nicks if actors[x]]
  channels = [actors[x] for x in channel_nicks if actors[x]]
  streams = display.prep_stream_dict(streams, actors)
  entries = display.prep_entry_list(entries, streams, actors)

  return (contacts, channels, streams, entries)
示例#3
0
def actor_followers(request, nick=None, format='html'):
    nick = clean.nick(nick)

    view = api.actor_lookup_nick(request.user, nick)

    if not view:
        raise exception.UserDoesNotExistError(nick, request.user)

    handled = common_views.handle_view_action(
        request, {
            'actor_add_contact': request.path,
            'actor_remove_contact': request.path,
        })
    if handled:
        return handled

    per_page = CONTACTS_PER_PAGE
    offset, prev = util.page_offset_nick(request)

    follower_nicks = api.actor_get_followers(request.user,
                                             view.nick,
                                             limit=(per_page + 1),
                                             offset=offset)
    actor_nicks = follower_nicks
    actors = api.actor_get_actors(request.user, actor_nicks)
    # clear deleted actors
    actors = dict([(k, v) for k, v in actors.iteritems() if v])
    per_page = per_page - (len(follower_nicks) - len(actors))

    # TODO(termie): incorporate this into paging so we only fetch the range
    #               on this page
    # add some extra info so we can let the user do contextual actions
    # on these homeboys
    if request.user and request.user.nick == view.nick:
        for actor in actors:
            if api.actor_is_contact(request.user, view.nick, actor):
                actors[actor].my_contact = True
        whose = 'Your'
    else:
        whose = "%s's" % view.display_nick()

    # here comes lots of munging data into shape
    actor_tiles = [actors[x] for x in follower_nicks if x in actors]

    actor_tiles_count = view.extra.get('follower_count', 0)
    actor_tiles, actor_tiles_more = util.page_actors(request, actor_tiles,
                                                     per_page)

    area = 'people'

    c = template.RequestContext(request, locals())

    # TODO: Other output formats.
    if format == 'html':
        t = loader.get_template('actor/templates/followers.html')
        return http.HttpResponse(t.render(c))
示例#4
0
文件: views.py 项目: webatat/webatat
def actor_followers(request, nick=None, format='html'):
  nick = clean.nick(nick)

  view = api.actor_lookup_nick(request.user, nick)

  if not view:
    raise exception.UserDoesNotExistError(nick, request.user)

  handled = common_views.handle_view_action(
      request,
      { 'actor_add_contact': request.path,
        'actor_remove_contact': request.path, })
  if handled:
    return handled

  per_page = CONTACTS_PER_PAGE
  offset, prev = util.page_offset_nick(request)

  follower_nicks = api.actor_get_followers(request.user,
                                           view.nick,
                                           limit=(per_page + 1),
                                           offset=offset)
  actor_nicks = follower_nicks
  actors = api.actor_get_actors(request.user, actor_nicks)
  # clear deleted actors
  actors = dict([(k, v) for k, v in actors.iteritems() if v])
  per_page = per_page - (len(follower_nicks) - len(actors))

  # TODO(termie): incorporate this into paging so we only fetch the range
  #               on this page
  # add some extra info so we can let the user do contextual actions
  # on these homeboys
  if request.user and request.user.nick == view.nick:
    for actor in actors:
      if api.actor_is_contact(request.user, view.nick, actor):
        actors[actor].my_contact = True
    whose = 'Your'
  else:
    whose = "%s's" % view.display_nick()

  # here comes lots of munging data into shape
  actor_tiles = [actors[x] for x in follower_nicks if x in actors]

  actor_tiles_count = view.extra.get('follower_count', 0)
  actor_tiles, actor_tiles_more = util.page_actors(request,
                                                   actor_tiles,
                                                   per_page)

  area = 'people'

  c = template.RequestContext(request, locals())

  # TODO: Other output formats.
  if format == 'html':
    t = loader.get_template('actor/templates/followers.html')
    return http.HttpResponse(t.render(c))
示例#5
0
def channel_item(request, nick, item=None, format='html'):
  nick = clean.channel(nick)
  view = api.actor_lookup_nick(request.user, nick)

  if not view:
    raise http.Http404()

  stream_ref = api.stream_get_presence(request.user, view.nick)

  entry = '%s/%s' % (stream_ref.key().name(), item)

  entry_ref = api.entry_get(request.user, entry)
  if not entry_ref:
    raise http.Http404()

  handled = common_views.handle_view_action(
      request,
      {'entry_add_comment': entry_ref.url(), 
       'entry_remove': view.url(),
       'entry_remove_comment': entry_ref.url(),
       'entry_mark_as_spam': entry_ref.url()
       }
      )
  if handled:
    return handled

  admins = api.channel_get_admins(request.user, channel=view.nick)
  user_is_admin = request.user and request.user.nick in admins

  comments = api.entry_get_comments(request.user, entry)

  actor_nicks = [entry_ref.owner, entry_ref.actor] + [c.actor for c in comments]
  actors = api.actor_get_actors(request.user, actor_nicks)

  # display munge
  entry = display.prep_entry(entry_ref,
                             { stream_ref.key().name(): stream_ref },
                             actors)
  comments = display.prep_comment_list(comments, actors)

  # config for template
  green_top = True
  sidebar_green_top = True

  # rendering
  c = template.RequestContext(request, locals())
  if format == 'html':
    t = loader.get_template('channel/templates/item.html')
    return http.HttpResponse(t.render(c))
  elif format == 'json':
    t = loader.get_template('actor/templates/item.json')
    r = http.HttpResponse(t.render(c))
    r['Content-type'] = 'text/javascript'
    return r
示例#6
0
def invite_email(request, code):
  """User has received the invite email, and has followed the link to accept or
     or refuse it."""
  
  if request.user:
    handled = common_views.handle_view_action(
        request,
        {'invite_accept': request.user.url('/overview'),
         'invite_reject': request.user.url('/overview')
         }
        )
    if handled:
      return handled

  # Retrieve the invite
  invite = api.invite_get(api.ROOT, code)
  from_actor = invite.from_actor
  
  # Translate the from_actor into a display name
  from_actor_ref = api.actor_get(api.ROOT, from_actor)
  view = from_actor_ref

  if not from_actor_ref:
    # Corner case: from_actor was deleted since the invite was sent.
    # In this case, do we want to consider the invitation invalid?
    # (probably we do, because it's more likely that it was spam)
    return util.RedirectError("That invite is no longer valid")
    
    
  # We use api.ROOT in the next set of functions because the
  # invite is giving possibly private access to the user
  inbox = api.inbox_get_actor_contacts(api.ROOT,
                                       view.nick,
                                       limit=5)
  entries = api.entry_get_entries(api.ROOT, inbox)
  stream_keys = [e.stream for e in entries]
  streams = api.stream_get_streams(api.ROOT, stream_keys)
  actor_nicks = ([view.nick] +
                 [s.owner for s in streams.values() if s] +
                 [e.owner for e in entries] +
                 [e.actor for e in entries])
  actors = api.actor_get_actors(api.ROOT, actor_nicks)

  streams = display.prep_stream_dict(streams, actors)
  entries = display.prep_entry_list(entries, streams, actors)


  sidebar_green_top = True
  c = template.RequestContext(request, locals())

  t = loader.get_template('email.html')
  return http.HttpResponse(t.render(c))
示例#7
0
def explore_recent(request, format="html"):

    per_page = ENTRIES_PER_PAGE
    offset, prev = util.page_offset(request)

    inbox = api.inbox_get_explore(request.user,
                                  limit=(per_page + 1),
                                  offset=offset)

    # START inbox generation chaos
    # TODO(termie): refacccttttooorrrrr
    entries = api.entry_get_entries(request.user, inbox)
    per_page = per_page - (len(inbox) - len(entries))
    entries, more = util.page_entries(request, entries, per_page)

    stream_keys = [e.stream for e in entries]

    streams = api.stream_get_streams(request.user, stream_keys)

    actor_nicks = [e.owner for e in entries] + [e.actor for e in entries]
    actors = api.actor_get_actors(request.user, actor_nicks)

    # here comes lots of munging data into shape
    streams = prep_stream_dict(streams, actors)
    entries = prep_entry_list(entries, streams, actors)

    # END inbox generation chaos

    area = 'explore'
    sidebar_green_top = True

    c = template.RequestContext(request, locals())

    if format == 'html':
        t = loader.get_template('explore/templates/recent.html')
        return http.HttpResponse(t.render(c))
    elif format == 'json':
        t = loader.get_template('explore/templates/recent.json')
        r = util.HttpJsonResponse(t.render(c), request)
        return r
    elif format == 'atom':
        t = loader.get_template('explore/templates/recent.atom')
        r = util.HttpAtomResponse(t.render(c), request)
        return r
    elif format == 'rss':
        t = loader.get_template('explore/templates/recent.rss')
        r = util.HttpRssResponse(t.render(c), request)
        return r
示例#8
0
def explore_recent(request, format="html"):

  per_page = ENTRIES_PER_PAGE
  offset, prev = util.page_offset(request)

  inbox = api.inbox_get_explore(request.user, limit=(per_page + 1),
                                offset=offset)

  # START inbox generation chaos
  # TODO(termie): refacccttttooorrrrr
  entries = api.entry_get_entries(request.user, inbox)
  per_page = per_page - (len(inbox) - len(entries))
  entries, more = util.page_entries(request, entries, per_page)

  stream_keys = [e.stream for e in entries]

  streams = api.stream_get_streams(request.user, stream_keys)

  actor_nicks = [e.owner for e in entries] + [e.actor for e in entries]
  actors = api.actor_get_actors(request.user, actor_nicks)

  # here comes lots of munging data into shape
  streams = prep_stream_dict(streams, actors)
  entries = prep_entry_list(entries, streams, actors)

  # END inbox generation chaos

  area = 'explore'
  sidebar_green_top = True
  
  c = template.RequestContext(request, locals())

  if format == 'html':
    t = loader.get_template('recent.html')
    return http.HttpResponse(t.render(c));
  elif format == 'json':
    t = loader.get_template('recent.json')
    r = util.HttpJsonResponse(t.render(c), request)
    return r
  elif format == 'atom':
    t = loader.get_template('recent.atom')
    r = util.HttpAtomResponse(t.render(c), request)
    return r
  elif format == 'rss':
    t = loader.get_template('recent.rss')
    r = util.HttpRssResponse(t.render(c), request)
    return r
示例#9
0
def channel_members(request, nick=None, format='html'):
  nick = clean.channel(nick)

  view = api.actor_lookup_nick(request.user, nick)

  if not view:
    raise exception.UserDoesNotExistError(nick, request.user)

  handled = common_views.handle_view_action(
      request,
      { 'actor_add_contact': request.path,
        'actor_remove_contact': request.path, })
  if handled:
    return handled

  per_page = CONTACTS_PER_PAGE
  offset, prev = util.page_offset_nick(request)

  follower_nicks = api.channel_get_members(request.user,
                                           view.nick,
                                           limit=(per_page + 1),
                                           offset=offset)
  actor_nicks = follower_nicks
  actors = api.actor_get_actors(request.user, actor_nicks)
  # clear deleted actors
  actors = dict([(k, v) for k, v in actors.iteritems() if v])
  per_page = per_page - (len(follower_nicks) - len(actors))

  whose = "%s's" % view.display_nick()

  # here comes lots of munging data into shape
  actor_tiles = [actors[x] for x in follower_nicks if x in actors]

  actor_tiles_count = view.extra.get('member_count', 0)
  actor_tiles, actor_tiles_more = util.page_actors(request,
                                                   actor_tiles,
                                                   per_page)

  area = 'channels'

  c = template.RequestContext(request, locals())

  # TODO: Other output formats.
  if format == 'html':
    t = loader.get_template('channel/templates/members.html')
    return http.HttpResponse(t.render(c))
示例#10
0
def channel_members(request, nick=None, format='html'):
    nick = clean.channel(nick)

    view = api.actor_lookup_nick(request.user, nick)

    if not view:
        raise exception.UserDoesNotExistError(nick, request.user)

    handled = common_views.handle_view_action(
        request, {
            'actor_add_contact': request.path,
            'actor_remove_contact': request.path,
        })
    if handled:
        return handled

    per_page = CONTACTS_PER_PAGE
    offset, prev = util.page_offset_nick(request)

    follower_nicks = api.channel_get_members(request.user,
                                             view.nick,
                                             limit=(per_page + 1),
                                             offset=offset)
    actor_nicks = follower_nicks
    actors = api.actor_get_actors(request.user, actor_nicks)
    # clear deleted actors
    actors = dict([(k, v) for k, v in actors.iteritems() if v])
    per_page = per_page - (len(follower_nicks) - len(actors))

    whose = "%s's" % view.display_nick()

    # here comes lots of munging data into shape
    actor_tiles = [actors[x] for x in follower_nicks if x in actors]

    actor_tiles_count = view.extra.get('member_count', 0)
    actor_tiles, actor_tiles_more = util.page_actors(request, actor_tiles,
                                                     per_page)

    area = 'channels'

    c = template.RequestContext(request, locals())

    if format == 'html':
        t = loader.get_template('channel/templates/members.html')
        return http.HttpResponse(t.render(c))
示例#11
0
def invite_email(request, code):
    """User has received the invite email, and has followed the link to accept or
     or refuse it."""

    if request.user:
        handled = common_views.handle_view_action(
            request, {
                'invite_accept': request.user.url('/overview'),
                'invite_reject': request.user.url('/overview')
            })
        if handled:
            return handled

    # Retrieve the invite
    invite = api.invite_get(api.ROOT, code)
    from_actor = invite.from_actor

    # Translate the from_actor into a display name
    from_actor_ref = api.actor_get(api.ROOT, from_actor)
    view = from_actor_ref

    if not from_actor_ref:
        # Corner case: from_actor was deleted since the invite was sent.
        # In this case, do we want to consider the invitation invalid?
        # (probably we do, because it's more likely that it was spam)
        return util.RedirectError("That invite is no longer valid")

    # We use api.ROOT in the next set of functions because the
    # invite is giving possibly private access to the user
    inbox = api.inbox_get_actor_contacts(api.ROOT, view.nick, limit=5)
    entries = api.entry_get_entries(api.ROOT, inbox)
    stream_keys = [e.stream for e in entries]
    streams = api.stream_get_streams(api.ROOT, stream_keys)
    actor_nicks = ([view.nick] + [s.owner for s in streams.values() if s] +
                   [e.owner for e in entries] + [e.actor for e in entries])
    actors = api.actor_get_actors(api.ROOT, actor_nicks)

    streams = display.prep_stream_dict(streams, actors)
    entries = display.prep_entry_list(entries, streams, actors)

    sidebar_green_top = True
    c = template.RequestContext(request, locals())

    t = loader.get_template('invite/templates/email.html')
    return http.HttpResponse(t.render(c))
示例#12
0
文件: views.py 项目: CollabQ/CollabQ
def channel_twitter(request, nick):
  _nick = nick
  nick = clean.channel(nick)

  view = api.channel_get_safe(request.user, nick)
  if not view:
    return http.HttpResponseRedirect('/hashtag/%s' % _nick)
  
  #twitter
  unauth = twitter.is_unauth(request)
  if 'twitter' in request.POST:
    status = twitter.post_update(request)

  handled = common_views.handle_view_action(
      request,
      {
       'channel_join': request.path,
       'channel_part': request.path,
       'channel_post': request.path,
       }
      )

  if handled:
    return handled

  page = util.paging_get_page(request)
  more = page+1

  try:
    entries = twitter.twitter_search(request, '#%s' % _nick, page)
    twitter_error = False
  except:
    entries = []
    twitter_error = True

  if not twitter_error:
    size_entries = len(entries)

  user_can_post, user_is_admin = _user_permissions(request, view)
  
  for entry in entries:
    entry['source'] =util.htmlentities_decode(entry.get('source'))

  # for sidebar_members
  members_count = view.extra['member_count']
  members_more = members_count > CONTACTS_PER_PAGE
  
  members = api.channel_get_members(request.user, channel=view.nick)
  actors = api.actor_get_actors(request.user, members)
  members = [actors[x] for x in members if actors[x]]

  if(request.user):
    channels = api.actor_get_channels_member(request.user, request.user.nick,
                              limit=(CHANNELS_SIDEBAR + 1))
  else:
    channels = []

  childs = api.channel_get_related(request.user, view)
  
  area = 'channel'
  tab = 'channel-twitter'

  green_top = True
  sidebar_green_top = True

  c = template.RequestContext(request, locals())
  t = loader.get_template('channel/templates/twitter.html')
  return http.HttpResponse(t.render(c))
示例#13
0
文件: views.py 项目: webatat/webatat
def actor_item(request, nick=None, item=None, format='html'):
  # The nick passed in the url looks ugly with the escaped @ in it and is
  # generally just shorter if we only use the lead part of the nick
  # however the entire system expects full nicks so we should expand this
  # as soon as possible
  nick = clean.nick(nick)

  # Most pages have the concept of a viewer and an actor being viewed,
  # in all cases the viewer is `request.user` and the actor being viewed
  # should be named `view`
  view = api.actor_lookup_nick(request.user, nick)

  if not view:
    raise exception.UserDoesNotExistError(nick, request.user)

  # With very few exceptions, whenever we are referring to a an
  # instance that is an entity from the datastore we append `_ref`
  # to the variable name to distinguish it from the variable that
  # is simply a string identifier.
  # In the code below `stream_ref` and `entry_ref` are both entity
  # references, while `entry` is simply the string key_name of an entry
  stream_ref = api.stream_get_presence(request.user, view.nick)
  if not stream_ref:
    raise http.Http404()

  if item == 'last':
    entry_ref = api.entry_get_last(request.user, stream_ref.keyname())
    return http.HttpResponseRedirect(entry_ref.url())
  else:
    entry = '%s/%s' % (stream_ref.key().name(), item)
    entry_ref = api.entry_get_safe(request.user, entry)

  # Most api calls will return None if the entity being looked up does
  # not exist so we usually want to verify the return values
  if not entry_ref:
    raise http.Http404()


  # When handling user actions the following pattern more or less applies
  # if 'parameter_unique_to_action' in request.(POST|GET|REQUEST):
  #   try:
  #     validate.nonce(request, 'nonce_action')
  #     validate.anything_else_that_is_related_to_ui_rather_than_call()
  #
  #     local_variable = request.(POST|GET|REQUEST).get('request_arg')
  #     # or
  #     params = util.query_dict_to_keywords(request.(POST|GET|REQUEST))
  #
  #     # Our goal is to have most of the logic for any action translate
  #     # directly into an api call on behalf of the requesting user
  #     # such that the api call is responsible for validating all input
  #     # and raising any applicable errors
  #     result = api.some_api_method(request.user,
  #                                  method_variable=local_variable,
  #                                  ...)
  #     # or
  #     result = api.some_api_method(request.user,  **params)
  #
  #     # All actions should issue a redirect with a success message
  #     return util.RedirectFlash('some_url', 'some success message')
  #   except:
  #     exception.handle_exception(request)
  #
  # When an exception occurs we expect the rest of the page to be able
  # to be processed normally as if no action had been taken, the error
  # handling section of the template should display the errors caught
  # by the exception.handle_exception() call

  handled = common_views.handle_view_action(
      request,
      {'entry_add_comment': entry_ref.url(request=request),
       'entry_remove': view.url(request=request),
       'entry_remove_comment': entry_ref.url(request=request),
       'entry_mark_as_spam': entry_ref.url(request=request)
       }
      )
  if handled:
    return handled

  comments = api.entry_get_comments(request.user, entry_ref.key().name())

  # To minimize the number of lookups to the datastore once we know
  # all the data we will be displaying on a page we attempt to make
  # a list of all the actors associated with that data so that we can
  # fetch them all at once
  actor_nicks = [entry_ref.owner, entry_ref.actor] + [c.actor for c in comments]
  actors = api.actor_get_actors(request.user, actor_nicks)
  
  # Creates a copy of actors with lowercase keys (Django #6904: template filter
  # dictsort sorts case sensitive), excluding the currently logged in user.
  participants = {}
  for k, v in actors.iteritems():
    if (v and
        not (hasattr(request.user, 'nick') and request.user.nick == v.nick)):
      participants[k.lower()] = v

  # Due to restrictions on Django's templating language most of the time
  # we will have to take an additional step of preparing all of our data
  # for display, this usually translates to attaching references to
  # actor or stream entities.
  # Functions that handle this preparation should be added to the
  # common.display module
  entry = display.prep_entry(entry_ref,
                             {stream_ref.key().name(): stream_ref}, actors)
  comments = display.prep_comment_list(comments, actors)

  # Additionally, to minimize more logic in the templates some variables
  # can be defined to configure the output, these are usually template specific
  # though some are common variables for anything that inherits from the
  # base templates
  green_top = True
  sidebar_green_top = True

  # The quickest way to make sure we are getting all of the things we care
  # about passed to the template without the temptation of making last minute
  # changes is just to pass `locals()` to the template context
  c = template.RequestContext(request, locals())

  # Ideally this is all that should be necessary to add additional output
  # formats, in practice it is yet to be seen whether additional data
  # preparation will be necessary before outputting in JSON or ATOM formats
  if format == 'html':

    # We always use the full path to the template to prevent naming conflicts
    # and difficult searches.
    t = loader.get_template('actor/templates/item.html')
    return http.HttpResponse(t.render(c))

  elif format == 'json':
    t = loader.get_template('actor/templates/item.json')
    return util.HttpJsonResponse(t.render(c), request)
示例#14
0
 def get_actors_for_inboxes(self, inboxes):
     subscribers = [util.get_user_from_topic(inbox) for inbox in inboxes]
     subscribers = list(set(subscribers))
     subscribers_ref = api.actor_get_actors(api.ROOT, subscribers)
     subscribers_ref = [v for k, v in subscribers_ref.iteritems() if v]
     return subscribers_ref
示例#15
0
文件: views.py 项目: CollabQ/CollabQ
def hashtag_search(request, tag=''):
  view = request.user

  page = util.paging_get_page(request)

  if view is not None:
    unauth = twitter.is_unauth(request)
    twitter_options = twitter.twitter_options(request)
    if 'twitter' in request.POST:
      if unauth:
        return http.HttpResponseRedirect('/twitter/auth?redirect_to=/%s/overview' % view.display_nick())
      status = twitter.post_update(request)
      if status:
        flasherror = ["We have experimented some problems trying to post a cc in twitter"]

    handled = common_views.handle_view_action(
        request,
        {
          'entry_remove': request.path,
          'entry_remove_comment': request.path,
          'entry_mark_as_spam': request.path,
          'presence_set': request.path,
          'settings_hide_comments': request.path,
          'post': '/%s/overview' % view.display_nick(),
        }
    )
    logging.info('handled: %s' % handled)
    if handled:
      return handled

  q = request.GET.get('q', None)
  if q is not None:
    result = hashtag_re.search(q)
    if result:
      tag = result.groupdict()['tag']
    else:
      tag = q
    
  hashtag_match = tag_re.search(tag)
  if hashtag_match:
    match_dict = hashtag_match.groupdict()
    tag = '#'+match_dict['tag'].lower()

    limit = ENTRIES_PER_PAGE+ENTRIES_PER_PAGE*(page-1)
    more, relations = api.hashtag_get_relations( request.user, tag, limit)
    entries = api.hashtag_get_entries(request.user, [relation.uuid for relation in relations])
    entries = api.entry_get_entries(request.user, [entry.key().name() for entry in entries])

    stream_keys = [e.stream for e in entries]
    try:
      actor_streams = api.stream_get_actor(request.user, view.nick)
    except exception.ApiException:
      actor_streams = []
    except:
      actor_streams = []
      
    stream_keys += [s.key().name() for s in actor_streams]
    streams = api.stream_get_streams(request.user, stream_keys)

    try:
      contact_nicks = api.actor_get_contacts(request.user,
                                           view.nick,
                                           limit=CONTACTS_PER_PAGE)
    except exception.ApiException:
      contact_nicks = []
    except:
      contact_nicks = []

    actor_nicks = (contact_nicks +
                   [s.owner for s in streams.values()] +
                   [e.owner for e in entries] +
                   [e.actor for e in entries])
                   
    actors = api.actor_get_actors(request.user, actor_nicks)

    contacts = [actors[x] for x in contact_nicks if actors[x]]
    streams = display.prep_stream_dict(streams, actors)
    entries = display.prep_entry_list(entries, streams, actors)
    if more:
      last = entries[len(entries)-1].uuid
  else:
    entries = []

    try:
      contact_nicks = api.actor_get_contacts(request.user,
                                           view.nick,
                                           limit=CONTACTS_PER_PAGE)
    except exception.ApiException:
      contact_nicks 

    actor_nicks = (contact_nicks)
    actors = api.actor_get_actors(request.user, actor_nicks)
    contacts = [actors[x] for x in contact_nicks if actors[x]]

  is_owner = True
  if view is not None:
    channels_count = view.extra.get('channel_count', 0)
    channels_more = channels_count > CHANNELS_PER_PAGE
    followers_count = view.extra.get('follower_count', 0)
    contacts_count = view.extra.get('contact_count', 0)
  
    channels = api.actor_get_channels_member(request.user, view.nick,
                                limit=(CHANNELS_PER_PAGE + 1))
    templatebase = 'common/templates/base_sidebar.html'

  else:
    templatebase = 'common/templates/base_single.html'

  green_top = True
  sidebar_green_top = True
  next = page+1
  actor_link = True

  c = template.RequestContext(request, locals())
  t = loader.get_template('hashtag/templates/hashtag_search.html')

  return http.HttpResponse(t.render(c))
示例#16
0
def actor_item(request, nick=None, item=None, format='html'):
    # The nick passed in the url looks ugly with the escaped @ in it and is
    # generally just shorter if we only use the lead part of the nick
    # however the entire system expects full nicks so we should expand this
    # as soon as possible
    nick = clean.nick(nick)

    # Most pages have the concept of a viewer and an actor being viewed,
    # in all cases the viewer is `request.user` and the actor being viewed
    # should be named `view`
    view = api.actor_lookup_nick(request.user, nick)

    if not view:
        raise exception.UserDoesNotExistError(nick, request.user)

    # With very few exceptions, whenever we are referring to a an
    # instance that is an entity from the datastore we append `_ref`
    # to the variable name to distinguish it from the variable that
    # is simply a string identifier.
    # In the code below `stream_ref` and `entry_ref` are both entity
    # references, while `entry` is simply the string key_name of an entry
    stream_ref = api.stream_get_presence(request.user, view.nick)
    if not stream_ref:
        raise http.Http404()

    if item == 'last':
        entry_ref = api.entry_get_last(request.user, stream_ref.keyname())
        return http.HttpResponseRedirect(entry_ref.url())
    else:
        entry = '%s/%s' % (stream_ref.key().name(), item)
        entry_ref = api.entry_get_safe(request.user, entry)

    # Most api calls will return None if the entity being looked up does
    # not exist so we usually want to verify the return values
    if not entry_ref:
        raise http.Http404()

    # When handling user actions the following pattern more or less applies
    # if 'parameter_unique_to_action' in request.(POST|GET|REQUEST):
    #   try:
    #     validate.nonce(request, 'nonce_action')
    #     validate.anything_else_that_is_related_to_ui_rather_than_call()
    #
    #     local_variable = request.(POST|GET|REQUEST).get('request_arg')
    #     # or
    #     params = util.query_dict_to_keywords(request.(POST|GET|REQUEST))
    #
    #     # Our goal is to have most of the logic for any action translate
    #     # directly into an api call on behalf of the requesting user
    #     # such that the api call is responsible for validating all input
    #     # and raising any applicable errors
    #     result = api.some_api_method(request.user,
    #                                  method_variable=local_variable,
    #                                  ...)
    #     # or
    #     result = api.some_api_method(request.user,  **params)
    #
    #     # All actions should issue a redirect with a success message
    #     return util.RedirectFlash('some_url', 'some success message')
    #   except:
    #     exception.handle_exception(request)
    #
    # When an exception occurs we expect the rest of the page to be able
    # to be processed normally as if no action had been taken, the error
    # handling section of the template should display the errors caught
    # by the exception.handle_exception() call

    handled = common_views.handle_view_action(
        request, {
            'entry_add_comment': entry_ref.url(request=request),
            'entry_remove': view.url(request=request),
            'entry_remove_comment': entry_ref.url(request=request),
            'entry_mark_as_spam': entry_ref.url(request=request)
        })
    if handled:
        return handled

    comments = api.entry_get_comments(request.user, entry_ref.key().name())

    # To minimize the number of lookups to the datastore once we know
    # all the data we will be displaying on a page we attempt to make
    # a list of all the actors associated with that data so that we can
    # fetch them all at once
    actor_nicks = [entry_ref.owner, entry_ref.actor
                   ] + [c.actor for c in comments]
    actors = api.actor_get_actors(request.user, actor_nicks)

    # Creates a copy of actors with lowercase keys (Django #6904: template filter
    # dictsort sorts case sensitive), excluding the currently logged in user.
    participants = {}
    for k, v in actors.iteritems():
        if (v and not (hasattr(request.user, 'nick')
                       and request.user.nick == v.nick)):
            participants[k.lower()] = v

    # Due to restrictions on Django's templating language most of the time
    # we will have to take an additional step of preparing all of our data
    # for display, this usually translates to attaching references to
    # actor or stream entities.
    # Functions that handle this preparation should be added to the
    # common.display module
    entry = display.prep_entry(entry_ref,
                               {stream_ref.key().name(): stream_ref}, actors)
    comments = display.prep_comment_list(comments, actors)

    # Additionally, to minimize more logic in the templates some variables
    # can be defined to configure the output, these are usually template specific
    # though some are common variables for anything that inherits from the
    # base templates
    green_top = True
    sidebar_green_top = True

    # The quickest way to make sure we are getting all of the things we care
    # about passed to the template without the temptation of making last minute
    # changes is just to pass `locals()` to the template context
    c = template.RequestContext(request, locals())

    # Ideally this is all that should be necessary to add additional output
    # formats, in practice it is yet to be seen whether additional data
    # preparation will be necessary before outputting in JSON or ATOM formats
    if format == 'html':

        # We always use the full path to the template to prevent naming conflicts
        # and difficult searches.
        t = loader.get_template('actor/templates/item.html')
        return http.HttpResponse(t.render(c))

    elif format == 'json':
        t = loader.get_template('actor/templates/item.json')
        return util.HttpJsonResponse(t.render(c), request)
示例#17
0
def actor_history(request, nick=None, format='html'):
  nick = clean.nick(nick)
  view = api.actor_lookup_nick(request.user, nick)

  if not view:
    raise exception.UserDoesNotExistError(nick, request.user)

  called_subscribe, sub_ref = common_views.call_api_from_request(
      request, 'subscription_request')
  if called_subscribe:
    if sub_ref.state == 'subscribed':
      message = 'Subscribed.'
    else:
      message = 'Subscription requested.'
    return util.RedirectFlash(view.url(), message)

  handled = common_views.handle_view_action(
      request,
      { 'entry_remove': request.path,
        'entry_remove_comment': request.path,
        'entry_mark_as_spam': request.path,
        'subscription_remove': view.url(),
        'actor_add_contact': request.path,
        'actor_remove_contact': request.path,
        'post': request.path,
        'presence_set': request.path,
      }
  )
  if handled:
    return handled

  privacy = 'public'
  if request.user:
    if view.nick == request.user.nick:
      privacy = 'private'
    # ROOT because we care whether or not request.user is a contact of
    # the view user's, not whether the request.user can see the contacts
    elif api.actor_has_contact(api.ROOT, view.nick, request.user.nick):
      privacy = 'contacts'

  # we're going to hide a bunch of stuff if this user is private and we
  # aren't allowed to see
  user_is_private = False
  if view.privacy < models.PRIVACY_PUBLIC and privacy == 'public':
    user_is_private = True

  per_page = ENTRIES_PER_PAGE
  offset, prev = util.page_offset(request)

  if privacy == 'public':
    if user_is_private:
      inbox = []
    else:
      inbox = api.inbox_get_actor_public(request.user, view.nick,
                                         limit=(per_page + 1), offset=offset)
  elif privacy == 'contacts':
    inbox = api.inbox_get_actor_contacts(request.user, view.nick,
                                         limit=(per_page + 1), offset=offset)
  elif privacy == 'private':
    inbox = api.inbox_get_actor_private(request.user, view.nick,
                                        limit=(per_page + 1), offset=offset)

  # START inbox generation chaos
  # TODO(termie): refacccttttooorrrrr
  entries = api.entry_get_entries(request.user, inbox)
  per_page = per_page - (len(inbox) - len(entries))
  entries, more = util.page_entries(request, entries, per_page)

  stream_keys = [e.stream for e in entries]
  try:
    actor_streams = api.stream_get_actor(request.user, view.nick)
  except exception.ApiException:
    actor_streams = []
  stream_keys += [s.key().name() for s in actor_streams]
  streams = api.stream_get_streams(request.user, stream_keys)

  try:
    contact_nicks = api.actor_get_contacts(request.user, 
                                           view.nick, 
                                           limit=CONTACTS_PER_PAGE)
  except exception.ApiException:
    contact_nicks = []

  actor_nicks = (contact_nicks +
                 [view.nick] +
                 [s.owner for s in streams.values()] +
                 [e.owner for e in entries] +
                 [e.actor for e in entries])
  actors = api.actor_get_actors(request.user, actor_nicks)

  # here comes lots of munging data into shape
  contacts = [actors[x] for x in contact_nicks if actors[x]]
  streams = display.prep_stream_dict(streams, actors)
  entries = display.prep_entry_list(entries, streams, actors)

  # END inbox generation chaos

  # If not logged in, cannot write
  is_owner = request.user and view.nick == request.user.nick

  try:
    presence = api.presence_get(request.user, view.nick)
    presence_stream = api.stream_get_presence(request.user, view.nick)
    last_entry = api.entry_get_last(request.user, presence_stream.keyname())
    view.last_entry = last_entry
  except exception.ApiException:
    pass


  # for add/remove contact
  if request.user:
    user_is_contact = api.actor_has_contact(request.user,
                                            request.user.nick,
                                            view.nick)
    view.my_contact = user_is_contact
  else:
    user_is_contact = False

  # for sidebar streams
  view_streams = dict([(x.key().name(), streams[x.key().name()])
                       for x in actor_streams])
  if request.user:
    # un/subscribe buttons are possible only, when logged in

    # TODO(termie): what if there are quite a lot of streams?
    for stream in view_streams.values():
      stream.subscribed = api.subscription_exists(
          request.user,
          stream.key().name(),
          'inbox/%s/overview' % request.user.nick
          )


  # for sidebar_contacts
  contacts_count = view.extra.get('contact_count', 0)
  contacts_more = contacts_count > CONTACTS_PER_PAGE

  # Config for the template
  green_top = True
  sidebar_green_top = True
  selectable_icons = display.SELECTABLE_ICONS
  area = 'user'

  c = template.RequestContext(request, locals())

  if format == 'html':
    t = loader.get_template('actor/templates/history.html')
    return http.HttpResponse(t.render(c))
  elif format == 'json':
    t = loader.get_template('actor/templates/history.json')
    r = util.HttpJsonResponse(t.render(c), request)
    return r
  elif format == 'atom':
    t = loader.get_template('actor/templates/history.atom')
    r = util.HttpAtomResponse(t.render(c), request)
    return r
  elif format == 'rss':
    t = loader.get_template('actor/templates/history.rss')
    r = util.HttpRssResponse(t.render(c), request)
    return r
示例#18
0
def actor_contacts(request, nick=None, format='html'):
  nick = clean.nick(nick)

  view = api.actor_lookup_nick(request.user, nick)

  if not view:
    raise exception.UserDoesNotExistError(nick, request.user)

  handled = common_views.handle_view_action(
      request,
      { 'actor_add_contact': request.path,
        'actor_remove_contact': request.path, })
  if handled:
    return handled

  per_page = CONTACTS_PER_PAGE
  offset, prev = util.page_offset_nick(request)

  contact_nicks = api.actor_get_contacts(request.user, view.nick,
                                         limit=(per_page + 1), offset=offset)
  actor_nicks = contact_nicks
  actors = api.actor_get_actors(request.user, actor_nicks)
  # clear deleted actors
  actors = dict([(k, v) for k, v in actors.iteritems() if v])
  per_page = per_page - (len(contact_nicks) - len(actors))

  # TODO(termie): incorporate this into paging so we only fetch the range
  #               on this page
  # add some extra info so we can let the user do contextual actions
  # on these homeboys
  if request.user and request.user.nick == view.nick:
    # looking at self, find out who of these people follow me so
    # I can highlight them
    for actor in actors:
      if api.actor_is_follower(request.user, view.nick, actor):
        actors[actor].my_follower = True
      actors[actor].my_contact = True
      actors[actor].rel = 'contact'
    whose = 'Your'
  elif request.user:
    my_contacts_nicks = api.actor_get_contacts(request.user, request.user.nick)
    for f in my_contacts_nicks:
      try:
        actors[f].my_contact = True
      except:
        pass
    for x in actors:
      actors[x].rel = 'contact'
    whose = "%s's" % view.display_nick()

  # here comes lots of munging data into shape
  actor_tiles = [actors[x] for x in contact_nicks if x in actors]

  actor_tiles_count = view.extra.get('contact_count', 0)
  actor_tiles, actor_tiles_more = util.page_actors(request,
                                                   actor_tiles,
                                                   per_page)

  area = 'people'

  c = template.RequestContext(request, locals())

  if format == 'html':
    t = loader.get_template('actor/templates/contacts.html')
    return http.HttpResponse(t.render(c))
  elif format == 'json':
    t = loader.get_template('actor/templates/contacts.json')
    r = http.HttpResponse(t.render(c))
    r['Content-type'] = 'text/javascript'
    return r
示例#19
0
def front_front(request):
  # if the user is logged in take them to their overview
  if request.user:
    return HttpResponseRedirect(request.user.url() + "/overview")

  # NOTE: grab a bunch of extra so that we don't ever end up with
  #       less than 5
  per_page = ENTRIES_PER_PAGE * 2

  inbox = api.inbox_get_explore(request.user, limit=per_page)

  # START inbox generation chaos
  # TODO(termie): refacccttttooorrrrr
  entries = api.entry_get_entries(request.user, inbox)
  per_page = per_page - (len(inbox) - len(entries))
  entries, more = util.page_entries(request, entries, per_page)

  stream_keys = [e.stream for e in entries]
  streams = api.stream_get_streams(request.user, stream_keys)

  actor_nicks = [e.owner for e in entries] + [e.actor for e in entries]
  actors = api.actor_get_actors(request.user, actor_nicks)

  # take it back down and don't show a more link
  entries = entries[:ENTRIES_PER_PAGE]
  more = None

  # here comes lots of munging data into shape
  streams = prep_stream_dict(streams, actors)
  entries = prep_entry_list(entries, streams, actors)

  # END inbox generation chaos

  try:
    # Featured Channels -- Ones to which the ROOT user is a member
    featured_channels = api.actor_get_channels_member(
        request.user, api.ROOT.nick, limit=SIDEBAR_FETCH_LIMIT)
    random.shuffle(featured_channels)

    # Just in case any are deleted:
    featured_channels = featured_channels[:2*SIDEBAR_LIMIT]
    featured_channels = api.channel_get_channels(
        request.user, featured_channels)
    featured_channels = [x for x in featured_channels.values() if x]
    featured_channels = featured_channels[:SIDEBAR_LIMIT]

    featured_members = api.actor_get_contacts(
        request.user, api.ROOT.nick, limit=SIDEBAR_FETCH_LIMIT)
    random.shuffle(featured_members)

    # Just in case any are deleted:
    featured_members = featured_members[:2*SIDEBAR_LIMIT]
    featured_members = api.actor_get_actors(request.user, featured_members)
    featured_members = [x for x in featured_members.values() if x]
    featured_members = featured_members[:SIDEBAR_LIMIT]

  except exception.ApiNotFound:
    pass

  root = api.ROOT

  area = 'frontpage'

  t = loader.get_template('front/templates/front.html')
  c = RequestContext(request, locals())

  return HttpResponse(t.render(c));
示例#20
0
def channel_item(request, nick, item=None, format='html'):
    nick = clean.channel(nick)
    view = api.actor_lookup_nick(request.user, nick)

    if not view:
        raise http.Http404()

    stream_ref = api.stream_get_presence(request.user, view.nick)

    entry = '%s/%s' % (stream_ref.key().name(), item)

    entry_ref = api.entry_get(request.user, entry)
    if not entry_ref:
        raise http.Http404()

    handled = common_views.handle_view_action(
        request, {
            'entry_add_comment': entry_ref.url(request=request),
            'entry_remove': view.url(request=request),
            'entry_remove_comment': entry_ref.url(request=request),
            'entry_mark_as_spam': entry_ref.url(request=request)
        })
    if handled:
        return handled

    admins = api.channel_get_admins(request.user, channel=view.nick)
    user_is_admin = request.user and request.user.nick in admins

    comments = api.entry_get_comments(request.user, entry)

    actor_nicks = [entry_ref.owner, entry_ref.actor
                   ] + [c.actor for c in comments]
    actors = api.actor_get_actors(request.user, actor_nicks)

    # Creates a copy of actors with lowercase keys (Django #6904: template filter
    # dictsort sorts case sensitive), excluding channels and the currently
    # logged in user.
    participants = {}
    for k, v in actors.iteritems():
        if (v and not v.is_channel()
                and not (hasattr(request.user, 'nick')
                         and request.user.nick == v.nick)):
            participants[k.lower()] = v

    # display munge
    entry = display.prep_entry(entry_ref,
                               {stream_ref.key().name(): stream_ref}, actors)
    comments = display.prep_comment_list(comments, actors)

    # config for template
    green_top = True
    sidebar_green_top = True

    # rendering
    c = template.RequestContext(request, locals())
    if format == 'html':
        t = loader.get_template('channel/templates/item.html')
        return http.HttpResponse(t.render(c))
    elif format == 'json':
        t = loader.get_template('actor/templates/item.json')
        r = http.HttpResponse(t.render(c))
        r['Content-type'] = 'text/javascript'
        return r
示例#21
0
def channel_history(request, nick, format='html'):
  """ the page for a channel

  if the channel does not exist we go to create channel instead

  should let you join a channel or post to it if you already are a member
  also leave it if you are a member,
  display the posts to this channel and the member list if you are allowed
  to see them

  if you are an admin you should have the options to modify the channel
  """
  nick = clean.channel(nick)

  view = api.actor_lookup_nick(request.user, nick)
  if not view:
    return http.HttpResponseRedirect('/channel/create?channel=%s' % nick)

  admins = api.channel_get_admins(request.user, channel=view.nick)
  members = api.channel_get_members(request.user, channel=view.nick)

  handled = common_views.handle_view_action(
      request,
      {'channel_join': request.path,
       'channel_part': request.path,
       'channel_post': request.path,
       'entry_remove': request.path,
       'entry_remove_comment': request.path,
       'entry_mark_as_spam': request.path,
       'subscription_remove': request.path,
       'subscription_request': request.path,
       }
      )
  if handled:
    return handled

  privacy = 'public'

  user_can_post = False
  user_is_admin = False
  if not request.user:
    pass
  elif api.channel_has_admin(request.user, view.nick, request.user.nick):
    privacy = 'private'
    user_can_post = True
    user_is_admin = True
  elif api.channel_has_member(request.user, view.nick, request.user.nick):
    privacy = 'contacts'
    user_can_post = True

  per_page = CHANNEL_HISTORY_PER_PAGE
  offset, prev = util.page_offset(request)

  if privacy == 'public':
    inbox = api.inbox_get_actor_public(
        request.user,
        view.nick,
        limit=(per_page + 1),
        offset=offset)
  elif privacy == 'contacts':
    inbox = api.inbox_get_actor_contacts(
        request.user,
        view.nick,
        limit=(per_page + 1),
        offset=offset)
  elif privacy == 'private':
    inbox = api.inbox_get_actor_private(
        api.ROOT,
        view.nick,
        limit=(per_page + 1),
        offset=offset)

  # START inbox generation chaos
  # TODO(termie): refacccttttooorrrrr

  entries = api.entry_get_entries(request.user, inbox)
  # clear out deleted entries
  per_page = per_page - (len(inbox) - len(entries))
  entries, more = util.page_entries(request, entries, per_page)

  stream_keys = [e.stream for e in entries]
  actor_streams = api.stream_get_actor(request.user, view.nick)
  stream_keys += [s.key().name() for s in actor_streams]
  streams = api.stream_get_streams(request.user, stream_keys)

  contact_nicks = api.actor_get_contacts(request.user, view.nick)
  actor_nicks = (contact_nicks +
                 admins +
                 members +
                 [view.nick] +
                 [s.owner for s in streams.values()] +
                 [e.actor for e in entries])
  actors = api.actor_get_actors(request.user, actor_nicks)

  # here comes lots of munging data into shape
  contacts = [actors[x] for x in contact_nicks if actors[x]]
  streams = display.prep_stream_dict(streams, actors)
  entries = display.prep_entry_list(entries, streams, actors)
  admins = [actors[x] for x in admins if actors[x]]
  members = [actors[x] for x in members if actors[x]]

  # END inbox generation chaos

  presence = api.presence_get(request.user, view.nick)

  # for sidebar_members
  members_count = view.extra['member_count']
  members_more = members_count > CONTACTS_PER_PAGE

  # for sidebar_admins
  admins_count = view.extra['admin_count']
  admins_more = admins_count > CONTACTS_PER_PAGE

  # config for templates
  green_top = True
  sidebar_green_top = True
  selectable_icons = display.SELECTABLE_ICONS


  # for sidebar streams (copied from actor/views.py.  refactor)
  view_streams = dict([(x.key().name(), streams[x.key().name()])
                       for x in actor_streams])
  if request.user:
    # un/subscribe buttons are possible only, when logged in

    # TODO(termie): what if there are quite a lot of streams?
    for stream in view_streams.values():
      stream.subscribed = api.subscription_exists(
          request.user,
          stream.key().name(),
          'inbox/%s/overview' % request.user.nick
          )

  area = 'channel'
  c = template.RequestContext(request, locals())

  if format == 'html':
    t = loader.get_template('channel/templates/history.html')
    return http.HttpResponse(t.render(c))
  elif format == 'json':
    t = loader.get_template('channel/templates/history.json')
    r = util.HttpJsonResponse(t.render(c), request)
    return r
  elif format == 'atom':
    t = loader.get_template('channel/templates/history.atom')
    r = util.HttpAtomResponse(t.render(c), request)
    return r
  elif format == 'rss':
    t = loader.get_template('channel/templates/history.rss')
    r = util.HttpRssResponse(t.render(c), request)
    return r
示例#22
0
文件: views.py 项目: CollabQ/CollabQ
def explore_recent(request, format="html"):
  if request.user:
    view = request.user
    logging.info("entering here")
    #twitter
    unauth = twitter.is_unauth(request)
    if 'twitter' in request.POST:
      if unauth:
        return http.HttpResponseRedirect('/twitter/auth?redirect_to=/')
      status = twitter.post_update(request)
      if status:
        flasherror = ["We have experimented some problems trying to post a cc in twitter"]
        
    handled = common_views.handle_view_action(
        request,
        { 'entry_remove': request.path,
          'entry_remove_comment': request.path,
          'entry_mark_as_spam': request.path,
          'actor_add_contact': request.path,
          'actor_remove_contact': request.path,
          'post': request.path,
          'presence_set': request.path,
        }
    )
    if handled:
      return handled

  subtab = 'explore'
  per_page = ENTRIES_PER_PAGE
  offset, prev = util.page_offset(request)

  inbox = api.inbox_get_explore(request, limit=(per_page + 1),
                                offset=offset)

  entries, more = helper.get_inbox_entries(request, inbox)
  stream_keys = [e.stream for e in entries]
  streams = api.stream_get_streams(request.user, stream_keys)
  actor_nicks = [e.owner for e in entries] + [e.actor for e in entries]
  actors = api.actor_get_actors(request.user, actor_nicks)
                                              
  # here comes lots of munging data into shape
  entries = prep_entry_list(entries, streams, actors)

  if request.user:
    channels_count = view.extra.get('channel_count', 0)
    channels_more = channels_count > CHANNELS_PER_PAGE
    followers_count = view.extra.get('follower_count', 0)
    contacts_count = view.extra.get('contact_count', 0)
    contacts_more = contacts_count > CONTACTS_PER_PAGE
    contact_nicks = api.actor_get_contacts_safe(request.user,
                                              view.nick,
                                              limit=CONTACTS_PER_PAGE)
    contacts = api.actor_get_actors(request.user, contact_nicks)
    contacts = [contacts[x] for x in contact_nicks if contacts[x]]

    green_top = True
    sidebar_green_top = True
  # END inbox generation chaos

  area = 'explore'
  actor_link = True
  
  c = template.RequestContext(request, locals())

  if format == 'html':
    t = loader.get_template('explore/templates/recent.html')
    return http.HttpResponse(t.render(c));
  elif format == 'json':
    t = loader.get_template('explore/templates/recent.json')
    r = util.HttpJsonResponse(t.render(c), request)
    return r
  elif format == 'atom':
    t = loader.get_template('explore/templates/recent.atom')
    r = util.HttpAtomResponse(t.render(c), request)
    return r
  elif format == 'rss':
    t = loader.get_template('explore/templates/recent.rss')
    r = util.HttpRssResponse(t.render(c), request)
    return r
示例#23
0
def actor_overview(request, nick, format='html'):
    nick = clean.nick(nick)

    view = api.actor_lookup_nick(request.user, nick)

    if not view:
        raise exception.UserDoesNotExistError(nick, request.user)

    if not request.user or view.nick != request.user.nick:
        # Instead of displaying the overview, redirect to the public-facing page
        return http.HttpResponseRedirect(view.url())

    handled = common_views.handle_view_action(
        request, {
            'entry_remove': request.path,
            'entry_remove_comment': request.path,
            'entry_mark_as_spam': request.path,
            'presence_set': request.path,
            'settings_hide_comments': request.path,
            'post': request.path,
        })
    if handled:
        return handled

    per_page = ENTRIES_PER_PAGE
    offset, prev = util.page_offset(request)

    inbox = api.inbox_get_actor_overview(request.user,
                                         view.nick,
                                         limit=(per_page + 1),
                                         offset=offset)

    # START inbox generation chaos
    # TODO(termie): refacccttttooorrrrr
    entries = api.entry_get_entries(request.user, inbox)

    if view.extra.get('comments_hide', 0):
        # TODO(tyler): This is certainly not the most eloquent way to filter
        # through entries to remove comments.
        entries = [x for x in entries if not x.stream.endswith('comments')]

    per_page = per_page - (len(inbox) - len(entries))
    entries, more = util.page_entries(request, entries, per_page)

    stream_keys = [e.stream for e in entries]
    actor_streams = api.stream_get_actor(request.user, view.nick)
    stream_keys += [s.key().name() for s in actor_streams]
    streams = api.stream_get_streams(request.user, stream_keys)

    contact_nicks = api.actor_get_contacts(request.user,
                                           view.nick,
                                           limit=CONTACTS_PER_PAGE)
    actor_nicks = (contact_nicks + [view.nick] +
                   [s.owner for s in streams.values()] +
                   [e.owner for e in entries] + [e.actor for e in entries])
    actors = api.actor_get_actors(request.user, actor_nicks)

    # here comes lots of munging data into shape
    # clear deleted contacts
    contacts = [actors[x] for x in contact_nicks if actors[x]]
    streams = display.prep_stream_dict(streams, actors)
    entries = display.prep_entry_list(entries, streams, actors)

    # END inbox generation chaos

    # Check for unconfirmed emails
    unconfirmeds = api.activation_get_actor_email(request.user, view.nick)
    if unconfirmeds:
        unconfirmed_email = unconfirmeds[0].content

    # If not logged in, cannot write
    is_owner = False
    try:
        is_owner = view.nick == request.user.nick
    except:
        pass
    presence = api.presence_get(request.user, view.nick)

    # for sidebar streams
    view_streams = dict([(x.key().name(), streams[x.key().name()])
                         for x in actor_streams])

    # for sidebar_contacts
    contacts_count = view.extra.get('contact_count', 0)
    contacts_more = contacts_count > CONTACTS_PER_PAGE

    # Config for the template
    green_top = True
    sidebar_green_top = True
    selectable_icons = display.SELECTABLE_ICONS

    area = 'home'

    # TODO(tyler/termie):  This conflicts with the global settings import.
    # Also, this seems fishy.  Do none of the settings.* items work in templates?
    import settings

    c = template.RequestContext(request, locals())

    if format == 'html':
        t = loader.get_template('actor/templates/overview.html')
        return http.HttpResponse(t.render(c))
    elif format == 'json':
        t = loader.get_template('actor/templates/overview.json')
        r = util.HttpJsonResponse(t.render(c), request)
        return r
    elif format == 'atom':
        t = loader.get_template('actor/templates/overview.atom')
        r = util.HttpAtomResponse(t.render(c), request)
        return r
    elif format == 'rss':
        t = loader.get_template('actor/templates/overview.rss')
        r = util.HttpRssResponse(t.render(c), request)
        return r
示例#24
0
def front_front(request):
  # if the user is logged in take them to their overview
  if request.user:
    url = request.user.url(request=request)
    return HttpResponseRedirect(url + "/overview")

  # NOTE: grab a bunch of extra so that we don't ever end up with
  #       less than 5
  per_page = ENTRIES_PER_PAGE * 2

  inbox = api.inbox_get_explore(request.user, limit=per_page)

  # START inbox generation chaos
  # TODO(termie): refacccttttooorrrrr
  entries = api.entry_get_entries(request.user, inbox)
  per_page = per_page - (len(inbox) - len(entries))
  entries, more = util.page_entries(request, entries, per_page)

  stream_keys = [e.stream for e in entries]
  streams = api.stream_get_streams(request.user, stream_keys)

  actor_nicks = [e.owner for e in entries] + [e.actor for e in entries]
  actors = api.actor_get_actors(request.user, actor_nicks)

  # take it back down and don't show a more link
  entries = entries[:ENTRIES_PER_PAGE]
  more = None

  # here comes lots of munging data into shape
  streams = prep_stream_dict(streams, actors)
  entries = prep_entry_list(entries, streams, actors)

  # END inbox generation chaos

  try:
    # Featured Channels -- Ones to which the ROOT user is a member
    featured_channels = api.actor_get_channels_member(
        request.user, api.ROOT.nick, limit=SIDEBAR_FETCH_LIMIT)
    random.shuffle(featured_channels)

    # Just in case any are deleted:
    featured_channels = featured_channels[:2*SIDEBAR_LIMIT]
    featured_channels = api.channel_get_channels(
        request.user, featured_channels)
    featured_channels = [x for x in featured_channels.values() if x]
    featured_channels = featured_channels[:SIDEBAR_LIMIT]

    featured_members = api.actor_get_contacts(
        request.user, api.ROOT.nick, limit=SIDEBAR_FETCH_LIMIT)
    random.shuffle(featured_members)

    # Just in case any are deleted:
    featured_members = featured_members[:2*SIDEBAR_LIMIT]
    featured_members = api.actor_get_actors(request.user, featured_members)
    featured_members = [x for x in featured_members.values() if x]
    featured_members = featured_members[:SIDEBAR_LIMIT]

  except exception.ApiNotFound:
    pass

  root = api.ROOT

  area = 'frontpage'

  t = loader.get_template('front/templates/front.html')
  c = RequestContext(request, locals())

  return HttpResponse(t.render(c));
示例#25
0
 def get_actors_for_inboxes(self, inboxes):
   subscribers = [util.get_user_from_topic(inbox) for inbox in inboxes]
   subscribers = list(set(subscribers))
   subscribers_ref = api.actor_get_actors(api.ROOT, subscribers)
   subscribers_ref = [v for k, v in subscribers_ref.iteritems() if v]
   return subscribers_ref
示例#26
0
def actor_history(request, nick=None, format='html'):
    nick = clean.nick(nick)
    view = api.actor_lookup_nick(request.user, nick)

    if not view:
        raise exception.UserDoesNotExistError(nick, request.user)

    called_subscribe, sub_ref = common_views.call_api_from_request(
        request, 'subscription_request')
    if called_subscribe:
        if sub_ref.state == 'subscribed':
            message = 'Subscribed.'
        else:
            message = 'Subscription requested.'
        return util.RedirectFlash(view.url(), message)

    handled = common_views.handle_view_action(
        request, {
            'entry_remove': request.path,
            'entry_remove_comment': request.path,
            'entry_mark_as_spam': request.path,
            'subscription_remove': view.url(),
            'actor_add_contact': request.path,
            'actor_remove_contact': request.path,
            'post': request.path,
            'presence_set': request.path,
        })
    if handled:
        return handled

    privacy = 'public'
    if request.user:
        if view.nick == request.user.nick:
            privacy = 'private'
        # ROOT because we care whether or not request.user is a contact of
        # the view user's, not whether the request.user can see the contacts
        elif api.actor_has_contact(api.ROOT, view.nick, request.user.nick):
            privacy = 'contacts'

    # we're going to hide a bunch of stuff if this user is private and we
    # aren't allowed to see
    user_is_private = False
    if view.privacy < models.PRIVACY_PUBLIC and privacy == 'public':
        user_is_private = True

    per_page = ENTRIES_PER_PAGE
    offset, prev = util.page_offset(request)

    if privacy == 'public':
        if user_is_private:
            inbox = []
        else:
            inbox = api.inbox_get_actor_public(request.user,
                                               view.nick,
                                               limit=(per_page + 1),
                                               offset=offset)
    elif privacy == 'contacts':
        inbox = api.inbox_get_actor_contacts(request.user,
                                             view.nick,
                                             limit=(per_page + 1),
                                             offset=offset)
    elif privacy == 'private':
        inbox = api.inbox_get_actor_private(request.user,
                                            view.nick,
                                            limit=(per_page + 1),
                                            offset=offset)

    # START inbox generation chaos
    # TODO(termie): refacccttttooorrrrr
    entries = api.entry_get_entries(request.user, inbox)
    per_page = per_page - (len(inbox) - len(entries))
    entries, more = util.page_entries(request, entries, per_page)

    stream_keys = [e.stream for e in entries]
    try:
        actor_streams = api.stream_get_actor(request.user, view.nick)
    except exception.ApiException:
        actor_streams = []
    stream_keys += [s.key().name() for s in actor_streams]
    streams = api.stream_get_streams(request.user, stream_keys)

    try:
        contact_nicks = api.actor_get_contacts(request.user,
                                               view.nick,
                                               limit=CONTACTS_PER_PAGE)
    except exception.ApiException:
        contact_nicks = []

    actor_nicks = (contact_nicks + [view.nick] +
                   [s.owner for s in streams.values()] +
                   [e.owner for e in entries] + [e.actor for e in entries])
    actors = api.actor_get_actors(request.user, actor_nicks)

    # here comes lots of munging data into shape
    contacts = [actors[x] for x in contact_nicks if actors[x]]
    streams = display.prep_stream_dict(streams, actors)
    entries = display.prep_entry_list(entries, streams, actors)

    # END inbox generation chaos

    # If not logged in, cannot write
    is_owner = request.user and view.nick == request.user.nick

    try:
        presence = api.presence_get(request.user, view.nick)
        presence_stream = api.stream_get_presence(request.user, view.nick)
        last_entry = api.entry_get_last(request.user,
                                        presence_stream.keyname())
        view.last_entry = last_entry
    except exception.ApiException:
        pass

    # for add/remove contact
    if request.user:
        user_is_contact = api.actor_has_contact(request.user,
                                                request.user.nick, view.nick)
        view.my_contact = user_is_contact
    else:
        user_is_contact = False

    # for sidebar streams
    view_streams = dict([(x.key().name(), streams[x.key().name()])
                         for x in actor_streams])
    if request.user:
        # un/subscribe buttons are possible only, when logged in

        # TODO(termie): what if there are quite a lot of streams?
        for stream in view_streams.values():
            stream.subscribed = api.subscription_exists(
                request.user,
                stream.key().name(), 'inbox/%s/overview' % request.user.nick)

    # for sidebar_contacts
    contacts_count = view.extra.get('contact_count', 0)
    contacts_more = contacts_count > CONTACTS_PER_PAGE

    # Config for the template
    green_top = True
    sidebar_green_top = True
    selectable_icons = display.SELECTABLE_ICONS
    area = 'user'

    c = template.RequestContext(request, locals())

    if format == 'html':
        t = loader.get_template('actor/templates/history.html')
        return http.HttpResponse(t.render(c))
    elif format == 'json':
        t = loader.get_template('actor/templates/history.json')
        r = util.HttpJsonResponse(t.render(c), request)
        return r
    elif format == 'atom':
        t = loader.get_template('actor/templates/history.atom')
        r = util.HttpAtomResponse(t.render(c), request)
        return r
    elif format == 'rss':
        t = loader.get_template('actor/templates/history.rss')
        r = util.HttpRssResponse(t.render(c), request)
        return r
示例#27
0
def actor_overview(request, nick, format='html'):
  nick = clean.nick(nick)

  view = api.actor_lookup_nick(request.user, nick)

  if not view:
    raise exception.UserDoesNotExistError(nick, request.user)

  if not request.user or view.nick != request.user.nick:
    # Instead of displaying the overview, redirect to the public-facing page
    return http.HttpResponseRedirect(view.url())

  handled = common_views.handle_view_action(
      request,
      { 
        'entry_remove': request.path,
        'entry_remove_comment': request.path,
        'entry_mark_as_spam': request.path,
        'presence_set': request.path,
        'settings_hide_comments': request.path,
        'post': request.path,
      }
  )
  if handled:
    return handled

  per_page = ENTRIES_PER_PAGE
  offset, prev = util.page_offset(request)

  inbox = api.inbox_get_actor_overview(request.user,  
                                       view.nick,
                                       limit=(per_page + 1), 
                                       offset=offset)

  # START inbox generation chaos
  # TODO(termie): refacccttttooorrrrr
  entries = api.entry_get_entries(request.user, inbox)
  
  if view.extra.get('comments_hide', 0):
    # TODO(tyler): This is certainly not the most eloquent way to filter
    # through entries to remove comments.
    entries = [x for x in entries if not x.stream.endswith('comments')]

  per_page = per_page - (len(inbox) - len(entries))
  entries, more = util.page_entries(request, entries, per_page)

  stream_keys = [e.stream for e in entries]
  actor_streams = api.stream_get_actor(request.user, view.nick)
  stream_keys += [s.key().name() for s in actor_streams]
  streams = api.stream_get_streams(request.user, stream_keys)

  contact_nicks = api.actor_get_contacts(request.user, 
                                         view.nick, 
                                         limit=CONTACTS_PER_PAGE)
  actor_nicks = (contact_nicks +
                 [view.nick] +
                 [s.owner for s in streams.values()] +
                 [e.owner for e in entries] +
                 [e.actor for e in entries])
  actors = api.actor_get_actors(request.user, actor_nicks)

  # here comes lots of munging data into shape
  # clear deleted contacts
  contacts = [actors[x] for x in contact_nicks if actors[x]]
  streams = display.prep_stream_dict(streams, actors)
  entries = display.prep_entry_list(entries, streams, actors)

  # END inbox generation chaos
  
  # Check for unconfirmed emails
  unconfirmeds = api.activation_get_actor_email(request.user, view.nick)
  if unconfirmeds:
    unconfirmed_email = unconfirmeds[0].content

  # If not logged in, cannot write
  is_owner = False
  try:
    is_owner = view.nick == request.user.nick
  except:
    pass
  presence = api.presence_get(request.user, view.nick)

  # for sidebar streams
  view_streams = dict([(x.key().name(), streams[x.key().name()])
                       for x in actor_streams])

  # for sidebar_contacts
  contacts_count = view.extra.get('contact_count', 0)
  contacts_more = contacts_count > CONTACTS_PER_PAGE

  # Config for the template
  green_top = True
  sidebar_green_top = True
  selectable_icons = display.SELECTABLE_ICONS

  area = 'home'

  # TODO(tyler/termie):  This conflicts with the global settings import.
  # Also, this seems fishy.  Do none of the settings.* items work in templates?
  import settings
  
  c = template.RequestContext(request, locals())

  if format == 'html':
    t = loader.get_template('actor/templates/overview.html')
    return http.HttpResponse(t.render(c))
  elif format == 'json':
    t = loader.get_template('actor/templates/overview.json')
    r = util.HttpJsonResponse(t.render(c), request)
    return r
  elif format == 'atom':
    t = loader.get_template('actor/templates/overview.atom')
    r = util.HttpAtomResponse(t.render(c), request)
    return r
  elif format == 'rss':
    t = loader.get_template('actor/templates/overview.rss')
    r = util.HttpRssResponse(t.render(c), request)
    return r
示例#28
0
def actor_mentions(request, nick, format='html'):
  nick = clean.nick(nick)

  view = api.actor_lookup_nick(request.user, nick)

  if not view:
    raise exception.UserDoesNotExistError(nick, request.user)

  if not request.user or view.nick != request.user.nick:
    return http.HttpResponseRedirect(view.url())

  unauth = twitter.is_unauth(request)
  twitter_options = twitter.twitter_options(request)
  if 'twitter' in request.POST:
    if unauth:
      return http.HttpResponseRedirect('/twitter/auth?redirect_to=/%s/mentions' % view.display_nick())
    status = twitter.post_update(request)
    if status:
      flasherror = ["We have experimented some problems trying to post a cc in twitter"]
    
  handled = common_views.handle_view_action(
      request,
      { 
        'entry_remove': request.path,
        'entry_remove_comment': request.path,
        'entry_mark_as_spam': request.path,
        'presence_set': request.path,
        'settings_hide_comments': request.path,
        'post': request.path,
      }
  )
  if handled:
    return handled

  per_page = ENTRIES_PER_PAGE
  offset, prev = util.page_offset(request)

  inbox = api.inbox_get_actor_mentions(request.user,
                                       view.nick,
                                       limit=(per_page + 1), 
                                       offset=offset)

  # START inbox generation chaos
  # TODO(termie): refacccttttooorrrrr
  entries = api.entry_get_entries(request.user, inbox)

  #begin @zero code
  #if view.extra.get('comments_hide', 0):
    # TODO(tyler): This is certainly not the most eloquent way to filter
    # through entries to remove comments.
    # entries = [x for x in entries if not x.stream.endswith('comments')]
  entries = api.filter_entries_mentions(request.user, entries, view.nick)

  per_page = per_page - (len(inbox) - len(entries))
  entries, more = util.page_entries(request, entries, per_page)

  stream_keys = [e.stream for e in entries]
  actor_streams = api.stream_get_actor(request.user, view.nick)
  stream_keys += [s.key().name() for s in actor_streams]
  streams = api.stream_get_streams(request.user, stream_keys)

  contact_nicks = api.actor_get_contacts(request.user, 
                                         view.nick, 
                                         limit=CONTACTS_PER_PAGE)
  actor_nicks = (contact_nicks +
                 [view.nick] +
                 [s.owner for s in streams.values()] +
                 [e.owner for e in entries] +
                 [e.actor for e in entries])
  actors = api.actor_get_actors(request.user, actor_nicks)

  channels = api.actor_get_channels_member(request.user, view.nick,
                                limit=(CHANNELS_PER_PAGE + 1))

  # here comes lots of munging data into shape
  # clear deleted contacts
  contacts = [actors[x] for x in contact_nicks if actors[x]]
  streams = display.prep_stream_dict(streams, actors)
  entries = display.prep_entry_list(entries, streams, actors)

  # END inbox generation chaos
  
  # Check for unconfirmed emails
  unconfirmeds = api.activation_get_actor_email(request.user, view.nick)
  if unconfirmeds:
    unconfirmed_email = unconfirmeds[0].content

  # If not logged in, cannot write
  is_owner = False
  try:
    is_owner = view.nick == request.user.nick
  except:
    pass
  presence = api.presence_get(request.user, view.nick)

  # for sidebar streams
  view_streams = dict([(x.key().name(), streams[x.key().name()])
                       for x in actor_streams])

  channels_count = view.extra.get('channel_count', 0)
  channels_more = channels_count > CHANNELS_PER_PAGE
  followers_count = view.extra.get('follower_count', 0)

  # for sidebar_contacts
  contacts_count = view.extra.get('contact_count', 0)
  contacts_more = contacts_count > CONTACTS_PER_PAGE

  # Config for the template
  green_top = True
  sidebar_green_top = True
  selectable_icons = display.SELECTABLE_ICONS

  area = 'mentions'
  subtab = 'mentions'

  # TODO(tyler/termie):  This conflicts with the global settings import.
  # Also, this seems fishy.  Do none of the settings.* items work in templates?
  import settings
  
  c = template.RequestContext(request, locals())

  if format == 'html':
    t = loader.get_template('actor/templates/mentions.html')
    return http.HttpResponse(t.render(c))
示例#29
0
def actor_direct_messages(request, inbox='inbox'):
  logging.info('actor_direct_messages inbox: %s' % inbox)

  if not request.user:
    redirect_to = '/login?redirect_to=/inbox'
    if inbox == 'sent':
      redirect_to = '/login?redirect_to=/inbox/sent'
    return http.HttpResponseRedirect(redirect_to)
  
  view = request.user
  logging.info('View is: %s' % view)

  handled = common_views.handle_view_action(
      request,
      {
        'dm': '/inbox/sent',
        'dm_delete': request.path,
      }
  )
  if handled:
    return handled

  per_page = ENTRIES_PER_PAGE
  offset, prev = util.page_offset(request)

  if inbox == 'inbox':
    inbox_inbox = api.inbox_get_actor_inbox(request.user, request.user.nick,
                                            limit=(per_page + 1), offset=offset)
  elif inbox == 'sent':
    inbox_inbox = api.inbox_get_actor_sent(request.user, request.user.nick,
                                          limit=(per_page + 1), offset=offset)
  
  actor_streams = api.stream_get_actor_safe(request.user, request.user.nick)
  
  entries, more = helper.get_inbox_entries(request, inbox_inbox, True)
  entries = api.inbox_mark_unreaded(request.user, entries)
  
  contacts, channels, streams, entries = helper.assemble_inbox_data(request,
                                                              entries,
                                                              actor_streams,
                                                              request.user)
  
  friends_keys = api.actor_get_followers(view, view.nick, limit=200)
  friends_refs = api.actor_get_actors(view, friends_keys)

  fs = friends_refs.items()
  fs.sort()
  
  reply = request.REQUEST.get('reply', None)
  reply = clean.nick(reply)
  try:
    reply_ref = api.actor_get_safe(api.ROOT, reply)
  except:
    reply_ref = None

  if reply in friends_keys:
    let_send = True
  else:
    let_send = False

  friends = [value for key, value in fs]
  
  green_top = True
  sidebar_green_top = True
  subtab = 'dm'
  
  # for sidebar info
  channels_count = view.extra.get('channel_count', 0)
  channels_more = channels_count > CHANNELS_PER_PAGE
  followers_count = view.extra.get('follower_count', 0)
  contacts_count = view.extra.get('contact_count', 0)
  contacts_more = contacts_count > CONTACTS_PER_PAGE
  
  c = template.RequestContext(request, locals())
  t = loader.get_template('actor/templates/direct_messages.html')
  return http.HttpResponse(t.render(c))
示例#30
0
def channel_item(request, nick, item=None, format='html'):
  nick = clean.channel(nick)
  view = api.actor_lookup_nick(request.user, nick)

  if not view:
    raise http.Http404()

  stream_ref = api.stream_get_presence(request.user, view.nick)

  entry = '%s/%s' % (stream_ref.key().name(), item)

  entry_ref = api.entry_get(request.user, entry)
  if not entry_ref:
    raise http.Http404()

  handled = common_views.handle_view_action(
      request,
      {'entry_add_comment': entry_ref.url(request=request), 
       'entry_remove': view.url(request=request),
       'entry_remove_comment': entry_ref.url(request=request),
       'entry_mark_as_spam': entry_ref.url(request=request)
       }
      )
  if handled:
    return handled

  admins = api.channel_get_admins(request.user, channel=view.nick)
  user_is_admin = request.user and request.user.nick in admins

  comments = api.entry_get_comments(request.user, entry)

  actor_nicks = [entry_ref.owner, entry_ref.actor] + [c.actor for c in comments]
  actors = api.actor_get_actors(request.user, actor_nicks)

  # Creates a copy of actors with lowercase keys (Django #6904: template filter
  # dictsort sorts case sensitive), excluding channels and the currently
  # logged in user.
  participants = {}
  for k, v in actors.iteritems():
    if (v and
        not v.is_channel() and
        not (hasattr(request.user, 'nick') and request.user.nick == v.nick)):
      participants[k.lower()] = v

  # display munge
  entry = display.prep_entry(entry_ref,
                             { stream_ref.key().name(): stream_ref },
                             actors)
  comments = display.prep_comment_list(comments, actors)

  # config for template
  green_top = True
  sidebar_green_top = True

  # rendering
  c = template.RequestContext(request, locals())
  if format == 'html':
    t = loader.get_template('channel/templates/item.html')
    return http.HttpResponse(t.render(c))
  elif format == 'json':
    t = loader.get_template('actor/templates/item.json')
    r = http.HttpResponse(t.render(c))
    r['Content-type'] = 'text/javascript'
    return r
示例#31
0
def channel_history(request, nick, format='html'):
    """ the page for a channel

  if the channel does not exist we go to create channel instead

  should let you join a channel or post to it if you already are a member
  also leave it if you are a member,
  display the posts to this channel and the member list if you are allowed
  to see them

  if you are an admin you should have the options to modify the channel
  """
    nick = clean.channel(nick)

    view = api.actor_lookup_nick(request.user, nick)
    if not view:
        return http.HttpResponseRedirect('/channel/create?channel=%s' % nick)

    admins = api.channel_get_admins(request.user, channel=view.nick)
    members = api.channel_get_members(request.user, channel=view.nick)

    handled = common_views.handle_view_action(
        request, {
            'channel_join': request.path,
            'channel_part': request.path,
            'channel_post': request.path,
            'entry_remove': request.path,
            'entry_remove_comment': request.path,
            'entry_mark_as_spam': request.path,
            'subscription_remove': request.path,
            'subscription_request': request.path,
        })
    if handled:
        return handled

    privacy = 'public'

    user_can_post = False
    user_is_admin = False
    if not request.user:
        pass
    elif api.channel_has_admin(request.user, view.nick, request.user.nick):
        privacy = 'private'
        user_can_post = True
        user_is_admin = True
    elif api.channel_has_member(request.user, view.nick, request.user.nick):
        privacy = 'contacts'
        user_can_post = True

    per_page = CHANNEL_HISTORY_PER_PAGE
    offset, prev = util.page_offset(request)

    if privacy == 'public':
        inbox = api.inbox_get_actor_public(request.user,
                                           view.nick,
                                           limit=(per_page + 1),
                                           offset=offset)
    elif privacy == 'contacts':
        inbox = api.inbox_get_actor_contacts(request.user,
                                             view.nick,
                                             limit=(per_page + 1),
                                             offset=offset)
    elif privacy == 'private':
        inbox = api.inbox_get_actor_private(api.ROOT,
                                            view.nick,
                                            limit=(per_page + 1),
                                            offset=offset)

    # START inbox generation chaos
    # TODO(termie): refacccttttooorrrrr

    entries = api.entry_get_entries(request.user, inbox)
    # clear out deleted entries
    per_page = per_page - (len(inbox) - len(entries))
    entries, more = util.page_entries(request, entries, per_page)

    stream_keys = [e.stream for e in entries]
    actor_streams = api.stream_get_actor(request.user, view.nick)
    stream_keys += [s.key().name() for s in actor_streams]
    streams = api.stream_get_streams(request.user, stream_keys)

    contact_nicks = api.actor_get_contacts(request.user, view.nick)
    actor_nicks = (contact_nicks + admins + members + [view.nick] +
                   [s.owner
                    for s in streams.values()] + [e.actor for e in entries])
    actors = api.actor_get_actors(request.user, actor_nicks)

    # here comes lots of munging data into shape
    contacts = [actors[x] for x in contact_nicks if actors[x]]
    streams = display.prep_stream_dict(streams, actors)
    entries = display.prep_entry_list(entries, streams, actors)
    admins = [actors[x] for x in admins if actors[x]]
    members = [actors[x] for x in members if actors[x]]

    # END inbox generation chaos

    presence = api.presence_get(request.user, view.nick)

    # for sidebar_members
    members_count = view.extra['member_count']
    members_more = members_count > CONTACTS_PER_PAGE

    # for sidebar_admins
    admins_count = view.extra['admin_count']
    admins_more = admins_count > CONTACTS_PER_PAGE

    # config for templates
    green_top = True
    sidebar_green_top = True
    selectable_icons = display.SELECTABLE_ICONS

    # for sidebar streams (copied from actor/views.py.  refactor)
    view_streams = dict([(x.key().name(), streams[x.key().name()])
                         for x in actor_streams])
    if request.user:
        # un/subscribe buttons are possible only, when logged in

        # TODO(termie): what if there are quite a lot of streams?
        for stream in view_streams.values():
            stream.subscribed = api.subscription_exists(
                request.user,
                stream.key().name(), 'inbox/%s/overview' % request.user.nick)

    area = 'channel'
    c = template.RequestContext(request, locals())

    if format == 'html':
        t = loader.get_template('channel/templates/history.html')
        return http.HttpResponse(t.render(c))
    elif format == 'json':
        t = loader.get_template('channel/templates/history.json')
        r = util.HttpJsonResponse(t.render(c), request)
        return r
    elif format == 'atom':
        t = loader.get_template('channel/templates/history.atom')
        r = util.HttpAtomResponse(t.render(c), request)
        return r
    elif format == 'rss':
        t = loader.get_template('channel/templates/history.rss')
        r = util.HttpRssResponse(t.render(c), request)
        return r