我已经构建了一个应用程序,它有很多相似的视图,应该能够使用相同的基本代码。然而,每种方法在方法内的不同拐点处都有某些独特的特征,因此我无法找到一种方法来构造它以实际重用任何代码。相反,我创建了一种剪切和粘贴方法,并单独调整了每种方法。应用程序的这一部分是我编写的第一个 Python 代码,我知道肯定有更好的方法来做到这一点,但我被锁定在这样做并且“它有效”,所以我看不到出路.
下面是基本视图模板的基本外观:
def view_entity(request, entity_id=None):
if request.method == 'POST':
return _post_entity(request, entity_id)
else:
return _get_entity(request, entity_id)
def _get_entity(request, entity_id):
data = _process_entity(request, entity_id)
if 'redirect' in data:
return data['redirect']
else:
return _render_entity(request, data['form'])
def _post_entity(request, entity_id):
data = _process_entity(request, entity_id)
if 'redirect' in data:
return data['redirect']
elif data['form'].is_valid():
# custom post processing here
instance = data['form'].save()
return HttpResponseRedirect(reverse('entity', args=[instance.id]))
else:
return _render_entity(request, data['form'])
def _process_entity(request, entity_id):
data = {}
if entity_id != 'new': # READ/UPDATE
# sometimes there's custom code to retrieve the entity
e = entity_id and get_object_or_404(Entity.objects, pk=entity_id)
# sometimes there's custom code here that deauthorizes e
# sometimes extra values are added to data here (e.g. parent entity)
if e:
if request.method == 'POST':
data['form'] = EntityForm(request.POST, instance=e)
# sometimes there's a conditional here for CustomEntityForm
else:
data['form'] = EntityForm(instance=e)
else: # user not authorized for this entity
return {'redirect': HttpResponseRedirect(reverse('home'))}
# sometimes there's custom code here for certain entity types
else: # CREATE
if request.method == 'POST':
data['form'] = EntityForm(request.POST)
else:
data['form'] = EntityForm()
# sometimes extra key/values are added to data here
return data
我什至没有包括所有可能的变体,但正如您所见,该_process_entity
方法需要根据正在处理的实体类型进行大量个性化定制。这是我无法找到处理此问题的 DRY 方法的主要原因。
任何帮助表示赞赏,谢谢!