| Conditions | 8 |
| Total Lines | 311 |
| Code Lines | 273 |
| 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:
| 1 | # -*- coding: utf-8 -*- |
||
| 141 | ("bika_catalog", "allowedRolesAndUsers", "", "KeywordIndex"), |
||
| 142 | ("bika_catalog", "created", "", "DateIndex"), |
||
| 143 | ("bika_catalog", "getBlank", "", "BooleanIndex"), |
||
| 144 | ("bika_catalog", "getClientBatchID", "", "FieldIndex"), |
||
| 145 | ("bika_catalog", "getClientID", "", "FieldIndex"), |
||
| 146 | ("bika_catalog", "getClientTitle", "", "FieldIndex"), |
||
| 147 | ("bika_catalog", "getClientUID", "", "FieldIndex"), |
||
| 148 | ("bika_catalog", "getDateReceived", "", "DateIndex"), |
||
| 149 | ("bika_catalog", "getDateSampled", "", "DateIndex"), |
||
| 150 | ("bika_catalog", "getDueDate", "", "DateIndex"), |
||
| 151 | ("bika_catalog", "getExpiryDate", "", "DateIndex"), |
||
| 152 | ("bika_catalog", "getId", "", "FieldIndex"), |
||
| 153 | ("bika_catalog", "getReferenceDefinitionUID", "", "FieldIndex"), |
||
| 154 | ("bika_catalog", "getSampleTypeTitle", "", "FieldIndex"), |
||
| 155 | ("bika_catalog", "getSampleTypeUID", "", "FieldIndex"), |
||
| 156 | ("bika_catalog", "getSupportedServices", "", "KeywordIndex"), |
||
| 157 | ("bika_catalog", "id", "getId", "FieldIndex"), |
||
| 158 | ("bika_catalog", "isValid", "", "BooleanIndex"), |
||
| 159 | ("bika_catalog", "is_active", "", "BooleanIndex"), |
||
| 160 | ("bika_catalog", "path", "getPhysicalPath", "ExtendedPathIndex"), |
||
| 161 | ("bika_catalog", "portal_type", "", "FieldIndex"), |
||
| 162 | ("bika_catalog", "review_state", "", "FieldIndex"), |
||
| 163 | ("bika_catalog", "sortable_title", "", "FieldIndex"), |
||
| 164 | ("bika_catalog", "title", "", "FieldIndex"), |
||
| 165 | |||
| 166 | ("bika_setup_catalog", "Creator", "", "FieldIndex"), |
||
| 167 | ("bika_setup_catalog", "Description", "", "ZCTextIndex"), |
||
| 168 | ("bika_setup_catalog", "Identifiers", "", "KeywordIndex"), |
||
| 169 | ("bika_setup_catalog", "Title", "", "ZCTextIndex"), |
||
| 170 | ("bika_setup_catalog", "Type", "", "FieldIndex"), |
||
| 171 | ("bika_setup_catalog", "UID", "", "FieldIndex"), |
||
| 172 | ("bika_setup_catalog", "allowedRolesAndUsers", "", "KeywordIndex"), |
||
| 173 | ("bika_setup_catalog", "created", "", "DateIndex"), |
||
| 174 | ("bika_setup_catalog", "getAccredited", "", "FieldIndex"), |
||
| 175 | ("bika_setup_catalog", "getAnalyst", "", "FieldIndex"), |
||
| 176 | ("bika_setup_catalog", "getAvailableMethodUIDs", "", "KeywordIndex"), |
||
| 177 | ("bika_setup_catalog", "getBlank", "", "FieldIndex"), |
||
| 178 | ("bika_setup_catalog", "getCalculationTitle", "", "FieldIndex"), |
||
| 179 | ("bika_setup_catalog", "getCalculationUID", "", "FieldIndex"), |
||
| 180 | ("bika_setup_catalog", "getCalibrationExpiryDate", "", "FieldIndex"), |
||
| 181 | ("bika_setup_catalog", "getCategoryTitle", "", "FieldIndex"), |
||
| 182 | ("bika_setup_catalog", "getCategoryUID", "", "FieldIndex"), |
||
| 183 | ("bika_setup_catalog", "getClientUID", "", "FieldIndex"), |
||
| 184 | ("bika_setup_catalog", "getDepartmentTitle", "", "FieldIndex"), |
||
| 185 | ("bika_setup_catalog", "getDocumentID", "", "FieldIndex"), |
||
| 186 | ("bika_setup_catalog", "getDuplicateVariation", "", "FieldIndex"), |
||
| 187 | ("bika_setup_catalog", "getFormula", "", "FieldIndex"), |
||
| 188 | ("bika_setup_catalog", "getFullname", "", "FieldIndex"), |
||
| 189 | ("bika_setup_catalog", "getHazardous", "", "FieldIndex"), |
||
| 190 | ("bika_setup_catalog", "getId", "", "FieldIndex"), |
||
| 191 | ("bika_setup_catalog", "getInstrumentLocationName", "", "FieldIndex"), |
||
| 192 | ("bika_setup_catalog", "getInstrumentTitle", "", "FieldIndex"), |
||
| 193 | ("bika_setup_catalog", "getInstrumentType", "", "FieldIndex"), |
||
| 194 | ("bika_setup_catalog", "getInstrumentTypeName", "", "FieldIndex"), |
||
| 195 | ("bika_setup_catalog", "getKeyword", "", "FieldIndex"), |
||
| 196 | ("bika_setup_catalog", "getManagerEmail", "", "FieldIndex"), |
||
| 197 | ("bika_setup_catalog", "getManagerName", "", "FieldIndex"), |
||
| 198 | ("bika_setup_catalog", "getManagerPhone", "", "FieldIndex"), |
||
| 199 | ("bika_setup_catalog", "getMaxTimeAllowed", "", "FieldIndex"), |
||
| 200 | ("bika_setup_catalog", "getMethodID", "", "FieldIndex"), |
||
| 201 | ("bika_setup_catalog", "getModel", "", "FieldIndex"), |
||
| 202 | ("bika_setup_catalog", "getName", "", "FieldIndex"), |
||
| 203 | ("bika_setup_catalog", "getPointOfCapture", "", "FieldIndex"), |
||
| 204 | ("bika_setup_catalog", "getPrice", "", "FieldIndex"), |
||
| 205 | ("bika_setup_catalog", "getSamplePointTitle", "", "KeywordIndex"), |
||
| 206 | ("bika_setup_catalog", "getSamplePointUID", "", "FieldIndex"), |
||
| 207 | ("bika_setup_catalog", "getSampleTypeTitle", "", "FieldIndex"), |
||
| 208 | ("bika_setup_catalog", "getSampleTypeTitles", "", "KeywordIndex"), |
||
| 209 | ("bika_setup_catalog", "getSampleTypeUID", "", "FieldIndex"), |
||
| 210 | ("bika_setup_catalog", "getServiceUID", "", "FieldIndex"), |
||
| 211 | ("bika_setup_catalog", "getServiceUIDs", "", "KeywordIndex"), |
||
| 212 | ("bika_setup_catalog", "getTotalPrice", "", "FieldIndex"), |
||
| 213 | ("bika_setup_catalog", "getUnit", "", "FieldIndex"), |
||
| 214 | ("bika_setup_catalog", "getVATAmount", "getVATAmount", "FieldIndex"), |
||
| 215 | ("bika_setup_catalog", "getVolume", "", "FieldIndex"), |
||
| 216 | ("bika_setup_catalog", "id", "getId", "FieldIndex"), |
||
| 217 | ("bika_setup_catalog", "is_active", "", "BooleanIndex"), |
||
| 218 | ("bika_setup_catalog", "path", "getPhysicalPath", "ExtendedPathIndex"), |
||
| 219 | ("bika_setup_catalog", "portal_type", "", "FieldIndex"), |
||
| 220 | ("bika_setup_catalog", "review_state", "", "FieldIndex"), |
||
| 221 | ("bika_setup_catalog", "sortable_title", "", "FieldIndex"), |
||
| 222 | ("bika_setup_catalog", "title", "", "FieldIndex"), |
||
| 223 | |||
| 224 | ("portal_catalog", "Analyst", "", "FieldIndex"), |
||
| 225 | ) |
||
| 226 | |||
| 227 | COLUMNS = ( |
||
| 228 | # catalog, column name |
||
| 229 | ("bika_catalog", "path"), |
||
| 230 | ("bika_catalog", "UID"), |
||
| 231 | ("bika_catalog", "id"), |
||
| 232 | ("bika_catalog", "getId"), |
||
| 233 | ("bika_catalog", "Type"), |
||
| 234 | ("bika_catalog", "portal_type"), |
||
| 235 | ("bika_catalog", "creator"), |
||
| 236 | ("bika_catalog", "Created"), |
||
| 237 | ("bika_catalog", "Title"), |
||
| 238 | ("bika_catalog", "Description"), |
||
| 239 | ("bika_catalog", "sortable_title"), |
||
| 240 | ("bika_catalog", "getClientTitle"), |
||
| 241 | ("bika_catalog", "getClientID"), |
||
| 242 | ("bika_catalog", "getClientBatchID"), |
||
| 243 | ("bika_catalog", "getSampleTypeTitle"), |
||
| 244 | ("bika_catalog", "getDateReceived"), |
||
| 245 | ("bika_catalog", "getDateSampled"), |
||
| 246 | ("bika_catalog", "review_state"), |
||
| 247 | |||
| 248 | ("bika_setup_catalog", "path"), |
||
| 249 | ("bika_setup_catalog", "UID"), |
||
| 250 | ("bika_setup_catalog", "id"), |
||
| 251 | ("bika_setup_catalog", "getId"), |
||
| 252 | ("bika_setup_catalog", "Type"), |
||
| 253 | ("bika_setup_catalog", "portal_type"), |
||
| 254 | ("bika_setup_catalog", "Title"), |
||
| 255 | ("bika_setup_catalog", "Description"), |
||
| 256 | ("bika_setup_catalog", "title"), |
||
| 257 | ("bika_setup_catalog", "sortable_title"), |
||
| 258 | ("bika_setup_catalog", "description"), |
||
| 259 | ("bika_setup_catalog", "review_state"), |
||
| 260 | ("bika_setup_catalog", "getAccredited"), |
||
| 261 | ("bika_setup_catalog", "getInstrumentType"), |
||
| 262 | ("bika_setup_catalog", "getInstrumentTypeName"), |
||
| 263 | ("bika_setup_catalog", "getInstrumentLocationName"), |
||
| 264 | ("bika_setup_catalog", "getBlank"), |
||
| 265 | ("bika_setup_catalog", "getCalculationTitle"), |
||
| 266 | ("bika_setup_catalog", "getCalculationUID"), |
||
| 267 | ("bika_setup_catalog", "getCalibrationExpiryDate"), |
||
| 268 | ("bika_setup_catalog", "getCategoryTitle"), |
||
| 269 | ("bika_setup_catalog", "getCategoryUID"), |
||
| 270 | ("bika_setup_catalog", "getClientUID"), |
||
| 271 | ("bika_setup_catalog", "getDepartmentTitle"), |
||
| 272 | ("bika_setup_catalog", "getDuplicateVariation"), |
||
| 273 | ("bika_setup_catalog", "getFormula"), |
||
| 274 | ("bika_setup_catalog", "getFullname"), |
||
| 275 | ("bika_setup_catalog", "getHazardous"), |
||
| 276 | ("bika_setup_catalog", "getInstrumentTitle"), |
||
| 277 | ("bika_setup_catalog", "getKeyword"), |
||
| 278 | ("bika_setup_catalog", "getManagerName"), |
||
| 279 | ("bika_setup_catalog", "getManagerPhone"), |
||
| 280 | ("bika_setup_catalog", "getManagerEmail"), |
||
| 281 | ("bika_setup_catalog", "getMaxTimeAllowed"), |
||
| 282 | ("bika_setup_catalog", "getModel"), |
||
| 283 | ("bika_setup_catalog", "getName"), |
||
| 284 | ("bika_setup_catalog", "getPointOfCapture"), |
||
| 285 | ("bika_setup_catalog", "getPrice"), |
||
| 286 | ("bika_setup_catalog", "getSamplePointTitle"), |
||
| 287 | ("bika_setup_catalog", "getSamplePointUID"), |
||
| 288 | ("bika_setup_catalog", "getSampleTypeTitle"), |
||
| 289 | ("bika_setup_catalog", "getSampleTypeUID"), |
||
| 290 | ("bika_setup_catalog", "getServiceUID"), |
||
| 291 | ("bika_setup_catalog", "getTotalPrice"), |
||
| 292 | ("bika_setup_catalog", "getUnit"), |
||
| 293 | ("bika_setup_catalog", "getVATAmount"), |
||
| 294 | ("bika_setup_catalog", "getVolume"), |
||
| 295 | |||
| 296 | ("portal_catalog", "Analyst"), |
||
| 297 | ) |
||
| 298 | |||
| 299 | |||
| 300 | def pre_install(portal_setup): |
||
| 301 | """Runs before the first import step of the *default* profile |
||
| 302 | |||
| 303 | This handler is registered as a *pre_handler* in the generic setup profile |
||
| 304 | |||
| 305 | :param portal_setup: SetupTool |
||
| 306 | """ |
||
| 307 | logger.info("SENAITE PRE-INSTALL handler [BEGIN]") |
||
| 308 | |||
| 309 | # https://docs.plone.org/develop/addons/components/genericsetup.html#custom-installer-code-setuphandlers-py |
||
| 310 | profile_id = PROFILE_ID |
||
| 311 | |||
| 312 | context = portal_setup._getImportContext(profile_id) |
||
| 313 | portal = context.getSite() # noqa |
||
| 314 | |||
| 315 | logger.info("SENAITE PRE-INSTALL handler [DONE]") |
||
| 316 | |||
| 317 | |||
| 318 | def post_install(portal_setup): |
||
| 319 | """Runs after the last import step of the *default* profile |
||
| 320 | |||
| 321 | This handler is registered as a *post_handler* in the generic setup profile |
||
| 322 | |||
| 323 | :param portal_setup: SetupTool |
||
| 324 | """ |
||
| 325 | logger.info("SENAITE POST-INSTALL handler [BEGIN]") |
||
| 326 | |||
| 327 | # https://docs.plone.org/develop/addons/components/genericsetup.html#custom-installer-code-setuphandlers-py |
||
| 328 | profile_id = PROFILE_ID |
||
| 329 | |||
| 330 | context = portal_setup._getImportContext(profile_id) |
||
| 331 | portal = context.getSite() # noqa |
||
| 332 | |||
| 333 | logger.info("SENAITE POST-INSTALL handler [DONE]") |
||
| 334 | |||
| 335 | |||
| 336 | def setup_handler(context): |
||
| 337 | """SENAITE setup handler |
||
| 338 | """ |
||
| 339 | |||
| 340 | if context.readDataFile("bika.lims_various.txt") is None: |
||
| 341 | return |
||
| 342 | |||
| 343 | logger.info("SENAITE setup handler [BEGIN]") |
||
| 344 | |||
| 345 | portal = context.getSite() |
||
| 346 | |||
| 347 | # Run Installers |
||
| 348 | remove_default_content(portal) |
||
| 349 | hide_navbar_items(portal) |
||
| 350 | reindex_content_structure(portal) |
||
| 351 | setup_groups(portal) |
||
| 352 | setup_catalog_mappings(portal) |
||
| 353 | setup_core_catalogs(portal) |
||
| 354 | |||
| 355 | # Setting up all LIMS catalogs defined in catalog folder |
||
| 356 | setup_catalogs(portal, getCatalogDefinitions()) |
||
| 357 | |||
| 358 | # Run after all catalogs have been setup |
||
| 359 | setup_auditlog_catalog(portal) |
||
| 360 | |||
| 361 | logger.info("SENAITE setup handler [DONE]") |
||
| 362 | |||
| 363 | |||
| 364 | def remove_default_content(portal): |
||
| 365 | """Remove default Plone contents |
||
| 366 | """ |
||
| 367 | logger.info("*** Delete Default Content ***") |
||
| 368 | |||
| 369 | # Get the list of object ids for portal |
||
| 370 | object_ids = portal.objectIds() |
||
| 371 | delete_ids = filter(lambda id: id in object_ids, CONTENTS_TO_DELETE) |
||
| 372 | portal.manage_delObjects(ids=delete_ids) |
||
| 373 | |||
| 374 | |||
| 375 | def hide_navbar_items(portal): |
||
| 376 | """Hide root items in navigation |
||
| 377 | """ |
||
| 378 | logger.info("*** Hide Navigation Items ***") |
||
| 379 | |||
| 380 | # Get the list of object ids for portal |
||
| 381 | object_ids = portal.objectIds() |
||
| 382 | object_ids = filter(lambda id: id in object_ids, NAV_BAR_ITEMS_TO_HIDE) |
||
| 383 | for object_id in object_ids: |
||
| 384 | item = portal[object_id] |
||
| 385 | item.setExcludeFromNav(True) |
||
| 386 | item.reindexObject() |
||
| 387 | |||
| 388 | |||
| 389 | def reindex_content_structure(portal): |
||
| 390 | """Reindex contents generated by Generic Setup |
||
| 391 | """ |
||
| 392 | logger.info("*** Reindex content structure ***") |
||
| 393 | |||
| 394 | def reindex(obj, recurse=False): |
||
| 395 | # skip catalog tools etc. |
||
| 396 | if api.is_object(obj): |
||
| 397 | obj.reindexObject() |
||
| 398 | if recurse and hasattr(aq_base(obj), "objectValues"): |
||
| 399 | map(reindex, obj.objectValues()) |
||
| 400 | |||
| 401 | setup = api.get_setup() |
||
| 402 | setupitems = setup.objectValues() |
||
| 403 | rootitems = portal.objectValues() |
||
| 404 | |||
| 405 | for obj in itertools.chain(setupitems, rootitems): |
||
| 406 | logger.info("Reindexing {}".format(repr(obj))) |
||
| 407 | reindex(obj) |
||
| 408 | |||
| 409 | |||
| 410 | def setup_groups(portal): |
||
| 411 | """Setup roles and groups for BECHEM |
||
| 412 | """ |
||
| 413 | logger.info("*** Setup Roles and Groups ***") |
||
| 414 | |||
| 415 | portal_groups = api.get_tool("portal_groups") |
||
| 416 | |||
| 417 | for gdata in GROUPS: |
||
| 418 | group_id = gdata["id"] |
||
| 419 | # create the group and grant the roles |
||
| 420 | if group_id not in portal_groups.listGroupIds(): |
||
| 421 | logger.info("+++ Adding group {title} ({id})".format(**gdata)) |
||
| 422 | portal_groups.addGroup(group_id, |
||
| 423 | title=gdata["title"], |
||
| 424 | roles=gdata["roles"]) |
||
| 425 | # grant the roles to the existing group |
||
| 426 | else: |
||
| 427 | ploneapi.group.grant_roles( |
||
| 428 | groupname=gdata["id"], |
||
| 429 | roles=gdata["roles"],) |
||
| 430 | logger.info("+++ Granted group {title} ({id}) the roles {roles}" |
||
| 431 | .format(**gdata)) |
||
| 432 | |||
| 433 | |||
| 434 | def setup_catalog_mappings(portal): |
||
| 435 | """Setup portal_type -> catalog mappings |
||
| 436 | """ |
||
| 437 | logger.info("*** Setup Catalog Mappings ***") |
||
| 438 | |||
| 439 | at = api.get_tool("archetype_tool") |
||
| 440 | for portal_type, catalogs in CATALOG_MAPPINGS: |
||
| 441 | at.setCatalogsByType(portal_type, catalogs) |
||
| 442 | |||
| 443 | |||
| 444 | def setup_core_catalogs(portal): |
||
| 445 | """Setup core catalogs |
||
| 446 | """ |
||
| 447 | logger.info("*** Setup Core Catalogs ***") |
||
| 448 | |||
| 449 | to_reindex = [] |
||
| 450 | for catalog, name, attribute, meta_type in INDEXES: |
||
| 451 | c = api.get_tool(catalog) |
||
| 452 | indexes = c.indexes() |
||
| 538 |