Best Python code snippet using autotest_python
resources.py
Source:resources.py  
...39        return models.AtomicGroup.add_object(name=input_dict['name'])40    def update(self, input_dict):41        data = {'max_number_of_machines':42                input_dict.get('max_number_of_machines')}43        data = input_dict.remove_unspecified_fields(data)44        self.instance.update_object(**data)45class AtomicGroupClassCollection(resource_lib.Collection):46    queryset = models.AtomicGroup.valid_objects.all()47    entry_class = AtomicGroupClass48class Label(EntryWithInvalid):49    model = models.Label50    @classmethod51    def add_query_selectors(cls, query_processor):52        query_processor.add_field_selector('name')53        query_processor.add_field_selector(54                'is_platform', field='platform',55                value_transform=query_processor.read_boolean)56    @classmethod57    def from_uri_args(cls, request, label_name, **kwargs):58        return cls(request, models.Label.objects.get(name=label_name))59    def _uri_args(self):60        return {'label_name': self.instance.name}61    def short_representation(self):62        rep = super(Label, self).short_representation()63        rep.update({'name': self.instance.name,64                    'is_platform': bool(self.instance.platform)})65        return rep66    def full_representation(self):67        rep = super(Label, self).full_representation()68        atomic_group_class = AtomicGroupClass.from_optional_instance(69                self._request, self.instance.atomic_group)70        rep.update({'atomic_group_class':71                        atomic_group_class.short_representation(),72                    'hosts': HostLabelingCollection(fixed_entry=self).link()})73        return rep74    @classmethod75    def create_instance(cls, input_dict, containing_collection):76        cls._check_for_required_fields(input_dict, ('name',))77        return models.Label.add_object(name=input_dict['name'])78    def update(self, input_dict):79        # TODO update atomic group80        if 'is_platform' in input_dict:81            self.instance.platform = input_dict['is_platform']82            self.instance.save()83class LabelCollection(resource_lib.Collection):84    queryset = models.Label.valid_objects.all()85    entry_class = Label86class AtomicLabelTagging(resource_lib.Relationship):87    related_classes = {'label': Label, 'atomic_group_class': AtomicGroupClass}88class AtomicLabelTaggingCollection(resource_lib.RelationshipCollection):89    entry_class = AtomicLabelTagging90class User(resource_lib.InstanceEntry):91    model = models.User92    _permitted_methods = ('GET,')93    @classmethod94    def from_uri_args(cls, request, username, **kwargs):95        if username == '@me':96            username = models.User.current_user().login97        return cls(request, models.User.objects.get(login=username))98    def _uri_args(self):99        return {'username': self.instance.login}100    def short_representation(self):101        rep = super(User, self).short_representation()102        rep['username'] = self.instance.login103        return rep104    def full_representation(self):105        rep = super(User, self).full_representation()106        accessible_hosts = HostCollection(self._request)107        accessible_hosts.set_query_parameters(accessible_by=self.instance.login)108        rep.update({'jobs': 'TODO',109                    'recurring_runs': 'TODO',110                    'acls':111                    UserAclMembershipCollection(fixed_entry=self).link(),112                    'accessible_hosts': accessible_hosts.link()})113        return rep114class UserCollection(resource_lib.Collection):115    _permitted_methods = ('GET',)116    queryset = models.User.objects.all()117    entry_class = User118class Acl(resource_lib.InstanceEntry):119    _permitted_methods = ('GET',)120    model = models.AclGroup121    @classmethod122    def from_uri_args(cls, request, acl_name, **kwargs):123        return cls(request, models.AclGroup.objects.get(name=acl_name))124    def _uri_args(self):125        return {'acl_name': self.instance.name}126    def short_representation(self):127        rep = super(Acl, self).short_representation()128        rep['name'] = self.instance.name129        return rep130    def full_representation(self):131        rep = super(Acl, self).full_representation()132        rep.update({'users':133                    UserAclMembershipCollection(fixed_entry=self).link(),134                    'hosts':135                    HostAclMembershipCollection(fixed_entry=self).link()})136        return rep137    @classmethod138    def create_instance(cls, input_dict, containing_collection):139        cls._check_for_required_fields(input_dict, ('name',))140        return models.AclGroup.add_object(name=input_dict['name'])141    def update(self, input_dict):142        pass143class AclCollection(resource_lib.Collection):144    queryset = models.AclGroup.objects.all()145    entry_class = Acl146class UserAclMembership(resource_lib.Relationship):147    related_classes = {'user': User, 'acl': Acl}148    # TODO: check permissions149    # TODO: check for and add/remove "Everyone"150class UserAclMembershipCollection(resource_lib.RelationshipCollection):151    entry_class = UserAclMembership152class Host(EntryWithInvalid):153    model = models.Host154    @classmethod155    def add_query_selectors(cls, query_processor):156        query_processor.add_field_selector('hostname')157        query_processor.add_field_selector(158                'locked', value_transform=query_processor.read_boolean)159        query_processor.add_field_selector(160                'locked_by', field='locked_by__login',161                doc='Username of user who locked this host, if locked')162        query_processor.add_field_selector('status')163        query_processor.add_field_selector(164                'protection_level', field='protection',165                doc='Verify/repair protection level',166                value_transform=cls._read_protection)167        query_processor.add_field_selector(168                'accessible_by', field='aclgroup__users__login',169                doc='Username of user with access to this host')170        query_processor.add_related_existence_selector(171                'has_label', models.Label, 'name')172    @classmethod173    def _read_protection(cls, protection_input):174        return host_protections.Protection.get_value(protection_input)175    @classmethod176    def from_uri_args(cls, request, hostname, **kwargs):177        return cls(request, models.Host.objects.get(hostname=hostname))178    def _uri_args(self):179        return {'hostname': self.instance.hostname}180    def short_representation(self):181        rep = super(Host, self).short_representation()182        # TODO calling platform() over and over is inefficient183        platform_rep = (Label.from_optional_instance(self._request,184                                                     self.instance.platform())185                        .short_representation())186        rep.update({'hostname': self.instance.hostname,187                    'locked': bool(self.instance.locked),188                    'status': self.instance.status,189                    'platform': platform_rep})190        return rep191    def full_representation(self):192        rep = super(Host, self).full_representation()193        protection = host_protections.Protection.get_string(194                self.instance.protection)195        locked_by = (User.from_optional_instance(self._request,196                                                 self.instance.locked_by)197                     .short_representation())198        labels = HostLabelingCollection(fixed_entry=self)199        acls = HostAclMembershipCollection(fixed_entry=self)200        queue_entries = QueueEntryCollection(self._request)201        queue_entries.set_query_parameters(host=self.instance.hostname)202        health_tasks = HealthTaskCollection(self._request)203        health_tasks.set_query_parameters(host=self.instance.hostname)204        rep.update({'locked_by': locked_by,205                    'locked_on': self._format_datetime(self.instance.lock_time),206                    'invalid': self.instance.invalid,207                    'protection_level': protection,208                    # TODO make these efficient209                    'labels': labels.full_representation(),210                    'acls': acls.full_representation(),211                    'queue_entries': queue_entries.link(),212                    'health_tasks': health_tasks.link()})213        return rep214    @classmethod215    def create_instance(cls, input_dict, containing_collection):216        cls._check_for_required_fields(input_dict, ('hostname',))217        # include locked here, rather than waiting for update(), to avoid race218        # conditions219        host = models.Host.add_object(hostname=input_dict['hostname'],220                                      locked=input_dict.get('locked', False))221        return host222    def update(self, input_dict):223        data = {'locked': input_dict.get('locked'),224                'protection': input_dict.get('protection_level')}225        data = input_dict.remove_unspecified_fields(data)226        if 'protection' in data:227            data['protection'] = self._read_protection(data['protection'])228        self.instance.update_object(**data)229        if 'platform' in input_dict:230            label = self.resolve_link(input_dict['platform']) .instance231            if not label.platform:232                raise exceptions.BadRequest('Label %s is not a platform' % label.name)233            for label in self.instance.labels.filter(platform=True):234                self.instance.labels.remove(label)235            self.instance.labels.add(label)236class HostCollection(resource_lib.Collection):237    queryset = models.Host.valid_objects.all()238    entry_class = Host239class HostLabeling(resource_lib.Relationship):240    related_classes = {'host': Host, 'label': Label}241class HostLabelingCollection(resource_lib.RelationshipCollection):242    entry_class = HostLabeling243class HostAclMembership(resource_lib.Relationship):244    related_classes = {'host': Host, 'acl': Acl}245    # TODO: acl.check_for_acl_violation_acl_group()246    # TODO: models.AclGroup.on_host_membership_change()247class HostAclMembershipCollection(resource_lib.RelationshipCollection):248    entry_class = HostAclMembership249class Test(resource_lib.InstanceEntry):250    model = models.Test251    @classmethod252    def add_query_selectors(cls, query_processor):253        query_processor.add_field_selector('name')254    @classmethod255    def from_uri_args(cls, request, test_name, **kwargs):256        return cls(request, models.Test.objects.get(name=test_name))257    def _uri_args(self):258        return {'test_name': self.instance.name}259    def short_representation(self):260        rep = super(Test, self).short_representation()261        rep['name'] = self.instance.name262        return rep263    def full_representation(self):264        rep = super(Test, self).full_representation()265        rep.update({'author': self.instance.author,266                    'class': self.instance.test_class,267                    'control_file_type':268                    control_data.CONTROL_TYPE.get_string(269                        self.instance.test_type),270                    'control_file_path': self.instance.path,271                    'sync_count': self.instance.sync_count,272                    'dependencies':273                    TestDependencyCollection(fixed_entry=self).link(),274                    })275        return rep276    @classmethod277    def create_instance(cls, input_dict, containing_collection):278        cls._check_for_required_fields(input_dict,279                                       ('name', 'control_file_type',280                                        'control_file_path'))281        test_type = control_data.CONTROL_TYPE.get_value(282            input['control_file_type'])283        return models.Test.add_object(name=input_dict['name'],284                                      test_type=test_type,285                                      path=input_dict['control_file_path'])286    def update(self, input_dict):287        data = {'test_type': input_dict.get('control_file_type'),288                'path': input_dict.get('control_file_path'),289                'class': input_dict.get('class'),290                }291        data = input_dict.remove_unspecified_fields(data)292        self.instance.update_object(**data)293class TestCollection(resource_lib.Collection):294    queryset = models.Test.objects.all()295    entry_class = Test296class TestDependency(resource_lib.Relationship):297    related_classes = {'test': Test, 'label': Label}298class TestDependencyCollection(resource_lib.RelationshipCollection):299    entry_class = TestDependency300# TODO profilers301class ExecutionInfo(resource_lib.Resource):302    _permitted_methods = ('GET','POST')303    _job_fields = models.Job.get_field_dict()304    _DEFAULTS = {305            'control_file': '',...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!!
