~widelands-dev/widelands-website/django_staticfiles

« back to all changes in this revision

Viewing changes to wiki/views.py

  • Committer: franku
  • Date: 2016-12-13 18:28:51 UTC
  • mto: This revision was merged to the branch mainline in revision 443.
  • Revision ID: somal@arcor.de-20161213182851-bo5ebf8pdvw5beua
run the script

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
ALL_ARTICLES = Article.objects.all()
37
37
ALL_CHANGES = ChangeSet.objects.all()
38
38
 
 
39
 
39
40
def get_articles_by_group(article_qs, group_slug=None,
40
41
                          group_slug_field=None, group_qs=None):
41
42
    group = None
46
47
                                       object_id=group.id)
47
48
    return article_qs, group
48
49
 
 
50
 
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),
53
 
                                       object_id=object.id)
 
54
    return article_qs.filter(content_type=get_ct(object),
 
55
                             object_id=object.id)
 
56
 
54
57
 
55
58
def get_url(urlname, group=None, args=None, kw=None):
56
59
    if group is 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/.../
63
66
 
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
 
71
 
 
72
 
68
73
class ArticleEditLock(object):
69
 
    """ A soft lock to edting an article.
70
 
    """
 
74
    """A soft lock to edting an article."""
71
75
 
72
76
    def __init__(self, title, request, message_template=None):
73
77
        self.title = title
76
80
 
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')
80
84
 
81
85
        self.message_template = message_template
82
86
 
83
 
        cache.set(title, self, WIKI_LOCK_DURATION*60)
 
87
        cache.set(title, self, WIKI_LOCK_DURATION * 60)
84
88
 
85
89
    def create_message(self, request):
86
 
        """ Send a message to the user if there is another user
87
 
        editing this article.
88
 
        """
 
90
        """Send a message to the user if there is another user editing this
 
91
        article."""
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)
93
96
 
94
97
    def is_mine(self, request):
95
98
        return self.user_ip == get_real_ip(request)
105
108
        return False
106
109
    return True
107
110
 
 
111
 
108
112
def has_write_perm(user, group, is_member):
109
 
    """ Return True if the user have permission to edit Articles,
110
 
    False otherwise.
111
 
    """
 
113
    """Return True if the user have permission to edit Articles, False
 
114
    otherwise."""
112
115
    if (group is None) or (is_member is None) or is_member(user, group):
113
116
        return True
114
117
    return False
142
145
 
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)
148
151
        else:
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)
158
161
 
159
162
 
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:
193
197
            try:
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)
199
 
            except IndexError: 
 
204
            except IndexError:
200
205
                article = ArticleClass(**article_args)
201
206
 
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()
205
211
 
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',
240
246
    group = None
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
272
278
 
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."
277
283
            #     else:
278
284
            #         user_message = u"Your article was edited successfully."
279
285
            #     messages.success(request, user_message)
280
286
 
281
 
 
282
287
            if ((article is None) and (group_slug is not None)):
283
288
                form.group = group
284
289
 
309
314
            form = ArticleFormClass(instance=article,
310
315
                                    initial=initial)
311
316
    if not article:
312
 
        template_params = {'form': form, "new_article": True }
 
317
        template_params = {'form': form, 'new_article': True}
313
318
    else:
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(),
317
 
            "article": article,
318
 
        }
 
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(),
 
322
                           'article': article,
 
323
                           }
319
324
 
320
325
    if group_slug is not None:
321
326
        template_params['group'] = group
339
344
                   is_private=None,
340
345
                   *args, **kw):
341
346
 
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(
352
358
 
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
371
378
 
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
375
382
 
376
383
        template_params = {'article': article,
406
413
 
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()
420
428
 
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')
425
433
 
455
463
 
456
464
        group = None
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,
495
504
            *args, **kw):
496
505
 
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}
537
546
    group = None
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,
551
560
 
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')
555
564
 
556
565
    return redirect(article)
557
566
 
571
580
    article_args = {'title': title}
572
581
    group = None
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,
589
598
 
590
599
    return redirect(article)
591
600
 
592
 
def article_preview( request ):
593
 
    """
594
 
    This is a AJAX function that previews the body of the
595
 
    article as it is currently displayed.
596
 
 
597
 
    This function is actually pretty simple, it just
598
 
    runs the function through the view template and returns
599
 
    it to the caller
600
 
    """
601
 
    rv = do_wl_markdown( request.POST["body"], 'bleachit' )
602
 
    return HttpResponse(rv, content_type="text/html")
603
 
 
604
 
def article_diff( request ):
605
 
    """
606
 
    This is a AJAX function that diffs the body of the
607
 
    article as it is currently displayed with the current version
608
 
    of the article
609
 
    """
610
 
    current_article = get_object_or_404(Article, pk=int(request.POST["article"]))
611
 
    content = request.POST["body"]
 
601
 
 
602
def article_preview(request):
 
603
    """This is a AJAX function that previews the body of the article as it is
 
604
    currently displayed.
 
605
 
 
606
    This function is actually pretty simple, it just runs the function
 
607
    through the view template and returns it to the caller
 
608
 
 
609
    """
 
610
    rv = do_wl_markdown(request.POST['body'], 'bleachit')
 
611
    return HttpResponse(rv, content_type='text/html')
 
612
 
 
613
 
 
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']
612
620
 
613
621
    diffs = dmp.diff_main(current_article.content, content)
614
622
    dmp.diff_cleanupSemantic(diffs)
615
623
 
616
 
    return HttpResponse(dmp.diff_prettyHtml(diffs), content_type="text/html")
617
 
 
 
624
    return HttpResponse(dmp.diff_prettyHtml(diffs), content_type='text/html')