-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalbum_view.py
159 lines (139 loc) · 6.5 KB
/
album_view.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
from base_handler import *
import cgi
import MySQLdb
from google.appengine.api import users
import os
class ViewAlbum(BaseHandler):
def get(self,album_id):
user = users.get_current_user()
community_id = 0
if user:
email = user.email()
if (os.getenv('SERVER_SOFTWARE') and os.getenv('SERVER_SOFTWARE').startswith('Google App Engine/')):
db = MySQLdb.connect(unix_socket='/cloudsql/hack-the-north-1:its-not-django', db='musicsite', user='root')
else:
db = MySQLdb.connect(host='localhost', user='root', passwd="htndjango",db="musicsite")
cursor = db.cursor()
cursor.execute('SELECT community_id FROM albums WHERE id = %s' % album_id)
for rows in cursor:
community_id = rows[0]
cursor.execute('SELECT * FROM users WHERE email = "%s" AND invite_accepted=1 AND community_id=%s'%(email,community_id))
if (cursor.rowcount == 0):
self.redirect('/communities')
else:
cursor.execute('SELECT album_name, album_artist, album_genre, album_year, posted_by FROM albums WHERE id = %s' % album_id)
for rows in cursor:
album_name = rows[0]
album_artist = rows[1]
album_genre = rows[2]
album_year = rows[3]
album_posted_by = rows[4]
action = ''
cursor.execute('SELECT * FROM comments WHERE parent_album_id = %s AND posted_by = "%s"'%(album_id,email))
if (cursor.rowcount == 0):
action = 'Add'
else:
action = 'Edit'
cursor.execute('SELECT nickname FROM users WHERE email = "%s"' % album_posted_by)
for rows in cursor:
album_posted_by = rows[0]
template_messages={
"album_name":album_name,
"album_artist":album_artist,
"album_genre":album_genre,
"album_year":album_year,
"album_posted_by":album_posted_by,
"album_id":album_id,
"community_id":community_id,
"action":action
}
self.render_response('album_info.html', **template_messages)
html_string = ""
cursor.execute('SELECT posted_by,comment,last_edit,rating FROM comments WHERE parent_album_id = %s' % album_id)
cursor2 = db.cursor()
for rows in cursor:
comment_posted_by = rows[0]
cursor2.execute('SELECT nickname FROM users WHERE email = "%s"' % comment_posted_by)
for row2 in cursor2:
comment_posted_by = row2[0]
comment_text = rows[1]
comment_last_edit = rows[2]
comment_rating = rows[3]
comment_html = """<div><p><font size="5">%s:</font> %s<p></div>""" % (comment_posted_by, comment_text)
html_string = html_string + comment_html
self.response.write(html_string)
self.response.write('</div></body></html>')
else:
self.redirect('/communities')
def post(self,album_id):
user = users.get_current_user()
community_id = 0
if user:
email = user.email()
if (os.getenv('SERVER_SOFTWARE') and os.getenv('SERVER_SOFTWARE').startswith('Google App Engine/')):
db = MySQLdb.connect(unix_socket='/cloudsql/hack-the-north-1:its-not-django', db='musicsite', user='root')
else:
db = MySQLdb.connect(host='localhost', user='root', passwd="htndjango",db="musicsite")
cursor = db.cursor()
cursor.execute('SELECT community_id FROM albums WHERE id = %s' % album_id)
for rows in cursor:
community_id = rows[0]
cursor.execute('SELECT * FROM users WHERE email = "%s" AND invite_accepted=1 AND community_id=%s'%(email,community_id))
if (cursor.rowcount == 0):
self.redirect('/communities')
else:
get_comment_text = cgi.escape(self.request.get('post_comment_text'))
get_comment_rating = cgi.escape(self.request.get('post_comment_rating'))
cursor.execute('SELECT * FROM comments WHERE parent_album_id = %s AND posted_by = "%s"' % (album_id,email))
if (cursor.rowcount == 0):
#this is a new comment
cursor.execute('INSERT INTO comments (posted_by, parent_album_id, comment, last_edit, rating) VALUES ("%s",%s,"%s",NOW(),%s)'%(email,album_id,get_comment_text,get_comment_rating))
db.commit()
else:
#this is an edit to an existing comment
cursor.execute('UPDATE comments SET comment = "%s",rating=%s,last_edit=NOW() WHERE posted_by="%s" AND parent_album_id=%s'%(get_comment_text,get_comment_rating,email,album_id))
db.commit()
cursor.execute('SELECT album_name, album_artist, album_genre, album_year, posted_by FROM albums WHERE id = %s' % album_id)
for rows in cursor:
album_name = rows[0]
album_artist = rows[1]
album_genre = rows[2]
album_year = rows[3]
album_posted_by = rows[4]
action = ''
cursor.execute('SELECT * FROM comments WHERE parent_album_id = %s AND posted_by = "%s"'%(album_id,email))
if (cursor.rowcount == 0):
action = 'Add'
else:
action = 'Edit'
cursor.execute('SELECT nickname FROM users WHERE email = "%s"' % album_posted_by)
for rows in cursor:
album_posted_by = rows[0]
template_messages={
"album_name":album_name,
"album_artist":album_artist,
"album_genre":album_genre,
"album_year":album_year,
"album_posted_by":album_posted_by,
"album_id":album_id,
"community_id":community_id,
"action":action
}
self.render_response('album_info.html', **template_messages)
html_string = ""
cursor.execute('SELECT posted_by,comment,last_edit,rating FROM comments WHERE parent_album_id = %s' % album_id)
cursor2 = db.cursor()
for rows in cursor:
comment_posted_by = rows[0]
cursor2.execute('SELECT nickname FROM users WHERE email = "%s"' % comment_posted_by)
for row2 in cursor2:
comment_posted_by = row2[0]
comment_text = rows[1]
comment_last_edit = rows[2]
comment_rating = rows[3]
comment_html = """<div><p><font size="5">%s:</font> %s<p></div>""" % (comment_posted_by, comment_text)
html_string = html_string + comment_html
self.response.write(html_string)
self.response.write('</div></body></html>')
else:
self.redirect('/communities')