forked from agusmakmun/django-markdown-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
83 lines (68 loc) · 2.82 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.http import (HttpResponse, JsonResponse)
from django.utils.module_loading import import_string
from django.utils.translation import ugettext_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
from .utils import LazyEncoder
User = get_user_model()
def markdownfy_view(request):
if request.method == 'POST':
content = request.POST.get('content', '')
markdownify = import_string(MARTOR_MARKDOWNIFY_FUNCTION)
return HttpResponse(markdownify(content))
return HttpResponse(_('Invalid request!'))
@login_required
def markdown_imgur_uploader(request):
"""
Makdown image upload for uploading to imgur.com
and represent as json to markdown editor.
"""
if request.method == 'POST' and request.is_ajax():
if 'markdown-image-upload' in request.FILES:
image = request.FILES['markdown-image-upload']
response_data = imgur_uploader(image=image)
return HttpResponse(response_data, content_type='application/json')
return HttpResponse(_('Invalid request!'))
return HttpResponse(_('Invalid request!'))
@login_required
def markdown_search_user(request):
"""
Json usernames of the users registered & actived.
url(method=get):
/martor/search-user/?username={username}
Response:
error:
- `status` is status code (204)
- `error` is error message.
success:
- `status` is status code (204)
- `data` is list dict of usernames.
{ 'status': 200,
'data': [
{'usernane': 'john'},
{'usernane': 'albert'}]
}
"""
response_data = {}
username = request.GET.get('username')
if username is not None \
and username != '' \
and ' ' not in username:
queries = {'%s__iexact' % User.USERNAME_FIELD: username,
'%s__icontains' % User.USERNAME_FIELD: username}
users = User.objects.filter(**queries).filter(is_active=True)
if users.exists():
response_data.update({'status': 200,
'data': [{'username': u.username} for u in users]})
return JsonResponse(response_data)
response_data.update({'status': 204,
'error': _('No users registered as `%(username)s` '
'or user is unactived.') % {'username': username}})
else:
response_data.update({'status': 204,
'error': _('Validation Failed for field `username`')})
return JsonResponse(response_data)