1010import json
1111
1212from pgcommitfest .auth import user_search
13+ from models import CommitFest , Patch , MailThread , MailThreadAttachment
14+ from models import MailThreadAnnotation , PatchHistory
15+
1316
14- class HttpResponseServiceUnavailable (HttpResponse ):
17+ class HttpResponseServiceUnavailable (HttpResponse ):
1518 status_code = 503
1619
20+
1721class Http503 (Exception ):
1822 pass
1923
20- from models import CommitFest , Patch , MailThread , MailThreadAttachment
21- from models import MailThreadAnnotation , PatchHistory
2224
2325def _archivesAPI (suburl , params = None ):
2426 try :
25- resp = requests .get ("http{0}://{1}:{2}{3}" .format (settings .ARCHIVES_PORT == 443 and 's' or '' ,
26- settings .ARCHIVES_SERVER ,
27- settings .ARCHIVES_PORT ,
28- suburl ),
29- params = params ,
30- headers = {
31- 'Host' : settings .ARCHIVES_HOST ,
32- },
33- timeout = settings .ARCHIVES_TIMEOUT ,
27+ resp = requests .get (
28+ "http{0}://{1}:{2}{3}" .format (settings .ARCHIVES_PORT == 443 and 's' or '' ,
29+ settings .ARCHIVES_SERVER ,
30+ settings .ARCHIVES_PORT ,
31+ suburl ),
32+ params = params ,
33+ headers = {
34+ 'Host' : settings .ARCHIVES_HOST ,
35+ },
36+ timeout = settings .ARCHIVES_TIMEOUT ,
3437 )
3538 if resp .status_code != 200 :
3639 if resp .status_code == 404 :
@@ -43,9 +46,10 @@ def _archivesAPI(suburl, params=None):
4346 except Exception as e :
4447 raise Http503 ("Failed to communicate with archives backend: %s" % e )
4548
49+
4650def getThreads (request ):
47- search = request .GET .has_key ('s' ) and request . GET [ 's' ] or None
48- if request .GET .has_key ('a' ) and request . GET [ 'a' ] == "1" :
51+ search = request .GET .get ('s' , None )
52+ if request .GET .get ('a' , '0' ) == '1' :
4953 attachonly = 1
5054 else :
5155 attachonly = 0
@@ -58,6 +62,7 @@ def getThreads(request):
5862 r = _archivesAPI ('/list/pgsql-hackers/latest.json' , params )
5963 return sorted (r , key = lambda x : x ['date' ], reverse = True )
6064
65+
6166def getMessages (request ):
6267 threadid = request .GET ['t' ]
6368
@@ -67,6 +72,7 @@ def getMessages(request):
6772 r = _archivesAPI ('/message-id.json/%s' % thread .messageid )
6873 return sorted (r , key = lambda x : x ['date' ], reverse = True )
6974
75+
7076def refresh_single_thread (thread ):
7177 r = sorted (_archivesAPI ('/message-id.json/%s' % thread .messageid ), key = lambda x : x ['date' ])
7278 if thread .latestmsgid != r [- 1 ]['msgid' ]:
@@ -83,6 +89,7 @@ def refresh_single_thread(thread):
8389 p .lastmail = thread .latestmessage
8490 p .save ()
8591
92+
8693@transaction .atomic
8794def annotateMessage (request ):
8895 thread = get_object_or_404 (MailThread , pk = int (request .POST ['t' ]))
@@ -112,6 +119,7 @@ def annotateMessage(request):
112119 return 'OK'
113120 return 'Message not found in thread!'
114121
122+
115123@transaction .atomic
116124def deleteAnnotation (request ):
117125 annotation = get_object_or_404 (MailThreadAnnotation , pk = request .POST ['id' ])
@@ -125,6 +133,7 @@ def deleteAnnotation(request):
125133
126134 return 'OK'
127135
136+
128137def parse_and_add_attachments (threadinfo , mailthread ):
129138 for t in threadinfo :
130139 if len (t ['atts' ]):
@@ -142,6 +151,7 @@ def parse_and_add_attachments(threadinfo, mailthread):
142151 # In theory we should remove objects if they don't have an
143152 # attachment, but how could that ever happen? Ignore for now.
144153
154+
145155@transaction .atomic
146156def attachThread (request ):
147157 cf = get_object_or_404 (CommitFest , pk = int (request .POST ['cf' ]))
@@ -150,6 +160,7 @@ def attachThread(request):
150160
151161 return doAttachThread (cf , patch , msgid , request .user )
152162
163+
153164def doAttachThread (cf , patch , msgid , user ):
154165 # Note! Must be called in an open transaction!
155166 r = sorted (_archivesAPI ('/message-id.json/%s' % msgid ), key = lambda x : x ['date' ])
@@ -166,10 +177,10 @@ def doAttachThread(cf, patch, msgid, user):
166177 # While at it, we update the thread entry with the latest data from the
167178 # archives.
168179 thread .patches .add (patch )
169- thread .latestmessage = r [- 1 ]['date' ]
170- thread .latestauthor = r [- 1 ]['from' ]
171- thread .latestsubject = r [- 1 ]['subj' ]
172- thread .latestmsgid = r [- 1 ]['msgid' ]
180+ thread .latestmessage = r [- 1 ]['date' ]
181+ thread .latestauthor = r [- 1 ]['from' ]
182+ thread .latestsubject = r [- 1 ]['subj' ]
183+ thread .latestmsgid = r [- 1 ]['msgid' ]
173184 thread .save ()
174185 else :
175186 # No existing thread existed, so create it
@@ -195,6 +206,7 @@ def doAttachThread(cf, patch, msgid, user):
195206
196207 return 'OK'
197208
209+
198210@transaction .atomic
199211def detachThread (request ):
200212 cf = get_object_or_404 (CommitFest , pk = int (request .POST ['cf' ]))
@@ -209,16 +221,18 @@ def detachThread(request):
209221
210222 return 'OK'
211223
224+
212225def searchUsers (request ):
213- if request .GET .has_key ('s' ) and request . GET [ 's' ] :
226+ if request .GET .get ('s' , '' ) :
214227 return user_search (request .GET ['s' ])
215228 else :
216229 return []
217230
231+
218232def importUser (request ):
219- if request .GET .has_key ('u' ) and request . GET [ 'u' ] :
233+ if request .GET .get ('u' , '' ) :
220234 u = user_search (userid = request .GET ['u' ])
221- if len (u ) != 1 :
235+ if len (u ) != 1 :
222236 return "Internal error, duplicate user found"
223237
224238 u = u [0 ]
@@ -235,7 +249,8 @@ def importUser(request):
235249 else :
236250 raise Http404 ()
237251
238- _ajax_map = {
252+
253+ _ajax_map = {
239254 'getThreads' : getThreads ,
240255 'getMessages' : getMessages ,
241256 'attachThread' : attachThread ,
@@ -246,11 +261,12 @@ def importUser(request):
246261 'importUser' : importUser ,
247262}
248263
264+
249265# Main entrypoint for /ajax/<command>/
250266@csrf_exempt
251267@login_required
252268def main (request , command ):
253- if not _ajax_map . has_key ( command ) :
269+ if command not in _ajax_map :
254270 raise Http404
255271 try :
256272 resp = HttpResponse (content_type = 'application/json' )
0 commit comments