Пример #1
0
    def mediapage(self, report, title, media_handle, info):
        """
        Generate and output an individual Media page.

        @param: report       -- The instance of the main report class
                                for this report
        @param: title        -- Is the title of the web page
        @param: media_handle -- The media handle to use
        @param: info         -- A tuple containing the media handle for the
                                next and previous media, the current page
                                number, and the total number of media pages
        """
        media = report.database.get_media_from_handle(media_handle)
        BasePage.__init__(self, report, title, media.gramps_id)
        (prev, next_, page_number, total_pages) = info

        ldatec = media.get_change_time()

        # get media rectangles
        _region_items = self.media_ref_rect_regions(media_handle)

        output_file, sio = self.report.create_file(media_handle, "img")
        self.uplink = True

        self.bibli = Bibliography()

        # get media type to be used primarily with "img" tags
        mime_type = media.get_mime_type()
        #mtype = get_description(mime_type)

        if mime_type:
            #note_only = False
            newpath = self.copy_source_file(media_handle, media)
            target_exists = newpath is not None
        else:
            #note_only = True
            target_exists = False

        self.copy_thumbnail(media_handle, media)
        self.page_title = media.get_description()
        esc_page_title = html_escape(self.page_title)
        (mediapage, head,
         body) = self.write_header("%s - %s" % (self._("Media"),
                                                self.page_title))

        # if there are media rectangle regions, attach behaviour style sheet
        if _region_items:

            fname = "/".join(["css", "behaviour.css"])
            url = self.report.build_url_fname(fname, None, self.uplink)
            head += Html("link", href=url, type="text/css",
                         media="screen", rel="stylesheet")

        # begin MediaDetail division
        with Html("div", class_="content", id="GalleryDetail") as mediadetail:
            body += mediadetail

            # media navigation
            with Html("div", id="GalleryNav", role="navigation") as medianav:
                mediadetail += medianav
                if prev:
                    medianav += self.media_nav_link(prev,
                                                    self._("Previous"), True)
                data = self._('%(strong1_strt)s%(page_number)d%(strong_end)s '
                              'of %(strong2_strt)s%(total_pages)d%(strong_end)s'
                             ) % {'strong1_strt' :
                                  '<strong id="GalleryCurrent">',
                                  'strong2_strt' : '<strong id="GalleryTotal">',
                                  'strong_end' : '</strong>',
                                  'page_number' : page_number,
                                  'total_pages' : total_pages}
                medianav += Html("span", data, id="GalleryPages")
                if next_:
                    medianav += self.media_nav_link(next_, self._("Next"), True)

            # missing media error message
            errormsg = self._("The file has been moved or deleted.")

            # begin summaryarea division
            with Html("div", id="summaryarea") as summaryarea:
                mediadetail += summaryarea
                if mime_type:
                    if mime_type.startswith("image"):
                        if not target_exists:
                            with Html("div", id="MediaDisplay") as mediadisplay:
                                summaryarea += mediadisplay
                                mediadisplay += Html("span", errormsg,
                                                     class_="MissingImage")

                        else:
                            # Check how big the image is relative to the
                            # requested 'initial' image size.
                            # If it's significantly bigger, scale it down to
                            # improve the site's responsiveness. We don't want
                            # the user to have to await a large download
                            # unnecessarily. Either way, set the display image
                            # size as requested.
                            orig_image_path = media_path_full(self.r_db,
                                                              media.get_path())
                            #mtime = os.stat(orig_image_path).st_mtime
                            (width, height) = image_size(orig_image_path)
                            max_width = self.report.options[
                                'maxinitialimagewidth']
                            max_height = self.report.options[
                                'maxinitialimageheight']
                            if width != 0 and height != 0:
                                scale_w = (float(max_width)/width) or 1
                                           # the 'or 1' is so that a max of
                                           # zero is ignored
                                scale_h = (float(max_height)/height) or 1
                            else:
                                scale_w = 1.0
                                scale_h = 1.0
                            scale = min(scale_w, scale_h, 1.0)
                            new_width = int(width*scale)
                            new_height = int(height*scale)

                            # TODO. Convert disk path to URL.
                            url = self.report.build_url_fname(orig_image_path,
                                                              None, self.uplink)
                            with Html("div", id="GalleryDisplay",
                                      style='width: %dpx; height: %dpx' % (
                                          new_width,
                                          new_height)) as mediadisplay:
                                summaryarea += mediadisplay

                                # Feature #2634; display the mouse-selectable
                                # regions. See the large block at the top of
                                # this function where the various regions are
                                # stored in _region_items
                                if _region_items:
                                    ordered = Html("ol", class_="RegionBox")
                                    mediadisplay += ordered
                                    while len(_region_items) > 0:
                                        (name, coord_x, coord_y,
                                         width, height, linkurl
                                        ) = _region_items.pop()
                                        ordered += Html(
                                            "li",
                                            style="left:%d%%; "
                                                  "top:%d%%; "
                                                  "width:%d%%; "
                                                  "height:%d%%;" % (
                                                      coord_x, coord_y,
                                                      width, height)) + (
                                                          Html("a", name,
                                                               href=linkurl)
                                                          )

                                # display the image
                                if orig_image_path != newpath:
                                    url = self.report.build_url_fname(
                                        newpath, None, self.uplink)
                                mediadisplay += Html("a", href=url) + (
                                    Html("img", width=new_width,
                                         height=new_height, src=url,
                                         alt=esc_page_title)
                                )
                    else:
                        dirname = tempfile.mkdtemp()
                        thmb_path = os.path.join(dirname, "document.png")
                        if run_thumbnailer(mime_type,
                                           media_path_full(self.r_db,
                                                           media.get_path()),
                                           thmb_path, 320):
                            try:
                                path = self.report.build_path(
                                    "preview", media.get_handle())
                                npath = os.path.join(path, media.get_handle())
                                npath += ".png"
                                self.report.copy_file(thmb_path, npath)
                                path = npath
                                os.unlink(thmb_path)
                            except EnvironmentError:
                                path = os.path.join("images", "document.png")
                        else:
                            path = os.path.join("images", "document.png")
                        os.rmdir(dirname)

                        with Html("div", id="GalleryDisplay") as mediadisplay:
                            summaryarea += mediadisplay

                            img_url = self.report.build_url_fname(path,
                                                                  None,
                                                                  self.uplink)
                            if target_exists:
                                # TODO. Convert disk path to URL
                                url = self.report.build_url_fname(newpath,
                                                                  None,
                                                                  self.uplink)
                                hyper = Html("a", href=url,
                                             title=esc_page_title) + (
                                                 Html("img", src=img_url,
                                                      alt=esc_page_title)
                                                 )
                                mediadisplay += hyper
                            else:
                                mediadisplay += Html("span", errormsg,
                                                     class_="MissingImage")
                else:
                    with Html("div", id="GalleryDisplay") as mediadisplay:
                        summaryarea += mediadisplay
                        url = self.report.build_url_image("document.png",
                                                          "images", self.uplink)
                        mediadisplay += Html("img", src=url,
                                             alt=esc_page_title,
                                             title=esc_page_title)

                # media title
                title = Html("h3", html_escape(self.page_title.strip()),
                             inline=True)
                summaryarea += title

                # begin media table
                with Html("table", class_="infolist gallery") as table:
                    summaryarea += table

                    # Gramps ID
                    media_gid = media.gramps_id
                    if not self.noid and media_gid:
                        trow = Html("tr") + (
                            Html("td", self._("Gramps ID"),
                                 class_="ColumnAttribute",
                                 inline=True),
                            Html("td", media_gid, class_="ColumnValue",
                                 inline=True)
                            )
                        table += trow

                    # mime type
                    if mime_type:
                        trow = Html("tr") + (
                            Html("td", self._("File Type"),
                                 class_="ColumnAttribute",
                                 inline=True),
                            Html("td", mime_type, class_="ColumnValue",
                                 inline=True)
                            )
                        table += trow

                    # media date
                    date = media.get_date_object()
                    if date and date is not Date.EMPTY:
                        trow = Html("tr") + (
                            Html("td", self._("Date"), class_="ColumnAttribute",
                                 inline=True),
                            Html("td", self.rlocale.get_date(date),
                                 class_="ColumnValue",
                                 inline=True)
                            )
                        table += trow

            # get media notes
            notelist = self.display_note_list(media.get_note_list())
            if notelist is not None:
                mediadetail += notelist

            # get attribute list
            attrlist = media.get_attribute_list()
            if attrlist:
                attrsection, attrtable = self.display_attribute_header()
                self.display_attr_list(attrlist, attrtable)
                mediadetail += attrsection

            # get media sources
            srclist = self.display_media_sources(media)
            if srclist is not None:
                mediadetail += srclist

            # get media references
            reflist = self.display_bkref_list(Media, media_handle)
            if reflist is not None:
                mediadetail += reflist

        # add clearline for proper styling
        # add footer section
        footer = self.write_footer(ldatec)
        body += (FULLCLEAR, footer)

        # send page out for processing
        # and close the file
        self.xhtml_writer(mediapage, output_file, sio, ldatec)
Пример #2
0
    def mediapage(self, report, title, media_handle, info):
        """
        Generate and output an individual Media page.

        @param: report       -- The instance of the main report class
                                for this report
        @param: title        -- Is the title of the web page
        @param: media_handle -- The media handle to use
        @param: info         -- A tuple containing the media handle for the
                                next and previous media, the current page
                                number, and the total number of media pages
        """
        media = report.database.get_media_from_handle(media_handle)
        BasePage.__init__(self, report, title, media.gramps_id)
        (prev, next_, page_number, total_pages) = info

        ldatec = media.get_change_time()

        # get media rectangles
        _region_items = self.media_ref_rect_regions(media_handle)

        output_file, sio = self.report.create_file(media_handle, "img")
        self.uplink = True

        self.bibli = Bibliography()

        # get media type to be used primarily with "img" tags
        mime_type = media.get_mime_type()

        if mime_type:
            newpath = self.copy_source_file(media_handle, media)
            target_exists = newpath is not None
        else:
            target_exists = False

        self.copy_thumbnail(media_handle, media)
        self.page_title = media.get_description()
        esc_page_title = html_escape(self.page_title)
        result = self.write_header("%s - %s" %
                                   (self._("Media"), self.page_title))
        mediapage, head, dummy_body, outerwrapper = result

        # if there are media rectangle regions, attach behaviour style sheet
        if _region_items:

            fname = "/".join(["css", "behaviour.css"])
            url = self.report.build_url_fname(fname, None, self.uplink)
            head += Html("link",
                         href=url,
                         type="text/css",
                         media="screen",
                         rel="stylesheet")

        # begin MediaDetail division
        with Html("div", class_="content", id="GalleryDetail") as mediadetail:
            outerwrapper += mediadetail

            # media navigation
            with Html("div", id="GalleryNav", role="navigation") as medianav:
                mediadetail += medianav
                if prev:
                    medianav += self.media_nav_link(prev, self._("Previous"),
                                                    True)
                data = self._(
                    '%(strong1_strt)s%(page_number)d%(strong_end)s '
                    'of %(strong2_strt)s%(total_pages)d%(strong_end)s') % {
                        'strong1_strt': '<strong id="GalleryCurrent">',
                        'strong2_strt': '<strong id="GalleryTotal">',
                        'strong_end': '</strong>',
                        'page_number': page_number,
                        'total_pages': total_pages
                    }
                medianav += Html("span", data, id="GalleryPages")
                if next_:
                    medianav += self.media_nav_link(next_, self._("Next"),
                                                    True)

            # missing media error message
            errormsg = self._("The file has been moved or deleted.")

            # begin summaryarea division
            with Html("div", id="summaryarea") as summaryarea:
                mediadetail += summaryarea
                if mime_type:
                    if mime_type.startswith("image"):
                        if not target_exists:
                            with Html("div",
                                      id="MediaDisplay") as mediadisplay:
                                summaryarea += mediadisplay
                                mediadisplay += Html("span",
                                                     errormsg,
                                                     class_="MissingImage")

                        else:
                            # Check how big the image is relative to the
                            # requested 'initial' image size.
                            # If it's significantly bigger, scale it down to
                            # improve the site's responsiveness. We don't want
                            # the user to have to await a large download
                            # unnecessarily. Either way, set the display image
                            # size as requested.
                            orig_image_path = media_path_full(
                                self.r_db, media.get_path())
                            (width, height) = image_size(orig_image_path)
                            max_width = self.report.options[
                                'maxinitialimagewidth']

                            # TODO. Convert disk path to URL.
                            url = self.report.build_url_fname(
                                orig_image_path, None, self.uplink)
                            with Html("div",
                                      id="GalleryDisplay",
                                      style='max-width: %dpx; height: auto' %
                                      (max_width)) as mediadisplay:
                                summaryarea += mediadisplay

                                # Feature #2634; display the mouse-selectable
                                # regions. See the large block at the top of
                                # this function where the various regions are
                                # stored in _region_items
                                if _region_items:
                                    ordered = Html("ol", class_="RegionBox")
                                    mediadisplay += ordered
                                    while _region_items:
                                        (name, coord_x, coord_y, width, height,
                                         linkurl) = _region_items.pop()
                                        ordered += Html(
                                            "li",
                                            style="left:%d%%; "
                                            "top:%d%%; "
                                            "width:%d%%; "
                                            "height:%d%%;" %
                                            (coord_x, coord_y, width, height)
                                        ) + (Html("a", name, href=linkurl))

                                # display the image
                                if orig_image_path != newpath:
                                    url = self.report.build_url_fname(
                                        newpath, None, self.uplink)
                                s_width = 'width: %dpx;' % max_width
                                mediadisplay += Html("a", href=url) + (Html(
                                    "img",
                                    src=url,
                                    style=s_width,
                                    alt=esc_page_title))
                    else:
                        dirname = tempfile.mkdtemp()
                        thmb_path = os.path.join(dirname, "document.png")
                        if run_thumbnailer(
                                mime_type,
                                media_path_full(self.r_db, media.get_path()),
                                thmb_path, 320):
                            try:
                                path = self.report.build_path(
                                    "preview", media.get_handle())
                                npath = os.path.join(path, media.get_handle())
                                npath += ".png"
                                self.report.copy_file(thmb_path, npath)
                                path = npath
                                os.unlink(thmb_path)
                            except EnvironmentError:
                                path = os.path.join("images", "document.png")
                        else:
                            path = os.path.join("images", "document.png")
                        os.rmdir(dirname)

                        with Html("div", id="GalleryDisplay") as mediadisplay:
                            summaryarea += mediadisplay

                            img_url = self.report.build_url_fname(
                                path, None, self.uplink)
                            if target_exists:
                                # TODO. Convert disk path to URL
                                url = self.report.build_url_fname(
                                    newpath, None, self.uplink)
                                s_width = 'width: 48px;'
                                hyper = Html(
                                    "a", href=url, title=esc_page_title) + (
                                        Html("img",
                                             src=img_url,
                                             style=s_width,
                                             alt=esc_page_title))
                                mediadisplay += hyper
                            else:
                                mediadisplay += Html("span",
                                                     errormsg,
                                                     class_="MissingImage")
                else:
                    with Html("div", id="GalleryDisplay") as mediadisplay:
                        summaryarea += mediadisplay
                        url = self.report.build_url_image(
                            "document.png", "images", self.uplink)
                        s_width = 'width: 48px;'
                        mediadisplay += Html("img",
                                             src=url,
                                             style=s_width,
                                             alt=esc_page_title,
                                             title=esc_page_title)

                # media title
                title = Html("h3",
                             html_escape(self.page_title.strip()),
                             inline=True)
                summaryarea += title

                # begin media table
                with Html("table", class_="infolist gallery") as table:
                    summaryarea += table

                    # Gramps ID
                    media_gid = media.gramps_id
                    if not self.noid and media_gid:
                        trow = Html("tr") + (Html("td",
                                                  self._("Gramps ID"),
                                                  class_="ColumnAttribute",
                                                  inline=True),
                                             Html("td",
                                                  media_gid,
                                                  class_="ColumnValue",
                                                  inline=True))
                        table += trow

                    # mime type
                    if mime_type:
                        trow = Html("tr") + (Html("td",
                                                  self._("File Type"),
                                                  class_="ColumnAttribute",
                                                  inline=True),
                                             Html("td",
                                                  mime_type,
                                                  class_="ColumnValue",
                                                  inline=True))
                        table += trow

                    # media date
                    date = media.get_date_object()
                    if date and date is not Date.EMPTY:
                        trow = Html("tr") + (Html("td",
                                                  self._("Date"),
                                                  class_="ColumnAttribute",
                                                  inline=True),
                                             Html("td",
                                                  self.rlocale.get_date(date),
                                                  class_="ColumnValue",
                                                  inline=True))
                        table += trow

            # get media notes
            notelist = self.display_note_list(media.get_note_list(), Media)
            if notelist is not None:
                mediadetail += notelist

            # get attribute list
            attrlist = media.get_attribute_list()
            if attrlist:
                attrsection, attrtable = self.display_attribute_header()
                self.display_attr_list(attrlist, attrtable)
                mediadetail += attrsection

            # get media sources
            srclist = self.display_media_sources(media)
            if srclist is not None:
                mediadetail += srclist

            # get media references
            reflist = self.display_bkref_list(Media, media_handle)
            if reflist is not None:
                mediadetail += reflist

        # add clearline for proper styling
        # add footer section
        footer = self.write_footer(ldatec)
        outerwrapper += (FULLCLEAR, footer)

        # send page out for processing
        # and close the file
        self.xhtml_writer(mediapage, output_file, sio, ldatec)