def item_format(post): # Post format: (user, poster, message, image, time, id) data = {} data['poster_name'] = matedb.get_data(post[1], 'name') data['message'] = post[2] if post[3]: data['image'] = '<p><img src="' + post[3] + '"></img></p>'; else: data['image'] = '' login = authbar.get_current_login() if login in matedb.get_all_mates(post[0]) or post[0] == login: data['comment_form'] = get_comment_form(post[5]) else: data['comment_form'] = '' data['comments'] = '' comments = matedb.get_comments(post[5]) comments.sort(key=lambda item: item[2]) comments.reverse() for comment in comments: data['comments'] += comment_format(comment) return tempy.substitute('news_item.template', data)
def render(user): if authbar.get_current_login() != user: return '' # We know the user is logged in d = db.get_user_data(user) return tempy.substitute('editor.template', d)
def get_privacy_pane (user): if authbar.get_current_login() != user: return '' states = dict() for field in matedb.privacy_fields: states[field + '_state'] = cb_state (user, field) return tempy.substitute ('privacy.template', states)
def get_user_data(user): requester = authbar.get_current_login() mates = matedb.get_all_mates(user) data = matedb.get_user_data(user) for property in data.keys(): if private (user, property) and requester not in mates and requester != user: data[property] = '' return data
def do_upload(request): fileitem = request['file'] image_file_name = get_unique_image_name() user = authbar.get_current_login() if user and fileitem.filename: open(config.data_dir + image_file_name, 'wb').write(fileitem.file.read()) db.add_image(user, image_file_name) if 'profile' in request: db.set_data(user, 'profile_picture', image_file_name) return '<script type="text/javascript">window.location.href="unswmate.cgi?who=' + authbar.get_current_login() + '"</script>'
def do_privacy (request): user = authbar.get_current_login() for field in matedb.privacy_fields: if field in request: value = request[field].value if value == 'on': matedb.make_private(user, field) else: matedb.make_public(user, field) return '<script type="text/javascript">window.location.href="unswmate.cgi?who=' + user + '"</script>'
def do_edit(request): # This is where things get interesting user = authbar.get_current_login() if 'name' in request: db.set_data(user, 'name', make_safe(request['name'].value)) if 'about' in request: db.set_data(user, 'about', make_safe(request['about'].value)) if 'gender' in request: db.set_data(user, 'gender', make_safe(request['gender'].value)) if 'degree' in request: db.set_data(user, 'degree', make_safe(request['degree'].value)) if 'student_number' in request: db.set_data(user, 'student_number', make_safe(request['student_number'].value)) # Reload the edited page return '<script type="text/javascript">window.location.href="unswmate.cgi?who=' + user + '"</script>'
def get_control_panel(user): panel = '<form method="post"><br />' login = authbar.get_current_login() if user == login: panel += '<p class="text-success"><b>Me!</b></p>' elif user in db.get_all_mates(login): panel += '<p class="text-success"><b>Mates</b></p>' elif login: panel += '<button class="btn" type="submit">Send Mate Request</button>\n' panel += '<input type="hidden" name="action" value="mate">\n' panel += '<input type="hidden" name="user" value="' + user + '">\n' panel += '</form>' return panel
def get_news_pane(user): components = dict() login = authbar.get_current_login() if not privacy.permitted(user, 'news'): return '' if user in matedb.get_all_mates(login) or user == login: components['post_box'] = get_post_box(user) else: components['post_box'] = '' components['news_items'] = '' posts = matedb.get_news(user) # Sort by time. Could use a namedtuple, or a class. Could do lots of things... posts.sort(key=lambda item: item[4]) posts.reverse() for post in posts: components['news_items'] += item_format(post) return tempy.substitute ('news.template', components)
def do_comment(request): poster = authbar.get_current_login() if 'news_item_id' in request: news_item_id = request['news_item_id'].value else: news_item_id = '' if 'message' in request: message = request['message'].value else: message = '' # Rip the value out of the resulting tuple, if it exists user = matedb.get_owner(news_item_id) if user: user = user[0] if news_item_id and message: if poster in matedb.get_all_mates(user) or user == poster: matedb.post_comment(news_item_id, poster, safety.make_safe(message)) return '<script type="text/javascript">window.location.href="unswmate.cgi?who=' + user + '"</script>'
def do_news(request): poster = authbar.get_current_login() if 'user' in request: user = request['user'].value else: user = '' if 'message' in request: message = request['message'].value else: message = '' if 'image' in request: image = request['image'].value else: image = '' if user and message and poster: if poster in matedb.get_all_mates(user) or user == poster: matedb.post_news(user, poster, safety.make_safe(message), safety.make_safe(image)) return '<script type="text/javascript">window.location.href="unswmate.cgi?who=' + user + '"</script>'
def do_suggest(request): user = authbar.get_current_login() if not user: return "" ranked_list = [] for person, score in get_potential_mates(user).items(): ranked_list.append((person, score)) if "page" not in request: page = 0 else: page = request["page"].value ranked_list.sort(key=lambda pair: pair[1]) ranked_list.reverse() first = int(page) * suggestions_per_page last = first + suggestions_per_page data = dict(username=user, suggestions="") renderlist = [] for user in ranked_list[first:last]: renderlist.append(user[0]) data["suggestions"] = userlist.render(renderlist) if data["suggestions"]: data["next_page_link"] = '<a href="?action=suggest&page=' + (str(int(page) + 1)) + '">I need more!</a>' else: data[ "next_page_link" ] = "There's no more... You need to go outside. Meet some new people. Make some real friends. Go. There's things the internet just can't offer." return tempy.render("suggest.template", data)
def do_mate(request): d = {} requester = authbar.get_current_login() if requester and'user' in request: user = request['user'].value if 'token' not in request: user_address = db.get_data(user, 'email') if user_address == '*****@*****.**': csemail.send(user_address, "UNSW Mate request", get_mate_message(user, requester)) d['message'] = 'Mate request sent. Stay tuned...'; d['link'] = '?who=' + user d['link_message'] = "Return to their page. Stalk mode engage!" else : # Make a mateship db.add_mate(user, requester) # This is a bit interesting. The order is actually reversed # because it's the other user loading the link d['message'] = 'Mate added!' d['link'] = '?who=' + user d['link_message'] = "Go to their page" else: d['message'] = 'User not found'; if requester: d['link'] = '?who=' + requester d['link_message'] = "Back to my page..." else: d['link'] = '' d['link_message'] = "Return to UNSWMate" return tempy.render('mate.template', d)
def get_suggest_tab(user): if authbar.get_current_login() != user: return "" else: return '<li><a href="#suggest" data-toggle="tab">Find Mates</a></li>'
def get_privacy_tab (user): if authbar.get_current_login() != user: return '' else: return '<li><a href="#privacy" data-toggle="tab">Privacy</a></li>'
def permitted (user, property): requester = authbar.get_current_login() mates = matedb.get_all_mates(user) return requester == user or requester in mates or not private (user, property)
def get_upload_tab(user): if authbar.get_current_login() != user: return '' else: return '<li><a href="#upload" data-toggle="tab">Upload Images</a></li>'
def render(user): if authbar.get_current_login() != user: return '' else: return tempy.substitute('upload.template', None)
def get_suggest_pane(user): if authbar.get_current_login() != user: return "" return '<div class="row-fluid"><div class="span12"><h3 style="text-align: center"><a href="?action=suggest">Find me some mates...</a></h3></div></div>'
def get_edit_tab(user): if authbar.get_current_login() == user: return '<li><a href="#edit" data-toggle="tab">Edit Profile</a></li>' else: return ''