Adaption to new libraries

This commit is contained in:
Dirk Alders 2024-10-10 22:38:00 +02:00
parent 114a82e176
commit 5f1420a0a9
6 changed files with 97 additions and 80 deletions

View File

@ -1,5 +1,8 @@
from django.conf import settings
from django.utils.translation import gettext as _
from .help import actionbar as actionbar_add_help
import inspect
import logging
import os
from themes import gray_icon_url, color_icon_url
from users.context import menubar as menubar_user
@ -7,6 +10,9 @@ from users.context import PROFILE_ENTRY_UID
import pygal
from pygal.models import get_item_by_rel_path
logger = logging.getLogger(settings.ROOT_LOGGER_NAME).getChild(__name__)
MY_FAVOURITES_ENTRY_UID = 'my_favourites-main'
HELP_UID = 'help-main'
NAVIGATION_ENTRY_UID = 'navigation-main-%s'
@ -15,30 +21,57 @@ SEARCH_ENTRY_UID = 'search-main'
BACK_ENTRY_UID = 'back-main'
def context_adaption(context, request, rel_path, wrapper_instance=None, title='', current_help_page=None):
context[context.MENUBAR].append_entry(HELP_UID, _('Help'), color_icon_url(request, 'help.png'), pygal.url_helpview(request, 'main'), True, False)
menubar_user(context[context.MENUBAR], request)
menubar(context[context.MENUBAR], request, rel_path)
navigationbar(context[context.NAVIGATIONBAR], request, rel_path)
def context_adaption(context, request, **kwargs):
caller_name = inspect.currentframe().f_back.f_code.co_name
wrapper_instance = kwargs.get("wrapper_instance")
#
menubar(context, request, **kwargs)
navigationbar(context, request, **kwargs)
if wrapper_instance is None:
# Pages without direct connection to an item (e.g. Helpview, ...)
context.set_additional_title(title)
context[context.ACTIONBAR].append_entry(
BACK_ENTRY_UID, # uid
_('Back'), # name
color_icon_url(request, 'back.png'), # icon
'javascript:history.back()', # url
True, # left
False # active
)
context.set_additional_title(kwargs.get("title", ""))
#
actionbar(context, request, caller_name)
else:
wrapper_instance.context_adaption(context)
# HELP
if pygal.is_helpview(request):
actionbar_add_help(context, request, current_help_page=current_help_page)
actionbar_add_help(context, request, current_help_page=kwargs.get("current_help_page"))
for key in kwargs:
context[key] = kwargs[key]
logger.debug("context adapted: %s", repr(context))
def navigationbar(context, request, **kwargs):
bar = context[context.NAVIGATIONBAR]
rel_path = kwargs.get("rel_path", '')
#
if pygal.is_favouriteview(request):
if rel_path:
bar.append_entry(*navigation_entry_parameters(request, rel_path, None))
anchor = get_item_by_rel_path(rel_path).name
else:
anchor = None
elif pygal.is_searchview(request):
if rel_path:
bar.append_entry(*navigation_entry_parameters(request, rel_path, None))
anchor = get_item_by_rel_path(rel_path).name
else:
anchor = None
else:
anchor = None
while len(rel_path) > 0 and rel_path != os.path.sep:
bar.append_entry(*navigation_entry_parameters(request, rel_path, anchor))
anchor = get_item_by_rel_path(rel_path).name
rel_path = os.path.dirname(rel_path)
bar.append_entry(*home_entry_parameters(request, anchor))
def menubar(bar, request, rel_path):
def menubar(context, request, **kwargs):
bar = context[context.MENUBAR]
rel_path = kwargs.get("rel_path", '')
#
bar.append_entry(HELP_UID, _('Help'), color_icon_url(request, 'help.png'), pygal.url_helpview(request, 'main'), True, False)
menubar_user(bar, request)
if request.user.is_authenticated:
bar.append_entry(*my_favourites_entry_parameters(request, rel_path))
try:
@ -47,27 +80,16 @@ def menubar(bar, request, rel_path):
pass # Profile entry does not exist, so exchange is not needed (e.g. no user is logged in)
def navigationbar(bar, request, path):
if pygal.is_favouriteview(request):
if path:
bar.append_entry(*navigation_entry_parameters(request, path, None))
anchor = get_item_by_rel_path(path).name
else:
anchor = None
elif pygal.is_searchview(request):
if path:
bar.append_entry(*navigation_entry_parameters(request, path, None))
anchor = get_item_by_rel_path(path).name
else:
anchor = None
else:
anchor = None
while len(path) > 0 and path != os.path.sep:
bar.append_entry(*navigation_entry_parameters(request, path, anchor))
anchor = get_item_by_rel_path(path).name
path = os.path.dirname(path)
bar.append_entry(*home_entry_parameters(request, anchor))
def actionbar(context, request, caller_name, **kwargs):
bar = context[context.ACTIONBAR]
bar.append_entry(
BACK_ENTRY_UID, # uid
_('Back'), # name
color_icon_url(request, 'back.png'), # icon
'javascript:history.back()', # url
True, # left
False # active
)
def profile_entry_parameters(request):
return (

View File

@ -1,4 +1,5 @@
import datetime
from django.conf import settings
from django.contrib.auth.models import User
from django.db import models
from django.utils import formats, timezone
@ -11,6 +12,9 @@ import os
import pygal
import time
logger = logging.getLogger(settings.ROOT_LOGGER_NAME).getChild(__name__)
DEBUG = False
@ -25,14 +29,6 @@ EXTENTIONS_AUDIO = ['.mp3', ]
EXTENTIONS_VIDEO = ['.avi', '.mpg', '.mpeg', '.mpe', '.mov', '.qt', '.mp4', '.webm', '.ogv', '.flv', '.3gp', ]
# Get a logger instance
try:
from config import APP_NAME as ROOT_LOGGER_NAME
except ImportError:
ROOT_LOGGER_NAME = 'root'
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
def get_item_type(full_path):
if os.path.isdir(full_path):
return TYPE_FOLDER
@ -273,11 +269,11 @@ class Item(models.Model):
return reverse('admin:%s_%s_change' % info, args=(self.pk,))
def suspended(self, user):
if pygal.suspend_public() and not user.is_authenticated:
return True
return pygal.suspend_public() and not user.is_authenticated
def may_read(self, user):
if self.suspended(user):
logger.info("Permiision denied to '%s' due to suspended not authenticated user.", self.name)
return False
elif self.type == TYPE_FOLDER:
return True
@ -286,8 +282,10 @@ class Item(models.Model):
if parent.public_access is True:
return True
if user is None:
logger.info("Permiision denied to %s due to user is None.", self.name)
return False
if not user.is_authenticated:
logger.info("Permiision denied to %s due to user is not authenticated.", self.name)
return False
if user.is_superuser:
return True

View File

@ -8,11 +8,7 @@ from whoosh.qparser.dateparse import DateParserPlugin
from whoosh import index, qparser
from pygal.models import TYPE_FOLDER
try:
from config import APP_NAME as ROOT_LOGGER_NAME
except ImportError:
ROOT_LOGGER_NAME = 'root'
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
logger = logging.getLogger(settings.ROOT_LOGGER_NAME).getChild(__name__)
SCHEMA = Schema(

View File

@ -1,3 +1,4 @@
from django.conf import settings
from django.db.models.signals import post_save
from django.db.models.signals import post_delete
from django.dispatch import receiver
@ -7,12 +8,8 @@ from .search import load_index, delete_item, update_item
import shutil
from .views.xnail import base_item
# Get a logger instance
try:
from config import APP_NAME as ROOT_LOGGER_NAME
except ImportError:
ROOT_LOGGER_NAME = 'root'
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
logger = logging.getLogger(settings.ROOT_LOGGER_NAME).getChild(__name__)
__pre_remove__ = None
__pre_add__ = None
@ -21,7 +18,7 @@ __pre_add__ = None
@receiver(post_delete, sender=Item)
def item_delete(instance, **kwargs):
# delete cached xnails
clogger.info('Deleting Xnails stored in "%s".', base_item.__cache_image_folder__(None, instance.rel_path))
logger.info('Deleting Xnails stored in "%s".', base_item.__cache_image_folder__(None, instance.rel_path))
shutil.rmtree(base_item.__cache_image_folder__(None, instance.rel_path), True)
# delete index entry
ix = load_index()

View File

@ -73,7 +73,11 @@ def pygal_responses(request, responsetype=pygal.RESP_TYPE_USERVIEW, datatype=pyg
def pygal_search(request, rel_path):
context = Context(request) # needs to be executed first because of time mesurement
w = query_view(request, search_result_query(request))
context_adaption(context, request, '', wrapper_instance=w)
context_adaption(
context,
request,
wrapper_instance=w
)
context.set_additional_title(w.name)
return w.render(context)
@ -128,17 +132,12 @@ def pygal_helpview(request, page='main'):
context_adaption(
context, # the base context
request, # the request object to be used in context_adaption
rel_path='', # the current help_page to identify which taskbar entry has to be highlighted
title=_('Help'), # the title for the page (template)
current_help_page=page,
)
context['help_content'] = help_content # the help content itself (template)
return render(request, 'pygal/help.html', context=context)
context = Context(request) # needs to be executed first because of time mesurement
context_adaption(context, request, '', title=_('Help'))
return help_data(request, context)
def pygal_userview_infoview(request, responsetype, rel_path):
context = Context(request) # needs to be executed first because of time mesurement
@ -149,9 +148,14 @@ def pygal_userview_infoview(request, responsetype, rel_path):
else:
w = get_userview_wrapper_instance(full_path, request)
except LookupError:
context_adaption(context, request, '')
context_adaption(context, request)
return pygal_item_does_not_exist(request, context)
context_adaption(context, request, rel_path, wrapper_instance=w)
context_adaption(
context,
request,
rel_path=rel_path,
wrapper_instance=w
)
#
if w.may_read():
context.set_additional_title(w.name)
@ -194,11 +198,16 @@ def pygal_profile(request):
current_thumbnail_size = pygal.get_thumbnail_size(request)
current_webnail_size = pygal.get_webnail_size(request)
profile_pre_actions(request, context, UserProfileFormLanguageOnly)
context_adaption(context, request, '', title=_('Profile for %(username)s') % {'username': request.user.username})
context['THUMBNAIL_SIZES'] = settings.THUMBNAIL_SIZES
context['thumbnail_size'] = current_thumbnail_size
context['WEBNAIL_SIZES'] = settings.WEBNAIL_SIZES
context['webnail_size'] = current_webnail_size
context_adaption(
context,
request,
title=_('Profile for %(username)s') % {'username': request.user.username},
#
THUMBNAIL_SIZES=settings.THUMBNAIL_SIZES,
thumbnail_size=current_thumbnail_size,
WEBNAIL_SIZES=settings.WEBNAIL_SIZES,
webnail_size=current_webnail_size
)
if request.POST:
# store thumbnail size, if changed
thumbnail_size = int(request.POST.get('thumbnail_size'))
@ -241,7 +250,7 @@ def pygal_addtag(request, rel_path):
context_adaption(
context,
request,
rel_path,
rel_path=rel_path,
title=_('Add Tag for %s') % w.name
)
tag_context_adaption(context, w, request, get_item_type(full_path) in [TYPE_IMAGE])
@ -273,7 +282,7 @@ def pygal_tagedit(request, tag_id):
context_adaption(
context,
request,
t.item.rel_path,
rel_path=t.item.rel_path,
title=_('Edit Tag %(tag_id)d for %(item_name)s') % {'tag_id': tag_id, 'item_name': w.name}
)
tag_context_adaption(context, w, request, t.item.type in [TYPE_IMAGE])

View File

@ -7,12 +7,7 @@ from ..models import get_item_type, TYPE_IMAGE, TYPE_VIDEO
import os
import pygal
# Get a logger instance
try:
from config import APP_NAME as ROOT_LOGGER_NAME
except ImportError:
ROOT_LOGGER_NAME = 'root'
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
logger = logging.getLogger(settings.ROOT_LOGGER_NAME).getChild(__name__)
def get_image_class(full_path):