| Conditions | 36 |
| Total Lines | 241 |
| Code Lines | 176 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like build.bika.lims.utils.analysis.get_method_instrument_constraints() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | # -*- coding: utf-8 -*- |
||
| 376 | def get_method_instrument_constraints(context, uids): |
||
| 377 | """ |
||
| 378 | Returns a dictionary with the constraints and rules for |
||
| 379 | methods, instruments and results to be applied to each of the |
||
| 380 | analyses specified in the param uids (an array of uids). |
||
| 381 | See docs/imm_results_entry_behaviour.png for further details |
||
| 382 | """ |
||
| 383 | constraints = {} |
||
| 384 | uc = getToolByName(context, 'uid_catalog') |
||
| 385 | analyses = uc(portal_type=['Analysis', 'ReferenceAnalysis'], |
||
| 386 | UID=uids) |
||
| 387 | cached_servs = {} |
||
| 388 | for analysis in analyses: |
||
| 389 | if not analysis: |
||
| 390 | continue |
||
| 391 | analysis = analysis.getObject() |
||
| 392 | auid = analysis.UID() |
||
| 393 | suid = analysis.getServiceUID() |
||
| 394 | refan = analysis.portal_type == 'ReferenceAnalysis' |
||
| 395 | cachedkey = "qc" if refan else "re" |
||
| 396 | if suid in cached_servs.get(cachedkey, []): |
||
| 397 | constraints[auid] = cached_servs[cachedkey][suid] |
||
| 398 | continue |
||
| 399 | |||
| 400 | if not cached_servs.get(cachedkey, None): |
||
| 401 | cached_servs[cachedkey] = {suid: {}} |
||
| 402 | else: |
||
| 403 | cached_servs[cachedkey][suid] = {} |
||
| 404 | constraints[auid] = {} |
||
| 405 | |||
| 406 | allowed_instruments = analysis.getAllowedInstruments() |
||
| 407 | |||
| 408 | # Analysis allows manual/instrument entry? |
||
| 409 | s_mentry = analysis.getManualEntryOfResults() |
||
| 410 | s_ientry = analysis.getInstrumentEntryOfResults() |
||
| 411 | s_instrums = allowed_instruments if s_ientry else [] |
||
| 412 | s_instrums = [instr.UID() for instr in s_instrums] |
||
| 413 | a_dinstrum = analysis.getInstrument() if s_ientry else None |
||
| 414 | s_methods = analysis.getAllowedMethods() |
||
| 415 | s_dmethod = analysis.getMethod() |
||
| 416 | dmuid = s_dmethod.UID() if s_dmethod else '' |
||
| 417 | diuid = a_dinstrum.UID() if a_dinstrum else '' |
||
| 418 | |||
| 419 | # To take into account ASs with no method assigned by default or |
||
| 420 | # ASs that have an instrument assigned by default that doesn't have |
||
| 421 | # a method associated. |
||
| 422 | if s_mentry or not s_dmethod: |
||
| 423 | s_methods += [None] |
||
| 424 | |||
| 425 | for method in s_methods: |
||
| 426 | # Method manual entry? |
||
| 427 | m_mentry = method.isManualEntryOfResults() if method else True |
||
| 428 | |||
| 429 | instrs = [] |
||
| 430 | if method: |
||
| 431 | # Instruments available for this method and analysis? |
||
| 432 | instrs = [i for i in method.getInstruments() |
||
| 433 | if i.UID() in s_instrums] |
||
| 434 | else: |
||
| 435 | # What about instruments without a method assigned? |
||
| 436 | instrs = [i for i in allowed_instruments |
||
| 437 | if i.UID() in s_instrums and not i.getMethods()] |
||
| 438 | |||
| 439 | instuids = [i.UID() for i in instrs] |
||
| 440 | v_instrobjs = [i for i in instrs if i.isValid()] |
||
| 441 | v_instrs = [i.UID() for i in v_instrobjs] |
||
| 442 | muid = method.UID() if method else '' |
||
| 443 | |||
| 444 | # PREMISES |
||
| 445 | # p1: Analysis allows manual entry? |
||
| 446 | # p2: Analysis allows instrument entry? |
||
| 447 | # p3: Method selected and non empty? |
||
| 448 | # p4: Method allows manual entry? |
||
| 449 | # p5: At least one instrument available for this method? |
||
| 450 | # p6: Valid instruments available? |
||
| 451 | # p7: All instruments valid? |
||
| 452 | # p8: Methods allow the service's default instrument? |
||
| 453 | # p9: Default instrument valid? |
||
| 454 | premises = [ |
||
| 455 | "R" if not refan else 'Q', |
||
| 456 | "Y" if s_mentry else "N", |
||
| 457 | "Y" if s_ientry else "N", |
||
| 458 | "Y" if method else "N", |
||
| 459 | "Y" if m_mentry else "N", |
||
| 460 | "Y" if instrs else "N", |
||
| 461 | "Y" if v_instrs or not instrs else "N", |
||
| 462 | "Y" if len(v_instrs) == len(instrs) else "N", |
||
| 463 | "Y" if diuid in instuids else "N", |
||
| 464 | "Y" if a_dinstrum and a_dinstrum.isValid() else "N", |
||
| 465 | ] |
||
| 466 | tprem = ''.join(premises) |
||
| 467 | |||
| 468 | fiuid = v_instrs[0] if v_instrs else '' |
||
| 469 | instrtitle = to_unicode(a_dinstrum.Title()) if a_dinstrum else '' |
||
| 470 | iinstrs = ', '.join([to_unicode(i.Title()) for i in instrs |
||
| 471 | if i.UID() not in v_instrs]) |
||
| 472 | dmeth = to_unicode(method.Title()) if method else '' |
||
| 473 | m1 = _("Invalid instruments are not displayed: %s") % iinstrs |
||
| 474 | m2 = _("Default instrument %s is not valid") % instrtitle |
||
| 475 | m3 = _("No valid instruments available: %s ") % iinstrs |
||
| 476 | m4 = _("Manual entry of results for method %s is not allowed " |
||
| 477 | "and no valid instruments found: %s") % (dmeth, iinstrs) |
||
| 478 | m5 = _("The method %s is not valid: no manual entry allowed " |
||
| 479 | "and no instrument assigned") % dmeth |
||
| 480 | m6 = _("The method %s is not valid: only instrument entry for " |
||
| 481 | "this analysis is allowed, but the method has no " |
||
| 482 | "instrument assigned") % dmeth |
||
| 483 | m7 = _("Only instrument entry for this analysis is allowed, " |
||
| 484 | "but there is no instrument assigned") |
||
| 485 | |||
| 486 | """ |
||
| 487 | Matrix dict keys char positions: (True: Y, False: N) |
||
| 488 | 0: (R)egular analysis or (Q)C analysis |
||
| 489 | 1: Analysis allows manual entry? |
||
| 490 | 2: Analysis allows instrument entry? |
||
| 491 | 3: Method is not None? |
||
| 492 | 4: Method allows manual entry? |
||
| 493 | 5: At least one instrument avialable for the method? |
||
| 494 | 6: Valid instruments available? |
||
| 495 | 7: All instruments valid? |
||
| 496 | 8: Method allows the service's default instrument? |
||
| 497 | 9: Default instrument valid? |
||
| 498 | |||
| 499 | Matrix dict values array indexes: |
||
| 500 | 0: Method list visible? YES:1, NO:0, YES(a):2, YES(r):3 |
||
| 501 | 1: Add "None" in methods list? YES:1, NO:0, NO(g):2 |
||
| 502 | 2: Instr. list visible? YES:1, NO:0 |
||
| 503 | 3: Add "None" in instrums list? YES: 1, NO:0 |
||
| 504 | 4: UID of the selected instrument or '' if None |
||
| 505 | 5: Results field editable? YES: 1, NO:0 |
||
| 506 | 6: Alert message string |
||
| 507 | |||
| 508 | See docs/imm_results_entry_behaviour.png for further details |
||
| 509 | """ |
||
| 510 | matrix = { |
||
| 511 | # Regular analyses |
||
| 512 | 'RYYYYYYYY': [1, 1, 1, 1, diuid, 1, ''], # B1 |
||
| 513 | 'RYYYYYYYN': [1, 1, 1, 1, '', 1, ''], # B2 |
||
| 514 | 'RYYYYYYNYY': [1, 1, 1, 1, diuid, 1, m1], # B3 |
||
| 515 | 'RYYYYYYNYN': [1, 1, 1, 1, '', 1, m2], # B4 |
||
| 516 | 'RYYYYYYNN': [1, 1, 1, 1, '', 1, m1], # B5 |
||
| 517 | 'RYYYYYN': [1, 1, 1, 1, '', 1, m3], # B6 |
||
| 518 | 'RYYYYN': [1, 1, 1, 1, '', 1, ''], # B7 |
||
| 519 | 'RYYYNYYYY': [1, 1, 1, 0, diuid, 1, ''], # B8 |
||
| 520 | 'RYYYNYYYN': [1, 1, 1, 0, fiuid, 1, ''], # B9 |
||
| 521 | 'RYYYNYYNYY': [1, 1, 1, 0, diuid, 1, m1], # B10 |
||
| 522 | 'RYYYNYYNYN': [1, 1, 1, 1, '', 0, m2], # B11 |
||
| 523 | 'RYYYNYYNN': [1, 1, 1, 0, fiuid, 1, m1], # B12 |
||
| 524 | 'RYYYNYN': [1, 1, 1, 1, '', 0, m4], # B13 |
||
| 525 | 'RYYYNN': [1, 1, 1, 1, '', 0, m5], # B14 |
||
| 526 | 'RYYNYYYYY': [1, 1, 1, 1, diuid, 1, ''], # B15 |
||
| 527 | 'RYYNYYYYN': [1, 1, 1, 1, '', 1, ''], # B16 |
||
| 528 | 'RYYNYYYNYY': [1, 1, 1, 1, diuid, 1, m1], # B17 |
||
| 529 | 'RYYNYYYNYN': [1, 1, 1, 1, '', 1, m2], # B18 |
||
| 530 | 'RYYNYYYNN': [1, 1, 1, 1, '', 1, m1], # B19 |
||
| 531 | 'RYYNYYN': [1, 1, 1, 1, '', 1, m3], # B20 |
||
| 532 | 'RYYNYN': [1, 1, 1, 1, '', 1, ''], # B21 |
||
| 533 | 'RYNY': [2, 0, 0, 0, '', 1, ''], # B22 |
||
| 534 | 'RYNN': [0, 0, 0, 0, '', 1, ''], # B23 |
||
| 535 | 'RNYYYYYYY': [3, 2, 1, 1, diuid, 1, ''], # B24 |
||
| 536 | 'RNYYYYYYN': [3, 2, 1, 1, '', 1, ''], # B25 |
||
| 537 | 'RNYYYYYNYY': [3, 2, 1, 1, diuid, 1, m1], # B26 |
||
| 538 | 'RNYYYYYNYN': [3, 2, 1, 1, '', 1, m2], # B27 |
||
| 539 | 'RNYYYYYNN': [3, 2, 1, 1, '', 1, m1], # B28 |
||
| 540 | 'RNYYYYN': [3, 2, 1, 1, '', 1, m3], # B29 |
||
| 541 | 'RNYYYN': [3, 2, 1, 1, '', 0, m6], # B30 |
||
| 542 | 'RNYYNYYYY': [3, 2, 1, 0, diuid, 1, ''], # B31 |
||
| 543 | 'RNYYNYYYN': [3, 2, 1, 0, fiuid, 1, ''], # B32 |
||
| 544 | 'RNYYNYYNYY': [3, 2, 1, 0, diuid, 1, m1], # B33 |
||
| 545 | 'RNYYNYYNYN': [3, 2, 1, 1, '', 0, m2], # B34 |
||
| 546 | 'RNYYNYYNN': [3, 2, 1, 0, fiuid, 1, m1], # B35 |
||
| 547 | 'RNYYNYN': [3, 2, 1, 1, '', 0, m3], # B36 |
||
| 548 | 'RNYYNN': [3, 2, 1, 1, '', 0, m6], # B37 |
||
| 549 | 'RNYNYYYYY': [3, 1, 1, 0, diuid, 1, ''], # B38 |
||
| 550 | 'RNYNYYYYN': [3, 1, 1, 0, fiuid, 1, ''], # B39 |
||
| 551 | 'RNYNYYYNYY': [3, 1, 1, 0, diuid, 1, m1], # B40 |
||
| 552 | 'RNYNYYYNYN': [3, 1, 1, 1, '', 0, m2], # B41 |
||
| 553 | 'RNYNYYYNN': [3, 1, 1, 0, fiuid, 1, m1], # B42 |
||
| 554 | 'RNYNYYN': [3, 1, 1, 0, '', 0, m3], # B43 |
||
| 555 | 'RNYNYN': [3, 1, 1, 0, '', 0, m7], # B44 |
||
| 556 | # QC Analyses |
||
| 557 | 'QYYYYYYYY': [1, 1, 1, 1, diuid, 1, ''], # C1 |
||
| 558 | 'QYYYYYYYN': [1, 1, 1, 1, '', 1, ''], # C2 |
||
| 559 | 'QYYYYYYNYY': [1, 1, 1, 1, diuid, 1, ''], # C3 |
||
| 560 | 'QYYYYYYNYN': [1, 1, 1, 1, diuid, 1, ''], # C4 |
||
| 561 | 'QYYYYYYNN': [1, 1, 1, 1, '', 1, ''], # C5 |
||
| 562 | 'QYYYYYN': [1, 1, 1, 1, '', 1, ''], # C6 |
||
| 563 | 'QYYYYN': [1, 1, 1, 1, '', 1, ''], # C7 |
||
| 564 | 'QYYYNYYYY': [1, 1, 1, 0, diuid, 1, ''], # C8 |
||
| 565 | 'QYYYNYYYN': [1, 1, 1, 0, fiuid, 1, ''], # C9 |
||
| 566 | 'QYYYNYYNYY': [1, 1, 1, 0, diuid, 1, ''], # C10 |
||
| 567 | 'QYYYNYYNYN': [1, 1, 1, 0, diuid, 1, ''], # C11 |
||
| 568 | 'QYYYNYYNN': [1, 1, 1, 0, fiuid, 1, ''], # C12 |
||
| 569 | 'QYYYNYN': [1, 1, 1, 0, fiuid, 1, ''], # C13 |
||
| 570 | 'QYYYNN': [1, 1, 1, 1, '', 0, m5], # C14 |
||
| 571 | 'QYYNYYYYY': [1, 1, 1, 1, diuid, 1, ''], # C15 |
||
| 572 | 'QYYNYYYYN': [1, 1, 1, 1, '', 1, ''], # C16 |
||
| 573 | 'QYYNYYYNYY': [1, 1, 1, 1, diuid, 1, ''], # C17 |
||
| 574 | 'QYYNYYYNYN': [1, 1, 1, 1, diuid, 1, ''], # C18 |
||
| 575 | 'QYYNYYYNN': [1, 1, 1, 1, fiuid, 1, ''], # C19 |
||
| 576 | 'QYYNYYN': [1, 1, 1, 1, diuid, 1, ''], # C20 |
||
| 577 | 'QYYNYN': [1, 1, 1, 1, '', 1, ''], # C21 |
||
| 578 | 'QYNY': [2, 0, 0, 0, '', 1, ''], # C22 |
||
| 579 | 'QYNN': [0, 0, 0, 0, '', 1, ''], # C23 |
||
| 580 | 'QNYYYYYYY': [3, 2, 1, 1, diuid, 1, ''], # C24 |
||
| 581 | 'QNYYYYYYN': [3, 2, 1, 1, '', 1, ''], # C25 |
||
| 582 | 'QNYYYYYNYY': [3, 2, 1, 1, diuid, 1, ''], # C26 |
||
| 583 | 'QNYYYYYNYN': [3, 2, 1, 1, diuid, 1, ''], # C27 |
||
| 584 | 'QNYYYYYNN': [3, 2, 1, 1, '', 1, ''], # C28 |
||
| 585 | 'QNYYYYN': [3, 2, 1, 1, '', 1, ''], # C29 |
||
| 586 | 'QNYYYN': [3, 2, 1, 1, '', 0, m6], # C30 |
||
| 587 | 'QNYYNYYYY': [3, 2, 1, 0, diuid, 1, ''], # C31 |
||
| 588 | 'QNYYNYYYN': [3, 2, 1, 0, fiuid, 1, ''], # C32 |
||
| 589 | 'QNYYNYYNYY': [3, 2, 1, 0, diuid, 1, ''], # C33 |
||
| 590 | 'QNYYNYYNYN': [3, 2, 1, 0, diuid, 1, ''], # C34 |
||
| 591 | 'QNYYNYYNN': [3, 2, 1, 0, fiuid, 1, ''], # C35 |
||
| 592 | 'QNYYNYN': [3, 2, 1, 0, fiuid, 1, ''], # C36 |
||
| 593 | 'QNYYNN': [3, 2, 1, 1, '', 0, m5], # C37 |
||
| 594 | 'QNYNYYYYY': [3, 1, 1, 0, diuid, 1, ''], # C38 |
||
| 595 | 'QNYNYYYYN': [3, 1, 1, 0, fiuid, 1, ''], # C39 |
||
| 596 | 'QNYNYYYNYY': [3, 1, 1, 0, diuid, 1, ''], # C40 |
||
| 597 | 'QNYNYYYNYN': [3, 1, 1, 0, diuid, 1, ''], # C41 |
||
| 598 | 'QNYNYYYNN': [3, 1, 1, 0, fiuid, 1, ''], # C42 |
||
| 599 | 'QNYNYYN': [3, 1, 1, 0, fiuid, 1, ''], # C43 |
||
| 600 | 'QNYNYN': [3, 1, 1, 1, '', 0, m7], # C44 |
||
| 601 | } |
||
| 602 | targ = [v for k, v in matrix.items() if tprem.startswith(k)] |
||
| 603 | if not targ: |
||
| 604 | targ = [[1, 1, 1, 1, '', 0, 'Key not found: %s' % tprem], ] |
||
| 605 | targ = targ[0] |
||
| 606 | atitle = analysis.Title() if analysis else "None" |
||
| 607 | mtitle = method.Title() if method else "None" |
||
| 608 | instdi = {} |
||
| 609 | if refan and instrs: |
||
| 610 | instdi = {i.UID(): i.Title() for i in instrs} |
||
| 611 | elif not refan and v_instrobjs: |
||
| 612 | instdi = {i.UID(): i.Title() for i in v_instrobjs} |
||
| 613 | targ += [instdi, mtitle, atitle, tprem] |
||
| 614 | constraints[auid][muid] = targ |
||
| 615 | cached_servs[cachedkey][suid][muid] = targ |
||
| 616 | return constraints |
||
| 617 |