예제 #1
0
    def save(self, author_form):
        """Convert the string 'page' input into an integer (and set in_preface
        accordingly)."""
        section = super(SectionForm, self).save(commit=False)

        if section.book_id:
            # Clear the authors just in case they've changed.
            section.authors.clear()
        else:
            # We're creating the section for the first time.
            section.book = self.book

        page_number, in_preface = get_page_details(section.page_number)

        if section.rating is None:
            section.rating = 0

        section.page_number = page_number
        section.in_preface = in_preface
        section.save()
        self.save_m2m()

        # Set the authors according to author_form. If the mode is 'none', we
        # don't need to do anything since the section has no authors by
        # default.
        if author_form.cleaned_data['mode'] == 'default' and section.book.details:
            section.authors.add(*section.book.details.default_authors.all())
        elif author_form.cleaned_data['mode'] == 'custom':
            section.authors.add(*author_form.cleaned_data['authors'])

        return section
예제 #2
0
    def save(self, author_form, book=None):
        """Convert the string 'page' input into an integer (and set in_preface
        accordingly)."""
        # This will blank out the authors. We need to add them back later.
        note = super(NoteForm, self).save(commit=False)
        if note.book_id:
            # Clear the authors in case they've changed.
            note.authors.clear()
        else:
            note.book = self.book

        page_number, in_preface = get_page_details(note.page_number)
        note.page_number = page_number
        note.in_preface = in_preface

        section = self.cleaned_data.get('section')
        if section:
            note.section = section
        else:
            note.section = note.determine_section()

        note.save()
        self.save_m2m()

        if author_form.cleaned_data['mode'] == 'default':
            note.set_default_authors()
        elif author_form.cleaned_data['mode'] == 'custom':
            note.authors.add(*author_form.cleaned_data['authors'])

        return note
예제 #3
0
파일: forms.py 프로젝트: kznmft/bookmarker
    def save(self, author_form, term=None):
        """Convert the string 'page' input into an integer (and set in_preface
        accordingly)."""
        occurrence = super(TermOccurrenceForm, self).save(commit=False)

        if occurrence.book_id:
            occurrence.authors.clear()
        else:
            occurrence.book = self.book
            occurrence.term = term

        page_number, in_preface = get_page_details(occurrence.page_number)
        occurrence.page_number = page_number
        occurrence.in_preface = in_preface

        section = self.cleaned_data.get('section')
        if section:
            occurrence.section = section
        else:
            occurrence.section = occurrence.determine_section()

        occurrence.save()
        self.save_m2m()

        if author_form.cleaned_data['mode'] == 'default':
            occurrence.set_default_authors()
        elif author_form.cleaned_data['mode'] == 'custom':
            occurrence.authors.add(*author_form.cleaned_data['authors'])

        return occurrence
예제 #4
0
    def clean_sections(self):
        data = self.cleaned_data['sections']
        sections = []
        for line in data.splitlines():
            words = line.strip().split()
            if not words:
                # Skip empty lines and whitespace-only lines
                continue

            title = ' '.join(words[:-1])
            if not title:
                raise forms.ValidationError(
                    "Invalid line: {}".format(line)
                )

            try:
                page_number, in_preface = get_page_details(words[-1])
            except ValueError:
                raise forms.ValidationError(
                    "Invalid number: {}".format(words[-1])
            )

            sections.append({
                'title': title,
                'page_number': page_number,
                'in_preface': in_preface,
                'number': None,
            })

        # Check if any of the titles should be converted into section numbers.
        number = 1
        for i, section in enumerate(sections):
            # If the first 3 chapters don't have numbers, stop checking
            if number == 1 and i == 3:
                break

            number_string = '{}.'.format(number)
            if section['title'].startswith(number_string):
                section['title'] = section['title'][len(number_string):]
                section['number'] = number
                number += 1

        return sections
예제 #5
0
 def test_non_roman_numerals(self):
     self.assertEqual(get_page_details('1'), (1, False))
     self.assertEqual(get_page_details('100'), (100, False))
     self.assertEqual(get_page_details('1234'), (1234, False))
예제 #6
0
 def test_roman_numerals(self):
     self.assertEqual(get_page_details('vii'), (7, True))
     self.assertEqual(get_page_details('viii'), (8, True))
     self.assertEqual(get_page_details('i'), (1, True))