2019-10-30 13:06:55 +00:00
|
|
|
from django.contrib.auth.mixins import UserPassesTestMixin
|
2019-12-18 09:56:48 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2019-10-25 11:33:37 +00:00
|
|
|
from django.views.generic.base import ContextMixin
|
2022-02-26 18:54:27 +00:00
|
|
|
from django.conf import settings
|
2019-10-25 11:33:37 +00:00
|
|
|
|
2019-10-30 13:36:03 +00:00
|
|
|
from . import api, get_version
|
2019-10-30 13:06:55 +00:00
|
|
|
from .auth import SESSION_KEY_TOKEN
|
2019-10-25 11:33:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CustomContextMixin(ContextMixin):
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
2019-10-30 13:36:03 +00:00
|
|
|
# generate services menu items
|
|
|
|
services_menu = [
|
2019-12-18 09:56:48 +00:00
|
|
|
{'icon': 'globe-europe', 'pattern_name': 'musician:dashboard', 'title': _('Domains & websites')},
|
2021-09-27 11:52:27 +00:00
|
|
|
{'icon': 'envelope', 'pattern_name': 'musician:address-list', 'title': _('Mails')},
|
2019-12-18 09:56:48 +00:00
|
|
|
{'icon': 'mail-bulk', 'pattern_name': 'musician:mailing-lists', 'title': _('Mailing lists')},
|
2021-09-27 11:52:27 +00:00
|
|
|
{'icon': 'database', 'pattern_name': 'musician:database-list', 'title': _('Databases')},
|
|
|
|
{'icon': 'fire', 'pattern_name': 'musician:saas-list', 'title': _('SaaS')},
|
2019-10-30 13:36:03 +00:00
|
|
|
]
|
2019-10-25 11:33:37 +00:00
|
|
|
context.update({
|
2019-10-30 13:36:03 +00:00
|
|
|
'services_menu': services_menu,
|
2019-10-25 11:33:37 +00:00
|
|
|
'version': get_version(),
|
2022-02-26 18:54:27 +00:00
|
|
|
'languages': settings.LANGUAGES,
|
2019-10-25 11:33:37 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return context
|
2019-10-30 13:06:55 +00:00
|
|
|
|
|
|
|
|
2019-10-31 16:16:51 +00:00
|
|
|
class ExtendedPaginationMixin:
|
|
|
|
paginate_by = 20
|
|
|
|
paginate_by_kwarg = 'per_page'
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context.update({
|
|
|
|
'per_page_values': [5, 10, 20, 50],
|
|
|
|
'per_page_param': self.paginate_by_kwarg,
|
|
|
|
})
|
|
|
|
return context
|
|
|
|
|
|
|
|
def get_paginate_by(self, queryset):
|
|
|
|
per_page = self.request.GET.get(self.paginate_by_kwarg) or self.paginate_by
|
|
|
|
try:
|
|
|
|
paginate_by = int(per_page)
|
|
|
|
except ValueError:
|
|
|
|
paginate_by = self.paginate_by
|
|
|
|
return paginate_by
|
|
|
|
|
|
|
|
|
2019-10-30 13:06:55 +00:00
|
|
|
class UserTokenRequiredMixin(UserPassesTestMixin):
|
2019-12-10 12:04:04 +00:00
|
|
|
"""
|
|
|
|
Checks that the request has a token that authenticates him/her.
|
|
|
|
If the user is logged adds context variable 'profile' with its information.
|
|
|
|
"""
|
|
|
|
|
2019-10-30 13:06:55 +00:00
|
|
|
def test_func(self):
|
|
|
|
"""Check that the user has an authorized token."""
|
|
|
|
token = self.request.session.get(SESSION_KEY_TOKEN, None)
|
|
|
|
if token is None:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# initialize orchestra api orm
|
|
|
|
self.orchestra = api.Orchestra(auth_token=token)
|
|
|
|
|
|
|
|
# verify if the token is valid
|
|
|
|
if self.orchestra.verify_credentials() is None:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
2019-12-10 12:04:04 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context.update({
|
|
|
|
'profile': self.orchestra.retrieve_profile(),
|
|
|
|
})
|
|
|
|
return context
|