~widelands-dev/widelands-website/trunk

« back to all changes in this revision

Viewing changes to pybb/views.py

  • Committer: kaputtnik
  • Date: 2019-09-03 06:16:23 UTC
  • mfrom: (544.2.25 pybb_attachments)
  • Revision ID: kaputtnik-20190903061623-xu4kvqpabnzmuskw
Allow file uploads in the forum; added some basic file checks to validate files

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
from django.shortcuts import redirect
12
12
from django.db.models import Q
13
13
from django.http import Http404
 
14
from django.conf import settings
14
15
 
15
16
from pybb.util import render_to, build_form, quote_text, ajax, urlize
16
17
from pybb.models import Category, Forum, Topic, Post, Attachment,\
77
78
 
78
79
 
79
80
def show_topic_ctx(request, topic_id):
 
81
    """View of topic posts including a form to add a Post."""
 
82
 
 
83
    context = {}
80
84
    try:
81
85
        topic = Topic.objects.select_related().get(pk=topic_id)
 
86
        context.update({'topic': topic})
82
87
    except Topic.DoesNotExist:
83
88
        raise Http404()
84
89
 
92
97
        topic.update_read(request.user)
93
98
 
94
99
    last_post = topic.posts.order_by('-created')[0]
 
100
    context.update({'last_post': last_post})
95
101
 
96
102
    initial = {}
 
103
    user_is_mod = False
97
104
    if request.user.is_authenticated:
98
105
        initial = {'markup': 'markdown'}
99
 
    form = AddPostForm(topic=topic, initial=initial)
100
 
 
101
 
    user_is_mod = pybb_moderated_by(topic, request.user)
102
 
    subscribed = (request.user.is_authenticated and
103
 
                  request.user in topic.subscribers.all())
104
 
 
105
 
    is_spam = False
106
 
    if topic.is_hidden:
107
 
            is_spam = topic.posts.first().is_spam()
 
106
 
 
107
        form = AddPostForm(topic=topic,
 
108
                           initial=initial,
 
109
                           user=request.user,
 
110
                           )
 
111
        context.update({'form': form})
 
112
 
 
113
        user_is_mod = pybb_moderated_by(topic, request.user)
 
114
        context.update({'user_is_mod': user_is_mod})
 
115
        
 
116
        subscribed = (request.user.is_authenticated and
 
117
                      request.user in topic.subscribers.all())
 
118
        context.update({'subscribed': subscribed})
 
119
    
 
120
        is_spam = False
 
121
        if topic.is_hidden:
 
122
                is_spam = topic.posts.first().is_spam()
 
123
        context.update({'is_spam': is_spam})
108
124
 
109
125
    if user_is_mod:
110
126
        posts = topic.posts.select_related()
111
127
    else:
112
128
        posts = topic.posts.exclude(hidden=True).select_related()
113
 
 
 
129
    context.update({'posts': posts})
 
130
 
114
131
    # TODO: fetch profiles
115
132
    # profiles = Profile.objects.filter(user__pk__in=
116
133
    #     set(x.user.id for x in page.object_list))
119
136
    # for post in page.object_list:
120
137
    #     post.user.pybb_profile = profiles[post.user.id]
121
138
 
122
 
    load_related(posts, Attachment.objects.all(), 'post')
123
 
 
124
 
    return {'topic': topic,
125
 
            'last_post': last_post,
126
 
            'form': form,
127
 
            'user_is_mod': user_is_mod,
128
 
            'subscribed': subscribed,
129
 
            'posts': posts,
130
 
            'page_size': pybb_settings.TOPIC_PAGE_SIZE,
131
 
            'form_url': reverse('pybb_add_post', args=[topic.id]),
132
 
            'is_spam': is_spam,
133
 
            }
 
139
    if pybb_settings.PYBB_ATTACHMENT_ENABLE:
 
140
        load_related(posts, Attachment.objects.all(), 'post')
 
141
 
 
142
    context.update({
 
143
        'page_size': pybb_settings.TOPIC_PAGE_SIZE,
 
144
        'form_url': reverse('pybb_add_post', args=[topic.id]),
 
145
        'wikipage': settings.ATTACHMENT_DESCR_PAGE,
 
146
    })
 
147
 
 
148
    return context
 
149
 
134
150
show_topic = render_to('pybb/topic.html')(show_topic_ctx)
135
151
 
136
152
 
137
153
@login_required
138
154
def add_post_ctx(request, forum_id, topic_id):
 
155
    """ Standalone view for adding posts."""
 
156
 
139
157
    forum = None
140
158
    topic = None
141
159
 
224
242
            'topic': topic,
225
243
            'forum': forum,
226
244
            'form_url': form_url,
 
245
            'wikipage': settings.ATTACHMENT_DESCR_PAGE,
227
246
            }
228
247
add_post = render_to('pybb/add_post.html')(add_post_ctx)
229
248
 
297
316
    if not allowed:
298
317
        return HttpResponseRedirect(post.get_absolute_url())
299
318
 
300
 
    if 'POST' == request.method:
 
319
    if request.method == 'POST':
301
320
        topic = post.topic
302
321
        forum = post.topic.forum
303
322
        post.delete()
352
371
    return HttpResponseRedirect(reverse('pybb_topic', args=[topic.id]))
353
372
 
354
373
 
355
 
@login_required
356
374
def show_attachment(request, hash):
357
375
    attachment = get_object_or_404(Attachment, hash=hash)
358
 
    file_obj = file(attachment.get_absolute_path())
359
 
    return HttpResponse(file_obj, content_type=attachment.content_type)
360
 
 
 
376
    
 
377
    with open(attachment.get_absolute_path(), 'rb') as file_obj:
 
378
        return HttpResponse(file_obj, content_type=attachment.content_type)
 
379
    return HTTP404
361
380
 
362
381
@login_required
363
382
@ajax