Ejemplo n.º 1
0
class GroupModuleEditView(TemplateLoginRequiredMixin):
    template_name = 'administrator/maintenance/group_module/group_module_add.html'

    def __init__(self, **kwargs):
        self.group_module = None
        self.form_group_module = None
        self.form_groupmodule_submodule = None
        super().__init__(**kwargs)

    def get(self, request, *args, **kwargs):
        group_module_pk = request.GET['group_module_pk']
        module = request.GET['module']
        self.group_module = GroupModule.current.get(pk=group_module_pk)
        self.form_group_module = GroupModuleForm(
            auto_id='id_group_module_%s', instance=self.group_module)
        self.sub_module_count = SubModule.current.filter(
            module__text=module).count()
        self.form_groupmodule_submodule = GroupSubmoduleFormSet(
            instance=self.group_module, prefix='groupsubmodule_prefix')

        return super().render_to_response(self.get_context_data())

    def post(self, request, *args, **kwargs):
        data = loads(request.body.decode('utf-8'))
        data_group_module_pk = data['form_pk']
        data_formset = data['formset']
        module = data['module']
        self.group_module = GroupModule.current.get(pk=data_group_module_pk)
        self.form_group_module = GroupModuleForm(
            auto_id='id_group_module_%s', instance=self.group_module)
        self.sub_module_count = SubModule.current.filter(
            module__text=module).count()
        self.form_groupmodule_submodule = GroupSubmoduleFormSet(
            data_formset, prefix='groupsubmodule_prefix',
            instance=self.group_module)
        if self.form_groupmodule_submodule.is_valid():
            instances = self.form_groupmodule_submodule.save(commit=False)
            for instance in instances:
                instance.save()
            self.form_groupmodule_submodule = GroupSubmoduleFormSet(
                prefix='groupsubmodule_prefix', instance=self.group_module)
            messages.success(request, core_constants.MESSAGE_TAGS['success'])
        else:
            self.form_groupmodule_submodule = GroupSubmoduleFormSet(
                prefix='groupsubmodule_prefix', instance=self.group_module)
            messages.error(request, core_constants.MESSAGE_TAGS['error'])
        return super().render_to_response(self.get_context_data())

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['form_group_module'] = self.form_group_module
        context['formset_submoduleform'] = self.form_groupmodule_submodule
        # context['group_module_exist'] = self.group_module_exist
        context['count_sub_module'] = self.sub_module_count
        context['form_pk'] = self.group_module.id
        context['btn_edit'] = core_constants.BTN_EDIT
        return context
Ejemplo n.º 2
0
class GroupModuleView(TemplateLoginRequiredMixin):
    template_name = 'administrator/maintenance/group_module/group_module.html'

    def __init__(self, **kwargs):
        self.form_groupmodule_submodule = None
        self.form_group_module = None
        self.sub_module_count = None
        self.group_module_all = None
        self.group_module_exist = True
        super().__init__(**kwargs)

    def dispatch(self, request, *args, **kwargs):
        self.group_module_all = GroupSubModule.current.prefetch_related(
            'group_module')
        return super().dispatch(request, *args, **kwargs)

    def get(self, request, *args, **kwargs):
        self.form_group_module = GroupModuleForm(auto_id='id_group_module_%s')
        return super().render_to_response(self.get_context_data())

    def post(self, request, *args, **kwargs):
        total_forms = int(request.POST['groupsubmodule_prefix-TOTAL_FORMS'])
        module = request.POST.get('module')
        self.sub_module_count = SubModule.current.filter(module=module).count()
        initial_list = list()
        for _ in range(total_forms):
            initial_list.append(dict(module=module))
        self.form_group_module = GroupModuleForm(
            request.POST, auto_id='id_group_module_%s')
        self.form_groupmodule_submodule = GroupSubmoduleFormSet(
            request.POST, prefix='groupsubmodule_prefix', initial=initial_list)
        self.group_module_exist = False
        if self.form_group_module.is_valid() and self.form_groupmodule_submodule.is_valid():
            try:
                with transaction.atomic():
                    group_module = self.form_group_module.save(commit=False)
                    self.form_groupmodule_submodule = GroupSubmoduleFormSet(
                        request.POST, instance=group_module,
                        initial=initial_list,
                        prefix='groupsubmodule_prefix')
                    if self.form_groupmodule_submodule.is_valid():
                        group_module.save()
                        self.form_groupmodule_submodule.save()
                        self.form_group_module = GroupModuleForm(
                            auto_id='id_group_module_%s')
                self.group_module_exist = True
                messages.success(request,
                                 core_constants.MESSAGE_TAGS['success'])
            except IntegrityError:
                messages.warning(request,
                                 core_constants.msg_sub_module_duplicate)

        else:
            messages.warning(request, core_constants.MESSAGE_TAGS['warning'])
        return super().render_to_response(self.get_context_data())

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['form_group_module'] = self.form_group_module
        context['list_group_module'] = self.group_module_all
        context['formset_submoduleform'] = self.form_groupmodule_submodule
        context['group_module_exist'] = self.group_module_exist
        context['count_sub_module'] = self.sub_module_count
        return context