Skip to content

Commit

Permalink
fix: custom MARTOR_UPLOAD_URL not working #210
Browse files Browse the repository at this point in the history
  • Loading branch information
Agus Makmun committed Mar 15, 2024
1 parent fdadca8 commit 7868518
Show file tree
Hide file tree
Showing 19 changed files with 109 additions and 160 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ MARTOR_IMGUR_API_KEY = 'your-api-key'
MARTOR_MARKDOWNIFY_FUNCTION = 'martor.utils.markdownify' # default
MARTOR_MARKDOWNIFY_URL = '/martor/markdownify/' # default

# Delay in miliseconds to update editor preview when in living mode.
# Delay in milliseconds to update editor preview when in living mode.
MARTOR_MARKDOWNIFY_TIMEOUT = 0 # update the preview instantly
# or:
MARTOR_MARKDOWNIFY_TIMEOUT = 1000 # default
Expand Down Expand Up @@ -242,7 +242,7 @@ admin.site.register(YourModel, YourModelAdmin)

#### Template Renderer

Simply safely parse markdown content as html ouput by loading templatetags from `martor/templatetags/martortags.py`.
Simply safely parse markdown content as html output by loading templatetags from `martor/templatetags/martortags.py`.

```html
{% load martortags %}
Expand Down Expand Up @@ -340,7 +340,7 @@ If you want to save the images uploaded to your storage,

### Test Martor from this Repository

Assuming you are already setup with a virtual enviroment (virtualenv):
Assuming you are already setup with a virtual environment (virtualenv):

```
$ git clone https://github.com/agusmakmun/django-markdown-editor.git
Expand All @@ -356,7 +356,7 @@ Checkout at http://127.0.0.1:8000/simple-form/ on your browser.

### Martor Commands Reference

![command refference](https://raw.githubusercontent.com/agusmakmun/django-markdown-editor/master/.etc/images/bootstrap/martor-guide.png)
![command reference](https://raw.githubusercontent.com/agusmakmun/django-markdown-editor/master/.etc/images/bootstrap/martor-guide.png)


### Notes
Expand Down
4 changes: 2 additions & 2 deletions martor/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-

__VERSION__ = "1.6.37"
__RELEASE_DATE__ = "09-Mar-2024"
__VERSION__ = "1.6.38"
__RELEASE_DATE__ = "15-Mar-2024"
__AUTHOR__ = "Agus Makmun (Summon Agus)"
__AUTHOR_EMAIL__ = "[email protected]"
8 changes: 5 additions & 3 deletions martor/api.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import json
import base64
import json

import requests
from .settings import MARTOR_IMGUR_CLIENT_ID, MARTOR_IMGUR_API_KEY

from .settings import MARTOR_IMGUR_API_KEY, MARTOR_IMGUR_CLIENT_ID

requests.packages.urllib3.disable_warnings()

Expand Down Expand Up @@ -39,7 +41,7 @@ def imgur_uploader(image):
)

elif response.status_code == 415:
# Unsupport File type
# Unsupported File type
return json.dumps(
{
"status": response.status_code,
Expand Down
3 changes: 2 additions & 1 deletion martor/extensions/mention.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import markdown
from django.contrib.auth import get_user_model

from ..settings import MARTOR_ENABLE_CONFIGS, MARTOR_MARKDOWN_BASE_MENTION_URL

"""
Expand All @@ -26,7 +27,7 @@ def handleMatch(self, m):
username=username, is_active=True
) # noqa: E501

"""Makesure `username` is registered and actived."""
"""Makesure `username` is registered and activated."""
if MARTOR_ENABLE_CONFIGS["mention"] == "true":
if users.exists():
url = "{0}{1}/".format(
Expand Down
4 changes: 2 additions & 2 deletions martor/static/martor/css/martor-admin.min.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Name : Martor v1.6.37
* Name : Martor v1.6.38
* Created by : Agus Makmun (Summon Agus)
* Release date : 09-Mar-2024
* Release date : 15-Mar-2024
* License : GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007
* Repository : https://github.com/agusmakmun/django-markdown-editor
**/
Expand Down
4 changes: 2 additions & 2 deletions martor/static/martor/css/martor.bootstrap.min.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Name : Martor v1.6.37
* Name : Martor v1.6.38
* Created by : Agus Makmun (Summon Agus)
* Release date : 09-Mar-2024
* Release date : 15-Mar-2024
* License : GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007
* Repository : https://github.com/agusmakmun/django-markdown-editor
**/
Expand Down
4 changes: 2 additions & 2 deletions martor/static/martor/css/martor.semantic.min.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Name : Martor v1.6.37
* Name : Martor v1.6.38
* Created by : Agus Makmun (Summon Agus)
* Release date : 09-Mar-2024
* Release date : 15-Mar-2024
* License : GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007
* Repository : https://github.com/agusmakmun/django-markdown-editor
**/
Expand Down
4 changes: 2 additions & 2 deletions martor/static/martor/js/martor.bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Name : Martor v1.6.37
* Name : Martor v1.6.38
* Created by : Agus Makmun (Summon Agus)
* Release date : 09-Mar-2024
* Release date : 15-Mar-2024
* License : GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007
* Repository : https://github.com/agusmakmun/django-markdown-editor
**/
Expand Down
4 changes: 2 additions & 2 deletions martor/static/martor/js/martor.bootstrap.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions martor/static/martor/js/martor.semantic.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Name : Martor v1.6.37
* Name : Martor v1.6.38
* Created by : Agus Makmun (Summon Agus)
* Release date : 09-Mar-2024
* Release date : 15-Mar-2024
* License : GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007
* Repository : https://github.com/agusmakmun/django-markdown-editor
**/
Expand Down
4 changes: 2 additions & 2 deletions martor/static/martor/js/martor.semantic.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion martor/templatetags/martortags.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@register.filter
def safe_markdown(markdown_text):
"""
Safe the markdown text as html ouput.
Safe the markdown text as html output.
Usage:
{% load martortags %}
Expand Down
18 changes: 2 additions & 16 deletions martor/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
from django.contrib.auth.models import User
from django.core.signals import setting_changed
from django.test import TestCase, override_settings
from django.urls import resolve, reverse, NoReverseMatch, clear_url_caches
from django.urls import clear_url_caches, resolve, reverse

from martor.utils import markdownify
from martor.views import markdownfy_view, markdown_imgur_uploader, markdown_search_user
from martor.views import markdown_imgur_uploader, markdown_search_user, markdownfy_view


class SimpleTest(TestCase):
Expand Down Expand Up @@ -129,20 +129,6 @@ def test_markdownify_xss_handled(self):
)

def test_urls(self):
with override_settings(
MARTOR_MARKDOWNIFY_URL="test/url",
MARTOR_UPLOAD_URL="",
MARTOR_SEARCH_USERS_URL="",
):
found = resolve(reverse("martor_markdownfy"))
self.assertEqual(found.func, markdownfy_view)

with self.assertRaises(NoReverseMatch):
reverse("imgur_uploader")

with self.assertRaises(NoReverseMatch):
reverse("search_user_json")

with override_settings(
MARTOR_MARKDOWNIFY_URL="test/url",
MARTOR_UPLOAD_URL="test/upload",
Expand Down
21 changes: 5 additions & 16 deletions martor/tests/urls.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
import django
from django.urls import include, path

from .views import TestFormView


if django.VERSION >= (2, 0):
from django.urls import path, include

urlpatterns = [
path("test-form-view/", TestFormView.as_view()),
path("", include("martor.urls")),
]
else:
from django.conf.urls import url, include

urlpatterns = [
url(r"^test-form-view/$", TestFormView.as_view()),
url(r"^martor/", include("martor.urls")),
]
urlpatterns = [
path("test-form-view/", TestFormView.as_view()),
path("martor/", include("martor.urls")),
]
80 changes: 7 additions & 73 deletions martor/urls.py
Original file line number Diff line number Diff line change
@@ -1,75 +1,9 @@
import django
from django.urls import path

from .settings import MARTOR_MARKDOWNIFY_URL, MARTOR_UPLOAD_URL, MARTOR_SEARCH_USERS_URL
from .views import (
markdownfy_view,
markdown_imgur_uploader,
markdown_search_user,
)
from .views import markdown_imgur_uploader, markdown_search_user, markdownfy_view


def __normalize(path: str) -> str:
# to support Python < 3.9 we can't use removeprefix('/').removesuffix('/')
if path.startswith("/"):
path = path[1:]
if path.endswith("/"):
path = path[:-1]
return path


if django.VERSION >= (2, 0):
from django.urls import path

urlpatterns = [
path(
f"{__normalize(MARTOR_MARKDOWNIFY_URL)}/",
markdownfy_view,
name="martor_markdownfy",
),
]

if MARTOR_UPLOAD_URL:
urlpatterns.append(
path(
f"{__normalize(MARTOR_UPLOAD_URL)}/",
markdown_imgur_uploader,
name="imgur_uploader",
),
)

if MARTOR_SEARCH_USERS_URL:
urlpatterns.append(
path(
f"{__normalize(MARTOR_SEARCH_USERS_URL)}/",
markdown_search_user,
name="search_user_json",
),
)
else:
from django.conf.urls import url

urlpatterns = [
url(
r"^%s/$" % __normalize(MARTOR_MARKDOWNIFY_URL),
markdownfy_view,
name="martor_markdownfy"
),
]

if MARTOR_UPLOAD_URL:
urlpatterns.append(
url(
r"^%s/$" % __normalize(MARTOR_UPLOAD_URL),
markdown_imgur_uploader,
name="imgur_uploader",
),
)

if MARTOR_SEARCH_USERS_URL:
urlpatterns.append(
url(
r"^%s/$" % __normalize(MARTOR_SEARCH_USERS_URL),
markdown_search_user,
name="search_user_json",
),
)
urlpatterns = [
path("markdownify/", markdownfy_view, name="martor_markdownfy"),
path("uploader/", markdown_imgur_uploader, name="imgur_uploader"),
path("search-user/", markdown_search_user, name="search_user_json"),
]
15 changes: 8 additions & 7 deletions martor/utils.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import re
import bleach

from django.utils.functional import Promise
import bleach
from django.core.serializers.json import DjangoJSONEncoder
from django.utils.functional import Promise

try:
from django.utils.encoding import force_str # noqa: Django>=4.x
except ImportError:
from django.utils.encoding import force_text as force_str # noqa: Django<=3.x

import markdown

from .settings import (
MARTOR_MARKDOWN_EXTENSIONS,
MARTOR_MARKDOWN_EXTENSION_CONFIGS,
ALLOWED_URL_SCHEMES,
ALLOWED_HTML_TAGS,
ALLOWED_HTML_ATTRIBUTES,
ALLOWED_HTML_TAGS,
ALLOWED_URL_SCHEMES,
MARTOR_MARKDOWN_EXTENSION_CONFIGS,
MARTOR_MARKDOWN_EXTENSIONS,
)


Expand Down Expand Up @@ -63,7 +64,7 @@ class LazyEncoder(DjangoJSONEncoder):
This problem because we found error encoding,
as docs says, django has special `DjangoJSONEncoder` at
https://docs.djangoproject.com/en/dev/topics/serialization/#serialization-formats-json
also discused in this answer: http://stackoverflow.com/a/31746279/6396981
also discussed in this answer: http://stackoverflow.com/a/31746279/6396981
Usage:
>>> data = {}
Expand Down
6 changes: 3 additions & 3 deletions martor/views.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.contrib.auth import get_user_model
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse, JsonResponse
from django.utils.module_loading import import_string
from django.utils.translation import gettext_lazy as _
from django.contrib.auth.decorators import login_required
from django.contrib.auth import get_user_model

from .api import imgur_uploader
from .settings import MARTOR_MARKDOWNIFY_FUNCTION
Expand Down Expand Up @@ -44,7 +44,7 @@ def is_ajax(request):
@login_required
def markdown_search_user(request):
"""
Json usernames of the users registered & actived.
Json usernames of the users registered & activated.
url(method=get):
/martor/search-user/?username={username}
Expand Down
Loading

0 comments on commit 7868518

Please sign in to comment.