allura
Revision | c4c83ee90206155f82fe559a457392c6c1e581dc (tree) |
---|---|
Zeit | 2012-07-11 13:27:52 |
Autor | Dave Brondsema <dbrondsema@geek...> |
Commiter | Dave Brondsema |
[#4272] refactor paged_* methods to remove unrelated column vars and make app & user explicit params
@@ -577,14 +577,14 @@ class Ticket(VersionedArtifact, ActivityObject): | ||
577 | 577 | custom_fields=self.custom_fields) |
578 | 578 | |
579 | 579 | @classmethod |
580 | - def paged_query(cls, query, limit=None, page=0, sort=None, columns=None, **kw): | |
580 | + def paged_query(cls, app_config, user, query, limit=None, page=0, sort=None, **kw): | |
581 | 581 | """ |
582 | 582 | Query tickets, filtering for 'read' permission, sorting and paginating the result. |
583 | 583 | |
584 | 584 | See also paged_search which does a solr search |
585 | 585 | """ |
586 | 586 | limit, page, start = g.handle_paging(limit, page, default=25) |
587 | - q = cls.query.find(dict(query, app_config_id=c.app.config._id)) | |
587 | + q = cls.query.find(dict(query, app_config_id=app_config._id)) | |
588 | 588 | q = q.sort('ticket_num') |
589 | 589 | if sort: |
590 | 590 | field, direction = sort.split() |
@@ -599,29 +599,17 @@ class Ticket(VersionedArtifact, ActivityObject): | ||
599 | 599 | tickets = [] |
600 | 600 | count = q.count() |
601 | 601 | for t in q: |
602 | - if security.has_access(t, 'read'): | |
602 | + if security.has_access(t, 'read', user, app_config.project): | |
603 | 603 | tickets.append(t) |
604 | 604 | else: |
605 | 605 | count = count -1 |
606 | - sortable_custom_fields=c.app.globals.sortable_custom_fields_shown_in_search() | |
607 | - if not columns: | |
608 | - columns = [dict(name='ticket_num', sort_name='ticket_num', label='Ticket Number', active=True), | |
609 | - dict(name='summary', sort_name='summary', label='Summary', active=True), | |
610 | - dict(name='_milestone', sort_name='custom_fields._milestone', label='Milestone', active=True), | |
611 | - dict(name='status', sort_name='status', label='Status', active=True), | |
612 | - dict(name='assigned_to', sort_name='assigned_to_username', label='Owner', active=True)] | |
613 | - for field in sortable_custom_fields: | |
614 | - columns.append( | |
615 | - dict(name=field['name'], sort_name=field['name'], label=field['label'], active=True)) | |
616 | 606 | return dict( |
617 | 607 | tickets=tickets, |
618 | - sortable_custom_fields=sortable_custom_fields, | |
619 | - columns=columns, | |
620 | 608 | count=count, q=json.dumps(query), limit=limit, page=page, sort=sort, |
621 | 609 | **kw) |
622 | 610 | |
623 | 611 | @classmethod |
624 | - def paged_search(cls, q, limit=None, page=0, sort=None, columns=None, **kw): | |
612 | + def paged_search(cls, app_config, user, q, limit=None, page=0, sort=None, **kw): | |
625 | 613 | """Query tickets from Solr, filtering for 'read' permission, sorting and paginating the result. |
626 | 614 | |
627 | 615 | See also paged_query which does a mongo search. |
@@ -664,7 +652,7 @@ class Ticket(VersionedArtifact, ActivityObject): | ||
664 | 652 | # ticket_numbers is in sorted order |
665 | 653 | ticket_numbers = [match['ticket_num_i'] for match in matches.docs] |
666 | 654 | # but query, unfortunately, returns results in arbitrary order |
667 | - query = cls.query.find(dict(app_config_id=c.app.config._id, ticket_num={'$in':ticket_numbers})) | |
655 | + query = cls.query.find(dict(app_config_id=app_config._id, ticket_num={'$in':ticket_numbers})) | |
668 | 656 | # so stick all the results in a dictionary... |
669 | 657 | ticket_for_num = {} |
670 | 658 | for t in query: |
@@ -673,22 +661,11 @@ class Ticket(VersionedArtifact, ActivityObject): | ||
673 | 661 | tickets = [] |
674 | 662 | for tn in ticket_numbers: |
675 | 663 | if tn in ticket_for_num: |
676 | - if security.has_access(ticket_for_num[tn], 'read'): | |
664 | + if security.has_access(ticket_for_num[tn], 'read', user, app_config.project): | |
677 | 665 | tickets.append(ticket_for_num[tn]) |
678 | 666 | else: |
679 | 667 | count = count -1 |
680 | - sortable_custom_fields=c.app.globals.sortable_custom_fields_shown_in_search() | |
681 | - if not columns: | |
682 | - columns = [dict(name='ticket_num', sort_name='ticket_num_i', label='Ticket Number', active=True), | |
683 | - dict(name='summary', sort_name='snippet_s', label='Summary', active=True), | |
684 | - dict(name='_milestone', sort_name='_milestone_s', label='Milestone', active=True), | |
685 | - dict(name='status', sort_name='status_s', label='Status', active=True), | |
686 | - dict(name='assigned_to', sort_name='assigned_to_s', label='Owner', active=True)] | |
687 | - for field in sortable_custom_fields: | |
688 | - columns.append(dict(name=field['name'], sort_name=field['sortable_name'], label=field['label'], active=True)) | |
689 | 668 | return dict(tickets=tickets, |
690 | - sortable_custom_fields=sortable_custom_fields, | |
691 | - columns=columns, | |
692 | 669 | count=count, q=q, limit=limit, page=page, sort=sort, |
693 | 670 | solr_error=solr_error, **kw) |
694 | 671 |
@@ -306,6 +306,33 @@ class ForgeTrackerApp(Application): | ||
306 | 306 | def bins(self): |
307 | 307 | return TM.Bin.query.find(dict(app_config_id=self.config._id)).sort('summary').all() |
308 | 308 | |
309 | + | |
310 | + | |
311 | + | |
312 | + | |
313 | +### Controllers ### | |
314 | + | |
315 | +def mongo_columns(): | |
316 | + columns = [dict(name='ticket_num', sort_name='ticket_num', label='Ticket Number', active=True), | |
317 | + dict(name='summary', sort_name='summary', label='Summary', active=True), | |
318 | + dict(name='_milestone', sort_name='custom_fields._milestone', label='Milestone', active=True), | |
319 | + dict(name='status', sort_name='status', label='Status', active=True), | |
320 | + dict(name='assigned_to', sort_name='assigned_to_username', label='Owner', active=True)] | |
321 | + for field in c.app.globals.sortable_custom_fields_shown_in_search(): | |
322 | + columns.append( | |
323 | + dict(name=field['name'], sort_name=field['name'], label=field['label'], active=True)) | |
324 | + return columns | |
325 | + | |
326 | +def solr_columns(): | |
327 | + columns = [dict(name='ticket_num', sort_name='ticket_num_i', label='Ticket Number', active=True), | |
328 | + dict(name='summary', sort_name='snippet_s', label='Summary', active=True), | |
329 | + dict(name='_milestone', sort_name='_milestone_s', label='Milestone', active=True), | |
330 | + dict(name='status', sort_name='status_s', label='Status', active=True), | |
331 | + dict(name='assigned_to', sort_name='assigned_to_s', label='Owner', active=True)] | |
332 | + for field in c.app.globals.sortable_custom_fields_shown_in_search(): | |
333 | + columns.append(dict(name=field['name'], sort_name=field['sortable_name'], label=field['label'], active=True)) | |
334 | + return columns | |
335 | + | |
309 | 336 | class RootController(BaseController): |
310 | 337 | |
311 | 338 | def __init__(self): |
@@ -336,10 +363,12 @@ class RootController(BaseController): | ||
336 | 363 | @expose('jinja:forgetracker:templates/tracker/index.html') |
337 | 364 | def index(self, limit=25, columns=None, page=0, sort='ticket_num desc', **kw): |
338 | 365 | kw.pop('q', None) # it's just our original query mangled and sent back to us |
339 | - result = TM.Ticket.paged_query(c.app.globals.not_closed_mongo_query, | |
366 | + result = TM.Ticket.paged_query(c.app.config, c.user, | |
367 | + c.app.globals.not_closed_mongo_query, | |
340 | 368 | sort=sort, limit=int(limit), |
341 | - columns=columns, page=page, **kw) | |
342 | - c.subscribe_form = W.subscribe_form | |
369 | + page=page, **kw) | |
370 | + result['columns'] = columns or mongo_columns() | |
371 | + result['sortable_custom_fields'] = c.app.globals.sortable_custom_fields_shown_in_search() | |
343 | 372 | result['subscribed'] = M.Mailbox.subscribed() |
344 | 373 | result['allow_edit'] = has_access(c.app, 'update')() |
345 | 374 | result['help_msg'] = c.app.config.options.get('TicketHelpSearch') |
@@ -349,6 +378,7 @@ class RootController(BaseController): | ||
349 | 378 | sort_split = sort.split(' ') |
350 | 379 | solr_col = _mongo_col_to_solr_col(sort_split[0]) |
351 | 380 | result['url_sort'] = '%s %s' % (solr_col, sort_split[1]) |
381 | + c.subscribe_form = W.subscribe_form | |
352 | 382 | c.ticket_search_results = W.ticket_search_results |
353 | 383 | return result |
354 | 384 |
@@ -437,7 +467,9 @@ class RootController(BaseController): | ||
437 | 467 | bin = TM.Bin.query.find(dict(app_config_id=c.app.config._id,terms=q)).first() |
438 | 468 | if project: |
439 | 469 | redirect(c.project.url() + 'search?' + urlencode(dict(q=q, history=kw.get('history')))) |
440 | - result = TM.Ticket.paged_search(q, page=page, sort=sort, columns=columns, **kw) | |
470 | + result = TM.Ticket.paged_search(c.app.config, c.user, q, page=page, sort=sort, **kw) | |
471 | + result['columns'] = columns or solr_columns() | |
472 | + result['sortable_custom_fields'] = c.app.globals.sortable_custom_fields_shown_in_search() | |
441 | 473 | result['allow_edit'] = has_access(c.app, 'update')() |
442 | 474 | result['bin'] = bin |
443 | 475 | result['help_msg'] = c.app.config.options.get('TicketHelpSearch') |
@@ -448,10 +480,10 @@ class RootController(BaseController): | ||
448 | 480 | @h.vardec |
449 | 481 | @expose() |
450 | 482 | @validate(validators=search_validators) |
451 | - def search_feed(self, q=None, query=None, project=None, columns=None, page=0, sort=None, **kw): | |
483 | + def search_feed(self, q=None, query=None, project=None, page=0, sort=None, **kw): | |
452 | 484 | if query and not q: |
453 | 485 | q = query |
454 | - result = TM.Ticket.paged_search(q, page=page, sort=sort, columns=columns, **kw) | |
486 | + result = TM.Ticket.paged_search(c.app.config, c.user, q, page=page, sort=sort, **kw) | |
455 | 487 | response.headers['Content-Type'] = '' |
456 | 488 | response.content_type = 'application/xml' |
457 | 489 | d = dict(title='Ticket search results', link=h.absurl(c.app.url), description='You searched for %s' % q, language=u'en') |
@@ -554,9 +586,11 @@ class RootController(BaseController): | ||
554 | 586 | sort=validators.UnicodeString(if_empty='ticket_num_i asc'))) |
555 | 587 | def edit(self, q=None, limit=None, page=None, sort=None, **kw): |
556 | 588 | require_access(c.app, 'update') |
557 | - result = TM.Ticket.paged_search(q, sort=sort, limit=limit, page=page, **kw) | |
589 | + result = TM.Ticket.paged_search(c.app.config, c.user, q, sort=sort, limit=limit, page=page, **kw) | |
558 | 590 | # if c.app.globals.milestone_names is None: |
559 | 591 | # c.app.globals.milestone_names = '' |
592 | + result['columns'] = solr_columns() | |
593 | + result['sortable_custom_fields'] = c.app.globals.sortable_custom_fields_shown_in_search() | |
560 | 594 | result['globals'] = c.app.globals |
561 | 595 | result['cancel_href'] = url( |
562 | 596 | c.app.url + 'search/', |
@@ -1319,11 +1353,9 @@ class RootRestController(BaseController): | ||
1319 | 1353 | |
1320 | 1354 | @expose('json:') |
1321 | 1355 | def search(self, q=None, limit=100, page=0, sort=None, **kw): |
1322 | - results = TM.Ticket.paged_search(q, limit, page, sort) | |
1356 | + results = TM.Ticket.paged_search(c.app.config, c.user, q, limit, page, sort) | |
1323 | 1357 | results['tickets'] = [dict(ticket_num=t.ticket_num, summary=t.summary) |
1324 | 1358 | for t in results['tickets']] |
1325 | - results.pop('sortable_custom_fields', None) | |
1326 | - results.pop('columns', None) | |
1327 | 1359 | return results |
1328 | 1360 | |
1329 | 1361 | @expose() |
@@ -1386,8 +1418,10 @@ class MilestoneController(BaseController): | ||
1386 | 1418 | sort=validators.UnicodeString(if_empty=None))) |
1387 | 1419 | def index(self, q=None, columns=None, page=0, query=None, sort=None, **kw): |
1388 | 1420 | require(has_access(c.app, 'read')) |
1389 | - result = TM.Ticket.paged_query( | |
1390 | - self.mongo_query, page=page, sort=sort, columns=columns, **kw) | |
1421 | + result = TM.Ticket.paged_query(c.app.config, c.user, | |
1422 | + self.mongo_query, page=page, sort=sort, **kw) | |
1423 | + result['columns'] = columns or mongo_columns() | |
1424 | + result['sortable_custom_fields'] = c.app.globals.sortable_custom_fields_shown_in_search() | |
1391 | 1425 | result['allow_edit'] = has_access(c.app, 'update')() |
1392 | 1426 | result['help_msg'] = c.app.config.options.get('TicketHelpSearch') |
1393 | 1427 | progress = c.app.globals.milestone_count(self.progress_key) |
@@ -1415,8 +1449,10 @@ class MilestoneController(BaseController): | ||
1415 | 1449 | sort=validators.UnicodeString(if_empty='ticket_num_i asc'))) |
1416 | 1450 | def edit(self, q=None, limit=None, page=None, sort=None, columns=None, **kw): |
1417 | 1451 | require_access(c.app, 'update') |
1418 | - result = TM.Ticket.paged_query( | |
1419 | - self.mongo_query, page=page, sort=sort, columns=columns, **kw) | |
1452 | + result = TM.Ticket.paged_query(c.app.config, c.user, | |
1453 | + self.mongo_query, page=page, sort=sort, **kw) | |
1454 | + result['columns'] = columns or mongo_columns() | |
1455 | + result['sortable_custom_fields'] = c.app.globals.sortable_custom_fields_shown_in_search() | |
1420 | 1456 | # if c.app.globals.milestone_names is None: |
1421 | 1457 | # c.app.globals.milestone_names = '' |
1422 | 1458 | result.pop('q') |