Changeset - e0626084e9c5
[Not reviewed]
stable
0 1 0
Thomas De Schampheleire - 7 years ago 2019-04-18 21:33:23
thomas.de_schampheleire@nokia.com
files: remove need for webhelpers.html.literal

The following code is unnecessarily complex:
h.literal(_('There are no files yet. %s') % add_new)

First of all, the '%s' part in the translatable string is a whole new
sentence, independent of the first. There is no reason it needs to be part
of the same translatable string.

Secondly, the only reason for h.literal is to preserve the link in
'add_new' (which contains the result of 'h.link_to'). But, h.link_to
actually already is a 'literal' object. The problem is that the special
'literal' property is lost due to the coercion into a plain string via the
'%' operator.

The following code would be a possible solution for the second issue:
h.HTML(_('There are no files yet. %s')) % add_new
i.e. make sure that the format string is not a plain string but itself a
literal object (after its contents being escaped), before applying the '%'
operator.

To handle the first issue, this would become:
h.HTML(_('There are no files yet.')) + ' ' + h.HTML(add_new)
but, here h.HTML is unnecessary on the first string because there is nothing
special about it, and equally unnecessary on the 'add_new' variable because
h.link_to already returns a literal object.

So, the final code becomes:
_('There are no files yet.') + ' ' + add_new
1 file changed with 1 insertions and 2 deletions:
0 comments (0 inline, 0 general)
kallithea/controllers/files.py
Show inline comments
 
@@ -69,50 +69,49 @@ log = logging.getLogger(__name__)
 

	
 
class FilesController(BaseRepoController):
 

	
 
    def _before(self, *args, **kwargs):
 
        super(FilesController, self)._before(*args, **kwargs)
 

	
 
    def __get_cs(self, rev, silent_empty=False):
 
        """
 
        Safe way to get changeset if error occur it redirects to tip with
 
        proper message
 

	
 
        :param rev: revision to fetch
 
        :silent_empty: return None if repository is empty
 
        """
 

	
 
        try:
 
            return c.db_repo_scm_instance.get_changeset(rev)
 
        except EmptyRepositoryError as e:
 
            if silent_empty:
 
                return None
 
            url_ = url('files_add_home',
 
                       repo_name=c.repo_name,
 
                       revision=0, f_path='', anchor='edit')
 
            add_new = h.link_to(_('Click here to add new file'), url_, class_="alert-link")
 
            h.flash(h.literal(_('There are no files yet. %s') % add_new),
 
                    category='warning')
 
            h.flash(_('There are no files yet.') + ' ' + add_new, category='warning')
 
            raise HTTPNotFound()
 
        except (ChangesetDoesNotExistError, LookupError):
 
            msg = _('Such revision does not exist for this repository')
 
            h.flash(msg, category='error')
 
            raise HTTPNotFound()
 
        except RepositoryError as e:
 
            h.flash(safe_str(e), category='error')
 
            raise HTTPNotFound()
 

	
 
    def __get_filenode(self, cs, path):
 
        """
 
        Returns file_node or raise HTTP error.
 

	
 
        :param cs: given changeset
 
        :param path: path to lookup
 
        """
 

	
 
        try:
 
            file_node = cs.get_node(path)
 
            if file_node.is_dir():
 
                raise RepositoryError('given path is a directory')
 
        except ChangesetDoesNotExistError:
 
            msg = _('Such revision does not exist for this repository')
 
            h.flash(msg, category='error')
0 comments (0 inline, 0 general)