New admin dashboard and widget to display blog news.

see #891
This commit is contained in:
Antoine Nguyen
2016-09-07 15:15:26 +02:00
parent 576231f876
commit abe24d2093
10 changed files with 122 additions and 15 deletions
+5
View File
@@ -2,6 +2,11 @@
"""Core application constants."""
ADMIN_GROUPS = [
"SuperAdmins",
"Resellers",
"DomainAdmins",
]
LANGUAGES = (
("cs", u"čeština"),
+11 -6
View File
@@ -6,6 +6,7 @@ from django.core.urlresolvers import reverse
from django.db import models
from django.utils import timezone
from django.utils.encoding import python_2_unicode_compatible, smart_text
from django.utils.functional import cached_property
from django.utils.translation import ugettext as _, ugettext_lazy
from django.contrib.auth.hashers import make_password, is_password_usable
@@ -321,14 +322,13 @@ class User(PermissionsMixin):
def role(self):
"""Return user role."""
if not hasattr(self, "_role"):
self._role = None
if self._role is None:
if self.is_superuser:
self._role = "SuperAdmins"
try:
self._role = self.groups.all()[0].name
except IndexError:
self._role = "---"
else:
try:
self._role = self.groups.all()[0].name
except IndexError:
self._role = "---"
return self._role
@role.setter
@@ -357,6 +357,11 @@ class User(PermissionsMixin):
self.save()
self._role = role
@cached_property
def is_admin(self):
"""Shortcut to check if user is administrator."""
return self.role in constants.ADMIN_GROUPS
def post_create(self, creator):
"""Grant permission on this user to creator."""
from modoboa.lib.permissions import grant_access_to_object
@@ -0,0 +1,40 @@
{% extends "fluid.html" %}
{% load i18n %}
{% block container_content %}
<div class="row">
<div class="col-xs-12 col-sm-offset-1 col-sm-10">
<h1>{% blocktrans with user=user trimmed %}Hello {{ user }}.{% endblocktrans %}</h1>
<div class="row">
<div class="col-xs-12 col-sm-6">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">{% trans "Latest news" %}</h3>
</div>
<table class="table">
<tbody>
{% for post in news %}
<tr>
<td><a href="{{ post.link }}" target="_blank">{{ post.title }}</a></td>
<td class="text-right">{{ post.published|date:"DATE_FORMAT" }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<div class="panel-body text-center">
{% blocktrans trimmed %}
Visit to the <a href="https://modoboa.org/blog/" target="_blank">official weblog</a> for more information.
{% endblocktrans %}
</div>
</div>
</div>
</div>
</div>
</div>
{% endblock container_content %}
+11 -2
View File
@@ -18,8 +18,17 @@ register = template.Library()
@register.simple_tag
def core_menu(selection, user):
entries = \
events.raiseQueryEvent("AdminMenuDisplay", "top_menu", user)
"""Build the top level menu."""
entries = []
if user.is_admin:
entries += [
{"name": "dashboard",
"label": "",
"img": "fa fa-home fa-lg",
"url": reverse("core:dashboard")}
]
entries += (
events.raiseQueryEvent("AdminMenuDisplay", "top_menu", user))
if user.is_superuser:
entries += [
{"name": "settings",
+4
View File
@@ -1,9 +1,13 @@
from django.conf.urls import patterns, url
from . import views
urlpatterns = patterns(
'',
url(r'^dashboard/$', views.DashboardView.as_view(),
name="dashboard"),
url(r'^accounts/login/$', 'modoboa.core.views.auth.dologin',
name="login"),
url(r'^accounts/logout/$', 'modoboa.core.views.auth.dologout',
+7
View File
@@ -0,0 +1,7 @@
"""Core views."""
from .dashboard import DashboardView
__all__ = [
"DashboardView",
]
+3 -4
View File
@@ -12,6 +12,8 @@ from modoboa.core.forms import LoginForm
from modoboa.lib import events
from modoboa.lib.web_utils import _render_to_string
logger = logging.getLogger("modoboa.auth")
def dologin(request):
"""Try to authenticate."""
@@ -45,10 +47,7 @@ def dologin(request):
if nextlocation is None:
nextlocation = request.POST.get("next", None)
if nextlocation is None or nextlocation == "None":
if user.group == "SimpleUsers":
nextlocation = reverse("topredirection")
else:
nextlocation = reverse("admin:domain_list")
nextlocation = reverse("topredirection")
return HttpResponseRedirect(nextlocation)
error = _(
"Your username and password didn't match. Please try again.")
+37
View File
@@ -0,0 +1,37 @@
"""Core dashboard views."""
from dateutil import parser
import feedparser
from django.views import generic
from django.contrib.auth import mixins as auth_mixins
class DashboardView(auth_mixins.AccessMixin, generic.TemplateView):
"""Dashboard view."""
template_name = "core/dashboard.html"
def dispatch(self, request, *args, **kwargs):
"""Check if user can access dashboard."""
if not request.user.is_admin:
return self.handle_no_permission()
return super(DashboardView, self).dispatch(request, *args, **kwargs)
def get_context_data(self, **kwargs):
"""Add context variables."""
context = super(DashboardView, self).get_context_data(**kwargs)
context.update({"selection": "dashboard"})
if self.request.user.language == "fr":
lang = "fr"
else:
lang = "en"
posts = feedparser.parse(
"https://modoboa.org/{}/weblog/feeds/".format(lang))
entries = []
for entry in posts["entries"][:5]:
entry["published"] = parser.parse(entry["published"])
entries.append(entry)
context.update({"news": entries})
return context
+2 -3
View File
@@ -154,7 +154,7 @@ def topredirection(request):
from modoboa.lib import parameters
from modoboa.core.extensions import exts_pool
if request.user.group == 'SimpleUsers':
if request.user.role == "SimpleUsers":
topredir = parameters.get_admin("DEFAULT_TOP_REDIRECTION", app="core")
if topredir != "user":
infos = exts_pool.get_extension_infos(topredir)
@@ -162,8 +162,7 @@ def topredirection(request):
else:
path = reverse("core:user_index")
else:
# FIXME
path = reverse("admin:domain_list")
path = reverse("core:dashboard")
return HttpResponseRedirect(path)
+2
View File
@@ -19,3 +19,5 @@ gevent==1.1.1
ipaddress
dnspython==1.12.0
jsonfield==1.0.3
py-dateutil==2.2
feedparser==5.2.1