如何将所有这些相似的观点浓缩为一个?

时间:2019-05-30 16:29:48

标签: 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(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文件。我不知道如何进行精简,以便所有按钮仍能正常工作。最后,它们都有不同的模板,我是否也必须将它们压缩到一个文件中?那就是我最终希望做的。

1 个答案:

答案 0 :(得分:1)

我不确定必须“通过上下文传递一些变量”是什么意思,但是继承绝对是解决问题的方法。

即使在每个视图中也要注意一些重复,例如获取可以抽象为单独方法的Info对象。还要注意,如果改为添加django.contrib.auth.mixins.UserPassesTestMixin并定义dispatch,则可以避免定义test_func只是为了装饰它。

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作为接受视图类作为参数的实用程序方法有点奇怪。您确定不应该在视图上使用该方法吗?)