| [13497] | 1 | # |
|---|
| 2 | # Copyright (C) 2005-2006 Team5 |
|---|
| [18245] | 3 | # Copyright (C) 2016-2021 Cinc |
|---|
| 4 | # |
|---|
| [13497] | 5 | # All rights reserved. |
|---|
| 6 | # |
|---|
| 7 | # This software is licensed as described in the file COPYING.txt, which |
|---|
| 8 | # you should have received as part of this distribution. |
|---|
| 9 | # |
|---|
| [717] | 10 | # Author: Team5 |
|---|
| 11 | # |
|---|
| 12 | |
|---|
| [13497] | 13 | import datetime |
|---|
| 14 | import itertools |
|---|
| 15 | |
|---|
| [17432] | 16 | from codereview.model import PeerReviewModel |
|---|
| [717] | 17 | from trac.core import * |
|---|
| [18248] | 18 | from trac.util.datefmt import format_date, to_datetime, to_utimestamp, user_time, utc |
|---|
| [18245] | 19 | from trac.web.chrome import add_stylesheet, add_script, add_script_data, Chrome, INavigationContributor |
|---|
| [717] | 20 | from trac.web.main import IRequestHandler |
|---|
| [13497] | 21 | |
|---|
| [18250] | 22 | from .peerReviewMain import add_ctxt_nav_items |
|---|
| 23 | from .model import get_users |
|---|
| [717] | 24 | |
|---|
| [15205] | 25 | |
|---|
| [15217] | 26 | class PeerReviewSearch(Component): |
|---|
| [15170] | 27 | implements(IRequestHandler, INavigationContributor) |
|---|
| [13497] | 28 | |
|---|
| [717] | 29 | # IRequestHandler methods |
|---|
| [17432] | 30 | |
|---|
| [717] | 31 | def match_request(self, req): |
|---|
| [18261] | 32 | if req.path_info == '/peerreviewsearch': |
|---|
| 33 | return True |
|---|
| 34 | elif req.path_info == '/peerReviewSearch': |
|---|
| 35 | self.env.log.info("Legacy URL 'peerReviewSearch' called from: %s", req.get_header('Referer')) |
|---|
| 36 | return True |
|---|
| 37 | return False |
|---|
| [717] | 38 | |
|---|
| 39 | # INavigationContributor methods |
|---|
| [17432] | 40 | |
|---|
| [717] | 41 | def get_active_navigation_item(self, req): |
|---|
| [18261] | 42 | return 'peerreviewmain' |
|---|
| [13497] | 43 | |
|---|
| [717] | 44 | def get_navigation_items(self, req): |
|---|
| 45 | return [] |
|---|
| [13497] | 46 | |
|---|
| [717] | 47 | def process_request(self, req): |
|---|
| [15164] | 48 | req.perm.require('CODE_REVIEW_DEV') |
|---|
| [3854] | 49 | data = {} |
|---|
| 50 | |
|---|
| [17432] | 51 | # if the doSearch parameter is 'yes', perform the search |
|---|
| 52 | # this parameter is set when someone searches |
|---|
| 53 | if req.args.get('doSearch') == 'yes' or True: # always start with the full list for now |
|---|
| [13497] | 54 | results = self.performSearch(req, data) |
|---|
| 55 | if len(results) == 0: |
|---|
| [17432] | 56 | noValResult = ["No results match query.", "", "", "", "", ""] |
|---|
| [13497] | 57 | results.append(noValResult) |
|---|
| 58 | data['results'] = results |
|---|
| 59 | data['doSearch'] = 'yes' |
|---|
| 60 | |
|---|
| [15205] | 61 | users = get_users(self.env) |
|---|
| [17432] | 62 | # sets the possible users for the user combo-box |
|---|
| [3854] | 63 | data['users'] = users |
|---|
| [17432] | 64 | # creates a year array containing the last 10 |
|---|
| 65 | # years - for the year combo-box |
|---|
| [717] | 66 | now = datetime.datetime.now() |
|---|
| [13497] | 67 | year = now.year |
|---|
| [717] | 68 | years = [] |
|---|
| [13497] | 69 | for i in range(0, 11): |
|---|
| [717] | 70 | years.append(year - i) |
|---|
| 71 | |
|---|
| [3854] | 72 | data['years'] = years |
|---|
| 73 | data['cycle'] = itertools.cycle |
|---|
| 74 | |
|---|
| [13502] | 75 | add_stylesheet(req, 'common/css/code.css') |
|---|
| 76 | add_stylesheet(req, 'common/css/browser.css') |
|---|
| [15217] | 77 | add_stylesheet(req, 'hw/css/peerreview.css') |
|---|
| [16617] | 78 | add_script(req, 'hw/js/peerReviewSearch.js') |
|---|
| [17432] | 79 | if req.args.get('doSearch'): |
|---|
| [15218] | 80 | add_script_data(req, {'dateIndexSelected': '01', |
|---|
| 81 | 'monthSelected': data['searchValues_month'], |
|---|
| 82 | 'daySelected': data['searchValues_day'], |
|---|
| 83 | 'yearSelected': data['searchValues_year'], |
|---|
| 84 | 'statusSelected': data['searchValues_status'], |
|---|
| 85 | 'authorSelected': data['searchValues_author'], |
|---|
| 86 | 'nameSelected': data['searchValues_name']}) |
|---|
| 87 | else: |
|---|
| 88 | add_script_data(req, {'dateIndexSelected': '', |
|---|
| 89 | 'monthSelected': '', |
|---|
| 90 | 'daySelected': '', |
|---|
| 91 | 'yearSelected': '', |
|---|
| 92 | 'statusSelected': '', |
|---|
| 93 | 'authorSelected': '', |
|---|
| 94 | 'nameSelected': ''}) |
|---|
| [15172] | 95 | add_ctxt_nav_items(req) |
|---|
| [18245] | 96 | if hasattr(Chrome, 'jenv'): |
|---|
| 97 | return 'peerreview_search_jinja.html', data |
|---|
| 98 | else: |
|---|
| 99 | return 'peerreview_search.html', data, None |
|---|
| [13497] | 100 | |
|---|
| [17432] | 101 | # Performs the search |
|---|
| 102 | |
|---|
| [3854] | 103 | def performSearch(self, req, data): |
|---|
| [17432] | 104 | # get the search parameters from POST |
|---|
| 105 | author = req.args.get('Author', '') |
|---|
| 106 | name = req.args.get('CodeReviewName', '') |
|---|
| 107 | status = req.args.get('Status', '') |
|---|
| [15171] | 108 | month = req.args.get('DateMonth', '0') |
|---|
| 109 | day = req.args.get('DateDay', '0') |
|---|
| 110 | year = req.args.get('DateYear', '0') |
|---|
| [717] | 111 | |
|---|
| [17432] | 112 | # store date values for JavaScript |
|---|
| [13497] | 113 | data['searchValues_month'] = month |
|---|
| 114 | data['searchValues_day'] = day |
|---|
| 115 | data['searchValues_year'] = year |
|---|
| 116 | data['searchValues_status'] = status |
|---|
| 117 | data['searchValues_author'] = author |
|---|
| 118 | data['searchValues_name'] = name |
|---|
| [717] | 119 | |
|---|
| [17432] | 120 | # dates are utimes for reviews |
|---|
| 121 | fromdate = None |
|---|
| 122 | date = None |
|---|
| [13497] | 123 | if (month != '0') and (day != '0') and (year != '0'): |
|---|
| [17432] | 124 | # implicitely converts to suitable timezone |
|---|
| 125 | date = to_datetime(datetime.datetime(int(year), int(month), int(day))) |
|---|
| 126 | fromdate = to_utimestamp(date) |
|---|
| [717] | 127 | |
|---|
| 128 | selectString = 'Select...' |
|---|
| 129 | |
|---|
| [17432] | 130 | returnArray = [] |
|---|
| [717] | 131 | |
|---|
| [17432] | 132 | rev_model = PeerReviewModel(self.env) |
|---|
| 133 | rev_model.clear_props() |
|---|
| 134 | if status and status != selectString: |
|---|
| 135 | rev_model['status'] = status |
|---|
| 136 | if name: |
|---|
| 137 | rev_model['name'] = "%" + name + "%" |
|---|
| 138 | if author and author != selectString: |
|---|
| 139 | rev_model['owner'] = author |
|---|
| [717] | 140 | |
|---|
| [17432] | 141 | for rev in rev_model.list_matching_objects(exact_match=False): |
|---|
| 142 | tempArray = [rev['review_id'], rev['owner'], rev['status'], |
|---|
| [18248] | 143 | user_time(req, format_date, to_datetime(rev['created'])), rev['name']] |
|---|
| [17432] | 144 | if rev['closed']: |
|---|
| [18248] | 145 | tempArray.append(user_time(req, format_date, to_datetime(rev['closed']))) |
|---|
| [17266] | 146 | else: |
|---|
| 147 | tempArray.append('') |
|---|
| [17432] | 148 | if date: |
|---|
| 149 | if rev['created'] > fromdate: |
|---|
| 150 | returnArray.append(tempArray) |
|---|
| 151 | else: |
|---|
| 152 | returnArray.append(tempArray) |
|---|
| [13497] | 153 | |
|---|
| 154 | return returnArray |
|---|