Compare commits

...

6 Commits

6 changed files with 161 additions and 55 deletions

View File

@ -1,3 +1,69 @@
# mycreole
Django Library Mycreole
With the django library mycreole, you are abel to use the creole language to create html output in your django application.
## Requirements
You need to ensure that python-mycreole is available in your python environment.
## Integration
Clone the library in your django application.
### Configurations in your settings.py
Add the following line to the list ```INSTALLED_APPS```:
```
'mycreole.apps.MycreoleConfig',
```
### Parameter
All parameters can be added in the django ```settings.py``` or in a ```config.py``` in the root django folder. The definitions in the ```config.py``` will be used before the definitions in ```settings.py```.
#### MYCREOLE_ROOT
Define the folder, where the attachments are stored with the following line:
```
MYCREOLE_ROOT = os.path.join(BASE_DIR, 'data', 'pages')
```
#### MYCREOLE_ATTACHMENT_ACCESS
Define the methods to grant or deny the access to the attachments by:
```
MYCREOLE_ATTACHMENT_ACCESS = {
'read': 'pages.access.read_attachment',
'modify': 'pages.access.modify_attachment',
}
```
#### MYCREOLE_BAR
Define the methods to extend the navigationbar and menubar by this statement:
```
MYCREOLE_BAR = {
'navibar': 'pages.context.navigationbar',
'menubar': 'pages.context.menubar',
}
```
## Usage
### Creole help page
In this example, you see a method in ```views.py``` which returns a creole help page:
```
from django.conf import settings
from django.http import HttpResponse
import mycreole
import os
def help_on_creole(request):
attachment_path = os.path.join(settings.MYCREOLE_ROOT, 'creole_help_page')
html = mycreole.render(request, creole.help.MYCREOLE_HELP, self.attachment_path)
```
### Usage of next_achor
Defines the additional html anchor, which will be used after an upload.
### Usage of macros
You can give a dictonary as macros parameter to the render method, to define your own macros. Here an example of such a dict:
```
macros={
'mymacro': method_to_be_called,
}
```

View File

@ -4,14 +4,12 @@ from django.urls.base import reverse
from .help import MYCREOLE_HELP
import importlib
import os
from mycreole import parameter
import shutil
def get_full_path(rel_path):
try:
return os.path.join(settings.MYCREOLE_ROOT, *rel_path.split('/'))
except AttributeError:
raise AttributeError("You need to define a root directory for mycreole in settings.py: MYCREOLE_ROOT")
return os.path.join(parameter.get(parameter.MYCREOLE_ROOT), *rel_path.split('/'))
def delete_attachment_target_path(attachment_target_path):
@ -22,11 +20,11 @@ def mycreole_help_pagecontent():
return creole.creole2html(MYCREOLE_HELP)
def render_simple(text):
return creole.creole2html(text)
def render_simple(text, macros=None):
return creole.creole2html(text, macros=macros)
def render(request, text, attachment_target_path, next_anchor=''):
def render(request, text, attachment_target_path, next_anchor='', macros=None):
def get_attachment_name(text, state):
end_idx = text.index(']]' if state == '[' else '}}')
try:
@ -84,9 +82,9 @@ def render(request, text, attachment_target_path, next_anchor=''):
else:
render_txt += '[[%s|Upload]]' % url_upload(request, rel_path, next_anchor)
text = text[text.index(']]' if state == '[' else '}}') + 2:]
return creole.creole2html(render_txt)
return creole.creole2html(render_txt, macros=macros)
except:
return creole.creole2html(text)
return creole.creole2html(text, macros=macros)
def url_delete(request, rel_path, next_anchor=''):

View File

@ -1,23 +1,25 @@
from django.conf import settings
from django.utils.translation import gettext as _
import logging
import importlib
from mycreole import url_upload
from users.context import menubar as user_menubar
from patt.context import navigationbar
from mycreole import parameter
from themes import color_icon_url
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__)
NEW_ATTACHMENT_UID = 'new_attachment'
def context_adaption(context, request, title, rel_path, **kwargs):
context.set_additional_title(title)
user_menubar(context[context.MENUBAR], request)
navigationbar(context, request)
add_bar = parameter.get(parameter.MYCREOLE_BAR)
for key in add_bar:
method = add_bar[key]
if method is not None:
method(context, request, caller_name="mycreole-attachments", **kwargs)
add_new(request, context[context.ACTIONBAR], rel_path, kwargs.get('next'))
for key in kwargs:
context[key] = kwargs[key]

View File

@ -1,7 +1,7 @@
MYCREOLE_HELP = """\
= Creole Markup
The Fields //"Description"// and //"Name"// of Tasks and Projects are interpreted as creole. \
Some Fields are interpreted as creole. \
See http://www.wikicreole.org for more details.
== Text Markup
@ -12,6 +12,8 @@ See http://www.wikicreole.org for more details.
| Underline | {{{__text__}}} | __text__ |
| Raw Link | {{{https://python.org}}} | https://python.org |
| Text Link | {{{[[https://python.org|Python]]}}} | [[https://python.org|Python]] |
== Additional syntax
| Image | {{{ {{/media/theme/logo.png|logo}} }}} | {{/media/theme/logo.png|logo}} |
| Attachment Text Link | {{{[[attachment:file.ext|Python]]}}} | |
| Attachment Image | {{{ {{attachment:logo.png|logo}} }}} | |
@ -115,4 +117,4 @@ results in:
{{{
unprocessde data! <div>
}}}
"""
"""

66
parameter.py Normal file
View File

@ -0,0 +1,66 @@
import config
from django.conf import settings
import importlib
import os
MYCREOLE_ATTACHMENT_ACCESS = "MYCREOLE_ATTACHMENT_ACCESS"
ACCESS_READ = 'read'
ACCESS_MODIFY = 'modify'
MYCREOLE_BAR = "MYCREOLE_BAR"
BAR_MENUBAR = "menubar"
BAR_NAVIBAR = "navibar"
MYCREOLE_ROOT = "MYCREOLE_ROOT"
def no_access(*args, **kwargs):
return False
DEFAULTS = {
MYCREOLE_ATTACHMENT_ACCESS: {
ACCESS_READ: 'mycreole.parameter.no_access',
ACCESS_MODIFY: 'mycreole.parameter.no_access',
},
MYCREOLE_BAR: {
BAR_MENUBAR: None,
BAR_NAVIBAR: None,
},
MYCREOLE_ROOT: os.path.join(settings.BASE_DIR, 'data', 'mycreole'),
}
def __get_object_by_name__(object_name):
class_data = object_name.split(".")
module_path = ".".join(class_data[:-1])
class_str = class_data[-1]
#
module = importlib.import_module(module_path)
return getattr(module, class_str)
def get(key):
# take data from config, settings or defaults
try:
data = getattr(config, key)
except AttributeError:
try:
data = getattr(settings, key)
except AttributeError:
data = DEFAULTS.get(key)
# adapt the data
if key in [MYCREOLE_BAR, MYCREOLE_ATTACHMENT_ACCESS]:
# Change given string to object
rv = {}
for skey in DEFAULTS[key]:
# take the value from data or the default
if skey in data:
value = data[skey]
else:
value = DEFAULTS[key][skey]
# take the object or None
if value is not None:
rv[skey] = __get_object_by_name__(value)
else:
rv[skey] = None
return rv
return data

View File

@ -9,24 +9,12 @@ from mycreole import url_upload, url_attachment, url_delete
import logging
import mycreole
import os
from mycreole import parameter
import stringtools
import themes
from django.contrib import messages
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_method(import_string):
class_data = import_string.split(".")
module_path = ".".join(class_data[:-1])
class_str = class_data[-1]
#
module = importlib.import_module(module_path)
return getattr(module, class_str)
logger = logging.getLogger(settings.ROOT_LOGGER_NAME).getChild(__name__)
def get_next(request):
@ -37,25 +25,9 @@ def get_next(request):
def access(access_type, request, rel_path):
def log_warning(access_type, e):
logger.warning('Could not import %s_access method for checking access rights: %s - %s', access_type, type(e).__name__, e)
try:
method_name = settings.MYCREOLE_ATTACHMENT_ACCESS[access_type]
except (AttributeError, KeyError) as e:
log_warning(access_type, e)
return False
else:
if method_name in [True, None]:
return True
elif method_name is False:
return False
else:
try:
return get_method(method_name)(request, rel_path)
except AttributeError as e:
log_warning(access_type, e)
return False
access = parameter.get(parameter.MYCREOLE_ATTACHMENT_ACCESS)
method = access[access_type]
return method(request, rel_path)
def mycreole_attachment(request, rel_path):
@ -93,7 +65,7 @@ def mycreole_upload(request, rel_path):
try:
os.makedirs(os.path.dirname(full_path), exist_ok=True)
except PermissionError:
raise PermissionError("Ensure that we have access to MYCREOLE_ROOT=%s" % repr(settings.MYCREOLE_ROOT))
raise PermissionError("Ensure that we have access to MYCREOLE_ROOT=%s" % repr(parameter.get(parameter.MYCREOLE_ROOT)))
else:
with open(full_path, 'wb') as fh:
fh.write(request.FILES['file'].read())