Browse Source

Adaption to new libraries

master
Dirk Alders 2 months ago
parent
commit
5f1420a0a9
6 changed files with 97 additions and 80 deletions
  1. 59
    37
      context.py
  2. 8
    10
      models.py
  3. 1
    5
      search.py
  4. 4
    7
      signals.py
  5. 24
    15
      views/__init__.py
  6. 1
    6
      views/xnail.py

+ 59
- 37
context.py View File

1
+from django.conf import settings
1
 from django.utils.translation import gettext as _
2
 from django.utils.translation import gettext as _
2
 from .help import actionbar as actionbar_add_help
3
 from .help import actionbar as actionbar_add_help
4
+import inspect
5
+import logging
3
 import os
6
 import os
4
 from themes import gray_icon_url, color_icon_url
7
 from themes import gray_icon_url, color_icon_url
5
 from users.context import menubar as menubar_user
8
 from users.context import menubar as menubar_user
7
 import pygal
10
 import pygal
8
 from pygal.models import get_item_by_rel_path
11
 from pygal.models import get_item_by_rel_path
9
 
12
 
13
+logger = logging.getLogger(settings.ROOT_LOGGER_NAME).getChild(__name__)
14
+
15
+
10
 MY_FAVOURITES_ENTRY_UID = 'my_favourites-main'
16
 MY_FAVOURITES_ENTRY_UID = 'my_favourites-main'
11
 HELP_UID = 'help-main'
17
 HELP_UID = 'help-main'
12
 NAVIGATION_ENTRY_UID = 'navigation-main-%s'
18
 NAVIGATION_ENTRY_UID = 'navigation-main-%s'
15
 BACK_ENTRY_UID = 'back-main'
21
 BACK_ENTRY_UID = 'back-main'
16
 
22
 
17
 
23
 
18
-def context_adaption(context, request, rel_path, wrapper_instance=None, title='', current_help_page=None):
19
-    context[context.MENUBAR].append_entry(HELP_UID, _('Help'), color_icon_url(request, 'help.png'), pygal.url_helpview(request, 'main'), True, False)
20
-    menubar_user(context[context.MENUBAR], request)
21
-    menubar(context[context.MENUBAR], request, rel_path)
22
-    navigationbar(context[context.NAVIGATIONBAR], request, rel_path)
24
+def context_adaption(context, request, **kwargs):
25
+    caller_name = inspect.currentframe().f_back.f_code.co_name
26
+    wrapper_instance = kwargs.get("wrapper_instance")
27
+    #
28
+    menubar(context, request, **kwargs)
29
+    navigationbar(context, request, **kwargs)
23
     if wrapper_instance is None:
30
     if wrapper_instance is None:
24
         # Pages without direct connection to an item (e.g. Helpview, ...)
31
         # Pages without direct connection to an item (e.g. Helpview, ...)
25
-        context.set_additional_title(title)
26
-        context[context.ACTIONBAR].append_entry(
27
-            BACK_ENTRY_UID,                             # uid
28
-            _('Back'),                                  # name
29
-            color_icon_url(request, 'back.png'),        # icon
30
-            'javascript:history.back()',                # url
31
-            True,                                       # left
32
-            False                                       # active
33
-        )
32
+        context.set_additional_title(kwargs.get("title", ""))
33
+        #
34
+        actionbar(context, request, caller_name)
34
     else:
35
     else:
35
         wrapper_instance.context_adaption(context)
36
         wrapper_instance.context_adaption(context)
36
     # HELP
37
     # HELP
37
     if pygal.is_helpview(request):
38
     if pygal.is_helpview(request):
38
-        actionbar_add_help(context, request, current_help_page=current_help_page)
39
-
40
-
41
-def menubar(bar, request, rel_path):
42
-    if request.user.is_authenticated:
43
-        bar.append_entry(*my_favourites_entry_parameters(request, rel_path))
44
-    try:
45
-        bar.replace_entry(PROFILE_ENTRY_UID, *profile_entry_parameters(request))
46
-    except ValueError:
47
-        pass        # Profile entry does not exist, so exchange is not needed (e.g. no user is logged in)
48
-
49
-
50
-def navigationbar(bar, request, path):
39
+        actionbar_add_help(context, request, current_help_page=kwargs.get("current_help_page"))
40
+    for key in kwargs:
41
+        context[key] = kwargs[key]
42
+    logger.debug("context adapted: %s", repr(context))
43
+
44
+def navigationbar(context, request, **kwargs):
45
+    bar = context[context.NAVIGATIONBAR]
46
+    rel_path = kwargs.get("rel_path", '')
47
+    #
51
     if pygal.is_favouriteview(request):
48
     if pygal.is_favouriteview(request):
52
-        if path:
53
-            bar.append_entry(*navigation_entry_parameters(request, path, None))
54
-            anchor = get_item_by_rel_path(path).name
49
+        if rel_path:
50
+            bar.append_entry(*navigation_entry_parameters(request, rel_path, None))
51
+            anchor = get_item_by_rel_path(rel_path).name
55
         else:
52
         else:
56
             anchor = None
53
             anchor = None
57
     elif pygal.is_searchview(request):
54
     elif pygal.is_searchview(request):
58
-        if path:
59
-            bar.append_entry(*navigation_entry_parameters(request, path, None))
60
-            anchor = get_item_by_rel_path(path).name
55
+        if rel_path:
56
+            bar.append_entry(*navigation_entry_parameters(request, rel_path, None))
57
+            anchor = get_item_by_rel_path(rel_path).name
61
         else:
58
         else:
62
             anchor = None
59
             anchor = None
63
     else:
60
     else:
64
         anchor = None
61
         anchor = None
65
-        while len(path) > 0 and path != os.path.sep:
66
-            bar.append_entry(*navigation_entry_parameters(request, path, anchor))
67
-            anchor = get_item_by_rel_path(path).name
68
-            path = os.path.dirname(path)
62
+        while len(rel_path) > 0 and rel_path != os.path.sep:
63
+            bar.append_entry(*navigation_entry_parameters(request, rel_path, anchor))
64
+            anchor = get_item_by_rel_path(rel_path).name
65
+            rel_path = os.path.dirname(rel_path)
69
     bar.append_entry(*home_entry_parameters(request, anchor))
66
     bar.append_entry(*home_entry_parameters(request, anchor))
70
 
67
 
71
 
68
 
69
+def menubar(context, request, **kwargs):
70
+    bar = context[context.MENUBAR]
71
+    rel_path = kwargs.get("rel_path", '')
72
+    #
73
+    bar.append_entry(HELP_UID, _('Help'), color_icon_url(request, 'help.png'), pygal.url_helpview(request, 'main'), True, False)
74
+    menubar_user(bar, request)
75
+    if request.user.is_authenticated:
76
+        bar.append_entry(*my_favourites_entry_parameters(request, rel_path))
77
+    try:
78
+        bar.replace_entry(PROFILE_ENTRY_UID, *profile_entry_parameters(request))
79
+    except ValueError:
80
+        pass        # Profile entry does not exist, so exchange is not needed (e.g. no user is logged in)
81
+
82
+
83
+def actionbar(context, request, caller_name, **kwargs):
84
+    bar = context[context.ACTIONBAR]
85
+    bar.append_entry(
86
+        BACK_ENTRY_UID,                             # uid
87
+        _('Back'),                                  # name
88
+        color_icon_url(request, 'back.png'),        # icon
89
+        'javascript:history.back()',                # url
90
+        True,                                       # left
91
+        False                                       # active
92
+    )
93
+
72
 def profile_entry_parameters(request):
94
 def profile_entry_parameters(request):
73
     return (
95
     return (
74
         PROFILE_ENTRY_UID,                          # uid
96
         PROFILE_ENTRY_UID,                          # uid

+ 8
- 10
models.py View File

1
 import datetime
1
 import datetime
2
+from django.conf import settings
2
 from django.contrib.auth.models import User
3
 from django.contrib.auth.models import User
3
 from django.db import models
4
 from django.db import models
4
 from django.utils import formats, timezone
5
 from django.utils import formats, timezone
11
 import pygal
12
 import pygal
12
 import time
13
 import time
13
 
14
 
15
+logger = logging.getLogger(settings.ROOT_LOGGER_NAME).getChild(__name__)
16
+
17
+
14
 DEBUG = False
18
 DEBUG = False
15
 
19
 
16
 
20
 
25
 EXTENTIONS_VIDEO = ['.avi', '.mpg', '.mpeg', '.mpe', '.mov', '.qt', '.mp4', '.webm', '.ogv', '.flv', '.3gp', ]
29
 EXTENTIONS_VIDEO = ['.avi', '.mpg', '.mpeg', '.mpe', '.mov', '.qt', '.mp4', '.webm', '.ogv', '.flv', '.3gp', ]
26
 
30
 
27
 
31
 
28
-# Get a logger instance
29
-try:
30
-    from config import APP_NAME as ROOT_LOGGER_NAME
31
-except ImportError:
32
-    ROOT_LOGGER_NAME = 'root'
33
-logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
34
-
35
-
36
 def get_item_type(full_path):
32
 def get_item_type(full_path):
37
     if os.path.isdir(full_path):
33
     if os.path.isdir(full_path):
38
         return TYPE_FOLDER
34
         return TYPE_FOLDER
273
         return reverse('admin:%s_%s_change' % info, args=(self.pk,))
269
         return reverse('admin:%s_%s_change' % info, args=(self.pk,))
274
 
270
 
275
     def suspended(self, user):
271
     def suspended(self, user):
276
-        if pygal.suspend_public() and not user.is_authenticated:
277
-            return True
272
+        return pygal.suspend_public() and not user.is_authenticated
278
 
273
 
279
     def may_read(self, user):
274
     def may_read(self, user):
280
         if self.suspended(user):
275
         if self.suspended(user):
276
+            logger.info("Permiision denied to '%s' due to suspended not authenticated user.", self.name)
281
             return False
277
             return False
282
         elif self.type == TYPE_FOLDER:
278
         elif self.type == TYPE_FOLDER:
283
             return True
279
             return True
286
             if parent.public_access is True:
282
             if parent.public_access is True:
287
                 return True
283
                 return True
288
             if user is None:
284
             if user is None:
285
+                logger.info("Permiision denied to %s due to user is None.", self.name)
289
                 return False
286
                 return False
290
             if not user.is_authenticated:
287
             if not user.is_authenticated:
288
+                logger.info("Permiision denied to %s due to user is not authenticated.", self.name)
291
                 return False
289
                 return False
292
             if user.is_superuser:
290
             if user.is_superuser:
293
                 return True
291
                 return True

+ 1
- 5
search.py View File

8
 from whoosh import index, qparser
8
 from whoosh import index, qparser
9
 from pygal.models import TYPE_FOLDER
9
 from pygal.models import TYPE_FOLDER
10
 
10
 
11
-try:
12
-    from config import APP_NAME as ROOT_LOGGER_NAME
13
-except ImportError:
14
-    ROOT_LOGGER_NAME = 'root'
15
-logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
11
+logger = logging.getLogger(settings.ROOT_LOGGER_NAME).getChild(__name__)
16
 
12
 
17
 
13
 
18
 SCHEMA = Schema(
14
 SCHEMA = Schema(

+ 4
- 7
signals.py View File

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

+ 24
- 15
views/__init__.py View File

73
 def pygal_search(request, rel_path):
73
 def pygal_search(request, rel_path):
74
     context = Context(request)      # needs to be executed first because of time mesurement
74
     context = Context(request)      # needs to be executed first because of time mesurement
75
     w = query_view(request, search_result_query(request))
75
     w = query_view(request, search_result_query(request))
76
-    context_adaption(context, request, '', wrapper_instance=w)
76
+    context_adaption(
77
+        context, 
78
+        request,
79
+        wrapper_instance=w
80
+    )
77
     context.set_additional_title(w.name)
81
     context.set_additional_title(w.name)
78
     return w.render(context)
82
     return w.render(context)
79
 
83
 
128
     context_adaption(
132
     context_adaption(
129
         context,                            # the base context
133
         context,                            # the base context
130
         request,                            # the request object to be used in context_adaption
134
         request,                            # the request object to be used in context_adaption
131
-        rel_path='',                      # the current help_page to identify which taskbar entry has to be highlighted
132
         title=_('Help'),                    # the title for the page (template)
135
         title=_('Help'),                    # the title for the page (template)
133
         current_help_page=page,
136
         current_help_page=page,
134
     )
137
     )
135
     context['help_content'] = help_content  # the help content itself (template)
138
     context['help_content'] = help_content  # the help content itself (template)
136
     return render(request, 'pygal/help.html', context=context)
139
     return render(request, 'pygal/help.html', context=context)
137
 
140
 
138
-    context = Context(request)      # needs to be executed first because of time mesurement
139
-    context_adaption(context, request, '', title=_('Help'))
140
-    return help_data(request, context)
141
-
142
 
141
 
143
 def pygal_userview_infoview(request, responsetype, rel_path):
142
 def pygal_userview_infoview(request, responsetype, rel_path):
144
     context = Context(request)      # needs to be executed first because of time mesurement
143
     context = Context(request)      # needs to be executed first because of time mesurement
149
         else:
148
         else:
150
             w = get_userview_wrapper_instance(full_path, request)
149
             w = get_userview_wrapper_instance(full_path, request)
151
     except LookupError:
150
     except LookupError:
152
-        context_adaption(context, request, '')
151
+        context_adaption(context, request)
153
         return pygal_item_does_not_exist(request, context)
152
         return pygal_item_does_not_exist(request, context)
154
-    context_adaption(context, request, rel_path, wrapper_instance=w)
153
+    context_adaption(
154
+        context,
155
+        request,
156
+        rel_path=rel_path,
157
+        wrapper_instance=w
158
+    )
155
     #
159
     #
156
     if w.may_read():
160
     if w.may_read():
157
         context.set_additional_title(w.name)
161
         context.set_additional_title(w.name)
194
     current_thumbnail_size = pygal.get_thumbnail_size(request)
198
     current_thumbnail_size = pygal.get_thumbnail_size(request)
195
     current_webnail_size = pygal.get_webnail_size(request)
199
     current_webnail_size = pygal.get_webnail_size(request)
196
     profile_pre_actions(request, context, UserProfileFormLanguageOnly)
200
     profile_pre_actions(request, context, UserProfileFormLanguageOnly)
197
-    context_adaption(context, request, '', title=_('Profile for %(username)s') % {'username': request.user.username})
198
-    context['THUMBNAIL_SIZES'] = settings.THUMBNAIL_SIZES
199
-    context['thumbnail_size'] = current_thumbnail_size
200
-    context['WEBNAIL_SIZES'] = settings.WEBNAIL_SIZES
201
-    context['webnail_size'] = current_webnail_size
201
+    context_adaption(
202
+        context,
203
+        request,
204
+        title=_('Profile for %(username)s') % {'username': request.user.username},
205
+        #
206
+        THUMBNAIL_SIZES=settings.THUMBNAIL_SIZES,
207
+        thumbnail_size=current_thumbnail_size,
208
+        WEBNAIL_SIZES=settings.WEBNAIL_SIZES,
209
+        webnail_size=current_webnail_size
210
+    )
202
     if request.POST:
211
     if request.POST:
203
         # store thumbnail size, if changed
212
         # store thumbnail size, if changed
204
         thumbnail_size = int(request.POST.get('thumbnail_size'))
213
         thumbnail_size = int(request.POST.get('thumbnail_size'))
241
         context_adaption(
250
         context_adaption(
242
             context,
251
             context,
243
             request,
252
             request,
244
-            rel_path,
253
+            rel_path=rel_path,
245
             title=_('Add Tag for %s') % w.name
254
             title=_('Add Tag for %s') % w.name
246
         )
255
         )
247
         tag_context_adaption(context, w, request, get_item_type(full_path) in [TYPE_IMAGE])
256
         tag_context_adaption(context, w, request, get_item_type(full_path) in [TYPE_IMAGE])
273
         context_adaption(
282
         context_adaption(
274
             context,
283
             context,
275
             request,
284
             request,
276
-            t.item.rel_path,
285
+            rel_path=t.item.rel_path,
277
             title=_('Edit Tag %(tag_id)d for %(item_name)s') % {'tag_id': tag_id, 'item_name': w.name}
286
             title=_('Edit Tag %(tag_id)d for %(item_name)s') % {'tag_id': tag_id, 'item_name': w.name}
278
         )
287
         )
279
         tag_context_adaption(context, w, request, t.item.type in [TYPE_IMAGE])
288
         tag_context_adaption(context, w, request, t.item.type in [TYPE_IMAGE])

+ 1
- 6
views/xnail.py View File

7
 import os
7
 import os
8
 import pygal
8
 import pygal
9
 
9
 
10
-# Get a logger instance
11
-try:
12
-    from config import APP_NAME as ROOT_LOGGER_NAME
13
-except ImportError:
14
-    ROOT_LOGGER_NAME = 'root'
15
-logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
10
+logger = logging.getLogger(settings.ROOT_LOGGER_NAME).getChild(__name__)
16
 
11
 
17
 
12
 
18
 def get_image_class(full_path):
13
 def get_image_class(full_path):

Loading…
Cancel
Save