From 82c655af89acacb0ae56d2d36e5af5a8979b59ef Mon Sep 17 00:00:00 2001 From: Leonardo Rossi Date: Thu, 18 Feb 2016 17:26:36 +0100 Subject: [PATCH] examples: print record details addition * Modifies the example app to show the record inside a template. Signed-off-by: Leonardo Rossi --- .editorconfig | 1 + .gitignore | 9 +++ MANIFEST.in | 4 + examples/app.py | 122 ++++++++++++++++++++++++++--- examples/static/app/scss/app.scss | 25 ++++++ examples/templates/app/base.html | 40 ++++++++++ examples/templates/app/detail.html | 116 +++++++++++++++++++++++++++ requirements-devel.txt | 1 + setup.cfg | 6 +- 9 files changed, 311 insertions(+), 13 deletions(-) create mode 100644 examples/static/app/scss/app.scss create mode 100644 examples/templates/app/base.html create mode 100644 examples/templates/app/detail.html diff --git a/.editorconfig b/.editorconfig index bbd9375..00b6236 100644 --- a/.editorconfig +++ b/.editorconfig @@ -37,6 +37,7 @@ charset = utf-8 indent_size = 4 # isort plugin configuration known_first_party = invenio_marc21 +known_third_party = dojson multi_line_output = 2 default_section = THIRDPARTY diff --git a/.gitignore b/.gitignore index 5872d50..a77fe7d 100644 --- a/.gitignore +++ b/.gitignore @@ -57,3 +57,12 @@ docs/_build/ # PyBuilder target/ + +# example app +examples/static/gen/ +examples/static/images/ +examples/static/js/ +examples/static/node_modules/ +examples/static/package.json +examples/static/scss +examples/static/.webassets-cache/ diff --git a/MANIFEST.in b/MANIFEST.in index f4017bc..f8f57ef 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -41,3 +41,7 @@ recursive-include invenio_marc21 *.html recursive-include invenio_marc21 *.po *.pot *.mo recursive-include invenio_marc21 *.json recursive-include tests *.py +recursive-include examples *.html +recursive-exclude examples/static/gen * +recursive-exclude examples/static/node_modules * +recursive-include examples/static/app * diff --git a/examples/app.py b/examples/app.py index bf76b50..6f47931 100644 --- a/examples/app.py +++ b/examples/app.py @@ -23,34 +23,136 @@ # as an Intergovernmental Organization or submit itself to any jurisdiction. -"""Minimal Flask application example for development. +r"""Minimal Flask application example for development. -Run example development server: +Usage: -.. code-block:: console +1. Install invenio-marc21: + + .. code-block:: console + + cdvirtualenv src/invenio-marc21 + pip install -e .[all] + pip install invenio-theme==1.0.0a9 + pip install invenio-assets==1.0.0a4 + +2. Create database and tables: + + .. code-block:: console + + $ cd examples + $ flask -a app.py db init + $ flask -a app.py db create + +You can find the database in `examples/app.db`. + +3. Load demo records from invenio-records (see +invenio_records/data/marc21/bibliographic.xml): + + .. code-block:: console + + $ wget "https://github.com/inveniosoftware/invenio-records/raw/\ + master/invenio_records/data/marc21/bibliographic.xml" + $ dojson -i bibliographic.xml -l marcxml do marc21 | \ + flask -a app.py records create + $ flask -a app.py fixtures records + +4. Download javascript and css libraries: + + .. code-block:: console + + flask -a app.py npm + cd static + npm install + cd .. + npm install -g node-sass clean-css requirejs uglify-js + +5. Collect static files and build bundles + + .. code-block:: console + + flask -a app.py collect -v + flask -a app.py assets build + + +6. Run the development server: + + .. code-block:: console + + flask -a app.py run -h 0.0.0.0 -p 5000 + + +7. Open in a browser the page `http://0.0.0.0:5000/example/[:pid]`. + + E.g. open `http://0.0.0.0:5000/example/1` - $ cd examples - $ flask -a app.py --debug run """ from __future__ import absolute_import, print_function -from flask import Flask +import os + +from flask import Flask, render_template from flask_babelex import Babel from flask_cli import FlaskCLI +from invenio_assets import InvenioAssets, NpmBundle +from invenio_db import InvenioDB, db +from invenio_pidstore.minters import recid_minter +from invenio_pidstore.models import PersistentIdentifier +from invenio_records import InvenioRecords +from invenio_records.models import RecordMetadata from invenio_search import InvenioSearch +from invenio_theme import InvenioTheme from invenio_marc21 import InvenioMARC21 # Create Flask application app = Flask(__name__) app.config.update( - ELASTICSEARCH_HOST="localhost:9200", - SEARCH_ELASTIC_HOSTS="localhost:9200", - SEARCH_ELASTIC_KEYWORD_MAPPING={None: ['_all']}, + APP_BASE_TEMPLATE="app/base.html", + SQLALCHEMY_DATABASE_URI=os.getenv('SQLALCHEMY_DATABASE_URI', + 'sqlite:///app.db'), ) - +app.debug = True Babel(app) FlaskCLI(app) +InvenioDB(app) +InvenioTheme(app) +assets = InvenioAssets(app) +InvenioRecords(app) InvenioSearch(app) InvenioMARC21(app) + +# register CSS bundle +css = NpmBundle( + # my scss file + 'app/scss/app.scss', + filters='scss, cleancss', + output='gen/styles.%(version)s.css', +) +# register my stylesheets +assets.env.register('app_css', css) + + +@app.route('/example/', methods=['GET']) +def example(index): + """Index page.""" + pid = PersistentIdentifier.query.filter_by(id=index).one() + record = RecordMetadata.query.filter_by(id=pid.object_uuid).first() + + return render_template("app/detail.html", record=record.json, pid=pid, + title="Demosite Invenio Org") + + +@app.cli.group() +def fixtures(): + """Initialize example data.""" + + +@fixtures.command() +def records(): + """Load fixtures.""" + with db.session.begin_nested(): + for record in RecordMetadata.query.all(): + recid_minter(record.id, record.json) + db.session.commit() diff --git a/examples/static/app/scss/app.scss b/examples/static/app/scss/app.scss new file mode 100644 index 0000000..c0babbe --- /dev/null +++ b/examples/static/app/scss/app.scss @@ -0,0 +1,25 @@ +.title{ + background-color: #7775f5; + color: #ebebeb; + text-align: center; +} +.container{ + display: inline-block; + width: 100%; + margin-bottom: 1em; + + .extra-info{ + float: right; + background-color: #e8e8e8; + line-height: 1.5em; + padding: 1em; + margin: 1.5em 1em; + } + h2 small{ + color: #9f9393; + } +} +.raw .panel{ + border-top: 1px solid; + color: #a69e9e; +} diff --git a/examples/templates/app/base.html b/examples/templates/app/base.html new file mode 100644 index 0000000..4a0049a --- /dev/null +++ b/examples/templates/app/base.html @@ -0,0 +1,40 @@ +{# -*- coding: utf-8 -*- +# +# This file is part of Invenio. +# Copyright (C) 2016 CERN. +# +# Invenio is free software; you can redistribute it +# and/or modify it under the terms of the GNU General Public License as +# published by the Free Software Foundation; either version 2 of the +# License, or (at your option) any later version. +# +# Invenio is distributed in the hope that it will be +# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Invenio; if not, write to the +# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, +# MA 02111-1307, USA. +# +# In applying this license, CERN does not +# waive the privileges and immunities granted to it by virtue of its status +# as an Intergovernmental Organization or submit itself to any jurisdiction. +-#} + + + + + + + + {% assets "app_css" %} + + {% endassets %} + + +

{{ title }}

+ {%- block page_body %}{%- endblock page_body %} + + diff --git a/examples/templates/app/detail.html b/examples/templates/app/detail.html new file mode 100644 index 0000000..1acff0e --- /dev/null +++ b/examples/templates/app/detail.html @@ -0,0 +1,116 @@ +{# -*- coding: utf-8 -*- +# +# This file is part of Invenio. +# Copyright (C) 2016 CERN. +# +# Invenio is free software; you can redistribute it +# and/or modify it under the terms of the GNU General Public License as +# published by the Free Software Foundation; either version 2 of the +# License, or (at your option) any later version. +# +# Invenio is distributed in the hope that it will be +# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Invenio; if not, write to the +# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, +# MA 02111-1307, USA. +# +# In applying this license, CERN does not +# waive the privileges and immunities granted to it by virtue of its status +# as an Intergovernmental Organization or submit itself to any jurisdiction. +-#} +{%- extends config.APP_BASE_TEMPLATE %} + +{%- macro record_content(data) %} + {% for key, value in data.items() recursive %} +
  • + {% if value is mapping %} + {{ key }}: +
      {{ loop(value.items()) }}
    + {% elif value is iterable and value is not string %} + {{ key }}: +
      + {% for item in value %} +
    1. + {% if item is mapping %} +
        + {{ record_content(item) }} +
      + {% else %} + {{ item }} + {% endif %} +
    2. + {% endfor %} +
    + {% else %} + {{ key }}: {{ value }} + {% endif %} +
  • + {% endfor %} +{%- endmacro %} + +{%- block page_body %} +
    +
    + {%- if pid %} + {%- block record_control_number %} + {{ _('control number:') }} + {{ pid.pid_type }} {{pid.pid_value}} + {%- endblock %} + {% endif %} +
    + {%- if 'publication_distribution_imprint' in record %} + {%- block record_publication_date %} + {{ _('Publication date:') }} + {{ record['publication_distribution_imprint'][0]['date_of_publication_distribution'][0] }} + {%- endblock %} + {% endif %} +
    + {%- if 'other_standard_identifier' in record %} + {%- block record_other_standard_identifier %} + {{ _('OAI Id:') }} + {{ record['other_standard_identifier'][0]['standard_number_or_code'] }} + {%- endblock %} + {% endif %} +
    +
    + {%- if 'title_statement' in record %} + {%- block record_title %} +

    {{ record['title_statement']['title'] }}
    +

    + {%- endblock %} + {% endif %} + {%- if 'main_entry_personal_name' in record %} + {%- block record_author %} +

    + {{ record['main_entry_personal_name']['personal_name'] }} + {%- if 'added_entry_personal_name' in record %} + {{ record['added_entry_personal_name'] + | map(attribute='personal_name') | join(', ') }} + {% endif %} +

    + {%- endblock %} + {% endif %} + {%- if 'summary' in record %} + {%- block record_summary %} +

    {{ record['summary'][0]['summary'] }}

    + {%- endblock %} + {%- endif %} +
    +
    +
    + {%- block record_raw %} + {% if record %} +
    +

    Record:

    +
      + {{ record_content(record) }} +
    +
    + {% endif %} + {%- endblock %} +
    +{%- endblock %} diff --git a/requirements-devel.txt b/requirements-devel.txt index f64557a..abd5e1d 100644 --- a/requirements-devel.txt +++ b/requirements-devel.txt @@ -27,3 +27,4 @@ # # -e git+git://github.com/mitsuhiko/werkzeug.git#egg=Werkzeug # -e git+git://github.com/mitsuhiko/jinja2.git#egg=Jinja2 +-e git+git://github.com/inveniosoftware/dojson.git#egg=dojson diff --git a/setup.cfg b/setup.cfg index 920f22e..04e3749 100644 --- a/setup.cfg +++ b/setup.cfg @@ -23,14 +23,14 @@ # as an Intergovernmental Organization or submit itself to any jurisdiction. -[aliases] -test=pytest - [build_sphinx] source-dir = docs/ build-dir = docs/_build all_files = 1 +[aliases] +test=pytest + [bdist_wheel] universal = 1