【发布时间】:2019-10-16 08:08:27
【问题描述】:
所有这些视图都非常相似,我想将它们浓缩为一个视图。
class SummerIntents(TemplateView):
template_name = 'admin/hr/intent_forms/summer_intents.html'
@user_is_staff
def dispatch(self, request, *args, **kwargs):
return super(SummerIntents, self).dispatch(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super(SummerIntents, self).get_context_data(**kwargs)
# These functions are found in util.py
update_date = get_update_date()
active_users = get_active_users(self)
context['summer_info'] = SummerInfo.objects.filter(employee__in=active_users, date_modified__gte=update_date.date, active=True).order_by('position','employee__last_name')
return context
def post(self, request, *args, **kwargs):
context = super(SummerIntents, self).get_context_data(**kwargs)
file_name = "summer_intent_forms"
update_date = get_update_date()
active_users = get_active_users(self)
info = SummerInfo.objects.filter(employee__in=active_users, date_modified__gte=update_date.date, active=True).order_by('position','employee__last_name')
student_intents = get_student_intents(active_users, update_date, 'summer', info)
return intents_to_csv(student_intents, file_name)
class WinterIntents(TemplateView):
template_name = 'admin/hr/intent_forms/winter_intents.html'
@user_is_staff
def dispatch(self, request, *args, **kwargs):
return super(WinterIntents, self).dispatch(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super(WinterIntents, self).get_context_data(**kwargs)
# These functions are found in util.py
update_date = get_update_date()
active_users = get_active_users(self)
context['winter_info'] = WinterInfo.objects.filter(employee__in=active_users, date_modified__gte=update_date.date, active=True).order_by('position','employee__last_name')
return context
def post(self, request, *args, **kwargs):
context = super(WinterIntents, self).get_context_data(**kwargs)
file_name = "winter_intent_forms"
update_date = get_update_date()
active_users = get_active_users(self)
info = WinterInfo.objects.filter(employee__in=active_users, date_modified__gte=update_date.date, active=True).order_by('position','employee__last_name')
student_intents = get_student_intents(active_users, update_date, 'winter', info)
return intents_to_csv(student_intents, file_name)
class FallIntents(TemplateView):
template_name = 'admin/hr/intent_forms/fall_intents.html'
@user_is_staff
def dispatch(self, request, *args, **kwargs):
return super(FallIntents, self).dispatch(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super(FallIntents, self).get_context_data(**kwargs)
# These functions are found in util.py
update_date = get_update_date()
active_users = get_active_users(self)
context['fall_info'] = FallInfo.objects.filter(employee__in=active_users, date_modified__gte=update_date.date, active=True).order_by('position','employee__last_name')
return context
def post(self, request, *args, **kwargs):
context = super(FallIntents, self).get_context_data(**kwargs)
file_name = "fall_intent_forms"
update_date = get_update_date()
active_users = get_active_users(self)
info = FallInfo.objects.filter(employee__in=active_users, date_modified__gte=update_date.date, active=True).order_by('position','employee__last_name')
student_intents = get_student_intents(active_users, update_date, 'fall')
return intents_to_csv(student_intents, file_name, info)
我对它们都继承自一个主要视图持开放态度,但这是否意味着我必须通过上下文传递一些变量?比如update_date和active_users?我不希望在这些方法之上添加另一种方法,特别是因为它们已经很短并且要继承的主视图不会有太大的好处。其次,它们每个都有一个 post 方法,可以在单击按钮时将模板保存为 CSV 文件,我不知道如何压缩它,以便所有按钮仍然正常工作。最后,它们都有不同的模板,我是否也必须将它们压缩到一个文件中?这就是我希望最终做到的。
【问题讨论】:
标签: html django view django-templates django-views