def ready(self): """Setup background tasks and update exchange rates.""" if canAppAccessDatabase(): self.remove_obsolete_tasks() self.start_background_tasks() if not isInTestMode(): # pragma: no cover self.update_exchange_rates() self.collect_notification_methods() if canAppAccessDatabase() or settings.TESTING_ENV: self.add_user_on_startup()
def ready(self): """ This function is called whenever the Part app is loaded. """ if canAppAccessDatabase(): self.update_trackable_status()
def ready(self): """ This function is called whenever the label app is loaded """ if canAppAccessDatabase(): self.create_labels() # pragma: no cover
def ready(self): """ This function is called whenever the Company app is loaded. """ if canAppAccessDatabase(): self.generate_company_thumbs()
def ready(self): if canAppAccessDatabase(): self.start_background_tasks() if not isInTestMode(): self.update_exchange_rates()
def ready(self): if settings.PLUGINS_ENABLED: if not canAppAccessDatabase(allow_test=True): logger.info("Skipping plugin loading sequence") # pragma: no cover else: logger.info('Loading InvenTree plugins') if not registry.is_loading: # this is the first startup try: from common.models import InvenTreeSetting if InvenTreeSetting.get_setting('PLUGIN_ON_STARTUP', create=False): # make sure all plugins are installed registry.install_plugin_file() except: # pragma: no cover pass # get plugins and init them registry.collect_plugins() registry.load_plugins() # drop out of maintenance # makes sure we did not have an error in reloading and maintenance is still active set_maintenance_mode(False) # check git version registry.git_is_modern = check_git_version() if not registry.git_is_modern: # pragma: no cover # simulating old git seems not worth it for coverage log_error(_('Your enviroment has an outdated git version. This prevents InvenTree from loading plugin details.'), 'load') else: logger.info("Plugins not enabled - skipping loading sequence") # pragma: no cover
def ready(self): """ This function is called whenever the report app is loaded """ if canAppAccessDatabase(allow_test=True): self.create_default_test_reports() self.create_default_build_reports()
def ready(self): """ This function is called whenever the label app is loaded """ if canAppAccessDatabase(): self.create_stock_item_labels() self.create_stock_location_labels()
def ready(self): if canAppAccessDatabase(): try: self.assign_permissions() except (OperationalError, ProgrammingError): pass try: self.update_owners() except (OperationalError, ProgrammingError): pass
def ready(self): """Called when the 'users' app is loaded at runtime""" if canAppAccessDatabase(allow_test=True): try: self.assign_permissions() except (OperationalError, ProgrammingError): pass try: self.update_owners() except (OperationalError, ProgrammingError): pass
def trigger_event(event, *args, **kwargs): """Trigger an event with optional arguments. This event will be stored in the database, and the worker will respond to it later on. """ if not settings.PLUGINS_ENABLED: # Do nothing if plugins are not enabled return # pragma: no cover # Make sure the database can be accessed and is not beeing tested rn if not canAppAccessDatabase() and not settings.PLUGIN_TESTING_EVENTS: logger.debug( f"Ignoring triggered event '{event}' - database not ready") return logger.debug(f"Event triggered: '{event}'") offload_task(register_event, event, *args, **kwargs)
def update_group_roles(group, debug=False): """ Iterates through all of the RuleSets associated with the group, and ensures that the correct permissions are either applied or removed from the group. This function is called under the following conditions: a) Whenever the InvenTree database is launched b) Whenver the group object is updated The RuleSet model has complete control over the permissions applied to any group. """ if not canAppAccessDatabase(allow_test=True): return # List of permissions already associated with this group group_permissions = set() # Iterate through each permission already assigned to this group, # and create a simplified permission key string for p in group.permissions.all(): (permission, app, model) = p.natural_key() permission_string = '{app}.{perm}'.format(app=app, perm=permission) group_permissions.add(permission_string) # List of permissions which must be added to the group permissions_to_add = set() # List of permissions which must be removed from the group permissions_to_delete = set() def add_model(name, action, allowed): """ Add a new model to the pile: args: name - The name of the model e.g. part_part action - The permission action e.g. view allowed - Whether or not the action is allowed """ if action not in ['view', 'add', 'change', 'delete']: raise ValueError("Action {a} is invalid".format(a=action)) permission_string = RuleSet.get_model_permission_string(model, action) if allowed: # An 'allowed' action is always preferenced over a 'forbidden' action if permission_string in permissions_to_delete: permissions_to_delete.remove(permission_string) permissions_to_add.add(permission_string) else: # A forbidden action will be ignored if we have already allowed it if permission_string not in permissions_to_add: permissions_to_delete.add(permission_string) # Get all the rulesets associated with this group for r in RuleSet.RULESET_CHOICES: rulename = r[0] try: ruleset = RuleSet.objects.get(group=group, name=rulename) except RuleSet.DoesNotExist: # Create the ruleset with default values (if it does not exist) ruleset = RuleSet.objects.create(group=group, name=rulename) # Which database tables does this RuleSet touch? models = ruleset.get_models() for model in models: # Keep track of the available permissions for each model add_model(model, 'view', ruleset.can_view) add_model(model, 'add', ruleset.can_add) add_model(model, 'change', ruleset.can_change) add_model(model, 'delete', ruleset.can_delete) def get_permission_object(permission_string): """ Find the permission object in the database, from the simplified permission string Args: permission_string - a simplified permission_string e.g. 'part.view_partcategory' Returns the permission object in the database associated with the permission string """ (app, perm) = permission_string.split('.') perm, model = split_permission(app, perm) try: content_type = ContentType.objects.get(app_label=app, model=model) permission = Permission.objects.get(content_type=content_type, codename=perm) except ContentType.DoesNotExist: logger.warning( f"Error: Could not find permission matching '{permission_string}'" ) permission = None return permission # Add any required permissions to the group for perm in permissions_to_add: # Ignore if permission is already in the group if perm in group_permissions: continue permission = get_permission_object(perm) if permission: group.permissions.add(permission) if debug: print(f"Adding permission {perm} to group {group.name}") # Remove any extra permissions from the group for perm in permissions_to_delete: # Ignore if the permission is not already assigned if perm not in group_permissions: continue permission = get_permission_object(perm) if permission: group.permissions.remove(permission) if debug: print(f"Removing permission {perm} from group {group.name}")
def ready(self): if canAppAccessDatabase(): self.start_background_tasks()