forms.py
1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# coding=utf-8
from django import forms
from django.utils.translation import ugettext_lazy as _
from subjects.models import Subject
from .models import Topic
class TopicForm(forms.ModelForm):
subject = None
def __init__(self, *args, **kwargs):
super(TopicForm, self).__init__(*args, **kwargs)
self.subject = kwargs['initial'].get('subject', None)
def clean_name(self):
name = self.cleaned_data.get('name', '')
repo = self.cleaned_data.get('repository', False)
if self.instance.id:
same_name = self.subject.topic_subject.filter(name__unaccent__iexact = name).exclude(id = self.instance.id).count()
else:
same_name = self.subject.topic_subject.filter(name__unaccent__iexact = name).count()
if same_name > 0:
if repo:
self._errors['name'] = [_('This subject already has a repository')]
else:
self._errors['name'] = [_('This subject already has a topic with this name')]
return ValueError
return name
class Meta:
model = Topic
fields = ['repository', 'name', 'description', 'visible' ]
widgets = {
'description': forms.Textarea,
}