소스 검색

Adaption to new libraries

master
Dirk Alders 1 개월 전
부모
커밋
5f1420a0a9
6개의 변경된 파일97개의 추가작업 그리고 80개의 파일을 삭제
  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 파일 보기

@@ -1,5 +1,8 @@
1
+from django.conf import settings
1 2
 from django.utils.translation import gettext as _
2 3
 from .help import actionbar as actionbar_add_help
4
+import inspect
5
+import logging
3 6
 import os
4 7
 from themes import gray_icon_url, color_icon_url
5 8
 from users.context import menubar as menubar_user
@@ -7,6 +10,9 @@ from users.context import PROFILE_ENTRY_UID
7 10
 import pygal
8 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 16
 MY_FAVOURITES_ENTRY_UID = 'my_favourites-main'
11 17
 HELP_UID = 'help-main'
12 18
 NAVIGATION_ENTRY_UID = 'navigation-main-%s'
@@ -15,60 +21,76 @@ SEARCH_ENTRY_UID = 'search-main'
15 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 30
     if wrapper_instance is None:
24 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 35
     else:
35 36
         wrapper_instance.context_adaption(context)
36 37
     # HELP
37 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 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 52
         else:
56 53
             anchor = None
57 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 58
         else:
62 59
             anchor = None
63 60
     else:
64 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 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 94
 def profile_entry_parameters(request):
73 95
     return (
74 96
         PROFILE_ENTRY_UID,                          # uid

+ 8
- 10
models.py 파일 보기

@@ -1,4 +1,5 @@
1 1
 import datetime
2
+from django.conf import settings
2 3
 from django.contrib.auth.models import User
3 4
 from django.db import models
4 5
 from django.utils import formats, timezone
@@ -11,6 +12,9 @@ import os
11 12
 import pygal
12 13
 import time
13 14
 
15
+logger = logging.getLogger(settings.ROOT_LOGGER_NAME).getChild(__name__)
16
+
17
+
14 18
 DEBUG = False
15 19
 
16 20
 
@@ -25,14 +29,6 @@ EXTENTIONS_AUDIO = ['.mp3', ]
25 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 32
 def get_item_type(full_path):
37 33
     if os.path.isdir(full_path):
38 34
         return TYPE_FOLDER
@@ -273,11 +269,11 @@ class Item(models.Model):
273 269
         return reverse('admin:%s_%s_change' % info, args=(self.pk,))
274 270
 
275 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 274
     def may_read(self, user):
280 275
         if self.suspended(user):
276
+            logger.info("Permiision denied to '%s' due to suspended not authenticated user.", self.name)
281 277
             return False
282 278
         elif self.type == TYPE_FOLDER:
283 279
             return True
@@ -286,8 +282,10 @@ class Item(models.Model):
286 282
             if parent.public_access is True:
287 283
                 return True
288 284
             if user is None:
285
+                logger.info("Permiision denied to %s due to user is None.", self.name)
289 286
                 return False
290 287
             if not user.is_authenticated:
288
+                logger.info("Permiision denied to %s due to user is not authenticated.", self.name)
291 289
                 return False
292 290
             if user.is_superuser:
293 291
                 return True

+ 1
- 5
search.py 파일 보기

@@ -8,11 +8,7 @@ from whoosh.qparser.dateparse import DateParserPlugin
8 8
 from whoosh import index, qparser
9 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 14
 SCHEMA = Schema(

+ 4
- 7
signals.py 파일 보기

@@ -1,3 +1,4 @@
1
+from django.conf import settings
1 2
 from django.db.models.signals import post_save
2 3
 from django.db.models.signals import post_delete
3 4
 from django.dispatch import receiver
@@ -7,12 +8,8 @@ from .search import load_index, delete_item, update_item
7 8
 import shutil
8 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 14
 __pre_remove__ = None
18 15
 __pre_add__ = None
@@ -21,7 +18,7 @@ __pre_add__ = None
21 18
 @receiver(post_delete, sender=Item)
22 19
 def item_delete(instance, **kwargs):
23 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 22
     shutil.rmtree(base_item.__cache_image_folder__(None, instance.rel_path), True)
26 23
     # delete index entry
27 24
     ix = load_index()

+ 24
- 15
views/__init__.py 파일 보기

@@ -73,7 +73,11 @@ def pygal_responses(request, responsetype=pygal.RESP_TYPE_USERVIEW, datatype=pyg
73 73
 def pygal_search(request, rel_path):
74 74
     context = Context(request)      # needs to be executed first because of time mesurement
75 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 81
     context.set_additional_title(w.name)
78 82
     return w.render(context)
79 83
 
@@ -128,17 +132,12 @@ def pygal_helpview(request, page='main'):
128 132
     context_adaption(
129 133
         context,                            # the base context
130 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 135
         title=_('Help'),                    # the title for the page (template)
133 136
         current_help_page=page,
134 137
     )
135 138
     context['help_content'] = help_content  # the help content itself (template)
136 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 142
 def pygal_userview_infoview(request, responsetype, rel_path):
144 143
     context = Context(request)      # needs to be executed first because of time mesurement
@@ -149,9 +148,14 @@ def pygal_userview_infoview(request, responsetype, rel_path):
149 148
         else:
150 149
             w = get_userview_wrapper_instance(full_path, request)
151 150
     except LookupError:
152
-        context_adaption(context, request, '')
151
+        context_adaption(context, request)
153 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 160
     if w.may_read():
157 161
         context.set_additional_title(w.name)
@@ -194,11 +198,16 @@ def pygal_profile(request):
194 198
     current_thumbnail_size = pygal.get_thumbnail_size(request)
195 199
     current_webnail_size = pygal.get_webnail_size(request)
196 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 211
     if request.POST:
203 212
         # store thumbnail size, if changed
204 213
         thumbnail_size = int(request.POST.get('thumbnail_size'))
@@ -241,7 +250,7 @@ def pygal_addtag(request, rel_path):
241 250
         context_adaption(
242 251
             context,
243 252
             request,
244
-            rel_path,
253
+            rel_path=rel_path,
245 254
             title=_('Add Tag for %s') % w.name
246 255
         )
247 256
         tag_context_adaption(context, w, request, get_item_type(full_path) in [TYPE_IMAGE])
@@ -273,7 +282,7 @@ def pygal_tagedit(request, tag_id):
273 282
         context_adaption(
274 283
             context,
275 284
             request,
276
-            t.item.rel_path,
285
+            rel_path=t.item.rel_path,
277 286
             title=_('Edit Tag %(tag_id)d for %(item_name)s') % {'tag_id': tag_id, 'item_name': w.name}
278 287
         )
279 288
         tag_context_adaption(context, w, request, t.item.type in [TYPE_IMAGE])

+ 1
- 6
views/xnail.py 파일 보기

@@ -7,12 +7,7 @@ from ..models import get_item_type, TYPE_IMAGE, TYPE_VIDEO
7 7
 import os
8 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 13
 def get_image_class(full_path):

Loading…
취소
저장