def network():
    id_network = request.args.get('id')
    c = Client(mock=False)

    id_user = current_user.id if is_logged_in(current_user) else None
    network_info = gather_network_info(id_network, id_user, c)
    upcoming_events = get_upcoming_events_by_network(c, id_network, 3)

    return render_template('network.html',
                           network_info=network_info,
                           form=NetworkJoinForm(),
                           upcoming_events=upcoming_events)
Example #2
0
def render_event():
    current_event_id = request.args.get('id')
    c = Client(mock=False)
    event = c.get_event(current_event_id)
    date_info = {}
    enhance_event_date_info(event)
    event['num_registered'] = c.get_event_reg_count(event['id'])['reg_count']

    network = c.get_network(event['id_network'])
    event['network_title'] = get_network_title(network)
    event['location'] = get_event_location(event)
    host = c.get_user(event['id_host'])

    role = None

    if is_logged_in(current_user):
        user_id = current_user.id
        if event['id_host'] == int(user_id):

          # The current user is hosting this event.
          role = 'hosting'
          host['username'] = '******'

        elif user_is_attending_event(c, user_id, event):

          # The current user is already signed up for this event.
          role = 'attending'
        else:

          # The current user is not signed up for this event.
          pass
    else:
        user_id = None

    return render_template(
      'event.html',
      event=event,
      host=host,
      role=role,
      curr_user_id=user_id,
      join_form=EventJoinForm(),
      leave_form=EventLeaveForm(),
      cancel_form=EventCancelForm()
    )
Example #3
0
def render_post():

  current_post_id = safe_get_query_arg(request, 'id')

  user_id = current_user.get_id()
  c = Client(mock=False)
  post = c.get_post(current_post_id)

  post['network_title'] = get_network_title(c.get_network(post['id_network']))
  post['username'] = c.get_user(post["id_user"])["username"]
  post['time_ago'] = get_time_ago(post['post_date'])

  # NOTE: this will not show more than the latest 100 replies
  replies = c.get_post_replies(post["id"], NUM_REPLIES_TO_SHOW)
  replies = sorted(replies, key=lambda x: int(x['id']))

  error_msg = None

  for reply in replies:
      reply['username'] = c.get_user(reply["id_user"])["username"]
      reply['time_ago'] = get_time_ago(reply['reply_date'])

  if request.method == 'GET':
    pass
  elif not is_logged_in(current_user):
    return redirect(
        url_for('render_login_page')
    )
  else:
    data = request.form
    form_submitted = CreatePostReplyForm(request.form)
    if form_submitted.validate():
      post_reply_content = data['post_reply_content']

      reply = {
        'id_parent': current_post_id,
        'id_user': user_id,
        'id_network': post['id_network'],
        'reply_text': post_reply_content
      }

      c.create_post_reply(current_user, current_post_id, reply)
      return redirect(
        url_for('posts.render_post') + "?id=%s" % str(current_post_id)
      )
    else:
      error_msg = "Oops. An error occurred. Did you forget to type a reply \
        before submitting?"


  new_form = CreatePostReplyForm()

  return render_template(
    'post.html',
    post=post,
    replies=replies,
    num_replies=len(replies),
    curr_user_id=user_id,
    form=new_form,
    error_msg=error_msg
  )
def network_events():
    # TODO: A lot of this code is repeated from network(), with just minor variations.
    # This should be factored out.
    id_network = request.args.get('id')
    if not id_network:
        return render_template('404.html')
    c = Client(mock=False)
    try:
        id_network = int(id_network)
    except ValueError:
        return render_template('404.html')
    network = c.get_network(id_network)
    if not network:
        return render_template('404.html')

    events = None

    old_index = request.args.get('index')
    if not old_index:
        events = c.get_network_events(id_network, 10)
    else:
        try:
            old_index = int(old_index)
        except ValueError:
            return render_template('404.html')
        events = c.get_network_events(id_network, 10, old_index - 1)

    # TODO: Add better handling for when there's no posts left.

    if not events:
        event_index = old_index
    else:
        event_index = events[-1]['id']

    for event in events:
        utils.enhance_event_date_info(event)
        event['num_registered'] = c.get_event_reg_count(
            event['id'])['reg_count']

    user_is_member = False

    if is_logged_in(current_user):
        user_networks = c.get_user_networks(current_user.get_id(), count=100)
        for network_ in user_networks:
            if int(id_network) == int(network_['id']):
                user_is_member = True
                break

    network_info = {}
    network_info['id'] = id_network
    network_info['events'] = events
    network_info['network_title'] = get_network_title(network)
    network_info['user_is_member'] = user_is_member

    num_users = c.get_network_user_count(id_network)['user_count']
    num_posts = c.get_network_post_count(id_network)['post_count']

    network_info['num_users'] = num_users
    network_info['num_posts'] = num_posts

    referrer = request.headers.get("Referer")

    return render_template('network_events.html',
                           network_info=network_info,
                           event_index=event_index,
                           referer_url=referrer)
def network_posts():
    # TODO: A lot of this code is repeated from network(), with just minor variations.
    # This should be factored out.
    id_network = request.args.get('id')
    if not id_network:
        return render_template('404.html')
    c = Client(mock=False)
    try:
        id_network = int(id_network)
    except ValueError:
        return render_template('404.html')

    network = c.get_network(id_network)
    if not network:
        return render_template('404.html')

    # TODO: Get user ID and work out if user is in network.
    # Add join us button to page if they're not.

    posts = None

    old_index = request.args.get('index')
    if not old_index:
        posts = c.get_network_posts(id_network, 10)
    else:
        try:
            old_index = int(old_index)
        except ValueError:
            return render_template('404.html')
        posts = c.get_network_posts(id_network, 10, old_index - 1)

    for post in posts:
        post['username'] = c.get_user(post['id_user'])['username']
        post['reply_count'] = c.get_post_reply_count(post['id'])['reply_count']
        post['time_ago'] = get_time_ago(post['post_date'])

    # TODO: Add better handling for when there's no events left.

    if not posts:
        post_index = old_index
    else:
        post_index = posts[-1]['id']

    user_is_member = False
    if is_logged_in(current_user):
        user_networks = c.get_user_networks(current_user.get_id(), count=100)
        for network_ in user_networks:
            if int(id_network) == int(network_['id']):
                user_is_member = True
                break

    network_info = {}
    network_info['id'] = id_network
    network_info['posts'] = posts
    network_info['user_is_member'] = user_is_member

    num_users = c.get_network_user_count(id_network)['user_count']
    num_posts = c.get_network_post_count(id_network)['post_count']

    network_info['num_users'] = num_users
    network_info['num_posts'] = num_posts

    network_info['network_title'] = get_network_title(network)
    return render_template('network_posts.html',
                           network_info=network_info,
                           post_index=post_index)