-
Notifications
You must be signed in to change notification settings - Fork 0
/
newfriends.py.txt
73 lines (56 loc) · 2.21 KB
/
newfriends.py.txt
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
66
67
68
69
70
71
72
73
#!/usr/bin/python
#import CGI handling modules
import cgi,cgitb
#create instance of FieldStorage
form = cgi.FieldStorage()
#get current user
userName = form.getvalue('user')
#get list of friends
friends = form.getlist('friend')
#open list of friends
with open('friends.txt','r') as friendList:
lists = friendList.readlines()
userNamePresent = False
for i, line in enumerate(lists):
#puts line into a temporary string
tempString=line.strip('\n')
#splits line into a list of users
currentList=line.split()
#check if the current user already has a registered friend list
if (userName==currentList[0]):
userNamePresent = True
#iterate through the selected friends
for item in friends:
#if user not already a friend, we add it onto the string
if (item not in tempString):
tempString += (' ' + item)
tempString += '\n'
with open ('friends.txt', 'r') as friendList:
newData = friendList.readlines()
newData[i]=tempString
#open friends.txt to write into
with open('friends.txt','w')as friendList:
friendList.writelines(newData)
#in the case the username was not present in the friends.txt file
if (userNamePresent == False):
tempString = userName
for item in friends:
if (item not in tempString):
tempString += (' ' + item)
tempString += '\n'
with open('friends.txt','a') as friendList:
friendList.write(tempString)
#HTML coding
print 'Content-type:text/html\r\n\r\n'
print '<html>'
print '<head>'
print '<title> You\'ve made new friends!</title>'
print '</head>'
print '<body style=\"margin:42;font-family:arial;background-color:#333333;color:#d3ffce;\"alink=\"#fac7d0\" vlink=\"#fac7d0\"link=\"#faeb7\"> <br />'
print 'You\'ve just added new friends!'
print '<br /><br /><br />'
#link back to dashboard
print '<a href=\"http://cs.mcgill.ca/~echoin2/dashboard.py?user=%s\">'%(userName)
print 'Return to dashboard</a>'
print '</body>'
print '</html>'