Html 我怎样才能把所有这些相似的观点浓缩成一个呢?

Html 我怎样才能把所有这些相似的观点浓缩成一个呢?,html,django,view,django-templates,django-views,Html,Django,View,Django Templates,Django Views,所有这些观点都非常相似,我想把它们浓缩成一个观点 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(requ

所有这些观点都非常相似,我想把它们浓缩成一个观点

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)

我对它们都继承自的一个主视图持开放态度,但这是否意味着我必须通过上下文传递一些变量?例如
更新日期
活动用户
?我不想在这些方法的基础上再添加一个方法,尤其是因为它们已经很短了,要从中继承的主视图不会有太多好处。其次,它们都有一个post方法,在单击按钮时将模板保存为CSV文件,我不知道如何压缩该方法,以便所有按钮仍能正常工作。最后,它们都有不同的模板,我是否也必须将它们压缩成一个文件?这就是我希望最终能做到的。

我不确定“通过上下文传递一些变量”是什么意思,但继承无疑是一种方法

注意,即使在每个视图中,您也有一些重复,例如获取可以抽象为单独方法的信息对象。另外请注意,如果您添加django.contrib.auth.mixin.userpassestestimixin并定义
test_func
,则可以避免定义
dispatch
只是为了修饰它

class Intents(UserPassesTestMixin, TemplateView):

    def test_func(self):
        return self.request.user.is_staff

    def get_template_names(self):
         return 'admin/hr/intent_forms/{}_intents.html'.format(self.season_name)

    def get_info_items(self):
        update_date = get_update_date()
        active_users = get_active_users(self)
        return self.info_model.objects.filter(employee__in=active_users, date_modified__gte=update_date.date, active=True).order_by('position','employee__last_name')

    def get_context_data(self, **kwargs):
        kwargs['{}_info'.format(self.season_name)] = self.get_info_items()
        return super().get_context_data(**kwargs)

    def post(self, request, *args, **kwargs):
        file_name = "{}_intent_forms".format(self.season_name)
        info = self.get_info_items()
        student_intents = get_student_intents(active_users, update_date, self.season_name, info)

        return intents_to_csv(student_intents, file_name)

class SummerIntents(Intents):
    season_name = 'summer'
    info_model = SummerInfo

class WinterIntents(Intents):
    season_name = 'winter'
    info_model = WinterInfo

class FallIntents(Intents):
    season_name = 'fall'
    info_model = FallInfo

(另外,将
获取活动用户
作为接受视图类作为参数的实用方法也有点奇怪。你确定这不应该是视图上的方法吗?

我不确定你所说的“通过上下文传递一些变量”是什么意思,但继承无疑是一种方法

注意,即使在每个视图中,您也有一些重复,例如获取可以抽象为单独方法的信息对象。另外请注意,如果您添加django.contrib.auth.mixin.userpassestestimixin并定义
test_func
,则可以避免定义
dispatch
只是为了修饰它

class Intents(UserPassesTestMixin, TemplateView):

    def test_func(self):
        return self.request.user.is_staff

    def get_template_names(self):
         return 'admin/hr/intent_forms/{}_intents.html'.format(self.season_name)

    def get_info_items(self):
        update_date = get_update_date()
        active_users = get_active_users(self)
        return self.info_model.objects.filter(employee__in=active_users, date_modified__gte=update_date.date, active=True).order_by('position','employee__last_name')

    def get_context_data(self, **kwargs):
        kwargs['{}_info'.format(self.season_name)] = self.get_info_items()
        return super().get_context_data(**kwargs)

    def post(self, request, *args, **kwargs):
        file_name = "{}_intent_forms".format(self.season_name)
        info = self.get_info_items()
        student_intents = get_student_intents(active_users, update_date, self.season_name, info)

        return intents_to_csv(student_intents, file_name)

class SummerIntents(Intents):
    season_name = 'summer'
    info_model = SummerInfo

class WinterIntents(Intents):
    season_name = 'winter'
    info_model = WinterInfo

class FallIntents(Intents):
    season_name = 'fall'
    info_model = FallInfo

(另外,将
get\u active\u users
作为接受视图类作为参数的实用方法有点奇怪。您确定这不应该是视图上的方法吗?

这看起来不错!我怎样才能让它工作呢?因为在复制、粘贴和导入UserPasseStMixin之后,我得到一个错误,即SummerIntents视图没有模板。我必须调用
get\u template\u names
函数吗?编辑:哦,我知道你可以定义方法或var
template\u name
。它仍然需要一个模板,尽管有些事情没有发生,很抱歉,我忘记了让子类从Intents类继承…好吧,在一些测试和更改一些事情之后,它工作了!非常感谢你!这真的帮了大忙。这看起来不错!我怎样才能让它工作呢?因为在复制、粘贴和导入UserPasseStMixin之后,我得到一个错误,即SummerIntents视图没有模板。我必须调用
get\u template\u names
函数吗?编辑:哦,我知道你可以定义方法或var
template\u name
。它仍然需要一个模板,尽管有些事情没有发生,很抱歉,我忘记了让子类从Intents类继承…好吧,在一些测试和更改一些事情之后,它工作了!非常感谢你!这真的很有帮助