3 import time, datetime, random
5 from django.core.files.storage import FileSystemStorage
6 from django.shortcuts import render_to_response, get_object_or_404
7 from django.contrib.auth.decorators import login_required
8 from django.http import HttpResponseRedirect, HttpResponse, HttpResponseForbidden, Http404
9 from django.template import RequestContext
10 from django.utils.html import *
11 from django.utils import simplejson
12 from django.utils.translation import ugettext as _
13 from django.core.urlresolvers import reverse
14 from django.core.exceptions import PermissionDenied
16 from forum.actions import AskAction, AnswerAction, ReviseAction, RollbackAction, RetagAction
17 from forum.modules.decorators import decoratable
18 from forum.forms import *
19 from forum.models import *
20 from forum.utils.forms import get_next_url
23 def upload(request):#ajax upload file to a question or answer
24 class FileTypeNotAllow(Exception):
26 class FileSizeNotAllow(Exception):
28 class UploadPermissionNotAuthorized(Exception):
31 xml_template = "<result><msg><![CDATA[%s]]></msg><error><![CDATA[%s]]></error><file_url>%s</file_url></result>"
34 f = request.FILES['file-upload']
35 # check upload permission
36 if not request.user.can_upload_files():
37 raise UploadPermissionNotAuthorized()
40 file_name_suffix = os.path.splitext(f.name)[1].lower()
42 if not file_name_suffix in ('.jpg', '.jpeg', '.gif', '.png', '.bmp', '.tiff', '.ico'):
43 raise FileTypeNotAllow()
45 storage = FileSystemStorage(str(settings.UPFILES_FOLDER), str(settings.UPFILES_ALIAS))
46 new_file_name = storage.save(f.name, f)
49 size = storage.size(new_file_name)
51 if size > float(settings.ALLOW_MAX_FILE_SIZE) * 1024 * 1024:
52 storage.delete(new_file_name)
53 raise FileSizeNotAllow()
55 result = xml_template % ('Good', '', str(settings.UPFILES_ALIAS) + new_file_name)
56 except UploadPermissionNotAuthorized:
57 result = xml_template % ('', _('uploading images is limited to users with >60 reputation points'), '')
58 except FileTypeNotAllow:
59 result = xml_template % ('', _("allowed file types are 'jpg', 'jpeg', 'gif', 'bmp', 'png', 'tiff'"), '')
60 except FileSizeNotAllow:
61 result = xml_template % ('', _("maximum upload file size is %sM") % settings.ALLOW_MAX_FILE_SIZE, '')
63 result = xml_template % ('', _('Error uploading file. Please contact the site administrator. Thank you. %s' % e), '')
65 return HttpResponse(result, mimetype="application/xml")
69 if request.POST and "text" in request.POST:
70 form = AskForm(request.POST)
72 if request.user.is_authenticated():
73 question = AskAction(user=request.user, ip=request.META['REMOTE_ADDR']).save(data=form.cleaned_data).node
74 return HttpResponseRedirect(question.get_absolute_url())
76 request.session['temp_node_data'] = request.POST
77 request.session['temp_node_type'] = 'question'
78 return HttpResponseRedirect(reverse('auth_action_signin', kwargs={'action': 'newquestion'}))
79 elif request.method == "POST" and "go" in request.POST:
80 form = AskForm({'title': request.POST['q']})
84 #tags = _get_tags_cache_json()
85 return render_to_response('ask.html', {
88 'email_validation_faq_url':reverse('faq') + '#validate',
89 }, context_instance=RequestContext(request))
92 def edit_question(request, id):
93 question = get_object_or_404(Question, id=id)
94 if question.deleted and not request.user.can_view_deleted_post(question):
96 if request.user.can_edit_post(question):
97 return _edit_question(request, question)
98 elif request.user.can_retag_questions():
99 return _retag_question(request, question)
103 def _retag_question(request, question):
104 if request.method == 'POST':
105 form = RetagQuestionForm(question, request.POST)
107 if form.has_changed():
108 RetagAction(user=request.user, node=question, ip=request.META['REMOTE_ADDR']).save(data=dict(tagnames=form.cleaned_data['tags']))
110 return HttpResponseRedirect(question.get_absolute_url())
112 form = RetagQuestionForm(question)
113 return render_to_response('question_retag.html', {
114 'question': question,
116 #'tags' : _get_tags_cache_json(),
117 }, context_instance=RequestContext(request))
119 def _edit_question(request, question):
120 if request.method == 'POST':
121 revision_form = RevisionForm(question, data=request.POST)
122 revision_form.is_valid()
123 revision = question.revisions.get(revision=revision_form.cleaned_data['revision'])
125 if 'select_revision' in request.POST:
126 form = EditQuestionForm(question, revision)
128 form = EditQuestionForm(question, revision, data=request.POST)
130 if not 'select_revision' in request.POST and form.is_valid():
131 if form.has_changed():
132 ReviseAction(user=request.user, node=question, ip=request.META['REMOTE_ADDR']).save(data=form.cleaned_data)
134 if not revision == question.active_revision:
135 RollbackAction(user=request.user, node=question).save(data=dict(activate=revision))
137 return HttpResponseRedirect(question.get_absolute_url())
139 revision_form = RevisionForm(question)
140 form = EditQuestionForm(question)
142 return render_to_response('question_edit.html', {
143 'question': question,
144 'revision_form': revision_form,
146 #'tags' : _get_tags_cache_json()
147 }, context_instance=RequestContext(request))
150 def edit_answer(request, id):
151 answer = get_object_or_404(Answer, id=id)
152 if answer.deleted and not request.user.can_view_deleted_post(answer):
154 elif not request.user.can_edit_post(answer):
157 if request.method == "POST":
158 revision_form = RevisionForm(answer, data=request.POST)
159 revision_form.is_valid()
160 revision = answer.revisions.get(revision=revision_form.cleaned_data['revision'])
162 if 'select_revision' in request.POST:
163 form = EditAnswerForm(answer, revision)
165 form = EditAnswerForm(answer, revision, data=request.POST)
167 if not 'select_revision' in request.POST and form.is_valid():
168 if form.has_changed():
169 ReviseAction(user=request.user, node=answer, ip=request.META['REMOTE_ADDR']).save(data=form.cleaned_data)
171 if not revision == answer.active_revision:
172 RollbackAction(user=request.user, node=answer, ip=request.META['REMOTE_ADDR']).save(data=dict(activate=revision))
174 return HttpResponseRedirect(answer.get_absolute_url())
177 revision_form = RevisionForm(answer)
178 form = EditAnswerForm(answer)
179 return render_to_response('answer_edit.html', {
181 'revision_form': revision_form,
183 }, context_instance=RequestContext(request))
186 def answer(request, id):
187 question = get_object_or_404(Question, id=id)
189 form = AnswerForm(question, request.POST)
191 if request.user.is_authenticated():
192 answer = AnswerAction(user=request.user, ip=request.META['REMOTE_ADDR']).save(dict(question=question, **form.cleaned_data)).node
193 return HttpResponseRedirect(answer.get_absolute_url())
195 request.session['temp_node_data'] = request.POST
196 request.session['temp_node_type'] = 'answer'
197 request.session['temp_question_id'] = id
198 return HttpResponseRedirect(reverse('auth_action_signin', kwargs={'action': 'newquestion'}))
200 request.session['redirect_POST_data'] = request.POST
201 return HttpResponseRedirect(question.get_absolute_url() + '#fmanswer')
203 return HttpResponseRedirect(question.get_absolute_url())