【问题标题】:How can I condense all these similar views into one?我怎样才能将所有这些相似的观点浓缩成一个?
【发布时间】: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_dateactive_users?我不希望在这些方法之上添加另一种方法,特别是因为它们已经很短并且要继承的主视图不会有太大的好处。其次,它们每个都有一个 post 方法,可以在单击按钮时将模板保存为 CSV 文件,我不知道如何压缩它,以便所有按钮仍然正常工作。最后,它们都有不同的模板,我是否也必须将它们压缩到一个文件中?这就是我希望最终做到的。

【问题讨论】:

    标签: html django view django-templates django-views


    【解决方案1】:

    我不确定你所说的必须“通过上下文传递一些变量”是什么意思,但继承绝对是这里的方法。

    请注意,即使在每个视图中,您也有一些重复,例如获取可以抽象为单独方法的 Info 对象。另请注意,如果您添加 django.contrib.auth.mixins.UserPassesTestMixin 并定义 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_active_users 作为接受视图类作为参数的实用方法有点奇怪。您确定不应该是视图上的方法吗?)

    【讨论】:

    • 这看起来不错!我怎样才能让它工作?因为在复制、粘贴和导入 UserPassesTestMixin 之后,我收到了 SummerIntents 视图没有模板的错误消息。我必须调用get_template_names 函数吗?编辑:哦,我知道您可以定义方法或 var template_names。它仍然需要一个模板,所以没有发生任何事情
    • 对不起,我忘了让子类实际上继承自 Intents 类...
    • 好的,经过一些测试和改变它的工作!太感谢了!这真的很有帮助
    猜你喜欢
    • 1970-01-01
    • 2020-01-01
    • 1970-01-01
    • 2022-01-24
    • 2021-11-13
    • 2022-11-09
    • 1970-01-01
    • 1970-01-01
    • 2011-01-29
    相关资源
    最近更新 更多