Skip to content

Commit

Permalink
global: example app restored
Browse files Browse the repository at this point in the history
* Instead of having different records, all files now reside under one record.
* removed accounts and requirement to login
* added setup and teardown scripts
* register records_ui blueprint properly
* moved temporary files where they belong
* Co-authored-by: Francois Decourcelle <francois.decourcelle@cern.ch>
  • Loading branch information
topless authored and lnielsen committed Feb 21, 2019
1 parent d13fa01 commit 70058ea
Show file tree
Hide file tree
Showing 20 changed files with 114 additions and 410 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ var/
.installed.cfg
*.egg

# Example application
examples/test.db
examples/static/
examples/temp/

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
Expand Down
8 changes: 7 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,15 @@ recursive-include docs *.rst
recursive-include docs Makefile
recursive-include examples *.csv
recursive-include examples *.html
recursive-include examples *.ipynb
recursive-include examples *.jpg
recursive-include examples *.json
recursive-include examples *.md
recursive-include examples *.pdf
recursive-include examples *.png
recursive-include examples *.py
recursive-include examples *.sh
recursive-include examples *.xml
recursive-include examples *.zip
recursive-include invenio_previewer *.html *.js *.css
recursive-include invenio_previewer *.po *.pot *.mo
Expand All @@ -52,4 +58,4 @@ recursive-include invenio_previewer *.gif
recursive-include invenio_previewer *.png
recursive-include invenio_previewer *.properties
recursive-include invenio_previewer *.svg
recursive-include tests *.py
recursive-include tests *.py
32 changes: 32 additions & 0 deletions examples/app-setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/sh

# quit on errors:
set -o errexit

# quit on unbound symbols:
set -o nounset

DIR=`dirname "$0"`

cd $DIR
export FLASK_APP=app.py

pip install -r requirements.txt

./app-teardown.sh

# Create the database
flask db init
flask db create

flask npm

cd static
npm install
cd ..

flask collect -v
flask assets build

flask fixtures
flask run
15 changes: 15 additions & 0 deletions examples/app-teardown.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh

# quit on errors:
set -o errexit

# quit on unbound symbols:
set -o nounset

DIR=`dirname "$0"`

cd $DIR
export FLASK_APP=app.py

flask db destroy --yes-i-know
rm -rf temp/ static/ test.db
167 changes: 35 additions & 132 deletions examples/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,86 +24,19 @@

r"""Minimal Flask application example for development.
1. Create the database and the tables:
1. Setup the application and create demo data:
.. code-block:: console
$ cd examples
$ pip install -r requirements.txt
$ flask db init
$ flask db create
$ ./app-setup.py
2. Our record with pid 1 contains several files. You can check out the
different types of files by changing the filename in the url
to one of the following values: markdown.md, csvfile.csv, zipfile.zip,
jsonfile.json, xmlfile.xml, notebook.ipynb, jpgfile.jpg, pngfile.png
2. Create the database and the tables:
.. code-block:: console
$ flask users create info@inveniosoftware.org -a
3. Collect npm, requirements from registered bundles:
.. code-block:: console
$ flask npm
4. Install the npm packages:
.. code-block:: console
$ cd static
$ npm install
$ cd ..
5. Copy the static files from the Python packages into the Flask
application's static folder:
.. code-block:: console
$ flask collect -v
6. Build the assets as they are defined in bundle.py.
This assumes that uglifyjs and r.js commands are available. To install
them, run `npm install -g uglifyjs requirejs`.
.. code-block:: console
$ flask assets build
7. Run the fixture CLI tool in order to populate the database with
example data:
.. code-block:: console
$ flask fixtures files
8. Run the test server:
.. code-block:: console
$ flask run
9. Login into the application. Open in a web browser
`http://localhost:5000/login/ and use the user previously created
(user: info@inveniosoftware.org).
10. Open a web browser and enter to the url
`http://localhost:5000/records/RECORD_PID/preview` where
`RECORD_ID` is a number between 1 and 10.
11. Open now a record that contains several files (The last record created).
By default, it is showing the first document, but you can set another file
using a query string like
`http://localhost:5000/records/6/preview?filename=csvfile.csv`
You can use (`csvfile.csv`, `markdown.md`, `pdffile.pdf`)
`http://localhost:5000/records/1/preview?filename=csvfile.csv`
"""

from __future__ import absolute_import, print_function
Expand All @@ -113,15 +46,17 @@

from flask import Flask
from flask_babelex import Babel
from invenio_access import InvenioAccess
from invenio_assets import InvenioAssets
from invenio_db import InvenioDB, db
from invenio_files_rest import InvenioFilesREST
from invenio_files_rest.models import Bucket, Location, ObjectVersion
from invenio_files_rest.models import Bucket, Location
from invenio_i18n import InvenioI18N
from invenio_pidstore.providers.recordid import RecordIdProvider
from invenio_records import InvenioRecords, Record
from invenio_records import InvenioRecords
from invenio_records_files.api import Record
from invenio_records_files.models import RecordsBuckets
from invenio_records_ui import InvenioRecordsUI
from invenio_records_ui.views import create_blueprint_from_app

from invenio_previewer import InvenioPreviewer

Expand All @@ -139,64 +74,44 @@
route='/records/<pid_value>',
template='invenio_records_ui/detail.html',
),
recid_files=dict(
pid_type='recid',
route='/records/<pid_value>/files/<filename>',
view_imp='invenio_records_files.utils:file_download_ui',
record_class='invenio_records_files.api:Record',
),
recid_previewer=dict(
pid_type='recid',
route='/records/<pid_value>/preview',
view_imp='invenio_previewer.views:preview',
),
record_class='invenio_records_files.api:Record',
)
)
)
Babel(app)
InvenioI18N(app)
InvenioAccess(app)
InvenioDB(app)
InvenioAssets(app)
InvenioRecords(app)
InvenioFilesREST(app)
InvenioPreviewer(app)
InvenioRecordsUI(app)
app.register_blueprint(create_blueprint_from_app(app))


@app.cli.group()
@app.cli.command()
def fixtures():
"""Command for working with test data."""


def create_object(bucket, file_name, stream):
"""Object creation inside the bucket using the file and its content."""
obj = ObjectVersion.create(bucket, file_name, stream=stream)
rec_uuid = uuid4()
provider = RecordIdProvider.create(object_type='rec', object_uuid=rec_uuid)
data = {
'pid_value': provider.pid.pid_value,
'files': [
{
'uri': '/files/{0}/{1}'.format(str(bucket.id), file_name),
'key': file_name,
'size': obj.file.size,
'bucket': str(bucket.id),
'local': True
}
]
}
Record.create(data, id_=rec_uuid)


@fixtures.command()
def files():
"""Load files."""
data_path = os.path.join(os.path.dirname(__file__), 'data')
temp_path = os.path.join(os.path.dirname(__file__), 'temp')
demo_files_path = os.path.join(os.path.dirname(__file__), 'demo_files')

# Create location
loc = Location(name='local', uri=data_path, default=True)
loc = Location(name='local', uri=temp_path, default=True)
db.session.add(loc)
db.session.commit()

# Bucket
bucket = Bucket.create()

# Example files from the data folder
example_files = (
demo_files = (
'markdown.md',
'csvfile.csv',
'zipfile.zip',
Expand All @@ -207,33 +122,21 @@ def files():
'pngfile.png',
)

# Create single file records
for f in example_files:
with open(os.path.join(data_path, f), 'rb') as fp:
create_object(bucket, f, fp)

# Create a multi-file record
rec_uuid = uuid4()
provider = RecordIdProvider.create(object_type='rec', object_uuid=rec_uuid)
data = {
'pid_value': provider.pid.pid_value,
'files': []
}

# Template to create different files
template_file = {
'uri': '/files/{0}/{1}',
'key': '',
'bucket': str(bucket.id),
'local': True
}

for filename in example_files:
file_data = template_file.copy()
file_data['uri'] = file_data['uri'].format(str(bucket.id), filename)
file_data['key'] = filename
data['files'].append(file_data)
record = Record.create(data, id_=rec_uuid)
bucket = Bucket.create()
RecordsBuckets.create(record=record.model, bucket=bucket)

Record.create(data, id_=rec_uuid)
# Add files to the record
for f in demo_files:
with open(os.path.join(demo_files_path, f), 'rb') as fp:
record.files[f] = fp

record.files.flush()
record.commit()
db.session.commit()
1 change: 0 additions & 1 deletion examples/data/csvfile.csv

This file was deleted.

Empty file removed examples/data/jsonfile.json
Empty file.
3 changes: 3 additions & 0 deletions examples/demo_files/csvfile.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Year,Make,Model
1997,Ford,E350
2000,Mercury,Cougar
File renamed without changes
3 changes: 3 additions & 0 deletions examples/demo_files/jsonfile.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"hello": "json"
}
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions examples/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
invenio-access==1.0.0
invenio-db==1.0.0
invenio-access>=1.1.0
invenio-accounts>=1.1.0
Loading

0 comments on commit 70058ea

Please sign in to comment.