Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
Nuovo sito
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
16
Issues
16
List
Boards
Labels
Milestones
Merge Requests
2
Merge Requests
2
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Gruppo Web
Nuovo sito
Commits
461cc752
Commit
461cc752
authored
Apr 28, 2018
by
Pietro Albini
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rewrite the navbar code
parent
10f5ad48
Pipeline
#63
passed with stage
in 0 seconds
Changes
11
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
201 additions
and
162 deletions
+201
-162
MANIFEST.in
MANIFEST.in
+1
-2
setup.py
setup.py
+1
-0
uitwww/__init__.py
uitwww/__init__.py
+5
-1
uitwww/__main__.py
uitwww/__main__.py
+1
-1
uitwww/data/navbar.yml
uitwww/data/navbar.yml
+43
-0
uitwww/download.py
uitwww/download.py
+12
-0
uitwww/navbar.json
uitwww/navbar.json
+0
-59
uitwww/navbar.py
uitwww/navbar.py
+118
-0
uitwww/pages.py
uitwww/pages.py
+0
-62
uitwww/templates/layout.html
uitwww/templates/layout.html
+20
-25
uitwww/templates/macros.html
uitwww/templates/macros.html
+0
-12
No files found.
MANIFEST.in
View file @
461cc752
recursive-include uitwww/templates *
recursive-include uitwww/templates *
recursive-include uitwww/static *
recursive-include uitwww/static *
recursive-include uitwww/data *.toml
recursive-include uitwww/data *
include uitwww/navbar.json
setup.py
View file @
461cc752
...
@@ -37,6 +37,7 @@ setuptools.setup(
...
@@ -37,6 +37,7 @@ setuptools.setup(
"requests"
,
"requests"
,
"itsdangerous"
,
"itsdangerous"
,
"toml"
,
"toml"
,
"pyyaml"
,
],
],
packages
=
[
packages
=
[
...
...
uitwww/__init__.py
View file @
461cc752
...
@@ -22,6 +22,7 @@ from . import pages
...
@@ -22,6 +22,7 @@ from . import pages
from
.
import
cache
from
.
import
cache
from
.
import
utils
from
.
import
utils
from
.
import
download
from
.
import
download
from
.
import
navbar
def
create_app
(
data_path
):
def
create_app
(
data_path
):
...
@@ -51,7 +52,10 @@ def create_app(data_path):
...
@@ -51,7 +52,10 @@ def create_app(data_path):
)
)
app
.
register_blueprint
(
pages
.
prepare_blueprint
(
app
))
app
.
register_blueprint
(
pages
.
prepare_blueprint
(
app
))
pages
.
prepare_navbar
(
app
)
nav
=
navbar
.
Navbar
()
nav
.
add_generator
(
"download-distros"
,
app
.
download
.
generate_navbar
)
nav
.
install
(
app
)
@
app
.
errorhandler
(
404
)
@
app
.
errorhandler
(
404
)
def
not_found
(
error
):
def
not_found
(
error
):
...
...
uitwww/__main__.py
View file @
461cc752
...
@@ -50,7 +50,7 @@ def run(data, gunicorn_config, port, public, workers, debug):
...
@@ -50,7 +50,7 @@ def run(data, gunicorn_config, port, public, workers, debug):
# In debug mode, run the flask builtin webserver
# In debug mode, run the flask builtin webserver
if
debug
:
if
debug
:
extra_files
=
[
extra_files
=
[
os
.
path
.
join
(
src_directory
,
"
navbar.json
"
),
os
.
path
.
join
(
src_directory
,
"
data/navbar.yml
"
),
]
]
app
.
run
(
debug
=
True
,
port
=
port
,
host
=
host
,
extra_files
=
extra_files
)
app
.
run
(
debug
=
True
,
port
=
port
,
host
=
host
,
extra_files
=
extra_files
)
...
...
uitwww/data/navbar.yml
0 → 100644
View file @
461cc752
-
name
:
Download
endpoint
:
download.index
# Evidenzia anche la pagina di ringraziamento sotto questo menu
also-match
:
download.thanks
:
null
# Le sottopagine sono generate automaticamente dalla lista di distro
generate-subs
:
download-distros
-
name
:
Scopri Ubuntu
endpoint
:
pages.scopri-ubuntu_index
-
name
:
Supporto
endpoint
:
pages.supporto
subs
:
-
name
:
Documentazione
external
:
https://wiki.ubuntu-it.org
-
name
:
Forum
external
:
https://forum.ubuntu-it.org
-
name
:
Domande e risposte
external
:
https://chat.ubuntu-it.org
-
name
:
Chat
external
:
https://chat.ubuntu-it.org
-
name
:
Comunità
endpoint
:
pages.comunita_index
-
name
:
Eventi
endpoint
:
pages.eventi_index
-
name
:
Contribuisci
endpoint
:
pages.contribuisci_index
-
name
:
Progetto
endpoint
:
pages.progetto_index
-
name
:
Contatti
endpoint
:
pages.contatti_index
uitwww/download.py
View file @
461cc752
...
@@ -279,3 +279,15 @@ class Downloads:
...
@@ -279,3 +279,15 @@ class Downloads:
return
flask
.
render_template
(
"download/thanks.html"
,
**
content
)
return
flask
.
render_template
(
"download/thanks.html"
,
**
content
)
return
bp
return
bp
def
generate_navbar
(
self
):
"""Generate the sub-navbar for /download"""
result
=
[]
for
name
,
distro
in
self
.
config
[
"distros"
].
items
():
result
.
append
({
"name"
:
distro
[
"name"
],
"endpoint"
:
"download.landing"
,
"endpoint-args"
:
{
"distro"
:
name
},
})
return
result
uitwww/navbar.json
deleted
100644 → 0
View file @
10f5ad48
[
{
"name"
:
"Download"
,
"endpoint"
:
"download.index"
,
"subs"
:
[]
},
{
"name"
:
"Scopri Ubuntu"
,
"endpoint"
:
"pages.scopri-ubuntu_index"
,
"subs"
:
[]
},
{
"name"
:
"Supporto"
,
"endpoint"
:
"pages.supporto"
,
"subs"
:
[
{
"name"
:
"Documentazione"
,
"external"
:
"http://wiki.ubuntu-it.org"
},
{
"name"
:
"Forum"
,
"external"
:
"http://forum.ubuntu-it.org"
},
{
"name"
:
"Domande e risposte"
,
"external"
:
"http://chiedi.ubuntu-it.org"
},
{
"name"
:
"Chat"
,
"external"
:
"http://chat.ubuntu-it.org"
}
]
},
{
"name"
:
"Comunità"
,
"endpoint"
:
"pages.comunita_index"
,
"subs"
:
[]
},
{
"name"
:
"Eventi"
,
"endpoint"
:
"pages.eventi_index"
,
"subs"
:
[]
},
{
"name"
:
"Contribuisci"
,
"endpoint"
:
"pages.contribuisci_index"
,
"subs"
:
[]
},
{
"name"
:
"Progetto"
,
"endpoint"
:
"pages.progetto_index"
,
"subs"
:
[]
},
{
"name"
:
"Contatti"
,
"endpoint"
:
"pages.contatti_index"
,
"subs"
:
[]
}
]
uitwww/navbar.py
0 → 100644
View file @
461cc752
# Source code of the Ubuntu-it website
# Copyright (C) 2018 Pietro Albini <pietroalbini@ubuntu.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; witout even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import
flask
import
json
import
pkg_resources
import
yaml
class
Navbar
:
def
__init__
(
self
):
self
.
navbars
=
[]
self
.
match
=
{}
self
.
generators
=
{}
def
add_generator
(
self
,
name
,
generator
):
"""Add a subpages generator"""
self
.
generators
[
name
]
=
generator
def
current_navbars
(
self
):
"""Get the list of current navbars"""
matcher
=
(
flask
.
request
.
endpoint
,
tuple
(
flask
.
request
.
view_args
.
items
()))
if
matcher
in
self
.
match
:
return
self
.
_return_navbars
(
self
.
match
[
matcher
])
elif
flask
.
request
.
endpoint
in
self
.
match
:
return
self
.
_return_navbars
(
self
.
match
[
flask
.
request
.
endpoint
])
else
:
return
self
.
_return_navbars
([(
0
,
-
1
)])
def
_return_navbars
(
self
,
navbars
):
"""Return a list of navbars"""
result
=
[]
for
bar_id
,
active_id
in
navbars
:
bar
=
[]
for
i
,
item
in
enumerate
(
self
.
navbars
[
bar_id
]):
bar
.
append
((
item
[
0
],
item
[
1
](),
i
==
active_id
))
result
.
append
(
bar
)
return
result
def
install
(
self
,
app
):
"""Install this navbar on the app"""
raw
=
pkg_resources
.
resource_string
(
"uitwww"
,
"data/navbar.yml"
)
config
=
yaml
.
load
(
raw
.
decode
(
"utf-8"
))
self
.
_prepare_navbar_cache
(
config
,
[])
# Add the _navbars variable to the templates
@
app
.
context_processor
def
inject_navbar
():
return
{
"_navbars"
:
self
.
current_navbars
(),
}
def
_prepare_navbar_cache
(
self
,
bar
,
parents
):
"""Add the bar to the cache"""
bar_id
=
len
(
self
.
navbars
)
self
.
navbars
.
append
([])
for
item
in
bar
:
item
.
setdefault
(
"endpoint-args"
,
None
)
# Load the list of subs dynamically if requested
if
"generate-subs"
in
item
:
item
[
"subs"
]
=
self
.
generators
[
item
[
"generate-subs"
]]()
# Add hte item to the navbar
item_id
=
len
(
self
.
navbars
[
bar_id
])
self
.
navbars
[
bar_id
].
append
((
item
[
"name"
],
self
.
_url_of
(
item
)))
# Load sub-navbars recursively
sub_id
=
None
if
"subs"
in
item
:
sub_id
=
self
.
_prepare_navbar_cache
(
item
[
"subs"
],
parents
+
[(
bar_id
,
item_id
)])
if
"endpoint"
in
item
:
# Generate the list of navbars to show for this match
# If this item has childs the childs are shown
navbars
=
parents
+
[(
bar_id
,
item_id
)]
if
sub_id
is
not
None
:
navbars
.
append
((
sub_id
,
-
1
))
# Add a match for each endpoint pointing to the list of navbars
match
=
{
item
[
"endpoint"
]:
item
[
"endpoint-args"
]}
if
"also-match"
in
item
:
match
.
update
(
item
[
"also-match"
])
for
endpoint
,
args
in
match
.
items
():
if
args
is
None
:
self
.
match
[
endpoint
]
=
navbars
else
:
self
.
match
[(
endpoint
,
tuple
(
args
.
items
()))]
=
navbars
return
bar_id
def
_url_of
(
self
,
item
):
"""Lazily the URL of an item"""
if
"external"
in
item
:
return
lambda
:
item
[
"external"
]
# This function return lambdas because flask.url_for is only available
# during requests
if
item
[
"endpoint-args"
]
is
not
None
:
return
lambda
:
flask
.
url_for
(
item
[
"endpoint"
],
**
item
[
"endpoint-args"
])
else
:
return
lambda
:
flask
.
url_for
(
item
[
"endpoint"
])
uitwww/pages.py
View file @
461cc752
...
@@ -87,65 +87,3 @@ def prepare_blueprint(app):
...
@@ -87,65 +87,3 @@ def prepare_blueprint(app):
return
flask
.
render_template
(
_tmpl
)
return
flask
.
render_template
(
_tmpl
)
return
bp
return
bp
def
prepare_navbar
(
app
):
"""Prepare the context processor which will provide navbar informations"""
# This will load the navbar.json file, and parse it as json
navbar_raw
=
pkg_resources
.
resource_string
(
"uitwww"
,
"navbar.json"
)
navbar
=
json
.
loads
(
navbar_raw
.
decode
(
"utf-8"
))
def
calculate_menu
(
current
,
recursive
=
True
,
_final
=
True
):
# This will process only a level of the result
active
=
False
active_i
=
None
# Make all items non-active by default
for
item
in
current
:
item
[
"active"
]
=
False
for
i
,
item
in
enumerate
(
current
):
active
=
False
# An item should be considered active if either is the current
# one, or one of its childs is active
endpoint
=
flask
.
request
.
endpoint
if
"endpoint"
in
item
and
item
[
"endpoint"
]
==
endpoint
:
active
=
True
elif
"subs"
in
item
and
recursive
:
active
=
calculate_menu
(
item
[
"subs"
],
_final
=
False
)
is
not
None
# Break the loop if this item is active
if
active
:
item
[
"active"
]
=
True
active_i
=
i
break
if
active
or
_final
:
return
current
,
active_i
# else None is returned
@
app
.
context_processor
def
add_menu
():
result
=
[]
result_active
=
[]
# Get all the possible navbar levels
next_to_calculate
=
navbar
while
True
:
calculated
,
next_i
=
calculate_menu
(
next_to_calculate
)
result
.
append
(
calculated
)
if
next_i
is
not
None
:
result_active
.
append
(
calculated
[
next_i
])
if
next_i
is
not
None
and
"subs"
in
calculated
[
next_i
]:
next_to_calculate
=
calculated
[
next_i
][
"subs"
]
else
:
break
# Force something in the second navbar
if
len
(
result
)
==
1
:
result
.
append
(
result
[
0
][
0
][
"subs"
])
return
{
"_navbar"
:
result
,
"_navbar_active"
:
result_active
}
uitwww/templates/layout.html
View file @
461cc752
...
@@ -56,37 +56,32 @@
...
@@ -56,37 +56,32 @@
<li><a
href=
"https://planet.ubuntu-it.org"
>
Planet
</a></li>
<li><a
href=
"https://planet.ubuntu-it.org"
>
Planet
</a></li>
</ul>
</ul>
<ul
class=
"right"
>
<ul
class=
"right"
>
<li><a
href=
"https://telegram.me/ubuntuit"
>
Telegram
</a></li>
<li><a
href=
"https://www.youtube.com/ubuntuitpromozione"
>
YouTube
</a></li>
<li><a
href=
"https://plus.google.com/+ubuntuit"
>
Google+
</a></li>
<li
<
a
href=
"https://twitter.com/ubuntuit"
>
Twitter
</a></li>
<li><a
href=
"https://www.facebook.com/ubuntu.it"
>
Facebook
</a></li>
<li><a
href=
"https://www.facebook.com/ubuntu.it"
>
Facebook
</a></li>
<li><a
href=
"https://twitter.com/ubuntuit"
>
Twitter
</a></li>
<li><a
href=
"https://plus.google.com/+ubuntuit"
>
Google+
</a></li>
<li><a
href=
"https://www.youtube.com/ubuntuitpromozione"
>
YouTube
</a></li>
<li><a
href=
"https://telegram.me/ubuntuit"
>
Telegram
</a></li>
</ul>
</ul>
</div>
</div>
</nav>
</nav>
<nav
class=
"main-navbar"
>
{% for navbar in _navbars %}
<nav
{%
-
if
loop.index =
=
1
%}
class=
"main-navbar"
{%
endif
%}
>
<div
class=
"container"
>
<div
class=
"container"
>
<ul>
<ul>
{% if loop.index == 1 %}
<li
class=
"site-name"
><a
href=
"{{ url_for("
pages
.
index
")
}}"
>
ubuntu-it
</a></li>
<li
class=
"site-name"
><a
href=
"{{ url_for("
pages
.
index
")
}}"
>
ubuntu-it
</a></li>
{% for item in _navbar[0] %}
{% endif -%}
{{ nav_item(item) }}
{% endfor %}
</ul>
</div>
</nav>
{% if _navbar[1] %}
{%- for name, url, active in navbar %}
<nav>
<li>
<div
class=
"container"
>
<a
{%
-
if
active
%}
class=
"active"
{%
endif
%}
href=
"{{ url }}"
>
{{ name }}
</a>
<ul>
</li>
{% for item in _navbar[1] %}
{{ nav_item(item) }}
{% endfor %}
{% endfor %}
</ul>
</ul>
</div>
</div>
</nav>
</nav>
{% end
if
%}
{% end
for
%}
<div
class=
"container"
>
<div
class=
"container"
>
{% block content %}{% endblock %}
{% block content %}{% endblock %}
...
...
uitwww/templates/macros.html
View file @
461cc752
...
@@ -14,15 +14,3 @@
...
@@ -14,15 +14,3 @@
# You should have received a copy of the GNU Affero General Public License
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see
<http:
//
www
.
gnu
.
org
/
licenses
/>
.
# along with this program. If not, see
<http:
//
www
.
gnu
.
org
/
licenses
/>
.
#}
#}
{% macro nav_item(item) %}
<li>
{% if "external" in item %}
<a
href=
"{{ item.external }}"
>
{{ item.name }}
</a>
{% elif item.active %}
<a
href=
"{{ url_for(item.endpoint) }}"
class=
"active"
>
{{ item.name }}
</a>
{% else %}
<a
href=
"{{ url_for(item.endpoint) }}"
>
{{ item.name }}
</a>
{% endif %}
</li>
{% endmacro %}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment