Skip to content

Commit

Permalink
Fix martor flake8 errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Ninjaclasher committed Dec 25, 2024
1 parent e6f131c commit 1556524
Show file tree
Hide file tree
Showing 16 changed files with 71 additions and 61 deletions.
5 changes: 4 additions & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[flake8]
max-line-length = 120
application-import-names = dmoj,judge,django_ace
application-import-names = dmoj,judge,django_ace,martor
import-order-style = pycharm
enable-extensions = G
ignore =
Expand All @@ -18,6 +18,9 @@ per-file-ignores =
./judge/management/commands/runmoss.py:F403,F405
# E501: line too long, ignore in migrations
./judge/migrations/*.py:E501
# E501: line too long, ignore in martor emoji and mention extensions
./martor/extensions/emoji.py:E501
./martor/extensions/mention.py:E501
exclude =
# belongs to the user
./dmoj/local_settings.py,
Expand Down
2 changes: 1 addition & 1 deletion dmoj/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from django.utils.functional import lazy
from django.utils.translation import gettext_lazy as _
from django.views.generic import RedirectView
from martor.views import markdown_search_user

from judge.feed import AtomBlogFeed, AtomCommentFeed, AtomProblemFeed, BlogFeed, CommentFeed, ProblemFeed
from judge.sitemap import sitemaps
Expand All @@ -22,6 +21,7 @@
ContestUserSearchSelect2View, OrganizationSelect2View, ProblemSelect2View, TicketUserSelect2View, \
UserSearchSelect2View, UserSelect2View
from judge.views.widgets import martor_image_uploader
from martor.views import markdown_search_user

admin.autodiscover()

Expand Down
5 changes: 2 additions & 3 deletions martor/admin.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from django.contrib import admin
from django.db import models

from .widgets import AdminMartorWidget
from .models import MartorField
from .widgets import AdminMartorWidget


class MartorModelAdmin(admin.ModelAdmin):

formfield_overrides = {
MartorField: {'widget': AdminMartorWidget}
MartorField: {'widget': AdminMartorWidget},
}
17 changes: 10 additions & 7 deletions martor/api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
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 All @@ -24,8 +27,8 @@ def imgur_uploader(image):
'key': MARTOR_IMGUR_API_KEY,
'image': base64.b64encode(image.read()),
'type': 'base64',
'name': image.name
}
'name': image.name,
},
)

"""
Expand All @@ -40,15 +43,15 @@ def imgur_uploader(image):
return json.dumps({
'status': respdata['status'],
'link': respdata['data']['link'],
'name': respdata['data']['name']
'name': respdata['data']['name'],
})
elif response.status_code == 415:
# Unsupport File type
return json.dumps({
'status': response.status_code,
'error': response.reason
'error': response.reason,
})
return json.dumps({
'status': response.status_code,
'error': response.content.decode('utf-8')
'error': response.content.decode('utf-8'),
})
6 changes: 3 additions & 3 deletions martor/extensions/del_ins.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
from markdown.inlinepatterns import SimpleTagPattern


DEL_RE = r"(\~\~)(.+?)(\~\~)"
INS_RE = r"(\+\+)(.+?)(\+\+)"
DEL_RE = r'(\~\~)(.+?)(\~\~)'
INS_RE = r'(\+\+)(.+?)(\+\+)'


class DelInsExtension(markdown.extensions.Extension):
Expand All @@ -49,6 +49,6 @@ def makeExtension(*args, **kwargs):
return DelInsExtension(*args, **kwargs)


if __name__ == "__main__":
if __name__ == '__main__':
import doctest
doctest.testmod()
4 changes: 2 additions & 2 deletions martor/extensions/emoji.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def handleMatch(self, m):
if emoji not in EMOJIS:
return emoji
url = '{0}{1}.png'.format(
MARTOR_MARKDOWN_BASE_EMOJI_URL, emoji.replace(':', '')
MARTOR_MARKDOWN_BASE_EMOJI_URL, emoji.replace(':', ''),
)
if MARTOR_MARKDOWN_BASE_EMOJI_USE_STATIC is True:
url = static(url)
Expand All @@ -121,6 +121,6 @@ def makeExtension(*args, **kwargs):
return EmojiExtension(*args, **kwargs)


if __name__ == "__main__":
if __name__ == '__main__':
import doctest
doctest.testmod()
4 changes: 2 additions & 2 deletions martor/extensions/mdx_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def add_inline(self, md, name, klass, re):
pattern = klass(re)
pattern.md = md
pattern.ext = self
md.inlinePatterns.add(name, pattern, "<reference")
md.inlinePatterns.add(name, pattern, '<reference')

def extendMarkdown(self, md, md_globals):
self.add_inline(md, 'dailymotion', Dailymotion,
Expand Down Expand Up @@ -134,6 +134,6 @@ def makeExtension(**kwargs):
return VideoExtension(**kwargs)


if __name__ == "__main__":
if __name__ == '__main__':
import doctest
doctest.testmod()
6 changes: 4 additions & 2 deletions martor/extensions/mention.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import markdown
from django.contrib.auth import get_user_model

from ..settings import (
MARTOR_ENABLE_CONFIGS,
MARTOR_MARKDOWN_BASE_MENTION_URL
MARTOR_MARKDOWN_BASE_MENTION_URL,
)

"""
Expand Down Expand Up @@ -46,6 +47,7 @@ def extendMarkdown(self, md, md_globals):
def makeExtension(*args, **kwargs):
return MentionExtension(*args, **kwargs)

if __name__ == "__main__":

if __name__ == '__main__':
import doctest
doctest.testmod()
7 changes: 4 additions & 3 deletions martor/extensions/urlize.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ def handleMatch(self, m):
text = url

if not url.split('://')[0] in ('http', 'https', 'ftp'):
if '@' in url and not '/' in url:
if '@' in url and '/' not in url:
url = 'mailto:' + url
else:
url = 'http://' + url

el = markdown.util.etree.Element("a")
el = markdown.util.etree.Element('a')
el.set('href', url)
el.text = markdown.util.AtomicString(text)
return el
Expand All @@ -83,6 +83,7 @@ def extendMarkdown(self, md, md_globals):
def makeExtension(*args, **kwargs):
return UrlizeExtension(*args, **kwargs)

if __name__ == "__main__":

if __name__ == '__main__':
import doctest
doctest.testmod()
3 changes: 2 additions & 1 deletion martor/fields.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from django import forms


from .settings import MARTOR_ENABLE_LABEL
from .widgets import (MartorWidget, AdminMartorWidget)
from .widgets import MartorWidget


class MartorFormField(forms.CharField):
Expand Down
26 changes: 13 additions & 13 deletions martor/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,33 @@
'living': 'false', # to enable/disable live updates in preview
'spellcheck': 'false', # to enable/disable spellcheck in form textareas
'hljs': 'true', # to enable/disable hljs highlighting in preview
}
},
)

# To setup the martor editor with label or not (default is False)
MARTOR_ENABLE_LABEL = getattr(
settings, 'MARTOR_ENABLE_LABEL', False
settings, 'MARTOR_ENABLE_LABEL', False,
)

# Imgur API Keys
MARTOR_IMGUR_CLIENT_ID = getattr(
settings, 'MARTOR_IMGUR_CLIENT_ID', ''
settings, 'MARTOR_IMGUR_CLIENT_ID', '',
)
MARTOR_IMGUR_API_KEY = getattr(
settings, 'MARTOR_IMGUR_API_KEY', ''
settings, 'MARTOR_IMGUR_API_KEY', '',
)

# Safe Mode
MARTOR_MARKDOWN_SAFE_MODE = getattr(
settings, 'MARTOR_MARKDOWN_SAFE_MODE', True
settings, 'MARTOR_MARKDOWN_SAFE_MODE', True,
)

# Markdownify
MARTOR_MARKDOWNIFY_FUNCTION = getattr(
settings, 'MARTOR_MARKDOWNIFY_FUNCTION', 'martor.utils.markdownify'
settings, 'MARTOR_MARKDOWNIFY_FUNCTION', 'martor.utils.markdownify',
)
MARTOR_MARKDOWNIFY_URL = getattr(
settings, 'MARTOR_MARKDOWNIFY_URL', '/martor/markdownify/'
settings, 'MARTOR_MARKDOWNIFY_URL', '/martor/markdownify/',
)

# Markdown extensions
Expand All @@ -54,30 +54,30 @@
'martor.extensions.mention', # to parse markdown mention
'martor.extensions.emoji', # to parse markdown emoji
'martor.extensions.mdx_video', # to parse embed/iframe video
]
],
)

# Markdown Extensions Configs
MARTOR_MARKDOWN_EXTENSION_CONFIGS = getattr(
settings, 'MARTOR_MARKDOWN_EXTENSION_CONFIGS', {}
settings, 'MARTOR_MARKDOWN_EXTENSION_CONFIGS', {},
)

# Markdown urls
MARTOR_UPLOAD_URL = getattr(
settings, 'MARTOR_UPLOAD_URL', '/martor/uploader/' # for imgur
settings, 'MARTOR_UPLOAD_URL', '/martor/uploader/', # for imgur
)
MARTOR_SEARCH_USERS_URL = getattr(
settings, 'MARTOR_SEARCH_USERS_URL', '/martor/search-user/' # for mention
settings, 'MARTOR_SEARCH_USERS_URL', '/martor/search-user/', # for mention
)

# Markdown Extensions
MARTOR_MARKDOWN_BASE_EMOJI_URL = getattr(
settings, 'MARTOR_MARKDOWN_BASE_EMOJI_URL', 'https://github.githubassets.com/images/icons/emoji/'
settings, 'MARTOR_MARKDOWN_BASE_EMOJI_URL', 'https://github.githubassets.com/images/icons/emoji/',
)
# to use static and keep backward compatibility
# set to true if using bucket like storage engine
MARTOR_MARKDOWN_BASE_EMOJI_USE_STATIC = getattr(
settings, 'MARTOR_MARKDOWN_BASE_EMOJI_USE_STATIC', False)
MARTOR_MARKDOWN_BASE_MENTION_URL = getattr(
settings, 'MARTOR_MARKDOWN_BASE_MENTION_URL', 'https://python.web.id/author/'
settings, 'MARTOR_MARKDOWN_BASE_MENTION_URL', 'https://python.web.id/author/',
)
1 change: 0 additions & 1 deletion martor/tests/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

from django.db import models

from martor.models import MartorField
Expand Down
7 changes: 4 additions & 3 deletions martor/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import django
from .views import (markdownfy_view,
markdown_imgur_uploader,
markdown_search_user)

from .views import (markdown_imgur_uploader,
markdown_search_user,
markdownfy_view)


if django.VERSION >= (2, 0):
Expand Down
14 changes: 7 additions & 7 deletions martor/utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from django.utils.functional import Promise
from django.utils.encoding import force_str
import markdown
from django.core.serializers.json import DjangoJSONEncoder
from django.utils.encoding import force_str
from django.utils.functional import Promise

import markdown
from .settings import (
MARTOR_MARKDOWN_SAFE_MODE,
MARTOR_MARKDOWN_EXTENSIONS,
MARTOR_MARKDOWN_EXTENSION_CONFIGS
MARTOR_MARKDOWN_EXTENSION_CONFIGS,
MARTOR_MARKDOWN_SAFE_MODE,
)


Expand All @@ -30,11 +30,11 @@ def markdownify(markdown_content):
markdown_content,
safe_mode=MARTOR_MARKDOWN_SAFE_MODE,
extensions=MARTOR_MARKDOWN_EXTENSIONS,
extension_configs=MARTOR_MARKDOWN_EXTENSION_CONFIGS
extension_configs=MARTOR_MARKDOWN_EXTENSION_CONFIGS,
)
except Exception:
raise VersionNotCompatible("The markdown isn't compatible, please reinstall "
"your python markdown into Markdown>=3.0")
'your python markdown into Markdown>=3.0')


class LazyEncoder(DjangoJSONEncoder):
Expand Down
15 changes: 8 additions & 7 deletions martor/views.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import json
from django.http import HttpResponse
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.models import User
from django.db.models import Q
from django.http import HttpResponse
from django.utils.module_loading import import_string
from django.utils.translation import gettext_lazy as _

from .api import imgur_uploader
from .settings import MARTOR_MARKDOWNIFY_FUNCTION
Expand Down Expand Up @@ -60,25 +61,25 @@ def markdown_search_user(request):
and username != '' \
and ' ' not in username:
users = User.objects.filter(
Q(username__icontains=username)
Q(username__icontains=username),
).filter(is_active=True)
if users.exists():
data.update({
'status': 200,
'data': [{'username': u.username} for u in users]
'data': [{'username': u.username} for u in users],
})
return HttpResponse(
json.dumps(data, cls=LazyEncoder),
content_type='application/json')
data.update({
'status': 204,
'error': _('No users registered as `%(username)s` '
'or user is unactived.') % {'username': username}
'or user is unactived.') % {'username': username},
})
else:
data.update({
'status': 204,
'error': _('Validation Failed for field `username`')
'error': _('Validation Failed for field `username`'),
})
return HttpResponse(
json.dumps(data, cls=LazyEncoder),
Expand Down
Loading

0 comments on commit 1556524

Please sign in to comment.