-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprofops.py
65 lines (55 loc) · 2.65 KB
/
profops.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# profops.py
# CS304-Final Project
# Created by: Megan Shum, Maxine Hood, Mina Hattori
#!/usr/local/bin/python2.7
# This file handles all the SQL requests needed for the profile page.
import sys
import MySQLdb
import dbconn2
def retrievePics(conn, username):
'''Returns all of the user's pic post from the database in the form of a dictionary'''
curs = conn.cursor(MySQLdb.cursors.DictCursor) # results as Dictionaries
curs.execute('select pic, description from posts where username = %s', [username])
return curs.fetchall()
def getFollow(conn, username):
'''Returns the number of followers'''
curs = conn.cursor(MySQLdb.cursors.DictCursor)
curs.execute('select count(*) as followers from followers where following = %s', [username])
info = curs.fetchone()
return info['followers']
def getFollowing(conn, username):
'''Returns the number of users this user is following'''
curs = conn.cursor(MySQLdb.cursors.DictCursor)
curs.execute('select count(*) as following from followers where follower = %s', [username])
info = curs.fetchone()
return info['following']
def follow(conn, follower, following):
'''Adds row in follow table to show that a user followed another user'''
curs = conn.cursor(MySQLdb.cursors.DictCursor)
curs.execute('insert into followers(follower,following) values (%s, %s)', [follower, following])
def unfollow(conn, follower, following):
'''Deletes a row in the follow table when a user unfollows another user'''
curs = conn.cursor(MySQLdb.cursors.DictCursor)
curs.execute('delete from followers where follower = %s and following = %s', [follower, following])
def isFollowing(conn, follower, following):
'''Checks if a user is already following another user'''
curs = conn.cursor(MySQLdb.cursors.DictCursor)
curs.execute('select follower from followers where follower = %s and following = %s', [follower, following])
info = curs.fetchone()
return (info is not None)
def numPosts(conn, username):
'''Returns the total number of posts a user has created'''
curs = conn.cursor(MySQLdb.cursors.DictCursor)
curs.execute('select count(*) from posts where username=%s',[username])
return curs.fetchone()['count(*)']
# ================================================================
# This starts the ball rolling, *if* the script is run as a script,
# rather than just being imported.
if __name__ == '__main__':
if len(sys.argv) < 2:
print "Usage: {name} nm".format(name=sys.argv[0])
else:
DSN = dbconn2.read_cnf()
DSN['db'] = 'mmm_db' # the database we want to connect to
dbconn2.connect(DSN)
print lookupByNM(sys.argv[1])