Best Python code snippet using autotest_python
monitor_db_cleanup.py
Source:monitor_db_cleanup.py  
...64            logging.warning('Aborting entry %s due to max runtime', queue_entry)65            queue_entry.abort()66    def _check_for_db_inconsistencies(self):67        logging.info('Cleaning db inconsistencies')68        self._check_all_invalid_related_objects()69    def _check_invalid_related_objects_one_way(self, first_model,70                                               relation_field, second_model):71        if 'invalid' not in first_model.get_field_dict():72            return []73        invalid_objects = list(first_model.objects.filter(invalid=True))74        first_model.objects.populate_relationships(invalid_objects,75                                                   second_model,76                                                   'related_objects')77        error_lines = []78        for invalid_object in invalid_objects:79            if invalid_object.related_objects:80                related_list = ', '.join(str(related_object) for related_object81                                         in invalid_object.related_objects)82                error_lines.append('Invalid %s %s is related to %ss: %s'83                                   % (first_model.__name__, invalid_object,84                                      second_model.__name__, related_list))85                related_manager = getattr(invalid_object, relation_field)86                related_manager.clear()87        return error_lines88    def _check_invalid_related_objects(self, first_model, first_field,89                                       second_model, second_field):90        errors = self._check_invalid_related_objects_one_way(91            first_model, first_field, second_model)92        errors.extend(self._check_invalid_related_objects_one_way(93            second_model, second_field, first_model))94        return errors95    def _check_all_invalid_related_objects(self):96        model_pairs = ((models.Host, 'labels', models.Label, 'host_set'),97                       (models.AclGroup, 'hosts', models.Host, 'aclgroup_set'),98                       (models.AclGroup, 'users', models.User, 'aclgroup_set'),99                       (models.Test, 'dependency_labels', models.Label,100                        'test_set'))101        errors = []102        for first_model, first_field, second_model, second_field in model_pairs:103            errors.extend(self._check_invalid_related_objects(104                first_model, first_field, second_model, second_field))105        if errors:106            subject = ('%s relationships to invalid models, cleaned all' %107                       len(errors))108            message = '\n'.join(errors)109            logging.warning(subject)...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
