/
comms.py
202 lines (154 loc) · 6.39 KB
/
comms.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
from tornado import Server
from time import time
from dbuser import User
from friends import *
import auth
import database_subject
from template_engine import template
import sqlite3
#users = {'Jordan':['hello', 'world']}
def age(old):
try:
UNITS = {86400:'day', 3600:'hour', 60:'minute', 1:'second'}
duration = int(time())-old
elapsed = ""
for unit in UNITS:
quotient = duration/unit
if quotient == 1:
elapsed += """%s %s """ % (str(quotient),UNITS[unit])
elif quotient > 1:
elapsed += """%s %ss """ % (str(quotient), UNITS[unit])
duration = duration % unit
if elapsed.split()[1] in ['day','days','hour','hours','minute','minutes']:
elapsed = " ".join(elapsed.split()[:2]) + ' '
return elapsed + 'ago'
except:
return 'moments ago'
class Message:
def __init__(self, author='', time='', message=''):
self.author = author
self.time = time
self.message = message
def __cmp__(self, other):
if self.time < other.time:
return -1
elif self.time > other.time:
return 1
elif self.time == other.time:
return 0
def display(self):
return self.author + ": " + self.message
class WallConnection:
def __init__(self, current_Wall):
self.current_Wall = current_Wall
self.connection = sqlite3.connect('./commsdb.sqlite')
self.cursor = self.connection.cursor()
self.cursor.execute('CREATE TABLE IF NOT EXISTS "WALL" ("ID" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , "author" TEXT NOT NULL , "target" TEXT NOT NULL , "time" INTEGER NOT NULL , "message" TEXT NOT NULL )')
def set_wall(self, m):
#users[current_Wall].append(author+' says ' + response.get_field('msg'))
#m.message = m.message.replace("&", "&").replace("<", "<").replace(">", ">").replace('"', """)
try:
self.cursor.execute('INSERT INTO "WALL" ("author","target","time","message") VALUES ("%s","%s","%s","%s")' % (m.author, self.current_Wall, m.time,m.message))
self.connection.commit()
return True
except sqlite3.OperationalError, e:
return e.args[0]
def get_wall(self):
final = []
data = self.cursor.execute('SELECT * FROM WALL ORDER BY time DESC')
if not data:
return 0
for row in data:
current_Row = [[row[0],row[1],row[1],row[2],row[4],age(int(row[3])),'']];
if row[2] == self.current_Wall:
author = User.get(row[1])
fullname = author.get_first_name() + " " + author.get_last_name()
path = author.get_profile_pic_path()
if not path:
current_Row[0][6] = '/static/images/default_avatar.jpeg'
else:
current_Row[0][6] = path
if current_Row[0][3].isdigit():
current_Row[0][3] = database_subject.get_subject(current_Row[0][3]).get_name()
current_Row[0][3] = current_Row[0][3].replace('_',' ')
current_Row[0][3] = 'Posted to ' + current_Row[0][3]
elif current_Row[0][3] == current_Row[0][2]:
current_Row[0][3] = ''
else:
current_Row[0][3] = 'Posted to ' + current_Row[0][3] +'\'s wall'
current_Row[0][1] = fullname
final.append(current_Row[0])
return final
class FeedConnection:
def __init__(self, response):
self.currentUserObject = auth.get_user(response);
self.current_User = self.currentUserObject.get_username()
self.connection = sqlite3.connect('./commsdb.sqlite')
self.cursor = self.connection.cursor()
self.cursor.execute('CREATE TABLE IF NOT EXISTS "WALL" ("ID" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , "author" TEXT NOT NULL , "target" TEXT NOT NULL , "time" INTEGER NOT NULL , "message" TEXT NOT NULL )')
def get_feed(self):
final = []
friendsList = get_friends(self.current_User)
data = self.cursor.execute('SELECT * FROM WALL ORDER BY time DESC LIMIT 15;')
if not data:
return 0
for row in data:
current_Row = [[row[0],row[1],row[1],row[2],row[4],age(int(row[3])),'']]
for user in friendsList:
if user.get_username() == row[1]:
if current_Row not in final:
fullname = user.get_first_name() + " " + user.get_last_name()
current_Row[0][1] = fullname
#set the picture
path = user.get_profile_pic_path()
if not path:
current_Row[0][6] = '/static/images/default_avatar.jpeg'
else:
current_Row[0][6] = path
if current_Row[0][3].isdigit():
current_Row[0][3] = database_subject.get_subject(current_Row[0][3]).get_name()
current_Row[0][3] = current_Row[0][3].replace('_',' ')
current_Row[0][3] = 'Posted to ' + current_Row[0][3]
elif current_Row[0][3] == current_Row[0][2]:
current_Row[0][3] = ''
else:
current_Row[0][3] = 'Posted to ' + current_Row[0][3] +'\'s wall'
final.append(current_Row[0])
if self.currentUserObject.get_username() == row[1]:
currentuser = self.currentUserObject
fullname = currentuser.get_first_name() + " " + currentuser.get_last_name()
current_Row[0][1] = fullname
path = currentuser.get_profile_pic_path()
if not path:
current_Row[0][6] = '/static/images/default_avatar.jpeg'
else:
current_Row[0][6] = path
if current_Row[0][3].isdigit():
current_Row[0][3] = database_subject.get_subject(current_Row[0][3]).get_name()
current_Row[0][3] = current_Row[0][3].replace('_',' ')
current_Row[0][3] = 'Posted to ' + current_Row[0][3]
elif current_Row[0][3] == current_Row[0][2]:
current_Row[0][3] = ''
else:
current_Row[0][3] = 'Posted to ' + current_Row[0][3]+'\'s wall'
final.append(current_Row[0])
# elif row[0] not in final[0]:
# print final[0]
return final
def _getWallData(response):
current_Wall = response.get_field('current_Wall')
w = WallConnection(current_Wall)
context = {"posts":w.get_wall()}
template.render_template('templates/wallcontent.html', context, response)
#final = header % (fullname+'\'s Wall', currentUrl)
#final += str(w.get_wall()) + '</div>'
def _getFeedData(response):
f = FeedConnection(response)
context = {"posts":f.get_feed()}
template.render_template('templates/wallcontent.html', context, response)
def _submit(response):
now = time()
m = Message(auth.get_user(response).get_username(), now, response.get_field('msg'))
w = WallConnection(response.get_field('current_Wall'))
w.set_wall(m)
return