class GroupView(TemplateLoginRequiredMixin): template_name = 'administrator/maintenance/groups/group.html' def __init__(self, **kwargs): self.form_group = None self.group_all = None super().__init__(**kwargs) def dispatch(self, request, *args, **kwargs): self.group_all = GroupState.current.all().prefetch_related('group') return super().dispatch(request, *args, **kwargs) def get(self, request, *args, **kwargs): self.form_group = GroupForm(auto_id='id_group_%s') return super().render_to_response(self.get_context_data()) def post(self, request, *args, **kwargs): self.form_group = GroupForm(request.POST, auto_id='id_group_%s') if self.form_group.is_valid(): self.form_group.save() messages.success(request, core_constants.MESSAGE_TAGS['success']) 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'] = self.form_group context['list_group'] = self.group_all return context
class GroupEditView(TemplateLoginRequiredMixin): template_name = 'administrator/maintenance/groups/group_add.html' def __init__(self, **kwargs): self.form_group = None self.group = None super().__init__(**kwargs) def get(self, request, *args, **kwargs): group = request.GET['group_id'] self.group = Group.objects.get(pk=group) self.form_group = GroupForm(auto_id='id_group_%s', instance=self.group) return super().render_to_response(self.get_context_data()) def post(self, request, *args, **kwargs): data = loads(request.body.decode('utf-8')) data_group_pk = data['form_pk'] data_form_group = data['form'] self.group = Group.objects.get(pk=data_group_pk) self.form_group = GroupForm( data_form_group, auto_id='id_group_%s', instance=self.group) if self.form_group.is_valid(): self.form_group.save(commit=False) self.form_group = GroupForm(auto_id='id_group_%s') messages.success(request, core_constants.MESSAGE_TAGS['success']) else: 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'] = self.form_group context['form_pk'] = self.group.id context['btn_edit'] = core_constants.BTN_EDIT return context