From 97f1c7ef2be4f6d83f2fc52989a0d7f94a51f27a Mon Sep 17 00:00:00 2001 From: Santiago Lamora Date: Thu, 22 Apr 2021 14:44:47 +0200 Subject: [PATCH] Replace field.rel.to with field.remote_field.model Field.rel and Field.remote_field.to are removed in Django 2.x --- orchestra/contrib/mailboxes/signals.py | 2 +- orchestra/contrib/orders/helpers.py | 6 ++-- orchestra/contrib/resources/admin.py | 2 +- orchestra/contrib/resources/models.py | 2 +- orchestra/contrib/saas/fields.py | 3 +- orchestra/contrib/saas/services/options.py | 40 +++++++++++----------- orchestra/contrib/services/admin.py | 2 +- orchestra/contrib/webapps/fields.py | 6 ++-- orchestra/models/utils.py | 4 +-- 9 files changed, 32 insertions(+), 35 deletions(-) diff --git a/orchestra/contrib/mailboxes/signals.py b/orchestra/contrib/mailboxes/signals.py index 8bb45533..5cfdcef0 100644 --- a/orchestra/contrib/mailboxes/signals.py +++ b/orchestra/contrib/mailboxes/signals.py @@ -27,7 +27,7 @@ def create_local_address(sender, *args, **kwargs): mbox = kwargs['instance'] local_domain = settings.MAILBOXES_LOCAL_DOMAIN if not mbox.pk and local_domain: - Domain = Address._meta.get_field('domain').rel.to + Domain = Address._meta.get_field('domain').remote_field.model try: domain = Domain.objects.get(name=local_domain) except Domain.DoesNotExist: diff --git a/orchestra/contrib/orders/helpers.py b/orchestra/contrib/orders/helpers.py index b5e1487a..ce0d1917 100644 --- a/orchestra/contrib/orders/helpers.py +++ b/orchestra/contrib/orders/helpers.py @@ -6,7 +6,7 @@ from orchestra.core import services def get_related_object(origin, max_depth=2): """ Introspects origin object and return the first related service object - + WARNING this is NOT an exhaustive search but a compromise between cost and flexibility. A more comprehensive approach may be considered if a use-case calls for it. @@ -16,12 +16,12 @@ def get_related_object(origin, max_depth=2): if hasattr(field, 'ct_field'): yield getattr(node, field.name) for field in node._meta.fields: - if field.rel: + if field.remote_field: try: yield getattr(node, field.name) except ObjectDoesNotExist: pass - + # BFS model relation transversal queue = [[origin]] while queue: diff --git a/orchestra/contrib/resources/admin.py b/orchestra/contrib/resources/admin.py index 462f717f..f758851e 100644 --- a/orchestra/contrib/resources/admin.py +++ b/orchestra/contrib/resources/admin.py @@ -98,7 +98,7 @@ class ResourceAdmin(ExtendedModelAdmin): """ filter service content_types """ if db_field.name == 'content_type': models = [ model._meta.model_name for model in services.get() ] - kwargs['queryset'] = db_field.rel.to.objects.filter(model__in=models) + kwargs['queryset'] = db_field.remote_field.model.objects.filter(model__in=models) return super(ResourceAdmin, self).formfield_for_dbfield(db_field, **kwargs) diff --git a/orchestra/contrib/resources/models.py b/orchestra/contrib/resources/models.py index b886c41a..50c1b14a 100644 --- a/orchestra/contrib/resources/models.py +++ b/orchestra/contrib/resources/models.py @@ -342,7 +342,7 @@ def create_resource_relation(): pass else: related._meta.private_fields = [ - field for field in related._meta.private_fields if field.rel.to != ResourceData + field for field in related._meta.private_fields if field.remote_field.model != ResourceData ] for ct, resources in Resource.objects.group_by('content_type').items(): diff --git a/orchestra/contrib/saas/fields.py b/orchestra/contrib/saas/fields.py index 785e5eda..74bf8b6a 100644 --- a/orchestra/contrib/saas/fields.py +++ b/orchestra/contrib/saas/fields.py @@ -11,5 +11,4 @@ class VirtualDatabaseRelation(GenericRelation): pks.append(obj.database_id) if not pks: return [] - # TODO renamed to self.remote_field in django 1.8 - return self.rel.to._base_manager.db_manager(using).filter(pk__in=pks) + return self.remote_field.model._base_manager.db_manager(using).filter(pk__in=pks) diff --git a/orchestra/contrib/saas/services/options.py b/orchestra/contrib/saas/services/options.py index 56a8dfef..168aecce 100644 --- a/orchestra/contrib/saas/services/options.py +++ b/orchestra/contrib/saas/services/options.py @@ -24,7 +24,7 @@ class SoftwareService(plugins.Plugin, metaclass=plugins.PluginMount): 'http': (Website.HTTP, (Website.HTTP, Website.HTTP_AND_HTTPS)), 'https': (Website.HTTPS_ONLY, (Website.HTTPS, Website.HTTP_AND_HTTPS, Website.HTTPS_ONLY)), } - + name = None verbose_name = None form = SaaSPasswordForm @@ -34,7 +34,7 @@ class SoftwareService(plugins.Plugin, metaclass=plugins.PluginMount): class_verbose_name = _("Software as a Service") plugin_field = 'service' allow_custom_url = False - + @classmethod @lru_cache() def get_plugins(cls, all=False): @@ -48,18 +48,18 @@ class SoftwareService(plugins.Plugin, metaclass=plugins.PluginMount): for cls in settings.SAAS_ENABLED_SERVICES: plugins.append(import_class(cls)) return plugins - + def get_change_readonly_fields(cls): fields = super(SoftwareService, cls).get_change_readonly_fields() return fields + ('name',) - + def get_site_domain(self): context = { 'site_name': self.instance.name, 'name': self.instance.name, } return self.site_domain % context - + def clean(self): if self.allow_custom_url: if self.instance.custom_url: @@ -69,7 +69,7 @@ class SoftwareService(plugins.Plugin, metaclass=plugins.PluginMount): raise ValidationError({ 'custom_url': _("Custom URL not allowed for this service."), }) - + def clean_data(self): data = super(SoftwareService, self).clean_data() if not self.instance.pk: @@ -88,10 +88,10 @@ class SoftwareService(plugins.Plugin, metaclass=plugins.PluginMount): if errors: raise ValidationError(errors) return data - + def get_directive_name(self): return '%s-saas' % self.name - + def get_directive(self, *args): if not args: instance = self.instance @@ -106,7 +106,7 @@ class SoftwareService(plugins.Plugin, metaclass=plugins.PluginMount): website__domains__name=url.netloc, website__account=account, ) - + def get_website(self): url = urlparse(self.instance.custom_url) account = self.instance.account @@ -117,10 +117,10 @@ class SoftwareService(plugins.Plugin, metaclass=plugins.PluginMount): directives__name=self.get_directive_name(), directives__value=url.path, ) - + def create_or_update_directive(self): return helpers.create_or_update_directive(self) - + def delete_directive(self): directive = None try: @@ -131,7 +131,7 @@ class SoftwareService(plugins.Plugin, metaclass=plugins.PluginMount): return if directive is not None: directive.delete() - + def save(self): # pre instance.save() if isinstalled('orchestra.contrib.websites'): @@ -139,11 +139,11 @@ class SoftwareService(plugins.Plugin, metaclass=plugins.PluginMount): self.create_or_update_directive() elif self.instance.pk: self.delete_directive() - + def delete(self): if isinstalled('orchestra.contrib.websites'): self.delete_directive() - + def get_related(self): return [] @@ -152,7 +152,7 @@ class DBSoftwareService(SoftwareService): db_name = None db_user = None abstract = True - + def get_db_name(self): context = { 'name': self.instance.name, @@ -161,15 +161,15 @@ class DBSoftwareService(SoftwareService): db_name = self.db_name % context # Limit for mysql database names return db_name[:65] - + def get_db_user(self): return self.db_user - + @cached def get_account(self): account_model = self.instance._meta.get_field('account') - return account_model.rel.to.objects.get_main() - + return account_model.remote_field.model.objects.get_main() + def validate(self): super(DBSoftwareService, self).validate() create = not self.instance.pk @@ -192,7 +192,7 @@ class DBSoftwareService(SoftwareService): raise ValidationError({ 'name': e.messages, }) - + def save(self): super(DBSoftwareService, self).save() account = self.get_account() diff --git a/orchestra/contrib/services/admin.py b/orchestra/contrib/services/admin.py index e8b823aa..b8e20634 100644 --- a/orchestra/contrib/services/admin.py +++ b/orchestra/contrib/services/admin.py @@ -59,7 +59,7 @@ class ServiceAdmin(ChangeViewActionsMixin, admin.ModelAdmin): """ Improve performance of account field and filter by account """ if db_field.name == 'content_type': models = [model._meta.model_name for model in services.get()] - queryset = db_field.rel.to.objects + queryset = db_field.remote_field.model.objects kwargs['queryset'] = queryset.filter(model__in=models) if db_field.name in ['match', 'metric', 'order_description']: kwargs['widget'] = forms.TextInput(attrs={'size':'160'}) diff --git a/orchestra/contrib/webapps/fields.py b/orchestra/contrib/webapps/fields.py index 7efd52b8..d430a414 100644 --- a/orchestra/contrib/webapps/fields.py +++ b/orchestra/contrib/webapps/fields.py @@ -12,8 +12,7 @@ class VirtualDatabaseRelation(GenericRelation): pks.append(db_id) if not pks: return [] - # TODO renamed to self.remote_field in django 1.8 - return self.rel.to._base_manager.db_manager(using).filter(pk__in=pks) + return self.remote_field.model._base_manager.db_manager(using).filter(pk__in=pks) class VirtualDatabaseUserRelation(GenericRelation): @@ -26,5 +25,4 @@ class VirtualDatabaseUserRelation(GenericRelation): pks.append(db_id) if not pks: return [] - # TODO renamed to self.remote_field in django 1.8 - return self.rel.to._base_manager.db_manager(using).filter(pk__in=pks) + return self.remote_field.model._base_manager.db_manager(using).filter(pk__in=pks) diff --git a/orchestra/models/utils.py b/orchestra/models/utils.py index e9514fe8..e0facdd1 100644 --- a/orchestra/models/utils.py +++ b/orchestra/models/utils.py @@ -50,9 +50,9 @@ def get_model_field_path(origin, target): if node == target: return path for field in node._meta.fields: - if field.rel: + if field.remote_field: new_model = list(model) - new_model.append(field.rel.to) + new_model.append(field.remote_field.model) new_path = list(path) new_path.append(field.name) queue.append((new_model, new_path))