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())
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
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())