Commit dd6130c3356bcd67e8ece5c3f617bd1a098a0c92

Authored by Felipe Henrique de Almeida Bormann
1 parent ca7c1550

solved update subject date problem, now is not an available option to use

Showing 2 changed files with 76 additions and 2 deletions   Show diff stats
subjects/forms.py
... ... @@ -117,6 +117,80 @@ class CreateSubjectForm(forms.ModelForm):
117 117 return ValueError
118 118 return subscribe_end
119 119  
  120 +
  121 +class UpdateSubjectForm(forms.ModelForm):
  122 + def __init__(self, *args, **kwargs):
  123 + super(UpdateSubjectForm, self).__init__(*args, **kwargs)
  124 +
  125 + if not kwargs['instance'] is None:
  126 + self.initial['tags'] = ", ".join(self.instance.tags.all().values_list("name", flat = True))
  127 +
  128 + # TODO: Define form fields here
  129 + tags = forms.CharField(label = _('Tags'), required = False)
  130 +
  131 + class Meta:
  132 + model = Subject
  133 +
  134 + fields = ('name', 'description_brief', 'description',
  135 + 'visible', 'professor', 'students', )
  136 +
  137 +
  138 + widgets = {
  139 + 'description_brief': forms.Textarea,
  140 + 'description': forms.Textarea,
  141 + 'professor': forms.SelectMultiple,
  142 + 'students': forms.SelectMultiple,
  143 + }
  144 +
  145 + def save(self, commit=True):
  146 + super(UpdateSubjectForm, self).save(commit = True)
  147 +
  148 + self.instance.save()
  149 +
  150 + previous_tags = self.instance.tags.all()
  151 +
  152 + tags = self.cleaned_data['tags'].split(",")
  153 +
  154 + #Excluding unwanted tags
  155 + for prev in previous_tags:
  156 + if not prev.name in tags:
  157 + self.instance.tags.remove(prev)
  158 +
  159 + for tag in tags:
  160 + tag = tag.strip()
  161 +
  162 + exist = Tag.objects.filter(name = tag).exists()
  163 +
  164 + if exist:
  165 + new_tag = Tag.objects.get(name = tag)
  166 + else:
  167 + new_tag = Tag.objects.create(name = tag)
  168 +
  169 + if not new_tag in self.instance.tags.all():
  170 + self.instance.tags.add(new_tag)
  171 +
  172 + return self.instance
  173 +
  174 + def clean(self):
  175 + cleaned_data = super(UpdateSubjectForm, self).clean()
  176 +
  177 + return cleaned_data
  178 +
  179 + def clean_name(self):
  180 + name = self.cleaned_data.get('name')
  181 + if self.instance.id:
  182 + same_name = Subject.objects.filter(name__unaccent__iexact = name).exclude(id = self.instance.id)
  183 + else:
  184 + same_name = Subject.objects.filter(name__unaccent__iexact = name)
  185 +
  186 + if same_name.count() > 0:
  187 + self._errors['name'] = [_('There is another subject with this name, try another one.')]
  188 +
  189 +
  190 + return name
  191 +
  192 +
  193 +
120 194 class CreateTagForm(forms.ModelForm):
121 195 class Meta:
122 196 model = Tag
... ...
subjects/views.py
... ... @@ -25,7 +25,7 @@ from .models import Tag
25 25 import time
26 26 import datetime
27 27 from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
28   -from .forms import CreateSubjectForm
  28 +from .forms import CreateSubjectForm, UpdateSubjectForm
29 29 from .utils import has_student_profile, has_professor_profile, count_subjects, get_category_page
30 30 from users.models import User
31 31  
... ... @@ -349,7 +349,7 @@ class SubjectUpdateView(LoginRequiredMixin, LogMixin, UpdateView):
349 349 log_context = {}
350 350  
351 351 model = Subject
352   - form_class = CreateSubjectForm
  352 + form_class = UpdateSubjectForm
353 353 template_name = 'subjects/update.html'
354 354  
355 355 login_url = reverse_lazy("users:login")
... ...