def ask(request): #view used to ask a new question """a view to ask a new question gives space for q title, body, tags and checkbox for to post as wiki user can start posting a question anonymously but then must login/register in order for the question go be shown """ form = forms.AskForm(request.REQUEST, user=request.user) if request.method == 'POST': if form.is_valid(): timestamp = datetime.datetime.now() title = form.cleaned_data['title'] wiki = form.cleaned_data['wiki'] tagnames = form.cleaned_data['tags'] text = form.cleaned_data['text'] ask_anonymously = form.cleaned_data['ask_anonymously'] post_privately = form.cleaned_data['post_privately'] group_id = form.cleaned_data.get('group_id', None) language = form.cleaned_data.get('language', None) if request.user.is_authenticated(): drafts = models.DraftQuestion.objects.filter( author=request.user) drafts.delete() user = form.get_post_user(request.user) try: question = user.post_question(title=title, body_text=text, tags=tagnames, wiki=wiki, is_anonymous=ask_anonymously, is_private=post_privately, timestamp=timestamp, group_id=group_id, language=language) signals.new_question_posted.send( None, question=question, user=user, form_data=form.cleaned_data) return HttpResponseRedirect(question.get_absolute_url()) except exceptions.PermissionDenied, e: request.user.message_set.create(message=unicode(e)) return HttpResponseRedirect(reverse('index')) else: request.session.flush() session_key = request.session.session_key models.AnonymousQuestion.objects.create( session_key=session_key, title=title, tagnames=tagnames, wiki=wiki, is_anonymous=ask_anonymously, text=text, added_at=timestamp, ip_addr=request.META['REMOTE_ADDR'], ) return HttpResponseRedirect(url_utils.get_login_url())
def setup_data(self, allow_anonymous=True, ask_anonymously=None): askbot_settings.update('ALLOW_ASK_ANONYMOUSLY', allow_anonymous) data = { 'title': 'test title', 'text': 'test content', 'tags': 'test', } if ask_anonymously: data['ask_anonymously'] = 'on' self.form = forms.AskForm(data, user=AnonymousUser()) self.form.full_clean()
def ask(request): #view used to ask a new question """a view to ask a new question gives space for q title, body, tags and checkbox for to post as wiki user can start posting a question anonymously but then must login/register in order for the question go be shown """ if request.method == "POST": form = forms.AskForm(request.POST) if form.is_valid(): timestamp = datetime.datetime.now() #todo: move this to clean_title title = form.cleaned_data['title'].strip() wiki = form.cleaned_data['wiki'] #todo: move this to clean_tagnames tagnames = form.cleaned_data['tags'].strip() text = form.cleaned_data['text'] ask_anonymously = form.cleaned_data['ask_anonymously'] if request.user.is_authenticated(): try: question = request.user.post_question( title=title, body_text=text, tags=tagnames, wiki=wiki, is_anonymous=ask_anonymously, timestamp=timestamp) return HttpResponseRedirect(question.get_absolute_url()) except exceptions.PermissionDenied, e: request.user.message_set.create(message=unicode(e)) return HttpResponseRedirect(reverse('index')) else: request.session.flush() session_key = request.session.session_key summary = strip_tags(text)[:120] question = models.AnonymousQuestion( session_key=session_key, title=title, tagnames=tagnames, wiki=wiki, is_anonymous=ask_anonymously, text=text, summary=summary, added_at=timestamp, ip_addr=request.META['REMOTE_ADDR'], ) question.save() return HttpResponseRedirect( reverse('user_signin_new_question'))
session_key=session_key, title=title, tagnames=tagnames, wiki=wiki, is_anonymous=ask_anonymously, text=text, summary=summary, added_at=timestamp, ip_addr=request.META['REMOTE_ADDR'], ) question.save() return HttpResponseRedirect( reverse('user_signin_new_question')) else: #this branch is for the initial load of ask form form = forms.AskForm() if 'title' in request.GET: #normally this title is inherited from search query #but it is possible to ask with a parameter title in the url query form.initial['title'] = request.GET['title'] else: #attempt to extract title from previous search query search_state = request.session.get('search_state', None) if search_state: query = search_state.query form.initial['title'] = query data = { 'active_tab': 'ask', 'page_class': 'ask-page', 'form': form,
def ask(request): #view used to ask a new question """a view to ask a new question gives space for q title, body, tags and checkbox for to post as wiki user can start posting a question anonymously but then must login/register in order for the question go be shown """ if request.method == "POST": form = forms.AskForm(request.POST) if form.is_valid(): timestamp = datetime.datetime.now() #todo: move this to clean_title title = form.cleaned_data['title'].strip() wiki = form.cleaned_data['wiki'] #todo: move this to clean_tagnames tagnames = form.cleaned_data['tags'].strip() text = form.cleaned_data['text'] ask_anonymously = form.cleaned_data['ask_anonymously'] if request.user.is_authenticated(): try: question = request.user.post_question( title=title, body_text=text, tags=tagnames, wiki=wiki, is_anonymous=ask_anonymously, timestamp=timestamp) return HttpResponseRedirect(question.get_absolute_url()) except exceptions.PermissionDenied, e: request.user.message_set.create(message=unicode(e)) return HttpResponseRedirect(reverse('index')) else: request.session.flush() session_key = request.session.session_key summary = strip_tags(text)[:120] question = models.AnonymousQuestion( session_key=session_key, title=title, tagnames=tagnames, wiki=wiki, is_anonymous=ask_anonymously, text=text, summary=summary, added_at=timestamp, ip_addr=request.META['REMOTE_ADDR'], ) question.save() return HttpResponseRedirect(url_utils.get_login_url()) else: form = forms.AskForm(request.POST) if 'title' in request.GET: #normally this title is inherited from search query #but it is possible to ask with a parameter title in the url query form.initial['title'] = request.GET['title'] else: #attempt to extract title from previous search query search_state = request.session.get('search_state', None) if search_state: query = search_state.query form.initial['title'] = query
request.session.flush() session_key = request.session.session_key models.AnonymousQuestion.objects.create( session_key=session_key, title=title, tagnames=tagnames, wiki=wiki, is_anonymous=ask_anonymously, text=text, added_at=timestamp, ip_addr=request.META['REMOTE_ADDR'], ) return HttpResponseRedirect(url_utils.get_login_url()) if request.method == 'GET': form = forms.AskForm(user=request.user) draft_title = '' draft_text = '' draft_tagnames = '' if request.user.is_authenticated(): drafts = models.DraftQuestion.objects.filter(author=request.user) if len(drafts) > 0: draft = drafts[0] draft_title = draft.title draft_text = draft.text draft_tagnames = draft.tagnames form.initial = { 'ask_anonymously': request.REQUEST.get('ask_anonymousy', False), 'tags': request.REQUEST.get('tags', draft_tagnames),
def ask(request): #view used to ask a new question """a view to ask a new question gives space for q title, body, tags and checkbox for to post as wiki user can start posting a question anonymously but then must login/register in order for the question go be shown """ if request.user.is_authenticated(): if request.user.is_read_only(): referer = request.META.get("HTTP_REFERER", reverse('questions')) request.user.message_set.create( message=_('Sorry, but you have only read access')) return HttpResponseRedirect(referer) if askbot_settings.READ_ONLY_MODE_ENABLED: return HttpResponseRedirect(reverse('index')) if request.method == 'POST': form = forms.AskForm(request.POST, user=request.user) if form.is_valid(): timestamp = timezone.now() title = form.cleaned_data['title'] wiki = form.cleaned_data['wiki'] tagnames = form.cleaned_data['tags'] text = form.cleaned_data['text'] ask_anonymously = form.cleaned_data['ask_anonymously'] post_privately = form.cleaned_data['post_privately'] group_id = form.cleaned_data.get('group_id', None) language = form.cleaned_data.get('language', None) if request.user.is_authenticated(): drafts = models.DraftQuestion.objects.filter( author=request.user) drafts.delete() user = form.get_post_user(request.user) elif request.user.is_anonymous( ) and askbot_settings.ALLOW_ASK_UNREGISTERED: user = models.get_or_create_anonymous_user() ask_anonymously = True else: user = None if user: try: question = user.post_question( title=title, body_text=text, tags=tagnames, wiki=wiki, is_anonymous=ask_anonymously, is_private=post_privately, timestamp=timestamp, group_id=group_id, language=language, ip_addr=request.META.get('REMOTE_ADDR')) signals.new_question_posted.send( None, question=question, user=user, form_data=form.cleaned_data) return HttpResponseRedirect(question.get_absolute_url()) except exceptions.PermissionDenied, e: request.user.message_set.create(message=unicode(e)) return HttpResponseRedirect(reverse('index')) else: request.session.flush() session_key = request.session.session_key models.AnonymousQuestion.objects.create( session_key=session_key, title=title, tagnames=tagnames, wiki=wiki, is_anonymous=ask_anonymously, text=text, added_at=timestamp, ip_addr=request.META.get('REMOTE_ADDR'), ) return HttpResponseRedirect(url_utils.get_login_url())
def ask(request): #view used to ask a new question """a view to ask a new question gives space for q title, body, tags and checkbox for to post as wiki user can start posting a question anonymously but then must login/register in order for the question go be shown """ if request.user.is_authenticated(): if request.user.is_read_only(): referer = request.META.get("HTTP_REFERER", reverse('questions')) request.user.message_set.create( message=_('Sorry, but you have only read access')) return HttpResponseRedirect(referer) if askbot_settings.READ_ONLY_MODE_ENABLED: return HttpResponseRedirect(reverse('index')) if request.method == 'POST': form = forms.AskForm(request.POST, user=request.user) if form.is_valid(): timestamp = timezone.now() title = form.cleaned_data['title'] wiki = form.cleaned_data['wiki'] tagnames = form.cleaned_data['tags'] text = form.cleaned_data['text'] ask_anonymously = form.cleaned_data['ask_anonymously'] post_privately = form.cleaned_data['post_privately'] group_id = form.cleaned_data.get('group_id', None) language = form.cleaned_data.get('language', None) content = '{}\n\n{}\n\n{}'.format(title, tagnames, text) if akismet_check_spam(content, request): message = _( 'Spam was detected on your post, sorry if it was a mistake' ) raise exceptions.PermissionDenied(message) if request.user.is_authenticated(): drafts = models.DraftQuestion.objects.filter( author=request.user) drafts.delete() user = form.get_post_user(request.user) elif request.user.is_anonymous( ) and askbot_settings.ALLOW_ASK_UNREGISTERED: user = models.get_or_create_anonymous_user() ask_anonymously = True else: user = None if user: try: question = user.post_question( title=title, body_text=text, tags=tagnames, wiki=wiki, is_anonymous=ask_anonymously, is_private=post_privately, timestamp=timestamp, group_id=group_id, language=language, ip_addr=request.META.get('REMOTE_ADDR')) signals.new_question_posted.send( None, question=question, user=user, form_data=form.cleaned_data) return HttpResponseRedirect(question.get_absolute_url()) except exceptions.PermissionDenied as e: request.user.message_set.create(message=unicode(e)) return HttpResponseRedirect(reverse('index')) else: session_key = request.session.session_key if session_key is None: return HttpResponseForbidden() models.AnonymousQuestion.objects.create( session_key=session_key, title=title, tagnames=tagnames, wiki=wiki, is_anonymous=ask_anonymously, text=text, added_at=timestamp, ip_addr=request.META.get('REMOTE_ADDR'), ) return HttpResponseRedirect(url_utils.get_login_url()) if request.method == 'GET': form = forms.AskForm(user=request.user) #session key used only to enable anonymous asking #as django will autodelete empty sessions request.session['askbot_write_intent'] = True draft_title = '' draft_text = '' draft_tagnames = '' if request.user.is_authenticated(): drafts = models.DraftQuestion.objects.filter(author=request.user) if len(drafts) > 0: draft = drafts[0] draft_title = draft.title draft_text = draft.get_text() draft_tagnames = draft.tagnames form.initial = { 'ask_anonymously': request.REQUEST.get('ask_anonymously', False), 'tags': request.REQUEST.get('tags', draft_tagnames), 'text': request.REQUEST.get('text', draft_text), 'title': request.REQUEST.get('title', draft_title), 'post_privately': request.REQUEST.get('post_privately', False), 'language': get_language(), 'wiki': request.REQUEST.get('wiki', False), } if 'group_id' in request.REQUEST: try: group_id = int(request.GET.get('group_id', None)) form.initial['group_id'] = group_id except Exception: pass editor_is_folded = (askbot_settings.QUESTION_BODY_EDITOR_MODE=='folded' and \ askbot_settings.MIN_QUESTION_BODY_LENGTH==0 and \ form.initial['text'] == '') data = { 'active_tab': 'ask', 'page_class': 'ask-page', 'form': form, 'editor_is_folded': editor_is_folded, 'mandatory_tags': models.tag.get_mandatory_tags(), 'email_validation_faq_url': reverse('faq') + '#validate', 'category_tree_data': askbot_settings.CATEGORY_TREE, 'tag_names': forms.split_tags(form.initial['tags']) } data.update(context.get_for_tag_editor()) return render(request, 'ask.html', data)
def ask(request): # view used to ask a new question """a view to ask a new question gives space for q title, body, tags and checkbox for to post as wiki user can start posting a question anonymously but then must login/register in order for the question go be shown """ if request.user.is_authenticated(): if request.user.is_read_only(): referer = request.META.get("HTTP_REFERER", reverse('questions')) # request.user.message_set.create(message=_('Sorry, but you have only read access')) django_messages.info(request, _('Sorry, but you have only read access')) return redirect(referer) if askbot_settings.READ_ONLY_MODE_ENABLED: return redirect('index') if request.method == 'POST': form = forms.AskForm(request.POST, user=request.user) if form.is_valid(): timestamp = timezone.now() title = form.cleaned_data['title'] wiki = form.cleaned_data['wiki'] tagnames = form.cleaned_data['tags'] text = form.cleaned_data['text'] ask_anonymously = form.cleaned_data['ask_anonymously'] post_privately = form.cleaned_data['post_privately'] group_id = form.cleaned_data.get('group_id', None) language = form.cleaned_data.get('language', None) if request.user.is_authenticated(): drafts = models.DraftQuestion.objects.filter( author=request.user) drafts.delete() user = form.get_post_user(request.user) elif request.user.is_anonymous( ) and askbot_settings.ALLOW_ASK_UNREGISTERED: user = models.get_or_create_anonymous_user() ask_anonymously = True else: user = None if user: try: question = user.post_question( title=title, body_text=text, tags=tagnames, wiki=wiki, is_anonymous=ask_anonymously, is_private=post_privately, timestamp=timestamp, group_id=group_id, language=language, ip_addr=request.META.get('REMOTE_ADDR')) signals.new_question_posted.send( None, question=question, user=user, form_data=form.cleaned_data) return redirect(question) except exceptions.PermissionDenied as e: traceback.print_exc() # request.user.message_set.create(message=force_text(e)) django_messages.info(request, force_text(e)) return redirect('index') else: request.session.flush() session_key = request.session.session_key models.AnonymousQuestion.objects.create( session_key=session_key, title=title, tagnames=tagnames, wiki=wiki, is_anonymous=ask_anonymously, text=text, added_at=timestamp, ip_addr=request.META.get('REMOTE_ADDR'), ) return redirect(url_utils.get_login_url()) if request.method == 'GET': form = forms.AskForm(user=request.user) draft_title = '' draft_text = '' draft_tagnames = '' if request.user.is_authenticated(): drafts = models.DraftQuestion.objects.filter(author=request.user) if len(drafts) > 0: draft = drafts[0] draft_title = draft.title draft_text = draft.text draft_tagnames = draft.tagnames form.initial = { 'ask_anonymously': request.REQUEST.get('ask_anonymously', False), 'tags': request.REQUEST.get('tags', draft_tagnames), 'text': request.REQUEST.get('text', draft_text), 'title': request.REQUEST.get('title', draft_title), 'post_privately': request.REQUEST.get('post_privately', False), 'language': get_language(), 'wiki': request.REQUEST.get('wiki', False), } if 'group_id' in request.REQUEST: try: group_id = int(request.GET.get('group_id', None)) form.initial['group_id'] = group_id except Exception: pass editor_is_folded = \ askbot_settings.QUESTION_BODY_EDITOR_MODE == 'folded' and \ askbot_settings.MIN_QUESTION_BODY_LENGTH == 0 and \ form.initial['text'] == '' data = { 'active_tab': 'ask', 'page_class': 'ask-page', 'form': form, 'editor_is_folded': editor_is_folded, 'mandatory_tags': models.tag.get_mandatory_tags(), 'email_validation_faq_url': reverse('faq') + '# validate', 'category_tree_data': askbot_settings.CATEGORY_TREE, 'tag_names': list( ), # need to keep context in sync with edit_question for tag editor } data.update(context.get_for_tag_editor()) return render(request, 'ask.jinja', data)
request.session.flush() session_key = request.session.session_key models.AnonymousQuestion.objects.create( session_key=session_key, title=title, tagnames=tagnames, wiki=wiki, is_anonymous=ask_anonymously, text=text, added_at=timestamp, ip_addr=request.META['REMOTE_ADDR'], ) return HttpResponseRedirect(url_utils.get_login_url()) if request.method == 'GET': form = forms.AskForm(user=request.user, initial={'language': get_language()}) draft_title = '' draft_text = '' draft_tagnames = '' if request.user.is_authenticated(): drafts = models.DraftQuestion.objects.filter(author=request.user) if len(drafts) > 0: draft = drafts[0] draft_title = draft.title draft_text = draft.text draft_tagnames = draft.tagnames form.initial = { 'title': request.REQUEST.get('title', draft_title), 'text': request.REQUEST.get('text', draft_text),