Я новичок в разработке и Django, все еще изучаю OOP, но я хотел узнать лучше с помощью некоторых примеров из кода, который я создал. Я создал функцию в своем файле просмотра. Функция становится очень длинной, и, вероятно, мне придется ее увеличить. Я думаю, что оператор if можно было бы написать как функцию, но не знаю, с чего начать. Каким образом можно было бы сократить код, чтобы его было легче поддерживать? Вот код:
def processView(request, pk):
process = ProcessInfo.objects.get(id=pk)
#bottlenecks = get_object_or_404(ProcessBottlenecks, process_info_id=process.id)
try:
details = ProcessDetails.objects.get(process_info_id=process.id)
except ProcessDetails.DoesNotExist:
details = None
try:
score = ProcessScoring.objects.get(process_score_id=pk)
except ProcessScoring.DoesNotExist:
score = None
try:
assumptions = ProcessAssumptions.objects.get(process_rel_process=process.id)
except ProcessAssumptions.DoesNotExist:
assumptions = None
color_bottleneck = None
color_quality = None
color_datacomplexity = None
color_technology = None
color_transformation = None
color_driversforchange = None
color_technology = None
#B
if score.tot_score_bottlenecks_scoring in range(0, 200):
color_bottleneck = 'badge-primary'
if score.tot_score_bottlenecks_scoring in range(100, 500):
color_bottleneck = 'badge-danger'
if score.tot_score_bottlenecks_scoring in range(500, 1000):
color_bottleneck = 'badge-warning'
if score.tot_score_bottlenecks_scoring >= 1000:
color_bottleneck = 'badge-success'
#P
if score.tot_score_dataquality in range(0, 200):
color_quality = 'badge-primary'
elif score.tot_score_dataquality in range(200, 500):
color_quality = 'badge-danger'
elif score.tot_score_dataquality in range(500, 100):
color_quality = 'badge-warning'
elif score.tot_score_dataquality >= 1000:
color_quality = 'badge-success'
#D
if score.tot_score_datacomplexity in range(0, 200):
color_datacomplexity = 'badge-primary'
if score.tot_score_datacomplexity in range(200, 500):
color_datacomplexity = 'badge-danger'
if score.tot_score_datacomplexity in range(500, 1000):
color_datacomplexity = 'badge-warning'
if score.tot_score_datacomplexity >= 1000:
color_datacomplexity = 'badge-success'
#TECHNOLOGY
if score.tot_score_technology in range(0, 200):
color_technology = 'badge-primary'
if score.tot_score_technology in range(200, 500):
color_technology = 'badge-danger'
if score.tot_score_technology in range(500, 1000):
color_technology = 'badge-warning'
if score.tot_score_technology >= 1000:
color_technology = 'badge-success'
#T
if score.tot_score_transformation in range(0, 200):
color_transformation = 'badge-primary'
if score.tot_score_transformation in range(200, 500):
color_transformation = 'badge-danger'
if score.tot_score_transformation in range(500, 1000):
color_transformation = 'badge-warning'
if score.tot_score_transformation >= 1000:
color_transformation = 'badge-success'
#DRIVERSFORCHANGE
if score.tot_score_driversforchange in range(0, 200):
color_driversforchange = 'badge-primary'
if score.tot_score_driversforchange in range(200, 500):
color_driversforchange = 'badge-danger'
if score.tot_score_driversforchange in range(500, 1000):
color_driversforchange = 'badge-warning'
if score.tot_score_driversforchange >= 1000:
color_driversforchange = 'badge-success'
#S
if score.tot_score_scalability in range(0, 200):
color_scalability = 'badge-primary'
if score.tot_score_scalability in range(200, 500):
color_scalability = 'badge-danger'
if score.tot_score_scalability in range(500, 1000):
color_scalability = 'badge-warning'
if score.tot_score_scalability >= 1000:
color_scalability = 'badge-success'
context ={
'process':process,
'details':details,
'score':score,
'assumptions':assumptions,
'color_bottleneck' : color_bottleneck,
'color_quality':color_quality,
'color_datacomplexity':color_datacomplexity,
'color_technology':color_technology,
'color_transformation' : color_transformation,
'color_driversforchange':color_driversforchange,
'color_scalability' : color_scalability
}
return render(request, 'process/process_view.html', context)
Вот еще один пример из forms.py. Здесь у меня повторяются поля формы и создаются сотни строк
swivel= forms.CharField(
label='Swivel Activities',
required=False,
help_text='Are swivel chair activities present?',
widget=forms.Select(choices=CHECK))
process_objectives= forms.CharField(
label='Clear Process Objectives',
required=False,
help_text='Are process outcomes objective and measurable?',
widget=forms.Select(choices=CHECK))
stable_rules= forms.CharField(
label='Stable Rules',
required=False,
help_text='Are the decision rules governing the process stable?',
widget=forms.Select(choices=CHECK))
.....