Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

from django import forms 

from django.core.exceptions import ValidationError 

 

from common.models import Chapter 

from fanfics.widgets.widget import SelectCustomWidget 

 

 

class ChapterChangeForm(forms.ModelForm): 

class Meta: 

model = Chapter 

exclude = ('id', 'fanfic') 

 

def clean(self): 

super(ChapterChangeForm, self).clean() 

clean_number_of_chapter(self) 

return self.cleaned_data 

 

def validate_unique(self): 

exclude = self._get_validation_exclusions() 

exclude.remove( 

'fanfic') # allow checking against the missing attribute 

try: 

self.instance.validate_unique(exclude=exclude) 

except ValidationError as e: 

self._update_errors(e.message_dict) 

 

 

class ChapterAddForm(forms.ModelForm): 

class Meta: 

model = Chapter 

exclude = ('id',) 

widgets = { 

'fanfic': SelectCustomWidget(attrs={'class': 'custom-select'}) 

} 

 

def clean(self): 

super(ChapterAddForm, self).clean() 

clean_number_of_chapter(self) 

 

return self.cleaned_data 

 

 

def clean_number_of_chapter(self): 

""" Clean the number of the chapter """ 

try: 

chapter = self.cleaned_data.get('num_chapter') 

chapter = int(chapter) 

 

if chapter <= 0: 

self.add_error( 

'num_chapter', "The number of the chapter has to be greater " 

"than zero.") 

except Exception: 

self.add_error('num_chapter', "The value is not valid")