##// END OF EJS Templates
hgweb: add a sendtemplate() helper function...
Gregory Szorc -
r36899:061635d4 default
parent child Browse files
Show More
@@ -196,6 +196,10 b' class requestcontext(object):'
196 196 resources=tres)
197 197 return tmpl
198 198
199 def sendtemplate(self, name, **kwargs):
200 """Helper function to send a response generated from a template."""
201 self.res.setbodygen(self.tmpl(name, **kwargs))
202 return self.res.sendresponse()
199 203
200 204 class hgweb(object):
201 205 """HTTP server for individual repositories.
@@ -368,8 +372,8 b' class hgweb(object):'
368 372 # process the web interface request
369 373
370 374 try:
371 tmpl = rctx.templater(req)
372 ctype = tmpl('mimetype', encoding=encoding.encoding)
375 rctx.tmpl = rctx.templater(req)
376 ctype = rctx.tmpl('mimetype', encoding=encoding.encoding)
373 377 ctype = templater.stringify(ctype)
374 378
375 379 # check read permissions non-static content
@@ -377,7 +381,7 b' class hgweb(object):'
377 381 self.check_perm(rctx, req, None)
378 382
379 383 if cmd == '':
380 req.qsparams['cmd'] = tmpl.cache['default']
384 req.qsparams['cmd'] = rctx.tmpl.cache['default']
381 385 cmd = req.qsparams['cmd']
382 386
383 387 # Don't enable caching if using a CSP nonce because then it wouldn't
@@ -400,7 +404,7 b' class hgweb(object):'
400 404 # override easily enough.
401 405 res.status = '200 Script output follows'
402 406 res.headers['Content-Type'] = ctype
403 return getattr(webcommands, cmd)(rctx, wsgireq, tmpl)
407 return getattr(webcommands, cmd)(rctx, wsgireq, rctx.tmpl)
404 408
405 409 except (error.LookupError, error.RepoLookupError) as err:
406 410 msg = pycompat.bytestr(err)
@@ -410,18 +414,15 b' class hgweb(object):'
410 414
411 415 res.status = '404 Not Found'
412 416 res.headers['Content-Type'] = ctype
413 res.setbodygen(tmpl('error', error=msg))
414 return res.sendresponse()
417 return rctx.sendtemplate('error', error=msg)
415 418 except (error.RepoError, error.RevlogError) as e:
416 419 res.status = '500 Internal Server Error'
417 420 res.headers['Content-Type'] = ctype
418 res.setbodygen(tmpl('error', error=pycompat.bytestr(e)))
419 return res.sendresponse()
421 return rctx.sendtemplate('error', error=pycompat.bytestr(e))
420 422 except ErrorResponse as e:
421 423 res.status = statusmessage(e.code, pycompat.bytestr(e))
422 424 res.headers['Content-Type'] = ctype
423 res.setbodygen(tmpl('error', error=pycompat.bytestr(e)))
424 return res.sendresponse()
425 return rctx.sendtemplate('error', error=pycompat.bytestr(e))
425 426
426 427 def check_perm(self, rctx, req, op):
427 428 for permhook in permhooks:
@@ -59,7 +59,8 b' class webcommand(object):'
59 59
60 60 The function returns a generator to be consumed by the WSGI application.
61 61 For most commands, this should be the result from
62 ``web.res.sendresponse()``.
62 ``web.res.sendresponse()``. Many commands will call ``web.sendtemplate()``
63 to render a template.
63 64
64 65 Usage:
65 66
@@ -151,7 +152,7 b' def _filerevision(web, req, tmpl, fctx):'
151 152 "linenumber": "% 6d" % (lineno + 1),
152 153 "parity": next(parity)}
153 154
154 web.res.setbodygen(tmpl(
155 return web.sendtemplate(
155 156 'filerevision',
156 157 file=f,
157 158 path=webutil.up(f),
@@ -160,9 +161,7 b' def _filerevision(web, req, tmpl, fctx):'
160 161 rename=webutil.renamelink(fctx),
161 162 permissions=fctx.manifest().flags(f),
162 163 ishead=int(ishead),
163 **pycompat.strkwargs(webutil.commonentry(web.repo, fctx))))
164
165 return web.res.sendresponse()
164 **pycompat.strkwargs(webutil.commonentry(web.repo, fctx)))
166 165
167 166 @webcommand('file')
168 167 def file(web, req, tmpl):
@@ -339,7 +338,7 b' def _search(web, tmpl):'
339 338 tip = web.repo['tip']
340 339 parity = paritygen(web.stripecount)
341 340
342 web.res.setbodygen(tmpl(
341 return web.sendtemplate(
343 342 'search',
344 343 query=query,
345 344 node=tip.hex(),
@@ -350,9 +349,7 b' def _search(web, tmpl):'
350 349 lessvars=lessvars,
351 350 modedesc=searchfunc[1],
352 351 showforcekw=showforcekw,
353 showunforcekw=showunforcekw))
354
355 return web.res.sendresponse()
352 showunforcekw=showunforcekw)
356 353
357 354 @webcommand('changelog')
358 355 def changelog(web, req, tmpl, shortlog=False):
@@ -436,7 +433,7 b' def changelog(web, req, tmpl, shortlog=F'
436 433 else:
437 434 nextentry = []
438 435
439 web.res.setbodygen(tmpl(
436 return web.sendtemplate(
440 437 'shortlog' if shortlog else 'changelog',
441 438 changenav=changenav,
442 439 node=ctx.hex(),
@@ -450,9 +447,7 b' def changelog(web, req, tmpl, shortlog=F'
450 447 revcount=revcount,
451 448 morevars=morevars,
452 449 lessvars=lessvars,
453 query=query))
454
455 return web.res.sendresponse()
450 query=query)
456 451
457 452 @webcommand('shortlog')
458 453 def shortlog(web, req, tmpl):
@@ -485,9 +480,10 b' def changeset(web, req, tmpl):'
485 480 templates related to diffs may all be used to produce the output.
486 481 """
487 482 ctx = webutil.changectx(web.repo, req)
488 web.res.setbodygen(tmpl('changeset',
489 **webutil.changesetentry(web, req, tmpl, ctx)))
490 return web.res.sendresponse()
483
484 return web.sendtemplate(
485 'changeset',
486 **webutil.changesetentry(web, req, tmpl, ctx))
491 487
492 488 rev = webcommand('rev')(changeset)
493 489
@@ -588,7 +584,7 b' def manifest(web, req, tmpl):'
588 584 "emptydirs": "/".join(emptydirs),
589 585 "basename": d}
590 586
591 web.res.setbodygen(tmpl(
587 return web.sendtemplate(
592 588 'manifest',
593 589 symrev=symrev,
594 590 path=abspath,
@@ -597,9 +593,7 b' def manifest(web, req, tmpl):'
597 593 fentries=filelist,
598 594 dentries=dirlist,
599 595 archives=web.archivelist(hex(node)),
600 **pycompat.strkwargs(webutil.commonentry(web.repo, ctx))))
601
602 return web.res.sendresponse()
596 **pycompat.strkwargs(webutil.commonentry(web.repo, ctx)))
603 597
604 598 @webcommand('tags')
605 599 def tags(web, req, tmpl):
@@ -628,14 +622,12 b' def tags(web, req, tmpl):'
628 622 "date": web.repo[n].date(),
629 623 "node": hex(n)}
630 624
631 web.res.setbodygen(tmpl(
625 return web.sendtemplate(
632 626 'tags',
633 627 node=hex(web.repo.changelog.tip()),
634 628 entries=lambda **x: entries(False, False, **x),
635 629 entriesnotip=lambda **x: entries(True, False, **x),
636 latestentry=lambda **x: entries(True, True, **x)))
637
638 return web.res.sendresponse()
630 latestentry=lambda **x: entries(True, True, **x))
639 631
640 632 @webcommand('bookmarks')
641 633 def bookmarks(web, req, tmpl):
@@ -669,14 +661,12 b' def bookmarks(web, req, tmpl):'
669 661 else:
670 662 latestrev = -1
671 663
672 web.res.setbodygen(tmpl(
664 return web.sendtemplate(
673 665 'bookmarks',
674 666 node=hex(web.repo.changelog.tip()),
675 667 lastchange=[{'date': web.repo[latestrev].date()}],
676 668 entries=lambda **x: entries(latestonly=False, **x),
677 latestentry=lambda **x: entries(latestonly=True, **x)))
678
679 return web.res.sendresponse()
669 latestentry=lambda **x: entries(latestonly=True, **x))
680 670
681 671 @webcommand('branches')
682 672 def branches(web, req, tmpl):
@@ -695,13 +685,11 b' def branches(web, req, tmpl):'
695 685 entries = webutil.branchentries(web.repo, web.stripecount)
696 686 latestentry = webutil.branchentries(web.repo, web.stripecount, 1)
697 687
698 web.res.setbodygen(tmpl(
688 return web.sendtemplate(
699 689 'branches',
700 690 node=hex(web.repo.changelog.tip()),
701 691 entries=entries,
702 latestentry=latestentry))
703
704 return web.res.sendresponse()
692 latestentry=latestentry)
705 693
706 694 @webcommand('summary')
707 695 def summary(web, req, tmpl):
@@ -772,7 +760,7 b' def summary(web, req, tmpl):'
772 760 if not desc:
773 761 desc = 'unknown'
774 762
775 web.res.setbodygen(tmpl(
763 return web.sendtemplate(
776 764 'summary',
777 765 desc=desc,
778 766 owner=get_contact(web.config) or 'unknown',
@@ -784,9 +772,7 b' def summary(web, req, tmpl):'
784 772 node=tip.hex(),
785 773 symrev='tip',
786 774 archives=web.archivelist('tip'),
787 labels=web.configlist('web', 'labels')))
788
789 return web.res.sendresponse()
775 labels=web.configlist('web', 'labels'))
790 776
791 777 @webcommand('filediff')
792 778 def filediff(web, req, tmpl):
@@ -827,15 +813,13 b' def filediff(web, req, tmpl):'
827 813 rename = []
828 814 ctx = ctx
829 815
830 web.res.setbodygen(tmpl(
816 return web.sendtemplate(
831 817 'filediff',
832 818 file=path,
833 819 symrev=webutil.symrevorshortnode(req, ctx),
834 820 rename=rename,
835 821 diff=diffs,
836 **pycompat.strkwargs(webutil.commonentry(web.repo, ctx))))
837
838 return web.res.sendresponse()
822 **pycompat.strkwargs(webutil.commonentry(web.repo, ctx)))
839 823
840 824 diff = webcommand('diff')(filediff)
841 825
@@ -902,7 +886,7 b' def comparison(web, req, tmpl):'
902 886 rename = []
903 887 ctx = ctx
904 888
905 web.res.setbodygen(tmpl(
889 return web.sendtemplate(
906 890 'filecomparison',
907 891 file=path,
908 892 symrev=webutil.symrevorshortnode(req, ctx),
@@ -912,9 +896,7 b' def comparison(web, req, tmpl):'
912 896 rightrev=rightrev,
913 897 rightnode=hex(rightnode),
914 898 comparison=comparison,
915 **pycompat.strkwargs(webutil.commonentry(web.repo, ctx))))
916
917 return web.res.sendresponse()
899 **pycompat.strkwargs(webutil.commonentry(web.repo, ctx)))
918 900
919 901 @webcommand('annotate')
920 902 def annotate(web, req, tmpl):
@@ -996,7 +978,7 b' def annotate(web, req, tmpl):'
996 978 diffopts = webutil.difffeatureopts(req, web.repo.ui, 'annotate')
997 979 diffopts = {k: getattr(diffopts, k) for k in diffopts.defaults}
998 980
999 web.res.setbodygen(tmpl(
981 return web.sendtemplate(
1000 982 'fileannotate',
1001 983 file=f,
1002 984 annotate=annotate,
@@ -1006,9 +988,7 b' def annotate(web, req, tmpl):'
1006 988 permissions=fctx.manifest().flags(f),
1007 989 ishead=int(ishead),
1008 990 diffopts=diffopts,
1009 **pycompat.strkwargs(webutil.commonentry(web.repo, fctx))))
1010
1011 return web.res.sendresponse()
991 **pycompat.strkwargs(webutil.commonentry(web.repo, fctx)))
1012 992
1013 993 @webcommand('filelog')
1014 994 def filelog(web, req, tmpl):
@@ -1133,7 +1113,7 b' def filelog(web, req, tmpl):'
1133 1113
1134 1114 latestentry = entries[:1]
1135 1115
1136 web.res.setbodygen(tmpl(
1116 return web.sendtemplate(
1137 1117 'filelog',
1138 1118 file=f,
1139 1119 nav=nav,
@@ -1146,9 +1126,7 b' def filelog(web, req, tmpl):'
1146 1126 revcount=revcount,
1147 1127 morevars=morevars,
1148 1128 lessvars=lessvars,
1149 **pycompat.strkwargs(webutil.commonentry(web.repo, fctx))))
1150
1151 return web.res.sendresponse()
1129 **pycompat.strkwargs(webutil.commonentry(web.repo, fctx)))
1152 1130
1153 1131 @webcommand('archive')
1154 1132 def archive(web, req, tmpl):
@@ -1379,7 +1357,7 b' def graph(web, req, tmpl):'
1379 1357
1380 1358 rows = len(tree)
1381 1359
1382 web.res.setbodygen(tmpl(
1360 return web.sendtemplate(
1383 1361 'graph',
1384 1362 rev=rev,
1385 1363 symrev=symrev,
@@ -1396,9 +1374,7 b' def graph(web, req, tmpl):'
1396 1374 jsdata=lambda **x: jsdata(),
1397 1375 nodes=lambda **x: nodes(),
1398 1376 node=ctx.hex(),
1399 changenav=changenav))
1400
1401 return web.res.sendresponse()
1377 changenav=changenav)
1402 1378
1403 1379 def _getdoc(e):
1404 1380 doc = e[0].__doc__
@@ -1454,13 +1430,12 b' def help(web, req, tmpl):'
1454 1430 for c, doc in other:
1455 1431 yield {'topic': c, 'summary': doc}
1456 1432
1457 web.res.setbodygen(tmpl(
1433 return web.sendtemplate(
1458 1434 'helptopics',
1459 1435 topics=topics,
1460 1436 earlycommands=earlycommands,
1461 1437 othercommands=othercommands,
1462 title='Index'))
1463 return web.res.sendresponse()
1438 title='Index')
1464 1439
1465 1440 # Render an index of sub-topics.
1466 1441 if topicname in helpmod.subtopics:
@@ -1472,12 +1447,11 b' def help(web, req, tmpl):'
1472 1447 'summary': summary,
1473 1448 })
1474 1449
1475 web.res.setbodygen(tmpl(
1450 return web.sendtemplate(
1476 1451 'helptopics',
1477 1452 topics=topics,
1478 1453 title=topicname,
1479 subindex=True))
1480 return web.res.sendresponse()
1454 subindex=True)
1481 1455
1482 1456 u = webutil.wsgiui.load()
1483 1457 u.verbose = True
@@ -1498,12 +1472,10 b' def help(web, req, tmpl):'
1498 1472 except error.Abort:
1499 1473 raise ErrorResponse(HTTP_NOT_FOUND)
1500 1474
1501 web.res.setbodygen(tmpl(
1475 return web.sendtemplate(
1502 1476 'help',
1503 1477 topic=topicname,
1504 doc=doc))
1505
1506 return web.res.sendresponse()
1478 doc=doc)
1507 1479
1508 1480 # tell hggettext to extract docstrings from these functions:
1509 1481 i18nfunctions = commands.values()
General Comments 0
You need to be logged in to leave comments. Login now