This repository has been archived by the owner on Aug 26, 2022. It is now read-only.
/
translate.py
274 lines (232 loc) · 10.6 KB
/
translate.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# -*- coding: utf-8 -*-
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist, PermissionDenied
from django.http import JsonResponse
from django.shortcuts import get_object_or_404, redirect, render
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _
import kuma.wiki.content
from kuma.attachments.forms import AttachmentRevisionForm
from kuma.core.decorators import block_user_agents, login_required, never_cache
from kuma.core.i18n import get_language_mapping
from kuma.core.urlresolvers import reverse
from kuma.core.utils import get_object_or_none, smart_int, urlparams
from ..decorators import (check_readonly, prevent_indexing,
process_document_path)
from ..forms import DocumentForm, RevisionForm
from ..models import Document, Revision
from .utils import document_form_initial, split_slug
@block_user_agents
@login_required
@process_document_path
def select_locale(request, document_slug, document_locale):
"""
Select a locale to translate the document to.
"""
doc = get_object_or_404(Document,
locale=document_locale,
slug=document_slug)
return render(request, 'wiki/select_locale.html', {'document': doc})
@block_user_agents
@login_required
@process_document_path
@check_readonly
@prevent_indexing
@never_cache
def translate(request, document_slug, document_locale, revision_id=None):
"""
Create a new translation of a wiki document.
* document_slug is for the default locale
* translation is to the request locale
"""
# TODO: Refactor this view into two views? (new, edit)
# That might help reduce the headache-inducing branchiness.
# The parent document to translate from
parent_doc = get_object_or_404(Document,
locale=settings.WIKI_DEFAULT_LANGUAGE,
slug=document_slug)
if not revision_id:
# HACK: Seems weird, but sticking the translate-to locale in a query
# param is the best way to avoid the MindTouch-legacy locale
# redirection logic.
document_locale = request.GET.get('tolocale',
document_locale)
# Set a "Discard Changes" page
discard_href = ''
if settings.WIKI_DEFAULT_LANGUAGE == document_locale:
# Don't translate to the default language.
return redirect(reverse(
'wiki.edit', locale=settings.WIKI_DEFAULT_LANGUAGE,
args=[parent_doc.slug]))
if not parent_doc.is_localizable:
message = _(u'You cannot translate this document.')
context = {'message': message}
return render(request, 'handlers/400.html', context, status=400)
if revision_id:
revision = get_object_or_404(Revision, pk=revision_id)
else:
revision = None
based_on_rev = parent_doc.current_or_latest_revision()
disclose_description = bool(request.GET.get('opendescription'))
try:
doc = parent_doc.translations.get(locale=document_locale)
slug_dict = split_slug(doc.slug)
except Document.DoesNotExist:
doc = None
disclose_description = True
slug_dict = split_slug(document_slug)
# Find the "real" parent topic, which is its translation
if parent_doc.parent_topic:
try:
parent_topic_translated_doc = (parent_doc.parent_topic
.translations
.get(locale=document_locale))
slug_dict = split_slug(parent_topic_translated_doc.slug +
'/' +
slug_dict['specific'])
except ObjectDoesNotExist:
pass
user_has_doc_perm = (not doc) or (doc and doc.allows_editing_by(request.user))
user_has_rev_perm = (not doc) or (doc and doc.allows_revision_by(request.user))
if not user_has_doc_perm and not user_has_rev_perm:
# User has no perms, bye.
raise PermissionDenied
doc_form = rev_form = None
if user_has_doc_perm:
if doc:
# If there's an existing doc, populate form from it.
discard_href = doc.get_absolute_url()
doc.slug = slug_dict['specific']
doc_initial = document_form_initial(doc)
else:
# If no existing doc, bring over the original title and slug.
discard_href = parent_doc.get_absolute_url()
doc_initial = {'title': based_on_rev.title,
'slug': slug_dict['specific']}
doc_form = DocumentForm(initial=doc_initial,
parent_slug=slug_dict['parent'])
if user_has_rev_perm:
initial = {
'based_on': based_on_rev.id,
'comment': '',
'toc_depth': based_on_rev.toc_depth,
'localization_tags': ['inprogress'],
}
content = None
if revision is not None:
content = revision.content
elif not doc:
content = based_on_rev.content
if content:
initial.update(content=kuma.wiki.content.parse(content)
.filterEditorSafety()
.serialize())
instance = doc and doc.current_or_latest_revision()
rev_form = RevisionForm(request=request,
instance=instance,
initial=initial,
parent_slug=slug_dict['parent'])
if request.method == 'POST':
which_form = request.POST.get('form-type', 'both')
doc_form_invalid = False
# Grab the posted slug value in case it's invalid
posted_slug = request.POST.get('slug', slug_dict['specific'])
if user_has_doc_perm and which_form in ['doc', 'both']:
disclose_description = True
post_data = request.POST.copy()
post_data.update({'locale': document_locale})
doc_form = DocumentForm(post_data, instance=doc,
parent_slug=slug_dict['parent'])
doc_form.instance.locale = document_locale
doc_form.instance.parent = parent_doc
if which_form == 'both':
# Sending a new copy of post so the slug change above
# doesn't cause problems during validation
rev_form = RevisionForm(request=request,
data=post_data,
parent_slug=slug_dict['parent'])
# If we are submitting the whole form, we need to check that
# the Revision is valid before saving the Document.
if doc_form.is_valid() and (which_form == 'doc' or
rev_form.is_valid()):
doc = doc_form.save(parent=parent_doc)
if which_form == 'doc':
url = urlparams(doc.get_edit_url(), opendescription=1)
return redirect(url)
else:
doc_form.data['slug'] = posted_slug
doc_form_invalid = True
if doc and user_has_rev_perm and which_form in ['rev', 'both']:
post_data = request.POST.copy()
if 'slug' not in post_data:
post_data['slug'] = posted_slug
# update the post data with the toc_depth of original
post_data['toc_depth'] = based_on_rev.toc_depth
# Pass in the locale for the akistmet "blog_lang".
post_data['locale'] = document_locale
rev_form = RevisionForm(request=request,
data=post_data,
parent_slug=slug_dict['parent'])
rev_form.instance.document = doc # for rev_form.clean()
if rev_form.is_valid() and not doc_form_invalid:
parent_id = request.POST.get('parent_id', '')
# Attempt to set a parent
if parent_id:
try:
parent_doc = get_object_or_404(Document, id=parent_id)
rev_form.instance.document.parent = parent_doc
doc.parent = parent_doc
rev_form.instance.based_on.document = doc.original
except Document.DoesNotExist:
pass
rev_form.save(doc)
# If this is an Ajax POST, then return a JsonResponse
if request.is_ajax():
data = {
'error': False,
'new_revision_id': rev_form.instance.id,
}
return JsonResponse(data)
return redirect(doc)
else:
# If this is an Ajax POST, then return a JsonResponse with error
if request.is_ajax():
data = {
"error": True,
"error_message": mark_safe(rev_form.errors['current_rev'][0]),
"new_revision_id": rev_form.instance.id,
}
return JsonResponse(data=data)
if doc:
from_id = smart_int(request.GET.get('from'), None)
to_id = smart_int(request.GET.get('to'), None)
revision_from = get_object_or_none(Revision,
pk=from_id,
document=doc.parent)
revision_to = get_object_or_none(Revision,
pk=to_id,
document=doc.parent)
else:
revision_from = revision_to = None
parent_split = split_slug(parent_doc.slug)
language_mapping = get_language_mapping()
language = language_mapping[document_locale.lower()]
default_locale = language_mapping[settings.WIKI_DEFAULT_LANGUAGE.lower()]
context = {
'parent': parent_doc,
'document': doc,
'document_form': doc_form,
'revision_form': rev_form,
'locale': document_locale,
'default_locale': default_locale,
'language': language,
'based_on': based_on_rev,
'disclose_description': disclose_description,
'discard_href': discard_href,
'attachment_form': AttachmentRevisionForm(),
'specific_slug': parent_split['specific'],
'parent_slug': parent_split['parent'],
'revision_from': revision_from,
'revision_to': revision_to,
}
return render(request, 'wiki/translate.html', context)