1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-07 09:51:26 +00:00
Files
seahub/seahub/base/context_processors.py

192 lines
7.5 KiB
Python
Raw Normal View History

2016-07-26 10:47:45 +08:00
# Copyright (c) 2012-2016 Seafile Ltd.
"""
A set of request processors that return dictionaries to be merged into a
template context. Each function takes the request object as its only parameter
and returns a dictionary to add to the context.
These are referenced from the setting TEMPLATE_CONTEXT_PROCESSORS and used by
RequestContext.
"""
import re
2017-05-17 16:59:38 +08:00
import os
2015-09-21 11:48:07 +08:00
2016-03-13 09:42:40 +08:00
from django.conf import settings as dj_settings
from django.utils.functional import lazy
2015-09-21 11:48:07 +08:00
from constance import config
2021-01-22 18:31:53 +08:00
import seaserv
from seahub.settings import SEAFILE_VERSION, SITE_DESCRIPTION, \
2018-07-06 22:41:25 +08:00
MAX_FILE_NAME, LOGO_PATH, BRANDING_CSS, LOGO_WIDTH, LOGO_HEIGHT,\
SHOW_REPO_DOWNLOAD_BUTTON, SITE_ROOT, ENABLE_GUEST_INVITATION, \
2022-01-07 17:01:07 +08:00
FAVICON_PATH, APPLE_TOUCH_ICON_PATH, THUMBNAIL_SIZE_FOR_ORIGINAL, \
2019-04-17 10:32:32 +08:00
MEDIA_ROOT, SHOW_LOGOUT_ICON, CUSTOM_LOGO_PATH, CUSTOM_FAVICON_PATH, \
2019-10-12 16:37:53 +08:00
ENABLE_SEAFILE_DOCS, LOGIN_BG_IMAGE_PATH, \
CUSTOM_LOGIN_BG_PATH, ENABLE_SHARE_LINK_REPORT_ABUSE, \
PRIVACY_POLICY_LINK, TERMS_OF_SERVICE_LINK, ENABLE_SEADOC, ENABLE_SEAFILE_AI
2023-03-07 18:11:33 +08:00
from seahub.organizations.models import OrgAdminSettings
from seahub.organizations.settings import ORG_ENABLE_ADMIN_CUSTOM_LOGO
from seahub.onlyoffice.settings import ENABLE_ONLYOFFICE, ONLYOFFICE_CONVERTER_EXTENSIONS
2017-09-09 15:27:04 +08:00
from seahub.constants import DEFAULT_ADMIN
2019-01-24 15:41:01 +08:00
from seahub.utils import get_site_name, get_service_url
2019-07-01 12:38:58 +08:00
from seahub.avatar.templatetags.avatar_tags import api_avatar_url
2017-09-09 15:27:04 +08:00
2023-08-25 10:23:01 +08:00
from seahub.utils import HAS_FILE_SEARCH, EVENTS_ENABLED, is_pro_version, ENABLE_REPO_AUTO_DEL, \
IS_DB_SQLITE3
2013-03-09 14:15:53 +08:00
try:
from seahub.settings import MULTI_TENANCY
except ImportError:
MULTI_TENANCY = False
try:
from seahub.settings import ENABLE_FILE_SCAN
except ImportError:
ENABLE_FILE_SCAN = False
2019-07-08 15:44:38 +08:00
from seahub.work_weixin.settings import ENABLE_WORK_WEIXIN
from seahub.weixin.settings import ENABLE_WEIXIN
try:
from seahub.settings import SIDE_NAV_FOOTER_CUSTOM_HTML
except ImportError:
SIDE_NAV_FOOTER_CUSTOM_HTML = ''
try:
from seahub.settings import ABOUT_DIALOG_CUSTOM_HTML
except ImportError:
ABOUT_DIALOG_CUSTOM_HTML = ''
def base(request):
"""
Add seahub base configure to the context.
2016-03-16 13:30:00 +08:00
"""
2012-08-31 17:28:50 +08:00
try:
org = request.user.org
except AttributeError:
org = None
2014-02-08 14:44:44 +08:00
# extra repo id from request path, use in search
repo_id_patt = r".*/([a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12})/.*"
m = re.match(repo_id_patt, request.get_full_path())
search_repo_id = m.group(1) if m is not None else None
2016-07-04 15:31:55 +08:00
file_server_root = config.FILE_SERVER_ROOT
if not file_server_root.endswith('/'):
file_server_root += '/'
2017-05-17 16:59:38 +08:00
logo_path = LOGO_PATH
favicon_path = FAVICON_PATH
login_bg_path = LOGIN_BG_IMAGE_PATH
2017-05-17 16:59:38 +08:00
# filter ajax/api request out
avatar_url = ''
username = request.user.username
2023-06-12 09:53:31 +08:00
if (not request.headers.get('x-requested-with') == 'XMLHttpRequest') and ("api2/" not in request.path) and \
2017-05-17 16:59:38 +08:00
("api/v2.1/" not in request.path):
# get logo path
custom_logo_file = os.path.join(MEDIA_ROOT, CUSTOM_LOGO_PATH)
if os.path.exists(custom_logo_file):
logo_path = CUSTOM_LOGO_PATH
2023-03-07 18:11:33 +08:00
if ORG_ENABLE_ADMIN_CUSTOM_LOGO and org:
org_logo_url = OrgAdminSettings.objects.get_org_logo_url(org.org_id)
if org_logo_url:
logo_path = org_logo_url
2017-05-17 16:59:38 +08:00
# get favicon path
custom_favicon_file = os.path.join(MEDIA_ROOT, CUSTOM_FAVICON_PATH)
if os.path.exists(custom_favicon_file):
favicon_path = CUSTOM_FAVICON_PATH
# get login bg path
custom_login_bg_file = os.path.join(MEDIA_ROOT, CUSTOM_LOGIN_BG_PATH)
if os.path.exists(custom_login_bg_file):
login_bg_path = CUSTOM_LOGIN_BG_PATH
2019-07-01 12:38:58 +08:00
avatar_url, is_default, date_uploaded = api_avatar_url(username, 72)
2017-09-09 15:27:04 +08:00
result = {
'seafile_version': SEAFILE_VERSION,
2018-04-08 12:48:51 +08:00
'site_title': config.SITE_TITLE,
'site_description': SITE_DESCRIPTION,
2018-07-06 22:41:25 +08:00
'branding_css': BRANDING_CSS,
'enable_branding_css': config.ENABLE_BRANDING_CSS,
2017-05-17 16:59:38 +08:00
'favicon_path': favicon_path,
2021-12-03 17:33:08 +08:00
'apple_touch_icon_path': APPLE_TOUCH_ICON_PATH,
'login_bg_path': login_bg_path,
2017-05-17 16:59:38 +08:00
'logo_path': logo_path,
'logo_width': LOGO_WIDTH,
'logo_height': LOGO_HEIGHT,
2012-08-01 22:34:35 +08:00
'cloud_mode': request.cloud_mode,
2012-08-31 17:28:50 +08:00
'org': org,
2018-04-08 12:48:51 +08:00
'site_name': get_site_name(),
2015-09-21 11:48:07 +08:00
'enable_signup': config.ENABLE_SIGNUP,
'enable_weixin': ENABLE_WEIXIN,
2012-12-25 15:05:12 +08:00
'max_file_name': MAX_FILE_NAME,
'has_file_search': HAS_FILE_SEARCH,
'show_repo_download_button': SHOW_REPO_DOWNLOAD_BUTTON,
'share_link_force_use_password': config.SHARE_LINK_FORCE_USE_PASSWORD,
'share_link_password_min_length': config.SHARE_LINK_PASSWORD_MIN_LENGTH,
'share_link_password_strength_level': config.SHARE_LINK_PASSWORD_STRENGTH_LEVEL,
2015-09-21 11:48:07 +08:00
'repo_password_min_length': config.REPO_PASSWORD_MIN_LENGTH,
'events_enabled': EVENTS_ENABLED,
'sysadmin_extra_enabled': True if is_pro_version() else False,
'multi_tenancy': MULTI_TENANCY,
2016-03-13 09:42:40 +08:00
'multi_institution': getattr(dj_settings, 'MULTI_INSTITUTION', False),
'search_repo_id': search_repo_id,
2015-10-12 18:36:24 +08:00
'SITE_ROOT': SITE_ROOT,
'CSRF_COOKIE_NAME': dj_settings.CSRF_COOKIE_NAME,
'constance_enabled': dj_settings.CONSTANCE_ENABLED,
2016-07-04 15:31:55 +08:00
'FILE_SERVER_ROOT': file_server_root,
2021-01-22 18:31:53 +08:00
'USE_GO_FILESERVER': seaserv.USE_GO_FILESERVER if hasattr(seaserv, 'USE_GO_FILESERVER') else False,
'LOGIN_URL': dj_settings.LOGIN_URL,
'enableOnlyoffice': ENABLE_ONLYOFFICE,
'onlyofficeConverterExtensions': ONLYOFFICE_CONVERTER_EXTENSIONS,
'thumbnail_size_for_original': THUMBNAIL_SIZE_FOR_ORIGINAL,
2016-07-20 15:30:39 +08:00
'enable_guest_invitation': ENABLE_GUEST_INVITATION,
2018-06-05 17:32:48 +08:00
'enable_terms_and_conditions': config.ENABLE_TERMS_AND_CONDITIONS,
2017-08-07 13:56:25 +08:00
'show_logout_icon': SHOW_LOGOUT_ICON,
2017-03-14 15:45:32 +08:00
'is_pro': True if is_pro_version() else False,
2023-08-25 10:23:01 +08:00
'is_db_sqlite3': IS_DB_SQLITE3,
2019-04-17 10:32:32 +08:00
'is_docs': ENABLE_SEAFILE_DOCS,
2018-11-14 10:55:11 +08:00
'enable_upload_folder': dj_settings.ENABLE_UPLOAD_FOLDER,
2019-01-24 15:41:01 +08:00
'enable_resumable_fileupload': dj_settings.ENABLE_RESUMABLE_FILEUPLOAD,
'service_url': get_service_url().rstrip('/'),
'enable_file_scan': ENABLE_FILE_SCAN,
2019-07-08 15:44:38 +08:00
'enable_work_weixin': ENABLE_WORK_WEIXIN,
'avatar_url': avatar_url if avatar_url else '',
'privacy_policy_link': PRIVACY_POLICY_LINK,
'terms_of_service_link': TERMS_OF_SERVICE_LINK,
'side_nav_footer_custom_html': SIDE_NAV_FOOTER_CUSTOM_HTML,
'about_dialog_custom_html': ABOUT_DIALOG_CUSTOM_HTML,
'enable_repo_auto_del': ENABLE_REPO_AUTO_DEL,
'enable_seadoc': ENABLE_SEADOC,
'enable_seafile_ai': ENABLE_SEAFILE_AI,
2017-09-09 15:27:04 +08:00
}
if request.user.is_staff:
result['is_default_admin'] = request.user.admin_role == DEFAULT_ADMIN
result['enable_share_link_report_abuse'] = ENABLE_SHARE_LINK_REPORT_ABUSE
2017-09-09 15:27:04 +08:00
return result
def debug(request):
"""
Returns context variables helpful for debugging.
"""
context_extras = {}
if dj_settings.DEBUG and request.META.get('REMOTE_ADDR') in dj_settings.INTERNAL_IPS or \
2019-11-27 13:24:26 +08:00
dj_settings.DEBUG and request.GET.get('_dev', '') == '1':
context_extras['debug'] = True
from django.db import connection
# Return a lazy reference that computes connection.queries on access,
# to ensure it contains queries triggered after this function runs.
context_extras['sql_queries'] = lazy(lambda: connection.queries, list)
return context_extras