Skip to content

Commit

Permalink
Merge pull request #233 from camptocamp/GSGMF-1573-message-css-class
Browse files Browse the repository at this point in the history
Allow to customize messages css class
  • Loading branch information
arnaud-morvan authored Apr 30, 2021
2 parents 996930b + 370054a commit bee0ea4
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ sudo: false
dist: trusty

python:
- "3.5"
- "3.6"

addons:
postgresql: "9.6"
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ L10N_SOURCE_FILES += $(shell find c2cgeoform/templates/ -type f -name '*.jinja2'
ifneq (,$(findstring CYGWIN, $(shell uname)))
PYTHON3 =
VENV_BIN = .build/venv/Scripts
PIP_UPGRADE = python.exe -m pip install --upgrade pip==9.0.1 setuptools==36.5
PIP_UPGRADE = python.exe -m pip install --upgrade pip==21.1 setuptools==56.0
else
PYTHON3 = -p python3
VENV_BIN = .build/venv/bin
PIP_UPGRADE = pip install --upgrade pip==9.0.1 setuptools==36.5.0
PIP_UPGRADE = pip install --upgrade pip==21.1 setuptools==56.0
endif


Expand Down
24 changes: 23 additions & 1 deletion c2cgeoform/scaffolds/c2cgeoform/+package+/views/excavation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
manytomany_validator,
)
from c2cgeoform.ext.deform_ext import RelationCheckBoxListWidget
from c2cgeoform.views.abstract_views import AbstractViews, ListField, ItemAction
from c2cgeoform.views.abstract_views import AbstractViews, ListField, ItemAction, UserMessage

from ..models.c2cgeoform_demo import Excavation, Situation
from ..i18n import _
Expand Down Expand Up @@ -57,6 +57,11 @@ class ExcavationViews(AbstractViews):
filter_column=Situation.name)
]

MSG_COL = {
**AbstractViews.MSG_COL,
"error": UserMessage(_("This is an error"), "alert-danger")
}

def _base_query(self):
return super()._base_query().distinct(). \
outerjoin('situations'). \
Expand All @@ -82,6 +87,23 @@ def _grid_actions(self):
)
]

def _item_actions(self, item, readonly=False):
actions = super()._item_actions(item, readonly)
if item is not None:
actions.append(
ItemAction(
name='error',
label=_('Show an error'),
icon='glyphicon glyphicon-alert',
url=self._request.route_url(
self._request.matched_route.name,
**self._request.matchdict,
_query=[('msg_col', 'error')]
)
)
)
return actions

@view_config(route_name='c2cgeoform_map',
renderer='../templates/map.jinja2')
def map(self):
Expand Down
4 changes: 2 additions & 2 deletions c2cgeoform/scaffolds/c2cgeoform/Makefile_tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ L10N_SOURCE_FILES += $(shell find {{package}}/templates/ -type f -name '*.jinja2
ifneq (,$(findstring CYGWIN, $(shell uname)))
PYTHON3 =
VENV_BIN = .build/venv/Scripts
PIP_UPGRADE = python.exe -m pip install --upgrade pip setuptools
PIP_UPGRADE = python.exe -m pip install --upgrade pip==21.1 setuptools==56.0
else
PYTHON3 = -p python3
VENV_BIN = .build/venv/bin
PIP_UPGRADE = pip install --upgrade pip==9.0.1 setuptools==36.5.0
PIP_UPGRADE = pip install --upgrade pip==21.1 setuptools==56.0
endif

.PHONY: help
Expand Down
10 changes: 5 additions & 5 deletions c2cgeoform/templates/widgets/form.pt
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@
<p class="error-msg">${field.errormsg}</p>
</div>

<div class="alert alert-success" tal:condition="exists: msg_col">
<tal:loop tal:repeat="msg msg_col">
<div class="msg-lbl">${msg}</div>
</tal:loop>
</div>
<tal:loop tal:repeat="msg msg_col" tal:condition="exists: msg_col">
<div class="alert ${msg.css_class()}">
<div class="msg-lbl">${msg.text()}</div>
</div>
</tal:loop>

<p class="section first" tal:condition="description">
${description}
Expand Down
26 changes: 23 additions & 3 deletions c2cgeoform/views/abstract_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,22 @@ def to_dict(self, request):
}


class UserMessage:
def __init__(self, text, css_class="alert-success"):
self._text = text
self._css_class = css_class

def text(self):
return self._text

def css_class(self):
return self._css_class

def __str__(self):
# For compatibility with old templates
return self._text


class AbstractViews():

_model = None # sqlalchemy model
Expand All @@ -170,8 +186,8 @@ class AbstractViews():
_base_schema = None # base colander schema

MSG_COL = {
'submit_ok': _('Your submission has been taken into account.'),
'copy_ok': _('Please check that the copy fits before submitting.'),
'submit_ok': UserMessage(_('Your submission has been taken into account.'), "alert-success"),
'copy_ok': UserMessage(_('Please check that the copy fits before submitting.'), "alert-info"),
}

def __init__(self, request):
Expand Down Expand Up @@ -413,7 +429,11 @@ def edit(self, schema=None, readonly=False):
'msg_col' in self._request.params.keys() and
self._request.params['msg_col'] in self.MSG_COL.keys()
):
kwargs.update({'msg_col': [self.MSG_COL[self._request.params['msg_col']]]})
msg = self.MSG_COL[self._request.params['msg_col']]
if isinstance(msg, str):
# For compatibility with old views
msg = UserMessage(msg, "alert-success")
kwargs.update({'msg_col': [msg]})
return {
'title': form.title,
'form': form,
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ geoalchemy2>=0.4.0
geojson
lingua>=2.4
psycopg2-binary>=2.7.7
pyramid>=1.10
pyramid<2>=1.10
pyramid_beaker
pyramid_chameleon
pyramid_jinja2
Expand Down

0 comments on commit bee0ea4

Please sign in to comment.