36
36
ALL_ARTICLES = Article.objects.all()
37
37
ALL_CHANGES = ChangeSet.objects.all()
39
40
def get_articles_by_group(article_qs, group_slug=None,
40
41
group_slug_field=None, group_qs=None):
46
47
object_id=group.id)
47
48
return article_qs, group
49
51
def get_articles_for_object(object, article_qs=None):
50
52
if article_qs is None:
51
53
article_qs = ALL_ARTICLES
52
return article_qs.filter( content_type=get_ct(object),
54
return article_qs.filter(content_type=get_ct(object),
55
58
def get_url(urlname, group=None, args=None, kw=None):
59
62
app = group._meta.app_label
60
63
urlconf = '.'.join([app, 'urls'])
61
64
url = reverse(urlname, urlconf, kwargs=kw)
62
return ''.join(['/', app, url]) # @@@ harcoded: /app/.../
65
return ''.join(['/', app, url]) # @@@ harcoded: /app/.../
64
67
# NOCOMM Franku: This Class is currently not used
65
68
# If we want this it has to be checked for the changes
66
69
# related to django 1.8.
67
70
# A javascript alert box is maybe a better solution
68
73
class ArticleEditLock(object):
69
""" A soft lock to edting an article.
74
"""A soft lock to edting an article."""
72
76
def __init__(self, title, request, message_template=None):
77
81
if message_template is None:
78
82
message_template = ('Possible edit conflict:'
79
' another user started editing this article at %s')
83
' another user started editing this article at %s')
81
85
self.message_template = message_template
83
cache.set(title, self, WIKI_LOCK_DURATION*60)
87
cache.set(title, self, WIKI_LOCK_DURATION * 60)
85
89
def create_message(self, request):
86
""" Send a message to the user if there is another user
90
"""Send a message to the user if there is another user editing this
89
92
if not self.is_mine(request):
90
93
user = request.user
91
94
user.message_set.create(
92
message=self.message_template%self.created_at)
95
message=self.message_template % self.created_at)
94
97
def is_mine(self, request):
95
98
return self.user_ip == get_real_ip(request)
108
112
def has_write_perm(user, group, is_member):
109
""" Return True if the user have permission to edit Articles,
113
"""Return True if the user have permission to edit Articles, False
112
115
if (group is None) or (is_member is None) or is_member(user, group):
143
146
if group_slug is not None:
144
147
template_params['group'] = group
145
new_article = ArticleClass(title="NewArticle",
148
new_article = ArticleClass(title='NewArticle',
146
149
content_type=get_ct(group),
147
150
object_id=group.id)
149
new_article = ArticleClass(title="NewArticle")
152
new_article = ArticleClass(title='NewArticle')
150
153
template_params['new_article'] = new_article
151
154
if extra_context is not None:
152
155
template_params.update(extra_context)
160
163
def view_article(request, title, revision=None,
161
ArticleClass=Article, # to create an unsaved instance
164
ArticleClass=Article, # to create an unsaved instance
162
165
group_slug=None, group_slug_field=None, group_qs=None,
163
166
article_qs=ALL_ARTICLES,
164
167
template_name='view.html',
171
174
if request.method == 'GET':
172
175
article_args = {'title': title}
173
176
if group_slug is not None:
174
group = get_object_or_404(group_qs,**{group_slug_field: group_slug})
177
group = get_object_or_404(
178
group_qs, **{group_slug_field: group_slug})
175
179
article_args.update({'content_type': get_ct(group),
176
180
'object_id': group.id})
177
181
allow_read = has_read_perm(request.user, group, is_member,
192
196
except ArticleClass.DoesNotExist:
194
198
# try to find an article that once had this title
195
article = ChangeSet.objects.filter(old_title=title).order_by('-revision')[0].article
199
article = ChangeSet.objects.filter(
200
old_title=title).order_by('-revision')[0].article
196
201
redirected_from = title
197
#if article is not None:
202
# if article is not None:
198
203
# return redirect(article, permanent=True)
200
205
article = ArticleClass(**article_args)
202
207
if revision is not None:
203
changeset = get_object_or_404(article.changeset_set, revision=revision)
208
changeset = get_object_or_404(
209
article.changeset_set, revision=revision)
204
210
article.content = changeset.get_content()
206
212
template_params = {'article': article,
227
233
def edit_article(request, title,
228
234
group_slug=None, group_slug_field=None, group_qs=None,
229
235
article_qs=ALL_ARTICLES,
230
ArticleClass=Article, # to get the DoesNotExist exception
236
ArticleClass=Article, # to get the DoesNotExist exception
231
237
ArticleFormClass=ArticleForm,
232
238
template_name='edit.html',
233
239
template_dir='wiki',
241
247
article_args = {'title': title}
242
248
if group_slug is not None:
243
group = get_object_or_404(group_qs,**{group_slug_field: group_slug})
249
group = get_object_or_404(group_qs, **{group_slug_field: group_slug})
244
250
group_ct = get_ct(group)
245
251
article_args.update({'content_type': group_ct,
246
252
'object_id': group.id})
271
277
# https://docs.djangoproject.com/en/1.8/ref/contrib/messages/#module-django.contrib.messages
273
279
if request.user.is_authenticated():
274
form.editor = request.user
280
form.editor = request.user
275
281
# if article is None:
276
282
# user_message = u"Your article was created successfully."
278
284
# user_message = u"Your article was edited successfully."
279
285
# messages.success(request, user_message)
282
287
if ((article is None) and (group_slug is not None)):
283
288
form.group = group
309
314
form = ArticleFormClass(instance=article,
312
template_params = {'form': form, "new_article": True }
317
template_params = {'form': form, 'new_article': True}
314
template_params = {'form': form, "new_article": False,
315
"content_type": ContentType.objects.get_for_model(Article).pk, "object_id": article.pk,
316
"images": article.all_images(),
319
template_params = {'form': form, 'new_article': False,
320
'content_type': ContentType.objects.get_for_model(Article).pk, 'object_id': article.pk,
321
'images': article.all_images(),
320
325
if group_slug is not None:
321
326
template_params['group'] = group
342
if request.method == "GET":
347
if request.method == 'GET':
343
348
article_args = {'article__title': title}
344
349
if group_slug is not None:
345
group = get_object_or_404(group_qs,**{group_slug_field: group_slug})
350
group = get_object_or_404(
351
group_qs, **{group_slug_field: group_slug})
346
352
article_args.update({'article__content_type': get_ct(group),
347
353
'article__object_id': group.id})
348
354
changeset = get_object_or_404(
353
359
article_args = {'title': title}
354
360
if group_slug is not None:
355
group = get_object_or_404(group_qs,**{group_slug_field: group_slug})
361
group = get_object_or_404(
362
group_qs, **{group_slug_field: group_slug})
356
363
article_args.update({'content_type': get_ct(group),
357
364
'object_id': group.id})
358
365
allow_read = has_read_perm(request.user, group, is_member,
370
377
revision_from = int(revision) - 1
372
379
from_value = None
373
if int(revision) is not int(revision_from)+1:
380
if int(revision) is not int(revision_from) + 1:
374
381
from_value = revision_from
376
383
template_params = {'article': article,
407
414
article_args = {'title': title}
408
415
if group_slug is not None:
409
group = get_object_or_404(group_qs,**{group_slug_field: group_slug})
416
group = get_object_or_404(
417
group_qs, **{group_slug_field: group_slug})
410
418
article_args.update({'content_type': get_ct(group),
411
419
'object_id': group.id})
412
420
allow_read = has_read_perm(request.user, group, is_member,
419
427
return HttpResponseForbidden()
421
429
article = get_object_or_404(article_qs, **article_args)
422
#changes = article.changeset_set.filter(
430
# changes = article.changeset_set.filter(
423
431
# reverted=False).order_by('-revision')
424
432
changes = article.changeset_set.all().order_by('-revision')
457
465
if group_slug is not None:
458
group = get_object_or_404(group_qs,**{group_slug_field: group_slug})
466
group = get_object_or_404(
467
group_qs, **{group_slug_field: group_slug})
459
468
article_args.update({'content_type': get_ct(group),
460
469
'object_id': group.id})
461
470
allow_read = has_read_perm(request.user, group, is_member,
497
506
if request.method == 'GET':
498
if group_slug is not None:
507
if group_slug is not None:
499
508
group = get_object_or_404(group_qs,
500
**{group_slug_field : group_slug})
509
**{group_slug_field: group_slug})
501
510
changes_qs = changes_qs.filter(article__content_type=get_ct(group),
502
511
article__object_id=group.id)
503
512
allow_read = has_read_perm(request.user, group, is_member,
536
545
article_args = {'title': title}
538
547
if group_slug is not None:
539
group = get_object_or_404(group_qs,**{group_slug_field: group_slug})
548
group = get_object_or_404(group_qs, **{group_slug_field: group_slug})
540
549
article_args.update({'content_type': get_ct(group),
541
550
'object_id': group.id})
542
551
allow_read = has_read_perm(request.user, group, is_member,
552
561
if not notification.is_observing(article, request.user):
553
562
notification.observe(article, request.user,
554
'wiki_observed_article_changed')
563
'wiki_observed_article_changed')
556
565
return redirect(article)
571
580
article_args = {'title': title}
573
582
if group_slug is not None:
574
group = get_object_or_404(group_qs,**{group_slug_field: group_slug})
583
group = get_object_or_404(group_qs, **{group_slug_field: group_slug})
575
584
article_args.update({'content_type': get_ct(group),
576
585
'object_id': group.id})
577
586
allow_read = has_read_perm(request.user, group, is_member,
590
599
return redirect(article)
592
def article_preview( request ):
594
This is a AJAX function that previews the body of the
595
article as it is currently displayed.
597
This function is actually pretty simple, it just
598
runs the function through the view template and returns
601
rv = do_wl_markdown( request.POST["body"], 'bleachit' )
602
return HttpResponse(rv, content_type="text/html")
604
def article_diff( request ):
606
This is a AJAX function that diffs the body of the
607
article as it is currently displayed with the current version
610
current_article = get_object_or_404(Article, pk=int(request.POST["article"]))
611
content = request.POST["body"]
602
def article_preview(request):
603
"""This is a AJAX function that previews the body of the article as it is
606
This function is actually pretty simple, it just runs the function
607
through the view template and returns it to the caller
610
rv = do_wl_markdown(request.POST['body'], 'bleachit')
611
return HttpResponse(rv, content_type='text/html')
614
def article_diff(request):
615
"""This is a AJAX function that diffs the body of the article as it is
616
currently displayed with the current version of the article."""
617
current_article = get_object_or_404(
618
Article, pk=int(request.POST['article']))
619
content = request.POST['body']
613
621
diffs = dmp.diff_main(current_article.content, content)
614
622
dmp.diff_cleanupSemantic(diffs)
616
return HttpResponse(dmp.diff_prettyHtml(diffs), content_type="text/html")
624
return HttpResponse(dmp.diff_prettyHtml(diffs), content_type='text/html')