Exemplo n.º 1
0
    def get_queryset(self):
        objects = Note.objects.all()
        filters = {}

        try:
            self.qualifier = Qualifier.objects.get(qualifier=self.request.GET.get('qual'))
        except Qualifier.DoesNotExist:
            pass
        else:
            filters['qualifier'] = Q(qualifiers=self.qualifier)

        self.language = self.request.GET.get('lang')
        if self.language:
            filters['language'] = Q(language=self.language)

        self.fn_type = self.request.GET.get('type')
        if self.fn_type:
            filters['fn_type'] = Q(fn_type=self.fn_type)

        self.letter = self.request.GET.get('ltr')
        if self.letter == "0-9":
            objects = objects.filter(sort_key__regex=r"^[0-9]")
            # filters['letter'] = Q(sort_key__regex=r"^[0-9]")
        elif self.letter:
            objects = objects.filter(sort_key__startswith=self.letter)
            # filters['letter'] = Q(sort_key__startswith=self.letter)

        self.letters = ["0-9"] + [chr(a) for a in range(ord('a'), ord('z')+1)]

        nobj = objects
        for key, fltr in filters.items():
            if key != 'qualifier':
                nobj = nobj.filter(fltr)
        self.qualifiers = Qualifier.objects.filter(note__in=nobj).distinct()

        nobj = objects
        for key, fltr in filters.items():
            if key != 'language':
                nobj = nobj.filter(fltr)
        self.languages = nobj.order_by('language').values_list(
            'language', flat=True).distinct()
        lang_names = dict(settings.LANGUAGES)
        self.languages = [
            (lang, lang_names.get(LANGUAGES_3TO2.get(lang, lang), lang))
            for lang in self.languages]

        nobj = objects
        for key, fltr in filters.items():
            if key != 'fn_type':
                nobj = nobj.filter(fltr)
        self.fn_types = nobj.order_by('fn_type').values_list(
            'fn_type', flat=True).distinct()
        self.fn_types = [(fnt, FN_TYPES[fnt]) for fnt in self.fn_types]

        for f in filters.values():
            objects = objects.filter(f)

        return objects
Exemplo n.º 2
0
    def build(self, fieldfile):
        from django.core.files.base import ContentFile
        from fnpdjango.utils.text.slughifi import slughifi
        from sortify import sortify
        from librarian import html
        from catalogue.models import Fragment, Tag

        book = fieldfile.instance

        html_output = self.transform(
                        book.wldocument(parse_dublincore=False),
                        fieldfile)

        # Delete old fragments, create from scratch if necessary.
        book.fragments.all().delete()

        if html_output:
            meta_tags = list(book.tags.filter(
                category__in=('author', 'epoch', 'genre', 'kind')))

            lang = book.language
            lang = LANGUAGES_3TO2.get(lang, lang)
            if lang not in [ln[0] for ln in settings.LANGUAGES]:
                lang = None

            fieldfile.save(None, ContentFile(html_output.get_string()),
                    save=False)
            type(book).objects.filter(pk=book.pk).update(**{
                fieldfile.field.attname: fieldfile
            })

            # Extract fragments
            closed_fragments, open_fragments = html.extract_fragments(fieldfile.path)
            for fragment in closed_fragments.values():
                try:
                    theme_names = [s.strip() for s in fragment.themes.split(',')]
                except AttributeError:
                    continue
                themes = []
                for theme_name in theme_names:
                    if not theme_name:
                        continue
                    if lang == settings.LANGUAGE_CODE:
                        # Allow creating themes if book in default language.
                        tag, created = Tag.objects.get_or_create(
                                            slug=slughifi(theme_name),
                                            category='theme')
                        if created:
                            tag.name = theme_name
                            setattr(tag, "name_%s" % lang, theme_name)
                            tag.sort_key = sortify(theme_name.lower())
                            tag.save()
                        themes.append(tag)
                    elif lang is not None:
                        # Don't create unknown themes in non-default languages.
                        try:
                            tag = Tag.objects.get(category='theme',
                                    **{"name_%s" % lang: theme_name})
                        except Tag.DoesNotExist:
                            pass
                        else:
                            themes.append(tag)
                if not themes:
                    continue

                text = fragment.to_string()
                short_text = truncate_html_words(text, 15)
                if text == short_text:
                    short_text = ''
                new_fragment = Fragment.objects.create(anchor=fragment.id,
                        book=book, text=text, short_text=short_text)

                new_fragment.save()
                new_fragment.tags = set(meta_tags + themes)
            book.html_built.send(sender=type(self), instance=book)
            return True
        return False
Exemplo n.º 3
0
    def get_queryset(self):
        objects = Note.objects.all()
        filters = {}

        try:
            self.qualifier = Qualifier.objects.get(
                qualifier=self.request.GET.get('qual'))
        except Qualifier.DoesNotExist:
            pass
        else:
            filters['qualifier'] = Q(qualifiers=self.qualifier)

        self.language = self.request.GET.get('lang')
        if self.language:
            filters['language'] = Q(language=self.language)

        self.fn_type = self.request.GET.get('type')
        if self.fn_type:
            filters['fn_type'] = Q(fn_type=self.fn_type)

        self.letter = self.request.GET.get('ltr')
        if self.letter == "0-9":
            objects = objects.filter(sort_key__regex=r"^[0-9]")
            # filters['letter'] = Q(sort_key__regex=r"^[0-9]")
        elif self.letter:
            objects = objects.filter(sort_key__startswith=self.letter)
            # filters['letter'] = Q(sort_key__startswith=self.letter)

        self.letters = ["0-9"
                        ] + [chr(a) for a in range(ord('a'),
                                                   ord('z') + 1)]

        nobj = objects
        for key, fltr in filters.items():
            if key != 'qualifier':
                nobj = nobj.filter(fltr)
        self.qualifiers = Qualifier.objects.filter(note__in=nobj).distinct()

        nobj = objects
        for key, fltr in filters.items():
            if key != 'language':
                nobj = nobj.filter(fltr)
        self.languages = nobj.order_by('language').values_list(
            'language', flat=True).distinct()
        lang_names = dict(settings.LANGUAGES)
        self.languages = [(lang,
                           lang_names.get(LANGUAGES_3TO2.get(lang, lang),
                                          lang)) for lang in self.languages]

        nobj = objects
        for key, fltr in filters.items():
            if key != 'fn_type':
                nobj = nobj.filter(fltr)
        self.fn_types = nobj.order_by('fn_type').values_list(
            'fn_type', flat=True).distinct()
        self.fn_types = [(fnt, FN_TYPES[fnt]) for fnt in self.fn_types]

        for f in filters.values():
            objects = objects.filter(f)

        return objects
Exemplo n.º 4
0
    def build(self, fieldfile):
        from django.core.files.base import ContentFile
        from fnpdjango.utils.text.slughifi import slughifi
        from sortify import sortify
        from librarian import html
        from catalogue.models import Fragment, Tag

        book = fieldfile.instance

        html_output = self.transform(book.wldocument(parse_dublincore=False),
                                     fieldfile)

        # Delete old fragments, create from scratch if necessary.
        book.fragments.all().delete()

        if html_output:
            meta_tags = list(
                book.tags.filter(category__in=('author', 'epoch', 'genre',
                                               'kind')))

            lang = book.language
            lang = LANGUAGES_3TO2.get(lang, lang)
            if lang not in [ln[0] for ln in settings.LANGUAGES]:
                lang = None

            fieldfile.save(None,
                           ContentFile(html_output.get_string()),
                           save=False)
            type(book).objects.filter(pk=book.pk).update(
                **{fieldfile.field.attname: fieldfile})

            # Extract fragments
            closed_fragments, open_fragments = html.extract_fragments(
                fieldfile.path)
            for fragment in closed_fragments.values():
                try:
                    theme_names = [
                        s.strip() for s in fragment.themes.split(',')
                    ]
                except AttributeError:
                    continue
                themes = []
                for theme_name in theme_names:
                    if not theme_name:
                        continue
                    if lang == settings.LANGUAGE_CODE:
                        # Allow creating themes if book in default language.
                        tag, created = Tag.objects.get_or_create(
                            slug=slughifi(theme_name), category='theme')
                        if created:
                            tag.name = theme_name
                            setattr(tag, "name_%s" % lang, theme_name)
                            tag.sort_key = sortify(theme_name.lower())
                            tag.save()
                        themes.append(tag)
                    elif lang is not None:
                        # Don't create unknown themes in non-default languages.
                        try:
                            tag = Tag.objects.get(
                                category='theme',
                                **{"name_%s" % lang: theme_name})
                        except Tag.DoesNotExist:
                            pass
                        else:
                            themes.append(tag)
                if not themes:
                    continue

                text = fragment.to_string()
                short_text = truncate_html_words(text, 15)
                if text == short_text:
                    short_text = ''
                new_fragment = Fragment.objects.create(anchor=fragment.id,
                                                       book=book,
                                                       text=text,
                                                       short_text=short_text)

                new_fragment.save()
                new_fragment.tags = set(meta_tags + themes)
            book.html_built.send(sender=type(self), instance=book)
            return True
        return False