Files
modoboa/modoboa/core/views/dashboard.py
T
Andrew Fyfe eaec271e02 pep8 fixes
- B007 Loop control variable not used within the loop body. If this is intended, start the name with an underscore.
    * modoboa/admin/lib.py
    * modoboa/admin/templatetags/admin_tags.py
    * modoboa/admin/tests/test_account.py
    * modoboa/admin/views/domain.py
    * modoboa/admin/views/identity.py
    * modoboa/core/views/dashboard.py
    * modoboa/limits/forms.py
    * modoboa/limits/handlers.py
    * modoboa/limits/tests/test_api.py
    * modoboa/limits/tests/test_domain_limits.py
    * modoboa/limits/tests/test_import.py
    * modoboa/limits/tests/test_user_limits.py
    * modoboa/limits/utils.py
    * modoboa/parameters/tools.py
    * modoboa/relaydomains/tests.py
- C402 Unnecessary generator - rewrite as a dict comprehension.
    * modoboa/admin/models/domain.py
    * modoboa/admin/views/domain.py
    * modoboa/admin/views/identity.py
    * modoboa/limits/searializers.py
- C407 Unnecessary list comprehension - can take a generator.
    * modoboa/core/password_validation.py
    * modoboa/parameters/tools.py
    * modoboa/transport/backends.py
- C408 Unnecessary dict call - rewrite as a literal.
    * modoboa/admin/tests/test_account.py
    * modoboa/admin/tests/test_alias.py
    * modoboa/admin/tests/test_password_schemes.py
    * modoboa/admin/views/domain.py
    * modoboa/admin/views/identity.py
    * modoboa/core/extensions.py
    * modoboa/lib/middleware.py
    * modoboa/lib/templatetags/form_tags.py
    * modoboa/lib/templatetags/lib_tags.py
    * modoboa/lib/web_utils.py
    * modoboa/limits/handlers.py
    * modoboa/limits/tests/test_user_limits.py
- E741 ambiguous variable name
    * modoboa/core/management/commands/cleanlogs.py
    * modoboa/limits/forms.py
    * modoboa/limits/tests/test_user_limits.py
    * modoboa/limits/utils.py
    * modoboa/relaydomains/tests.py
- F401 imported but unused
    * doc/conf.py
    * modoboa/admin/views/domain.py
    * modoboa/core/app_settings.py
    * modoboa/core/management/commands/load_initial_data.py
- F821 undefined name 'raw_input'
    * modoboa/core/commands/deploy.py
- F841 local variable 'test' is assigned to but never used
    * modoboa/core/app_settings.py
- N802 function name 'setUpTestData' should be lowercase xxx
    * modoboa/lib/email_utils.py - __ for name mangeling, _ for internal
- N805 first argument of a method should be named 'self'
    * modoboa/lib/connections.py
    * modoboa/lib/singleton.py

Ignore the following
- A001 "copyright" is a python builtin and is being shadowed, consider renaming the variable
    * doc/conf.py
- A003 is a python builtin, consider renaming the class attribute
    * modoboa/admin/factories.py
    * modoboa/admin/forms/domain.py
    * modoboa/admin/management/commands/handle_mailbox_operations.py
    * modoboa/admin/management/commands/modo.py
    * modoboa/admin/management/commands/subcommands/_repair.py
    * modoboa/admin/models/domain.py
    * modoboa/admin/models/mailbox.py
    * modoboa/core/commands/__init__.py
    * modoboa/core/commands/deploy.py
    * modoboa/core/commands/help.py
    * modoboa/core/management/commands/clean_inactive_accounts.py
    * modoboa/core/management/commands/cleanlogs.py
    * modoboa/core/management/commands/communicate_with_public_api.py
    * modoboa/core/management/commands/generate_postfix_maps.py
    * modoboa/core/management/commands/load_initial_data.py
    * modoboa/core/management/commands/set_default_site.py
- F401 imported but unused
    * modoboa/admin/apps.py - used to load handlers
    * modoboa/core/password_hashers/__init__.py - dynamically loaded
    * modoboa/limits/apps.py - used to load handlers
    * modoboa/relaydomains/apps.py - used to load handlers
    * modoboa/transport/apps.py - used to load handlers
- N802 function name should be lowercase xxx
    * modoboa/admin/tests/test_account.py
    * modoboa/admin/tests/test_alias.py
    * modoboa/admin/tests/test_domain_alias.py
    * modoboa/admin/tests/test_export.py
    * modoboa/admin/tests/test_mailbox_operations.py
    * modoboa/admin/tests/test_password_schemas.py
    * modoboa/admin/tests/test_repair.py
    * modoboa/core/tests/test_authentication.py
    * modoboa/core/tests/test_core.py
    * modoboa/core/tests/test_ldap.py
    * modoboa/core/tests/test_views.py
    * modoboa/lib/imap_utf7.py
    * modoboa/limits/tests/test_api.py
    * modoboa/limits/tests/test_domain_limits.py
    * modoboa/limits/tests/test_import.py
    * modoboa/limits/tests/test_user_limits.py
    * modoboa/relaydomains/tests.py

other fixes:
- use isinstance() instead of type() == Class
2018-02-03 00:44:00 +00:00

83 lines
2.9 KiB
Python

"""Core dashboard views."""
from __future__ import unicode_literals
import feedparser
import requests
from dateutil import parser
from requests.exceptions import RequestException
from django.contrib.auth import mixins as auth_mixins
from django.views import generic
from .. import signals
MODOBOA_WEBSITE_URL = "https://modoboa.org/"
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_authenticated or 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", "widgets": {"left": [], "right": []}
})
# Fetch latest news
if self.request.user.language == "fr":
lang = "fr"
else:
lang = "en"
context.update({"selection": "dashboard"})
feed_url = "{}{}/weblog/feeds/".format(MODOBOA_WEBSITE_URL, lang)
if self.request.user.role != "SuperAdmins":
custom_feed_url = (
self.request.localconfig.parameters.get_value("rss_feed_url"))
if custom_feed_url:
feed_url = custom_feed_url
posts = feedparser.parse(feed_url)
entries = []
for entry in posts["entries"][:5]:
entry["published"] = parser.parse(entry["published"])
entries.append(entry)
context["widgets"]["left"].append("core/_latest_news_widget.html")
context.update({"news": entries})
hide_features_widget = self.request.localconfig.parameters.get_value(
"hide_features_widget")
if self.request.user.is_superuser or not hide_features_widget:
url = "{}{}/api/projects/?featured=true".format(
MODOBOA_WEBSITE_URL, lang)
features = []
try:
response = requests.get(url)
except RequestException:
pass
else:
if response.status_code == 200:
features = response.json()
context["widgets"]["right"].append("core/_current_features.html")
context.update({"features": features})
# Extra widgets
result = signals.extra_admin_dashboard_widgets.send(
sender=self.__class__, user=self.request.user)
for _receiver, widgets in result:
for widget in widgets:
context["widgets"][widget["column"]].append(
widget["template"])
# FIXME: can raise conflicts...
context.update(widget["context"])
return context