Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thumbnails - Restore integration #12

Merged
merged 7 commits into from
Sep 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,4 @@ junit/*
!tests/fixtures/template_Isogeo.docx

tests/fixtures/api_search_*.json
tests/fixtures/thumbnails.csv
60 changes: 40 additions & 20 deletions isogeotodocx/isogeo2docx.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

# 3rd party library
from docxtpl import DocxTemplate, InlineImage, RichText, etree
from isogeo_pysdk import Event, Isogeo, IsogeoTranslator, IsogeoUtils, Metadata, Share
from isogeo_pysdk import Event, IsogeoTranslator, IsogeoUtils, Metadata, Share

# custom submodules
from isogeotodocx.utils import Formatter
Expand All @@ -37,6 +37,7 @@ class Isogeo2docx(object):
"""IsogeoToDocx class.

:param str lang: selected language for output
:param dict thumbnails: dictionary of metadatas associated to an image path
:param str url_base_edit: base url to format edit links (basically app.isogeo.com)
:param str url_base_view: base url to format view links (basically open.isogeo.com)
"""
Expand All @@ -45,20 +46,11 @@ def __init__(
self,
lang="FR",
default_values=("NR", "1970-01-01T00:00:00+00:00"),
thumbnails: dict = None,
url_base_edit: str = "https://app.Isogeo.com",
url_base_view: str = "https://open.isogeo.com",
):
"""Common variables for Word processing.

default_values (optional) -- values used to replace missing values.
Must be a tuple with 2 values structure:
(
str_for_missing_strings_and_integers,
str_for_missing_dates
)


"""
"""Processing matching between Isogeo metadata and a Miscrosoft Word template."""
super(Isogeo2docx, self).__init__()

# ------------ VARIABLES ---------------------
Expand Down Expand Up @@ -91,6 +83,13 @@ def __init__(
# FORMATTER
self.fmt = Formatter(output_type="Word")

# THUMBNAILS
if thumbnails is not None and isinstance(thumbnails, dict):
self.thumbnails = thumbnails
else:
self.thumbnails = {}
logger.debug("No valid thumbnails matching table passed.")

# URLS
utils.app_url = url_base_edit # APP
utils.oc_url = url_base_view # OpenCatalog url
Expand Down Expand Up @@ -282,7 +281,6 @@ def md2docx(self, docx_template: DocxTemplate, md: Metadata, share: Share = None

# FILLFULLING THE TEMPLATE #
context = {
# "varThumbnail": InlineImage(docx_template, md.thumbnail),
"varTitle": self.fmt.clean_xml(md.title),
"varAbstract": self.fmt.clean_xml(md.abstract),
"varNameTech": md.name,
Expand Down Expand Up @@ -323,6 +321,14 @@ def md2docx(self, docx_template: DocxTemplate, md: Metadata, share: Share = None
"varEditAPP": link_edit,
}

# -- THUMBNAIL -----------------------------------------------------------------
if md._id in self.thumbnails and Path(self.thumbnails.get(md._id)).is_file():
thumbnail = str(Path(self.thumbnails.get(md._id)).resolve())
context["varThumbnail"] = InlineImage(docx_template, thumbnail)
logger.info(
"Thumbnail found for {}: {}".format(md.title_or_name(1), thumbnail)
)

# fillfull file
try:
docx_template.render(context, autoescape=True)
Expand Down Expand Up @@ -366,9 +372,10 @@ def missing_values(self, idx_type=0):
# ###################################
if __name__ == "__main__":
"""
Standalone execution and tests
Standalone execution and basic tests
"""
# ------------ Specific imports ----------------
from csv import DictReader
from dotenv import load_dotenv
from logging.handlers import RotatingFileHandler
from os import environ
Expand Down Expand Up @@ -406,6 +413,10 @@ def missing_values(self, idx_type=0):
# get user ID as environment variables
load_dotenv("dev.env")

# misc
METADATA_TEST_FIXTURE_UUID = environ.get("ISOGEO_FIXTURES_METADATA_COMPLETE")
WORKGROUP_TEST_FIXTURE_UUID = environ.get("ISOGEO_WORKGROUP_TEST_UUID")

# ignore warnings related to the QA self-signed cert
if environ.get("ISOGEO_PLATFORM").lower() == "qa":
urllib3.disable_warnings()
Expand All @@ -422,10 +433,6 @@ def missing_values(self, idx_type=0):
# getting a token
isogeo.connect()

# misc
METADATA_TEST_FIXTURE_UUID = environ.get("ISOGEO_FIXTURES_METADATA_COMPLETE")
WORKGROUP_TEST_FIXTURE_UUID = environ.get("ISOGEO_WORKGROUP_TEST_UUID")

# ------------ Isogeo search --------------------------
search_results = isogeo.search(
include="all",
Expand All @@ -441,11 +448,24 @@ def missing_values(self, idx_type=0):
Path("_output/").mkdir(exist_ok=True)

# template
template_path = Path(r"tests\fixtures\template_Isogeo.docx")
template_path = Path("tests/fixtures/template_Isogeo.docx")
assert template_path.is_file()

# thumbnails table
thumbnails_table_csv_path = Path("tests/fixtures/thumbnails.csv")
assert thumbnails_table_csv_path.is_file()

# CSV structure
csv_headers = ["isogeo_uuid", "isogeo_title_slugged", "img_abs_path"]
thumbnails_dict = {}
with thumbnails_table_csv_path.open("r", newline="") as csv_thumbnails:
reader = DictReader(csv_thumbnails, fieldnames=csv_headers)
next(reader, None) # skip header line
for row in reader:
thumbnails_dict[row.get("isogeo_uuid")] = row.get("img_abs_path")

# instanciate
toDocx = Isogeo2docx()
toDocx = Isogeo2docx(thumbnails=thumbnails_dict)

# parse results and export it
for md in search_results.results:
Expand Down
Binary file added tests/fixtures/img/isogeo_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/fixtures/img/map_caen_michelin_550x382.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
93 changes: 76 additions & 17 deletions tests/fixturing.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,27 @@
# ##################################

# Standard library
import csv
import json
import logging
from os import environ, path
from os import environ
from pathlib import Path
from random import sample

# 3rd party
from dotenv import load_dotenv
import urllib3

# Isogeo
from isogeo_pysdk import Isogeo
from isogeo_pysdk import Isogeo, Metadata, MetadataSearch

# #############################################################################
# ######## Globals #################
# ##################################

# BASE DIRECTORY - Script is meant to be launched from the root of the repository
BASE_DIR = Path(__file__).parent

# env vars
load_dotenv("dev.env", override=True)

Expand All @@ -49,6 +55,11 @@
# ########## Fixturing ###############
# ####################################

# -- RETRIEVE ISOGEO SEARCH RESULTS ----------------------------------------------------
logger.info(
"Connecting to Isogeo API to download search results to be used by the following test."
)

# instanciating the class
isogeo = Isogeo(
auth_mode="group",
Expand All @@ -59,32 +70,80 @@
)
isogeo.connect()

# Downloading directly from Isogeo API
BASE_DIR = path.dirname(path.abspath(__file__))

# complete search - only Isogeo Tests
out_search_complete_tests = path.join(
BASE_DIR, "fixtures", "api_search_complete_tests.json"
)
if not path.isfile(out_search_complete_tests):
out_search_complete_tests = BASE_DIR / "fixtures" / "api_search_complete_tests.json"

if not out_search_complete_tests.is_file():
request = isogeo.search(
query="owner:{}".format(ISOGEO_WORKGROUP_TEST_UUID),
whole_results=1,
include="all",
augment=1,
)
with open(out_search_complete_tests, "w") as json_basic:
with out_search_complete_tests.open("w") as json_basic:
json.dump(request.to_dict(), json_basic, sort_keys=True)
else:
logging.info("JSON already exists. If you want to update it, delete it first.")
logger.info(
"JSON already exists at: {}. If you want to update it, delete it first.".format(
out_search_complete_tests.resolve()
)
)

# complete search
out_search_complete = path.join(BASE_DIR, "fixtures", "api_search_complete.json")
if not path.isfile(out_search_complete):
out_search_complete = BASE_DIR / "fixtures" / "api_search_complete.json"

if not out_search_complete.is_file():
request = isogeo.search(whole_results=1, include="all", augment=1)
with open(
path.join(BASE_DIR, "fixtures", "api_search_complete.json"), "w"
) as json_basic:
with out_search_complete.open("w") as json_basic:
json.dump(request.to_dict(), json_basic, sort_keys=True)
else:
logging.info("JSON already exists. If you want to update it, delete it first.")
logger.info(
"JSON already exists at: {}. If you want to update it, delete it first.".format(
out_search_complete.resolve()
)
)


# -- BUILD THUMBNAILS TABLE ------------------------------------------------------------
logger.info("Write the thumbnails table to test image insertion into Word export.")

thumbnails_table_out = BASE_DIR / "fixtures" / "thumbnails.csv"

# load previous genereated JSON
with out_search_complete.open("r") as f:
search = json.loads(f.read())
search = MetadataSearch(**search)

fixtures_images = list(Path(BASE_DIR / "fixtures" / "img").iterdir())

with thumbnails_table_out.open("w", newline="") as csvfile:
# CSV structure
csv_headers = ["isogeo_uuid", "isogeo_title_slugged", "img_abs_path"]
# write headers
writer = csv.DictWriter(csvfile, fieldnames=csv_headers)
writer.writeheader()

# pick random metadata
metadatas = sample(search.results, len(fixtures_images))

# parse fixtures images
for image, md in zip(fixtures_images, metadatas):
metadata = Metadata.clean_attributes(md)
writer.writerow(
{
"isogeo_uuid": metadata._id,
"isogeo_title_slugged": metadata.title_or_name(slugged=1),
"img_abs_path": image.resolve(),
}
)

# force a thumbnail for the fixture metadata
writer.writerow(
{
"isogeo_uuid": environ.get("ISOGEO_FIXTURES_METADATA_COMPLETE"),
"isogeo_title_slugged": "ISOGEO FIXTURES METADATA COMPLETE",
"img_abs_path": image.resolve(),
}
)

logger.info("{} thumbnails associated with metadatas.".format(len(fixtures_images) + 1))