~widelands-dev/widelands-website/django_staticfiles

« back to all changes in this revision

Viewing changes to threadedcomments/views.py

merged trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
from django.template import RequestContext, Context, Template
6
6
from django.utils.http import urlquote
7
7
from django.conf import settings
8
 
from threadedcomments.forms import FreeThreadedCommentForm, ThreadedCommentForm
9
 
from threadedcomments.models import ThreadedComment, FreeThreadedComment, DEFAULT_MAX_COMMENT_LENGTH
 
8
from threadedcomments.forms import ThreadedCommentForm
 
9
from threadedcomments.models import ThreadedComment, DEFAULT_MAX_COMMENT_LENGTH
10
10
from threadedcomments.utils import JSONResponse, XMLResponse
11
11
from wl_utils import get_real_ip
12
12
 
59
59
                  )
60
60
 
61
61
 
62
 
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={}):
63
 
    """Receives POST data and either creates a new ``ThreadedComment`` or
64
 
    ``FreeThreadedComment``, or edits an old one based upon the specified
65
 
    parameters.
 
62
@login_required
 
63
def comment(request, content_type=None, object_id=None, edit_id=None, parent_id=None, add_messages=False, ajax=False, context_processors=[], extra_context={}):
 
64
    """Receives POST data and creates a new ``ThreadedComment``, or
 
65
    edits an old one based upon the specified parameters.
66
66
 
67
67
    If there is a 'preview' key in the POST request, a preview will be forced and the
68
68
    comment will not be saved until a 'preview' key is no longer in the POST request.
74
74
    where the comment may be edited until it does not contain errors.
75
75
 
76
76
    """
 
77
    form_class = ThreadedCommentForm
 
78
    model = ThreadedComment
77
79
    if not edit_id and not (content_type and object_id):
78
80
        raise Http404  # Must specify either content_type and object_id or edit_id
79
81
    if 'preview' in request.POST:
87
89
    if form.is_valid():
88
90
        new_comment = form.save(commit=False)
89
91
        if not edit_id:
90
 
            new_comment.ip_address = get_real_ip(request)
91
92
            new_comment.content_type = get_object_or_404(
92
93
                ContentType, id=int(content_type))
93
94
            new_comment.object_id = int(object_id)
94
 
        if model == ThreadedComment:
95
 
            new_comment.user = request.user
 
95
 
 
96
        new_comment.user = request.user
 
97
 
96
98
        if parent_id:
97
99
            new_comment.parent = get_object_or_404(model, id=int(parent_id))
98
100
        new_comment.save()
99
 
        if model == ThreadedComment:
100
 
            if add_messages:
101
 
                request.user.message_set.create(
102
 
                    message='Your message has been posted successfully.')
103
 
        else:
104
 
            request.session['successful_data'] = {
105
 
                'name': form.cleaned_data['name'],
106
 
                'website': form.cleaned_data['website'],
107
 
                'email': form.cleaned_data['email'],
108
 
            }
 
101
        if add_messages:
 
102
            request.user.message_set.create(
 
103
                message='Your message has been posted successfully.')
 
104
 
109
105
        if ajax == 'json':
110
106
            return JSONResponse([new_comment, ])
111
107
        elif ajax == 'xml':
129
125
        return XMLResponse(response_str, is_iterable=False)
130
126
    else:
131
127
        return _preview(request, context_processors, extra_context, form_class=form_class)
132
 
 
133
 
 
134
 
def comment(*args, **kwargs):
135
 
    """Thin wrapper around free_comment which adds login_required status and
136
 
    also assigns the ``model`` to be ``ThreadedComment``."""
137
 
    kwargs['model'] = ThreadedComment
138
 
    kwargs['form_class'] = ThreadedCommentForm
139
 
    return free_comment(*args, **kwargs)
140
 
# Require login to be required, as request.user must exist and be valid.
141
 
comment = login_required(comment)
142
 
 
143
 
 
144
 
def can_delete_comment(comment, user):
145
 
    """Default callback function to determine wether the given user has the
146
 
    ability to delete the given comment."""
147
 
    if user.is_staff or user.is_superuser:
148
 
        return True
149
 
    if hasattr(comment, 'user') and comment.user == user:
150
 
        return True
151
 
    return False
152
 
 
153
 
# Todo: Next one is not used so far and may need adjustments to the render()
154
 
def comment_delete(request, object_id, model=ThreadedComment, extra_context={}, context_processors=[], permission_callback=can_delete_comment):
155
 
    """Deletes the specified comment, which can be either a
156
 
    ``FreeThreadedComment`` or a ``ThreadedComment``.
157
 
 
158
 
    If it is a POST request, then the comment will be deleted outright,
159
 
    however, if it is a GET request, a confirmation page will be shown.
160
 
 
161
 
    """
162
 
    tc = get_object_or_404(model, id=int(object_id))
163
 
    if not permission_callback(tc, request.user):
164
 
        login_url = settings.LOGIN_URL
165
 
        current_url = urlquote(request.get_full_path())
166
 
        return HttpResponseRedirect('%s?next=%s' % (login_url, current_url))
167
 
    if request.method == 'POST':
168
 
        tc.delete()
169
 
        return HttpResponseRedirect(_get_next(request))
170
 
    else:
171
 
        if model == ThreadedComment:
172
 
            is_free_threaded_comment = False
173
 
            is_threaded_comment = True
174
 
        else:
175
 
            is_free_threaded_comment = True
176
 
            is_threaded_comment = False
177
 
 
178
 
        extra_context.update(
179
 
            {'comment': tc,
180
 
             'is_free_threaded_comment': is_free_threaded_comment,
181
 
             'is_threaded_comment': is_threaded_comment,
182
 
             'next': _get_next(request),
183
 
             }
184
 
            )
185
 
        return render(request,
186
 
                      'threadedcomments/confirm_delete.html',
187
 
                      extra_context,
188
 
                      )