def render_post(): current_post_id = safe_get_query_arg(request, 'id') user_id = current_user.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 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 gather_network_info(id_network, id_user, client, scenario="normal"): network = client.get_network(id_network) recent_posts = client.get_network_posts(id_network, 3) recent_events = client.get_network_events(id_network, 3) for event in recent_events: utils.enhance_event_date_info(event) for post in recent_posts: post['username'] = client.get_user(post['id_user'])['username'] post['reply_count'] = client.get_post_reply_count( post['id'])['reply_count'] post['time_ago'] = get_time_ago(post['post_date']) user_is_member = False if not id_user == None: user_networks = client.get_user_networks(id_user, 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'] = recent_posts network_info['events'] = recent_events network_info['network_title'] = get_network_title(network) network_info['user_is_member'] = user_is_member if user_is_member and scenario == 'normal': network_info[ 'greeting'] = 'You are a member of this network. Welcome!' elif not user_is_member and scenario == 'normal': network_info[ 'greeting'] = 'You are not a member of this network. Join now to create posts and events.' elif user_is_member and scenario == 'join': network_info['greeting'] = 'You just joined this network. Welcome!' elif not user_is_member and scenario == 'join': network_info[ 'greeting'] = 'Looks like something went wrong. Try joining later.' elif user_is_member and scenario == 'leave': network_info[ 'greeting'] = 'Looks like something went wrong. Try leaving later.' elif not user_is_member and scenario == 'leave': network_info['greeting'] = 'You just left this network. Bye bye!' num_users = client.get_network_user_count(id_network)['user_count'] num_posts = client.get_network_post_count(id_network)['post_count'] network_info['num_users'] = num_users network_info['num_posts'] = num_posts return network_info
def edit_reply(): c = Client(mock=False) post_reply_id = safe_get_query_arg(request, 'id') post_reply = c.get_post_reply(post_reply_id) id_parent = post_reply['id_parent'] if post_reply['id_user'] != current_user.id: abort(httplib.NOT_FOUND) edit_post_reply_form = EditPostForm() post_reply['network_title'] = get_network_title( c.get_network(post_reply['id_network']) ) edit_post_reply_form.content.process_data(post_reply['reply_text']) error_msg = None if request.method == 'GET': pass else: data = request.form form_submitted = EditPostForm(request.form) if form_submitted.validate(): reply_text = data['content'] post_reply = { 'id': post_reply_id, 'reply_text': reply_text, } c.update_post_reply(current_user, id_parent, post_reply) return redirect( url_for('posts.render_post') + "?id=%s" % str(id_parent) ) else: error_msg = "Oops. An error ocurred. Keep in mind posts can't be empty." return render_template( 'edit_post.html', edit_post_form=edit_post_reply_form, is_post_reply=True, post=post_reply, network_title=post_reply['network_title'], current_user=current_user, error_msg=error_msg )
def user_profile(user_id): c = Client(mock=False) user = c.get_user(user_id) user['img_url'] = get_user_image_url(user) user_networks = c.get_user_networks(user_id, MAX_NETWORKS_TO_LOAD) networks = [] for network in user_networks: network_ = {'id': network['id']} network_['title'] = get_network_title(network) network_['join_date'] = get_short_network_join_date(network) num_users = c.get_network_user_count(network['id'])['user_count'] network_['user_count'] = num_users networks.append(network_) return render_template('profile.html', user=user, networks=networks)
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() )
def render_user_home_networks(): c = Client(mock=False) user = copy.deepcopy(current_user) user_id = user.id user.img_url = get_user_image_url(user) if user is None: return page_not_found("") user_networks = c.get_user_networks(user_id, count=MAX_NETWORKS_TO_LOAD) networks = [] for network in user_networks: network_ = {'id': network['id']} network_['title'] = get_network_title(network) network_['join_date'] = get_short_network_join_date(network) num_users = c.get_network_user_count(network['id'])['user_count'] network_['user_count'] = num_users networks.append(network_) return render_template('networks.html', user=user.as_dict, networks=networks)
def render_user_home(): user = copy.deepcopy(current_user) c = Client(mock=False) user.img_url = get_user_image_url(user) # Events user is hosting events_hosting = get_upcoming_events_by_user_hosting( c, user.id, NUM_EVENT_HOSTING_TO_DISPLAY) # Events user is attending events_attending = get_upcoming_events_by_user_attending( c, user.id, NUM_EVENT_ATTENDING_TO_DISPLAY) # Upcoming events in user's networks. upcoming_events = get_upcoming_events_by_user( c, user.id, NUM_UPCOMING_EVENTS_TO_DISPLAY) # Some latest posts in the user's networks. latest_posts = c.get_user_posts(user.id, NUM_LATEST_POSTS_TO_DISPLAY) for post in latest_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']) try: post['network'] = c.get_network(post['id_network']) post['network_title'] = get_network_title(post['network']) except HTTPException as e: post['network'] = None post['network_title'] = "Unknown" return render_template('dashboard.html', user=user.as_dict, events_hosting=events_hosting, events_attending=events_attending, latest_posts=latest_posts, upcoming_events_in_networks=upcoming_events)
def edit_event(): c = Client(mock=False) event_id = safe_get_query_arg(request, 'id') event = c.get_event(event_id) if event['id_host'] != current_user.id: abort(httplib.NOT_FOUND) edit_event_form = EditEventForm() event['network_title'] = get_network_title( c.get_network(event['id_network']) ) edit_event_form.title.process_data(event['title']) edit_event_form.country.process_data(event['country']) edit_event_form.region.process_data(event['region']) edit_event_form.city.process_data(event['city']) edit_event_form.address_1.process_data(event['address_1']) edit_event_form.address_2.process_data(event['address_2']) edit_event_form.event_date.process_data( parse_date(event['event_date']) ) edit_event_form.description.process_data(event['description']) error_msg = None if request.method == 'GET': pass else: data = request.form form_submitted = EditEventForm(request.form) if form_submitted.validate(): event_date = data['event_date'] title = data['title'] address_1 = data['address_1'] address_2 = data.get('address_2', None) country = data['country'] region = data.get('region', None) city = data.get('city', None) description = data['description'] event = { 'id': event_id, 'title': title, 'country': country, 'region': region, 'city': city, 'address_1': address_1, 'address_2': address_2, 'event_date': event_date, 'description': description } c.update_event(current_user, event) return redirect( url_for('events.render_event') + "?id=%s" % str(event_id) ) else: error_msg = "Oops. An error occurred. Did you enter all \ of the form fields correctly?" return render_template( 'edit_event.html', edit_event_form=edit_event_form, network_title=event['network_title'], current_user=current_user, curr_user_id=current_user.id, id_network=event['id_network'], 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'] id_user = current_user.id user_networks = c.get_user_networks(id_user, count=100) user_is_member = False 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'] id_user = current_user.id user_networks = c.get_user_networks(id_user, count=100) user_is_member = False 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)