def item_suggestions(userid): advertisements = [] #get the user user = userid_to_object(userid) #first, we get all the accounts the user has accounts = get_accounts(userid) #now get the past transactions and get similar items for account in accounts: transactions = get_transactions_from_account(account.accountno) for trans in transactions: ads = get_item_suggestions_transactions(trans) for ad in ads: advertisements.append(ad) #finally, get rid of duplicate items #this uses the __eq__ and __hash__ definition in the advertisement class #__eq__ compares two ads, __hash__ distinguishes an ad by an attribute #set creates a unique set, list converts it to a list (I believe) advertisements = list(set(advertisements)) #and send it to the template # get one of user's accounts to prefill in form account = get_user_account(userid) return render_template('customerreptasks/itemsuggestions.html', user=user, ads=advertisements, account=account)
def group_page(groupid): group = get_group(groupid) if not group: flash("Group does not exist.") return redirect(url_for('error')) owner = userid_to_object(group.groupownerid) #todo: make sure in_group method works if not in_group(session['userid'], groupid): flash("You can't view this page without joining the group.") return redirect(url_for('group_error', groupid=groupid)) post_form = PostForm(request.form) pageid = get_pageid_group(groupid) if post_form.validate() and request.method == 'POST': post_on_page(pageid, session['userid'], post_form.content.data) return redirect(url_for('group_page', groupid=groupid)) post_form = PostForm() posts = get_posts(pageid) return render_template('pages/grouppage.html', form=post_form, group=group, owner=owner, posts=posts, page=pageid)
def remove_from_group(gid, userid): if not in_group(userid, gid): flash("You can't remove a member not in the group.") return redirect(url_for('error')) user = userid_to_object(userid) delete_from_group(gid, userid) flash("Removed " + user.username + " from the group.") return redirect(url_for('delete_members', gid=gid))
def top_customer(): #todo: Fixed query to be correct. Check project phase 2 doc for views necessary to execute query. conn = mysql.connect() cursor = conn.cursor() query = "select c.userid, c.userrevenue from customerrevenue c where not exists( select c1.userid, c1.userrevenue from customerrevenue c1 where c1.userid <> c.userid and c1.userrevenue > c.userrevenue);" cursor.execute(query) data = cursor.fetchone() userid, revenue = data[0], data[1] user = userid_to_object(userid) return [user, data]
def add_customer_preference(userid): form = PreferenceForm(request.form) user = userid_to_object(userid) if request.method == 'POST' and form.validate(): insert_preference(userid, form.preference.data) preferences = get_preferences(userid) return render_template('/customerreptasks/preferences.html', form=form, preferences=preferences, user=user)
def get_comment(commentid): conn = mysql.connect() cursor = conn.cursor() cursor.execute( 'select commentid, authorid, postid, commentdate, content from comments where commentid=%s', (commentid)) data = cursor.fetchone() if not data: return None return Comment(data[0], data[1], data[2], data[3], data[4], userid_to_object(data[1]).username)
def get_comments(postid): conn = mysql.connect() cursor = conn.cursor() sql_string = "select commentid, authorid, postid, commentdate, content from comments where postid=%s order by commentdate desc" cursor.execute(sql_string, (postid)) data = cursor.fetchall() comments = [] for d in data: authorname = userid_to_object(d[1]).username comment = Comment(d[0], d[1], d[2], d[3], d[4], authorname) comment.set_likes(get_comment_likes(comment.commentid)) comments.append(comment) return comments
def get_post(postid): conn = mysql.connect() cursor = conn.cursor() sql_string = "select postid, authorid, pageid, postdate, content from Posts where postid=%s" cursor.execute(sql_string, (postid)) data = cursor.fetchone() if not data: return None # create Post object authorname = userid_to_object(data[1]).username post = Post(data[0], data[1], data[2], data[3], data[4], authorname) post.set_likes(get_post_likes(post.postid)) post.set_comments(get_comments(post.postid)) return post
def personalized_item_suggestions(userid): advertisements = [] user = userid_to_object(userid) preferences = get_user_preferences(userid) for pref in preferences: ads = get_item_suggestions_preferences(pref) for ad in ads: advertisements.append(ad) advertisements = list(set(advertisements)) # get user's primary account to prefill account = get_user_account(userid) return render_template('customerreptasks/itempreferences.html', user=user, ads=advertisements, account=account)
def post_page(postid): post = get_post(postid) if not post: flash("Post does not exist.") return redirect(url_for('error')) page = get_page(postid) # page post belongs to # Check whether post/comment thread rooted off a personal page or group page. if page.pagetype == 'Group': if not in_group(session['userid'], page.groupid): flash("You can't view this page without joining the group.") return redirect(url_for('group_error', groupid=page.groupid)) group = get_group(page.groupid) origin_type = 'group' else: user = userid_to_object(page.ownerid) origin_type = 'user' form = CommentForm(request.form) # write a comment if form.validate() and request.method == 'POST': comment_on_post(postid, session['userid'], form.content.data) redirect(url_for('post_page', postid=postid)) form = CommentForm() post = get_post(postid) #debug for c in post.comments: print(c.content, c.likes) # if origin_type == 'user': return render_template('pages/post.html', form=form, post=post, user=user, group=None) else: return render_template('pages/post.html', form=form, post=post, group=group, user=None)
def group_requests(): #dictionary (key is the group name, value are the users) cgdata = {} #get all the requests from the groups the user created created_groups = created_groups_controller() for group in created_groups: pending_members = get_pending_group_members(group.gid, 'owner') for mems in pending_members: # each key is a group name key = group # changed to groups, so we can get the gid in template. cgdata.setdefault(key, []) #as we find members for one group, we add it to the key user = userid_to_object(mems.userid) cgdata[key].append(user) #now, get all the requests from other groups other_groups = get_pending_user_requests(session['userid'], 'user') return render_template('groups/grouprequests.html', created_results=cgdata, other_results=other_groups, session=session)
def account_history(userid): #dictionary; key is the accountno for the user (many accounts to one user) accounts = {} # get the user user = userid_to_object(userid) #get the history from the user's accounts user_accounts = get_accounts(userid) for account in user_accounts: #collect all the information history = get_transactions_from_account(account.accountno) #and append it to the dictionary for h in history: key = account accounts.setdefault(key, []) accounts[key].append(h) #and send it to the template return render_template('customerreptasks/accounthistory.html', user=user, history=accounts)
def user_page(userid): user = userid_to_object(userid) if not user: flash("User does not exist.") return redirect(url_for('error')) post_form = PostForm(request.form) # get the pageid from the userid of the URL pageid = get_pageid_user(userid) # get posts on the page posts = get_posts(pageid) if post_form.validate() and request.method == 'POST': post_on_page(pageid, session['userid'], post_form.content.data) return redirect(url_for('user_page', userid=userid)) post_form = PostForm() return render_template('pages/userpage.html', form=post_form, user=user, posts=posts)
def get_posts(pageid): conn = mysql.connect() cursor = conn.cursor() # get posts on pageid from most recent to least recent sql_string = "select postid, authorid, pageid, postdate, content from Posts where pageid=%s order by postdate desc" cursor.execute(sql_string, (pageid)) data = cursor.fetchall() posts = [] # list to store post objects for d in data: #get username of post writer from id authorname = userid_to_object(d[1]).username post = Post(d[0], d[1], d[2], d[3], d[4], authorname) # get users who liked post post.set_likes(get_post_likes(post.postid)) # get comments on post post.set_comments(get_comments(post.postid)) posts.append(post) return posts
def confirm_friend(userid): confirm_friend_controller(userid) user = userid_to_object(userid) flash("You accepted " + user.username + "'s friend request!") #todo: link their user page return redirect(url_for('view_received_friend_requests'))
def add_member(gid, userid): insert_pending_group_member(gid, userid, 'user') username = userid_to_object(userid).username flash("Invited " + username + " to join the group.") return redirect(url_for('add_member_search', gid=gid))
def confirm_membership(gid, userid): group = get_group(gid) user = userid_to_object(userid) group_accept_controller(gid, userid) flash(user.username + " is now in " + group.groupname + ".") return redirect(url_for('group_requests'))