~widelands-dev/widelands-website/django_staticfiles

« back to all changes in this revision

Viewing changes to threadedcomments/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:
10
10
from threadedcomments.utils import JSONResponse, XMLResponse
11
11
from wl_utils import get_real_ip
12
12
 
 
13
 
13
14
def _adjust_max_comment_length(form, field_name='comment'):
14
 
    """
15
 
    Sets the maximum comment length to that default specified in the settings.
16
 
    """
 
15
    """Sets the maximum comment length to that default specified in the
 
16
    settings."""
17
17
    form.base_fields['comment'].max_length = DEFAULT_MAX_COMMENT_LENGTH
18
18
 
 
19
 
19
20
def _get_next(request):
20
 
    """
21
 
    The part that's the least straightforward about views in this module is how they 
22
 
    determine their redirects after they have finished computation.
 
21
    """The part that's the least straightforward about views in this module is
 
22
    how they determine their redirects after they have finished computation.
23
23
 
24
24
    In short, they will try and determine the next place to go in the following order:
25
25
 
30
30
    3. If Django can determine the previous page from the HTTP headers, the view will
31
31
    redirect to that previous page.
32
32
    4. Otherwise, the view raise a 404 Not Found.
 
33
 
33
34
    """
34
 
    next = request.POST.get('next', request.GET.get('next', request.META.get('HTTP_REFERER', None)))
 
35
    next = request.POST.get('next', request.GET.get(
 
36
        'next', request.META.get('HTTP_REFERER', None)))
35
37
    if not next or next == request.path:
36
 
        raise Http404 # No next url was supplied in GET or POST.
 
38
        raise Http404  # No next url was supplied in GET or POST.
37
39
    return next
38
40
 
 
41
 
39
42
def _preview(request, context_processors, extra_context, form_class=ThreadedCommentForm):
40
 
    """
41
 
    Returns a preview of the comment so that the user may decide if he or she wants to
42
 
    edit it before submitting it permanently.
43
 
    """
 
43
    """Returns a preview of the comment so that the user may decide if he or
 
44
    she wants to edit it before submitting it permanently."""
44
45
    _adjust_max_comment_length(form_class)
45
46
    form = form_class(request.POST or None)
46
47
    context = {
47
 
        'next' : _get_next(request),
48
 
        'form' : form,
 
48
        'next': _get_next(request),
 
49
        'form': form,
49
50
    }
50
51
    if form.is_valid():
51
52
        new_comment = form.save(commit=False)
54
55
        context['comment'] = None
55
56
    return render_to_response(
56
57
        'threadedcomments/preview_comment.html',
57
 
        extra_context, 
58
 
        context_instance = RequestContext(request, context, context_processors)
 
58
        extra_context,
 
59
        context_instance=RequestContext(request, context, context_processors)
59
60
    )
60
61
 
 
62
 
61
63
def free_comment(request, content_type=None, object_id=None, edit_id=None, parent_id=None, add_messages=False, ajax=False, model=FreeThreadedComment, form_class=FreeThreadedCommentForm, context_processors=[], extra_context={}):
62
 
    """
63
 
    Receives POST data and either creates a new ``ThreadedComment`` or 
64
 
    ``FreeThreadedComment``, or edits an old one based upon the specified parameters.
 
64
    """Receives POST data and either creates a new ``ThreadedComment`` or
 
65
    ``FreeThreadedComment``, or edits an old one based upon the specified
 
66
    parameters.
65
67
 
66
68
    If there is a 'preview' key in the POST request, a preview will be forced and the
67
69
    comment will not be saved until a 'preview' key is no longer in the POST request.
68
 
    
 
70
 
69
71
    If it is an *AJAX* request (either XML or JSON), it will return a serialized
70
72
    version of the last created ``ThreadedComment`` and there will be no redirect.
71
 
    
 
73
 
72
74
    If invalid POST data is submitted, this will go to the comment preview page
73
75
    where the comment may be edited until it does not contain errors.
 
76
 
74
77
    """
75
78
    if not edit_id and not (content_type and object_id):
76
 
        raise Http404 # Must specify either content_type and object_id or edit_id
77
 
    if "preview" in request.POST:
 
79
        raise Http404  # Must specify either content_type and object_id or edit_id
 
80
    if 'preview' in request.POST:
78
81
        return _preview(request, context_processors, extra_context, form_class=form_class)
79
82
    if edit_id:
80
83
        instance = get_object_or_404(model, id=edit_id)
86
89
        new_comment = form.save(commit=False)
87
90
        if not edit_id:
88
91
            new_comment.ip_address = get_real_ip(request)
89
 
            new_comment.content_type = get_object_or_404(ContentType, id = int(content_type))
 
92
            new_comment.content_type = get_object_or_404(
 
93
                ContentType, id=int(content_type))
90
94
            new_comment.object_id = int(object_id)
91
95
        if model == ThreadedComment:
92
96
            new_comment.user = request.user
93
97
        if parent_id:
94
 
            new_comment.parent = get_object_or_404(model, id = int(parent_id))
 
98
            new_comment.parent = get_object_or_404(model, id=int(parent_id))
95
99
        new_comment.save()
96
100
        if model == ThreadedComment:
97
101
            if add_messages:
98
 
                request.user.message_set.create(message="Your message has been posted successfully.")
 
102
                request.user.message_set.create(
 
103
                    message='Your message has been posted successfully.')
99
104
        else:
100
105
            request.session['successful_data'] = {
101
 
                'name' : form.cleaned_data['name'],
102
 
                'website' : form.cleaned_data['website'],
103
 
                'email' : form.cleaned_data['email'],
 
106
                'name': form.cleaned_data['name'],
 
107
                'website': form.cleaned_data['website'],
 
108
                'email': form.cleaned_data['email'],
104
109
            }
105
110
        if ajax == 'json':
106
 
            return JSONResponse([new_comment,])
 
111
            return JSONResponse([new_comment, ])
107
112
        elif ajax == 'xml':
108
 
            return XMLResponse([new_comment,])
 
113
            return XMLResponse([new_comment, ])
109
114
        else:
110
115
            return HttpResponseRedirect(_get_next(request))
111
 
    elif ajax=="json":
112
 
        return JSONResponse({'errors' : form.errors}, is_iterable=False)
113
 
    elif ajax=="xml":
 
116
    elif ajax == 'json':
 
117
        return JSONResponse({'errors': form.errors}, is_iterable=False)
 
118
    elif ajax == 'xml':
114
119
        template_str = """
115
120
<errorlist>
116
121
    {% for error,name in errors %}
120
125
    {% endfor %}
121
126
</errorlist>
122
127
        """
123
 
        response_str = Template(template_str).render(Context({'errors' : zip(form.errors.values(), form.errors.keys())}))
 
128
        response_str = Template(template_str).render(
 
129
            Context({'errors': zip(form.errors.values(), form.errors.keys())}))
124
130
        return XMLResponse(response_str, is_iterable=False)
125
131
    else:
126
132
        return _preview(request, context_processors, extra_context, form_class=form_class)
127
 
      
 
133
 
 
134
 
128
135
def comment(*args, **kwargs):
129
 
    """
130
 
    Thin wrapper around free_comment which adds login_required status and also assigns
131
 
    the ``model`` to be ``ThreadedComment``.
132
 
    """
 
136
    """Thin wrapper around free_comment which adds login_required status and
 
137
    also assigns the ``model`` to be ``ThreadedComment``."""
133
138
    kwargs['model'] = ThreadedComment
134
139
    kwargs['form_class'] = ThreadedCommentForm
135
140
    return free_comment(*args, **kwargs)
136
141
# Require login to be required, as request.user must exist and be valid.
137
142
comment = login_required(comment)
138
143
 
 
144
 
139
145
def can_delete_comment(comment, user):
140
 
    """
141
 
    Default callback function to determine wether the given user has the
142
 
    ability to delete the given comment.
143
 
    """
 
146
    """Default callback function to determine wether the given user has the
 
147
    ability to delete the given comment."""
144
148
    if user.is_staff or user.is_superuser:
145
149
        return True
146
150
    if hasattr(comment, 'user') and comment.user == user:
147
151
        return True
148
152
    return False
149
153
 
150
 
def comment_delete(request, object_id, model=ThreadedComment, extra_context = {}, context_processors = [], permission_callback=can_delete_comment):
151
 
    """
152
 
    Deletes the specified comment, which can be either a ``FreeThreadedComment`` or a
153
 
    ``ThreadedComment``.  If it is a POST request, then the comment will be deleted
154
 
    outright, however, if it is a GET request, a confirmation page will be shown.
 
154
 
 
155
def comment_delete(request, object_id, model=ThreadedComment, extra_context={}, context_processors=[], permission_callback=can_delete_comment):
 
156
    """Deletes the specified comment, which can be either a
 
157
    ``FreeThreadedComment`` or a ``ThreadedComment``.
 
158
 
 
159
    If it is a POST request, then the comment will be deleted outright,
 
160
    however, if it is a GET request, a confirmation page will be shown.
 
161
 
155
162
    """
156
163
    tc = get_object_or_404(model, id=int(object_id))
157
164
    if not permission_callback(tc, request.user):
158
165
        login_url = settings.LOGIN_URL
159
166
        current_url = urlquote(request.get_full_path())
160
 
        return HttpResponseRedirect("%s?next=%s" % (login_url, current_url))
161
 
    if request.method == "POST":
 
167
        return HttpResponseRedirect('%s?next=%s' % (login_url, current_url))
 
168
    if request.method == 'POST':
162
169
        tc.delete()
163
170
        return HttpResponseRedirect(_get_next(request))
164
171
    else:
170
177
            is_threaded_comment = False
171
178
        return render_to_response(
172
179
            'threadedcomments/confirm_delete.html',
173
 
            extra_context, 
174
 
            context_instance = RequestContext(
175
 
                request, 
 
180
            extra_context,
 
181
            context_instance=RequestContext(
 
182
                request,
176
183
                {
177
 
                    'comment' : tc, 
178
 
                    'is_free_threaded_comment' : is_free_threaded_comment,
179
 
                    'is_threaded_comment' : is_threaded_comment,
180
 
                    'next' : _get_next(request),
 
184
                    'comment': tc,
 
185
                    'is_free_threaded_comment': is_free_threaded_comment,
 
186
                    'is_threaded_comment': is_threaded_comment,
 
187
                    'next': _get_next(request),
181
188
                },
182
189
                context_processors
183
190
            )
184
 
        )
 
 
b'\\ No newline at end of file'
 
191
        )