我正在尝试删除Django模型中的重复代码。如下面的代码所示,send_salary_notification
和send_pension_notifications
之间的唯一区别是,一个使用相关字段jurisdiction
,另一个使用agency
。如何重构代码,以便根据record_type
设置管辖权/机构一次?
我尝试将类变量创建为record_type
,然后在第一个函数中将变量设置为cls.record_type = record_type
,然后执行以下操作:
agencies_changed.append(subscription.record_type)
但是这会导致错误消息record_type不是有效实例。
这是我的代码:
class Subscriber(models.Model):
email = models.EmailField(null=False, unique=True)
activation_key = models.CharField(max_length=64)
key_expires = models.DateTimeField(default=get_key_expiration)
verified = models.BooleanField(default=False)
@classmethod
def send_notifications(cls, record_type, slugs):
"""
Sends notifications for all subscribers.
"""
subscribers = cls.objects.all()
for subscriber in subscribers:
if record_type == 'salary':
subscriber.send_salary_notifications(slugs, record_type)
elif record_type == 'pension':
subscriber.send_pension_notifications(slugs, record_type)
def send_salary_notifications(self, slugs, record_type):
matching_subscriptions = self.get_matching_salary_subscriptions(slugs)
agencies_changed = []
if not matching_subscriptions:
return None
for subscription in matching_subscriptions:
agencies_changed.append(subscription.jurisdiction)
self.update_salary_last_year_sent(subscription)
message = self.build_notification_message(agencies_changed, record_type)
self.send_notification_email(message)
def send_pension_notifications(self, slugs, record_type):
matching_subscriptions = self.get_matching_pension_subscriptions(slugs)
agencies_changed = []
if not matching_subscriptions:
return None
for subscription in matching_subscriptions:
agencies_changed.append(subscription.agency)
self.update_pension_last_year_sent(subscription)
message = self.build_notification_message(agencies_changed, record_type)
self.send_notification_email(message)
class SalarySubscription(models.Model):
subscriber = models.ForeignKey('subscriptions.Subscriber', related_name='salary_subscriptions')
jurisdiction = models.ForeignKey('jurisdiction.Jurisdiction')
last_year_sent = models.PositiveSmallIntegerField(null=True)
class PensionSubscription(models.Model):
subscriber = models.ForeignKey('subscriptions.Subscriber', related_name='pension_subscriptions')
agency = models.ForeignKey('pensions.Agency')
last_year_sent = models.PositiveSmallIntegerField(null=True)
答案 0 :(得分:0)
假设您不想更改get_matching_pension_subscriptions
,get_matching_salary_subscriptions
,update_salary_last_year_sent
和update_pension_last_year_sent
方法,则可以将代码重构为遵循。您可以定义一个通知方法,并根据getattr
使用内置的record_type
动态获取相关属性。可能还有其他重构解决方案更适合您的代码库,但是看到您发布此解决方案的一段代码非常合理。
class Subscriber(models.Model):
@classmethod
def send_notifications(cls, record_type, slugs):
"""
Sends notifications for all subscribers.
"""
subscribers = cls.objects.all()
for subscriber in subscribers:
subscriber._send_notification(record_type, slugs)
def _send_notification(self, record_type, slugs):
matching_subscriptions = getattr(self, 'get_matching_{}_subscriptions'.format(record_type))(slugs)
if not matching_subscriptions:
return None
agencies_changed = []
agency_attribute = {
'salary': 'jurisdiction',
'pension': 'agency'
}[record_type]
for subscription in matching_subscriptions:
agencies_changed.append(getattr(subscription, agency_attribute))
getattr(self, 'update_{}_last_year_sent'.format(record_type))(subscription)
message = self.build_notification_message(agencies_changed, record_type)
self.send_notification_email(message)