Mercurial > kallithea
comparison rhodecode/lib/helpers.py @ 2012:ca39c02c7c3e beta
speed up generating changesets in journal log
- using a range generator instead of get_changeset could speed up things a lot
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Tue, 21 Feb 2012 20:01:05 +0200 |
parents | 324ac367a4da |
children | 8ccac2b0fcf3 |
comparison
equal
deleted
inserted
replaced
2011:4bf79abde983 | 2012:ca39c02c7c3e |
---|---|
404 | 404 |
405 return value | 405 return value |
406 | 406 |
407 | 407 |
408 def action_parser(user_log, feed=False): | 408 def action_parser(user_log, feed=False): |
409 """This helper will action_map the specified string action into translated | 409 """ |
410 This helper will action_map the specified string action into translated | |
410 fancy names with icons and links | 411 fancy names with icons and links |
411 | 412 |
412 :param user_log: user log instance | 413 :param user_log: user log instance |
413 :param feed: use output for feeds (no html and fancy icons) | 414 :param feed: use output for feeds (no html and fancy icons) |
414 """ | 415 """ |
420 | 421 |
421 if len(x) > 1: | 422 if len(x) > 1: |
422 action, action_params = x | 423 action, action_params = x |
423 | 424 |
424 def get_cs_links(): | 425 def get_cs_links(): |
425 revs_limit = 3 #display this amount always | 426 revs_limit = 3 # display this amount always |
426 revs_top_limit = 50 #show upto this amount of changesets hidden | 427 revs_top_limit = 50 # show upto this amount of changesets hidden |
427 revs = action_params.split(',') | 428 revs_ids = action_params.split(',') |
428 repo_name = user_log.repository.repo_name | 429 repo_name = user_log.repository.repo_name |
429 | 430 |
430 from rhodecode.model.scm import ScmModel | |
431 repo = user_log.repository.scm_instance | 431 repo = user_log.repository.scm_instance |
432 | 432 |
433 message = lambda rev: get_changeset_safe(repo, rev).message | 433 message = lambda rev: rev.message |
434 lnk = lambda rev, repo_name: ( | |
435 link_to('r%s:%s' % (rev.revision, rev.short_id), | |
436 url('changeset_home', repo_name=repo_name, | |
437 revision=rev.raw_id), | |
438 title=tooltip(message(rev)), class_='tooltip') | |
439 ) | |
440 # get only max revs_top_limit of changeset for performance/ui reasons | |
441 revs = [ | |
442 x for x in repo.get_changesets(revs_ids[0], | |
443 revs_ids[:revs_top_limit][-1]) | |
444 ] | |
445 | |
434 cs_links = [] | 446 cs_links = [] |
435 cs_links.append(" " + ', '.join ([link_to(rev, | 447 cs_links.append(" " + ', '.join( |
436 url('changeset_home', | 448 [lnk(rev, repo_name) for rev in revs[:revs_limit]] |
437 repo_name=repo_name, | 449 ) |
438 revision=rev), title=tooltip(message(rev)), | 450 ) |
439 class_='tooltip') for rev in revs[:revs_limit] ])) | 451 |
440 | 452 compare_view = ( |
441 compare_view = (' <div class="compare_view tooltip" title="%s">' | 453 ' <div class="compare_view tooltip" title="%s">' |
442 '<a href="%s">%s</a> ' | 454 '<a href="%s">%s</a> </div>' % ( |
443 '</div>' % (_('Show all combined changesets %s->%s' \ | 455 _('Show all combined changesets %s->%s') % ( |
444 % (revs[0], revs[-1])), | 456 revs_ids[0], revs_ids[-1] |
445 url('changeset_home', repo_name=repo_name, | 457 ), |
446 revision='%s...%s' % (revs[0], revs[-1]) | 458 url('changeset_home', repo_name=repo_name, |
447 ), | 459 revision='%s...%s' % (revs_ids[0], revs_ids[-1]) |
448 _('compare view')) | 460 ), |
449 ) | 461 _('compare view') |
450 | 462 ) |
451 # if we have exactly one more than normally displayed: | 463 ) |
452 # just display it, takes less space than displaying "and 1 more revisions" | 464 |
453 if len(revs) == revs_limit + 1: | 465 # if we have exactly one more than normally displayed |
466 # just display it, takes less space than displaying | |
467 # "and 1 more revisions" | |
468 if len(revs_ids) == revs_limit + 1: | |
454 rev = revs[revs_limit] | 469 rev = revs[revs_limit] |
455 cs_links.append(", " + link_to(rev, | 470 cs_links.append(", " + lnk(rev, repo_name)) |
456 url('changeset_home', | |
457 repo_name=repo_name, | |
458 revision=rev), title=tooltip(message(rev)), | |
459 class_='tooltip') ) | |
460 | 471 |
461 # hidden-by-default ones | 472 # hidden-by-default ones |
462 if len(revs) > revs_limit + 1: | 473 if len(revs_ids) > revs_limit + 1: |
463 uniq_id = revs[0] | 474 uniq_id = revs_ids[0] |
464 html_tmpl = ('<span> %s ' | 475 html_tmpl = ( |
465 '<a class="show_more" id="_%s" href="#more">%s</a> ' | 476 '<span> %s <a class="show_more" id="_%s" ' |
466 '%s</span>') | 477 'href="#more">%s</a> %s</span>' |
478 ) | |
467 if not feed: | 479 if not feed: |
468 cs_links.append(html_tmpl % (_('and'), uniq_id, _('%s more') \ | 480 cs_links.append(html_tmpl % ( |
469 % (len(revs) - revs_limit), | 481 _('and'), |
470 _('revisions'))) | 482 uniq_id, _('%s more') % (len(revs_ids) - revs_limit), |
483 _('revisions') | |
484 ) | |
485 ) | |
471 | 486 |
472 if not feed: | 487 if not feed: |
473 html_tmpl = '<span id="%s" style="display:none">, %s </span>' | 488 html_tmpl = '<span id="%s" style="display:none">, %s </span>' |
474 else: | 489 else: |
475 html_tmpl = '<span id="%s"> %s </span>' | 490 html_tmpl = '<span id="%s"> %s </span>' |
476 | 491 |
477 morelinks = ', '.join([link_to(rev, | 492 morelinks = ', '.join( |
478 url('changeset_home', | 493 [lnk(rev, repo_name) for rev in revs[revs_limit:]] |
479 repo_name=repo_name, revision=rev), | 494 ) |
480 title=message(rev), class_='tooltip') | 495 |
481 for rev in revs[revs_limit:revs_top_limit]]) | 496 if len(revs_ids) > revs_top_limit: |
482 | |
483 if len(revs) > revs_top_limit: | |
484 morelinks += ', ...' | 497 morelinks += ', ...' |
485 | 498 |
486 cs_links.append(html_tmpl % (uniq_id, morelinks)) | 499 cs_links.append(html_tmpl % (uniq_id, morelinks)) |
487 if len(revs) > 1: | 500 if len(revs) > 1: |
488 cs_links.append(compare_view) | 501 cs_links.append(compare_view) |
491 def get_fork_name(): | 504 def get_fork_name(): |
492 repo_name = action_params | 505 repo_name = action_params |
493 return _('fork name ') + str(link_to(action_params, url('summary_home', | 506 return _('fork name ') + str(link_to(action_params, url('summary_home', |
494 repo_name=repo_name,))) | 507 repo_name=repo_name,))) |
495 | 508 |
496 action_map = {'user_deleted_repo':(_('[deleted] repository'), None), | 509 action_map = {'user_deleted_repo': (_('[deleted] repository'), None), |
497 'user_created_repo':(_('[created] repository'), None), | 510 'user_created_repo': (_('[created] repository'), None), |
498 'user_created_fork':(_('[created] repository as fork'), None), | 511 'user_created_fork': (_('[created] repository as fork'), None), |
499 'user_forked_repo':(_('[forked] repository'), get_fork_name), | 512 'user_forked_repo': (_('[forked] repository'), get_fork_name), |
500 'user_updated_repo':(_('[updated] repository'), None), | 513 'user_updated_repo': (_('[updated] repository'), None), |
501 'admin_deleted_repo':(_('[delete] repository'), None), | 514 'admin_deleted_repo': (_('[delete] repository'), None), |
502 'admin_created_repo':(_('[created] repository'), None), | 515 'admin_created_repo': (_('[created] repository'), None), |
503 'admin_forked_repo':(_('[forked] repository'), None), | 516 'admin_forked_repo': (_('[forked] repository'), None), |
504 'admin_updated_repo':(_('[updated] repository'), None), | 517 'admin_updated_repo': (_('[updated] repository'), None), |
505 'push':(_('[pushed] into'), get_cs_links), | 518 'push': (_('[pushed] into'), get_cs_links), |
506 'push_local':(_('[committed via RhodeCode] into'), get_cs_links), | 519 'push_local': (_('[committed via RhodeCode] into'), get_cs_links), |
507 'push_remote':(_('[pulled from remote] into'), get_cs_links), | 520 'push_remote': (_('[pulled from remote] into'), get_cs_links), |
508 'pull':(_('[pulled] from'), None), | 521 'pull': (_('[pulled] from'), None), |
509 'started_following_repo':(_('[started following] repository'), None), | 522 'started_following_repo': (_('[started following] repository'), None), |
510 'stopped_following_repo':(_('[stopped following] repository'), None), | 523 'stopped_following_repo': (_('[stopped following] repository'), None), |
511 } | 524 } |
512 | 525 |
513 action_str = action_map.get(action, action) | 526 action_str = action_map.get(action, action) |
514 if feed: | 527 if feed: |
515 action = action_str[0].replace('[', '').replace(']', '') | 528 action = action_str[0].replace('[', '').replace(']', '') |
516 else: | 529 else: |
517 action = action_str[0].replace('[', '<span class="journal_highlight">')\ | 530 action = action_str[0]\ |
518 .replace(']', '</span>') | 531 .replace('[', '<span class="journal_highlight">')\ |
519 | 532 .replace(']', '</span>') |
520 action_params_func = lambda :"" | 533 |
534 action_params_func = lambda: "" | |
521 | 535 |
522 if callable(action_str[1]): | 536 if callable(action_str[1]): |
523 action_params_func = action_str[1] | 537 action_params_func = action_str[1] |
524 | 538 |
525 return [literal(action), action_params_func] | 539 return [literal(action), action_params_func] |
566 #============================================================================== | 580 #============================================================================== |
567 | 581 |
568 def gravatar_url(email_address, size=30): | 582 def gravatar_url(email_address, size=30): |
569 if (not str2bool(config['app_conf'].get('use_gravatar')) or | 583 if (not str2bool(config['app_conf'].get('use_gravatar')) or |
570 not email_address or email_address == 'anonymous@rhodecode.org'): | 584 not email_address or email_address == 'anonymous@rhodecode.org'): |
571 f=lambda a,l:min(l,key=lambda x:abs(x-a)) | 585 f = lambda a, l: min(l, key=lambda x: abs(x - a)) |
572 return url("/images/user%s.png" % f(size, [14, 16, 20, 24, 30])) | 586 return url("/images/user%s.png" % f(size, [14, 16, 20, 24, 30])) |
573 | 587 |
574 ssl_enabled = 'https' == request.environ.get('wsgi.url_scheme') | 588 ssl_enabled = 'https' == request.environ.get('wsgi.url_scheme') |
575 default = 'identicon' | 589 default = 'identicon' |
576 baseurl_nossl = "http://www.gravatar.com/avatar/" | 590 baseurl_nossl = "http://www.gravatar.com/avatar/" |
580 if isinstance(email_address, unicode): | 594 if isinstance(email_address, unicode): |
581 #hashlib crashes on unicode items | 595 #hashlib crashes on unicode items |
582 email_address = safe_str(email_address) | 596 email_address = safe_str(email_address) |
583 # construct the url | 597 # construct the url |
584 gravatar_url = baseurl + hashlib.md5(email_address.lower()).hexdigest() + "?" | 598 gravatar_url = baseurl + hashlib.md5(email_address.lower()).hexdigest() + "?" |
585 gravatar_url += urllib.urlencode({'d':default, 's':str(size)}) | 599 gravatar_url += urllib.urlencode({'d': default, 's': str(size)}) |
586 | 600 |
587 return gravatar_url | 601 return gravatar_url |
588 | 602 |
589 | 603 |
590 #============================================================================== | 604 #============================================================================== |
609 self.collection = collection | 623 self.collection = collection |
610 | 624 |
611 # The self.page is the number of the current page. | 625 # The self.page is the number of the current page. |
612 # The first page has the number 1! | 626 # The first page has the number 1! |
613 try: | 627 try: |
614 self.page = int(page) # make it int() if we get it as a string | 628 self.page = int(page) # make it int() if we get it as a string |
615 except (ValueError, TypeError): | 629 except (ValueError, TypeError): |
616 self.page = 1 | 630 self.page = 1 |
617 | 631 |
618 self.items_per_page = items_per_page | 632 self.items_per_page = items_per_page |
619 | 633 |
759 | 773 |
760 if l_type == 'd' and not a_v: | 774 if l_type == 'd' and not a_v: |
761 return ' '.join(map(map_getter, ['tr', 'br', 'tl', 'bl'])) | 775 return ' '.join(map(map_getter, ['tr', 'br', 'tl', 'bl'])) |
762 | 776 |
763 d_a = '<div class="added %s" style="width:%s%%">%s</div>' % ( | 777 d_a = '<div class="added %s" style="width:%s%%">%s</div>' % ( |
764 cgen('a'),a_p, a_v | 778 cgen('a'), a_p, a_v |
765 ) | 779 ) |
766 d_d = '<div class="deleted %s" style="width:%s%%">%s</div>' % ( | 780 d_d = '<div class="deleted %s" style="width:%s%%">%s</div>' % ( |
767 cgen('d'),d_p, d_v | 781 cgen('d'), d_p, d_v |
768 ) | 782 ) |
769 return literal('<div style="width:%spx">%s%s</div>' % (width, d_a, d_d)) | 783 return literal('<div style="width:%spx">%s%s</div>' % (width, d_a, d_d)) |
770 | 784 |
771 | 785 |
772 def urlify_text(text_): | 786 def urlify_text(text_): |
813 import traceback | 827 import traceback |
814 | 828 |
815 # urlify changesets | 829 # urlify changesets |
816 text_ = urlify_changesets(text_, repository) | 830 text_ = urlify_changesets(text_, repository) |
817 | 831 |
818 def linkify_others(t,l): | 832 def linkify_others(t, l): |
819 urls = re.compile(r'(\<a.*?\<\/a\>)',) | 833 urls = re.compile(r'(\<a.*?\<\/a\>)',) |
820 links = [] | 834 links = [] |
821 for e in urls.split(t): | 835 for e in urls.split(t): |
822 if not urls.match(e): | 836 if not urls.match(e): |
823 links.append('<a class="message-link" href="%s">%s</a>' % (l,e)) | 837 links.append('<a class="message-link" href="%s">%s</a>' % (l, e)) |
824 else: | 838 else: |
825 links.append(e) | 839 links.append(e) |
826 | 840 |
827 return ''.join(links) | 841 return ''.join(links) |
828 try: | 842 try: |