-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
admin: views for template definitions and counters
* Adds admin views for management of TemplateDefinition and Counter. * Adds a Jinja template for the admin interface, which provides a reset row action. Signed-off-by: Orestis Melkonian <melkon.or@gmail.com>
- Loading branch information
1 parent
2d3e246
commit 79ae908
Showing
10 changed files
with
361 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# -*- 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. | ||
|
||
|
||
"""Minimal Flask application example for development. | ||
Run example development server: | ||
.. code-block:: console | ||
$ cd examples | ||
$ export FLASK_APP=app.py | ||
$ flask db init | ||
$ flask db create | ||
$ flask fixtures sequences | ||
$ flask run --debugger | ||
""" | ||
|
||
from __future__ import absolute_import, print_function | ||
|
||
import os | ||
|
||
from flask import Flask | ||
from flask_admin import Admin | ||
from flask_babelex import Babel | ||
from flask_cli import FlaskCLI | ||
from invenio_db import InvenioDB, db | ||
from invenio_sequencegenerator.admin import counter_adminview as ca | ||
from invenio_sequencegenerator.admin import templatedefinition_adminview as ta | ||
from invenio_sequencegenerator.api import Sequence, Template | ||
|
||
# Create Flask application | ||
from invenio_sequencegenerator.ext import InvenioSequenceGenerator | ||
|
||
app = Flask(__name__) | ||
app.config.update( | ||
SECRET_KEY="CHANGE_ME", | ||
SECURITY_PASSWORD_SALT="CHANGE_ME_ALSO", | ||
SQLALCHEMY_DATABASE_URI=os.environ.get( | ||
'SQLALCHEMY_DATABASE_URI', 'sqlite:///test.db'), | ||
SQLALCHEMY_TRACK_MODIFICATIONS=True, | ||
) | ||
FlaskCLI(app) | ||
Babel(app) | ||
InvenioDB(app) | ||
InvenioSequenceGenerator(app) | ||
|
||
|
||
@app.cli.group() | ||
def fixtures(): | ||
"""Command for working with test data.""" | ||
|
||
|
||
@fixtures.command() | ||
def sequences(): | ||
"""Load test data fixture.""" | ||
pl = Template.create('PL', '{year}: Playlist {counter}', start=1) | ||
fl = Template.create('FL', '{PL} > Audio File {counter:02d}', start=1) | ||
pl15 = Sequence(pl, year=2015) | ||
pl15.next() | ||
pl15.next() | ||
pl16 = Sequence(pl, year=2016) | ||
pl16.next() | ||
fl15 = Sequence(fl, PL='2015: Playlist 2') | ||
fl15.next() | ||
fl15.next() | ||
fl16 = Sequence(fl, PL='2016: Playlist 1') | ||
fl16.next() | ||
db.session.commit() | ||
|
||
|
||
@app.route('/') | ||
def index(): | ||
return '<a href="/admin/">Click me to get to Admin!</a>' | ||
|
||
|
||
# Create admin | ||
admin = Admin(app, name='Example: Sequence Generator', | ||
template_mode='bootstrap3') | ||
|
||
# Add views | ||
admin.add_view(ta['modelview'](ta['model'], db.session)) | ||
admin.add_view(ca['modelview'](ca['model'], db.session)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# -*- 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. | ||
|
||
"""Admin views for invenio-accounts.""" | ||
|
||
from flask import flash, redirect, request, url_for | ||
from flask_admin import expose | ||
from flask_admin.actions import action | ||
from flask_admin.contrib.sqla import ModelView | ||
from flask_babelex import gettext as _ | ||
from invenio_db import db | ||
|
||
from .models import Counter, TemplateDefinition | ||
|
||
|
||
class TemplateDefinitionView(ModelView): | ||
"""Flask-Admin view for template definitions.""" | ||
|
||
column_list = ( | ||
'name', | ||
'meta_template', | ||
'parent_name', | ||
'start', | ||
'step', | ||
) | ||
|
||
|
||
class CounterView(ModelView): | ||
"""Admin view for counters.""" | ||
|
||
list_template = 'invenio_sequencegenerator/custom_list.html' | ||
|
||
column_list = ( | ||
'template_instance', | ||
'counter', | ||
'definition_name', | ||
) | ||
|
||
@expose('/reset', methods=('POST',)) | ||
def reset_view(self): | ||
"""Reset selected counter.""" | ||
start = request.form.get('start', default=0, type=int) | ||
template_instance = request.form['rowid'] | ||
Counter.query.get(template_instance).reset(start=start) | ||
db.session.commit() | ||
return redirect(url_for('.index_view')) | ||
|
||
|
||
templatedefinition_adminview = { | ||
'model': TemplateDefinition, | ||
'modelview': TemplateDefinitionView, | ||
'category': _('Sequences'), | ||
} | ||
|
||
counter_adminview = { | ||
'model': Counter, | ||
'modelview': CounterView, | ||
'category': _('Sequences'), | ||
} | ||
|
||
__all__ = ('templatedefinition_adminview', 'counter_adminview') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# -*- 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. | ||
|
||
"""Flask extension for Invenio-SequenceGenerator.""" | ||
|
||
from __future__ import absolute_import, print_function | ||
|
||
from flask import Blueprint | ||
|
||
|
||
class InvenioSequenceGenerator(object): | ||
"""Invenio-SequenceGenerator extension.""" | ||
|
||
def __init__(self, app=None): | ||
"""Extension initialization.""" | ||
if app: | ||
self.init_app(app) | ||
|
||
def init_app(self, app): | ||
"""Flask application initialization.""" | ||
app.extensions['invenio-sequencegenerator'] = self | ||
|
||
# Register dummy blueprint just for loading templates | ||
app.register_blueprint(Blueprint( | ||
'invenio_sequencegenerator', __name__, template_folder='templates') | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
invenio_sequencegenerator/templates/invenio_sequencegenerator/custom_list.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{# | ||
# | ||
# 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 'admin/model/list.html' %} | ||
{%- block list_row_actions %} | ||
{{ super() }} | ||
<form class="icon" method="POST" action="{{ url_for('.reset_view') }}"> | ||
<input name="start" value="0" type="number" size="1"> | ||
<input name="rowid" value="{{ get_pk_value(row) }}" type="hidden"> | ||
<button onclick="return confirm(_('Are you sure you want to reset selected counter?'));" title="{{ _('Reset') }}"> | ||
<span class="fa fa-step-backward glyphicon glyphicon-step-backward"></span> | ||
</button> | ||
</form> | ||
{%- endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.