def knowledge_ask(request, template='django_knowledge/ask.html'): if not user_can_ask_question(request.user): return redirect(reverse('knowledge_index')) if settings.LOGIN_REQUIRED and not request.user.is_authenticated(): return HttpResponseRedirect(settings.LOGIN_URL + "?next=%s" % request.path) form = QuestionForm(request.user, request.POST or None) if request.method == 'POST' and form.is_valid(): if request.user.is_authenticated( ) or not form.cleaned_data['phone_number']: question = form.save() return redirect(question.get_absolute_url()) else: return redirect('knowledge_index') return render( request, template, { 'request': request, 'form': form, 'categories': Category.objects.all(), 'BASE_TEMPLATE': settings.BASE_TEMPLATE, })
def test_form_question_status(self): # test the default for anonymous in tests/settings.py... form = QuestionForm(self.joe) self.assertIn('status', form.fields.keys()) QUESTION_POST = { 'title': 'This is a title friend!', 'body': 'This is the body friend!', 'status': 'public' } question = QuestionForm(self.joe, QUESTION_POST).save() self.assertEquals(question.status, 'public')
def test_question_form_display(self): self.assertEqual( None, QuestionForm(self.anon) ) self.assertNotEqual( None, QuestionForm(self.joe) ) self.assertNotEqual( None, ResponseForm(self.admin, self.question) )
def get_context_data(self, **kwargs): context = super(KnowledgeList, self).get_context_data(**kwargs) context['search'] = self.request.GET.get('title', None) context['my_questions'] = self.get_my_questions() context['categories'] = Category.objects.all() context['form'] = QuestionForm(self.request.user, initial={'title': context['search']}) return context
def test_form_saving(self): QUESTION_POST = { 'title': 'This is a title friend!', 'body': 'This is the body friend!', 'status': 'private' } form = QuestionForm(self.joe, QUESTION_POST) self.assertTrue(form.is_valid()) question = form.save() self.assertEquals(question.status, 'private') self.assertEquals(question.name, None) self.assertEquals(question.email, None) self.assertEquals(question.title, 'This is a title friend!') self.assertEquals(question.body, 'This is the body friend!') self.assertEquals(question.user, self.joe) RESPONSE_POST = {'body': 'This is the response body friend!'} form = ResponseForm(self.joe, question, RESPONSE_POST) self.assertTrue(form.is_valid()) response = form.save() self.assertEquals(response.status, 'inherit') self.assertEquals(response.name, None) self.assertEquals(response.email, None) self.assertEquals(response.body, 'This is the response body friend!') self.assertEquals(response.user, self.joe)
def test_AUTO_PUBLICIZE(self): self.assertFalse(settings.AUTO_PUBLICIZE) QUESTION_POST = { 'title': 'This is a title friend!', 'body': 'This is the body friend!', 'status': 'draft' } question = QuestionForm(self.joe, QUESTION_POST).save() self.assertEquals(question.status, 'draft') ############# flip setting ############## settings.AUTO_PUBLICIZE = not settings.AUTO_PUBLICIZE ############# flip setting ############## question = QuestionForm(self.joe, QUESTION_POST).save() self.assertEquals(question.status, 'public') ############# flip setting ############## settings.AUTO_PUBLICIZE = not settings.AUTO_PUBLICIZE
def test_ALLOW_ANONYMOUS(self): self.assertFalse(settings.ALLOW_ANONYMOUS) self.assertEqual( None, QuestionForm(self.anon) ) self.assertEqual( None, ResponseForm(self.anon, self.question) ) ############# flip setting ############## settings.ALLOW_ANONYMOUS = not settings.ALLOW_ANONYMOUS ############# flip setting ############## self.assertNotEqual( None, QuestionForm(self.anon) ) self.assertNotEqual( None, ResponseForm(self.anon, self.question) ) form = QuestionForm(self.anon) self.assertNotIn('status', list(form.fields.keys())) # missing the name/email... QUESTION_POST = { 'title': 'This is a title friend!', 'body': 'This is the body friend!' } form = QuestionForm(self.anon, QUESTION_POST) self.assertFalse(form.is_valid()) QUESTION_POST = { 'name': 'Test Guy', 'email': '*****@*****.**', 'title': 'This is a title friend!', 'body': 'This is the body friend!' } form = QuestionForm(self.anon, QUESTION_POST) self.assertTrue(form.is_valid()) question = form.save() # question has no user and is public by default self.assertFalse(question.user) self.assertEqual(question.name, 'Test Guy') self.assertEqual(question.email, '*****@*****.**') self.assertEqual(question.status, 'public') ############# flip setting ############## settings.ALLOW_ANONYMOUS = not settings.ALLOW_ANONYMOUS
def test_form_question_status(self): # test the default for anonymous in tests/settings.py... form = QuestionForm(self.joe) self.assertIn('status', form.fields.keys()) # internal is only selectable for admins QUESTION_POST = { 'title': 'This is a title friend!', 'body': 'This is the body friend!', 'status': 'internal' } self.assertFalse(QuestionForm(self.joe, QUESTION_POST).is_valid()) self.assertTrue(QuestionForm(self.admin, QUESTION_POST).is_valid()) QUESTION_POST = { 'title': 'This is a title friend!', 'body': 'This is the body friend!', 'status': 'public' } question = QuestionForm(self.joe, QUESTION_POST).save() self.assertEquals(question.status, 'public')
def test_sending_staffers_remove_self(self): self.assertTrue(settings.ALERTS) self.assertEqual(len(mail.outbox), 0) QUESTION_POST = { 'title': 'This is a title friend!', 'body': 'This is the body friend!', 'status': 'private' } question = QuestionForm(self.admin, QUESTION_POST).save() self.assertEqual(len(mail.outbox), 0) # none for admin
def knowledge_list(request, category_slug=None, template='django_knowledge/list.html'): if settings.LOGIN_REQUIRED and not request.user.is_authenticated(): return HttpResponseRedirect(settings.LOGIN_URL + "?next=%s" % request.path) search = request.GET.get('title', None) questions = Question.objects.can_view(request.user)\ .prefetch_related('responses__question') if search: questions = questions.filter( Q(title__icontains=search) | Q(body__icontains=search)) category = None if category_slug: category = get_object_or_404(Category, slug=category_slug) questions = questions.filter(categories=category) paginator, questions = paginate(questions, 50, request.GET.get('page', '1')) [setattr(q, '_requesting_user', request.user) for q in questions] form = None if user_can_ask_question(request.user): form = QuestionForm(request.user, initial={'title': search}) return render( request, template, { 'request': request, 'search': search, 'questions': questions, 'category': category, 'categories': Category.objects.all(), 'form': form, 'BASE_TEMPLATE': settings.BASE_TEMPLATE, })
def test_ALLOW_ANONYMOUS(self): self.assertFalse(settings.ALLOW_ANONYMOUS) self.assertEqual(None, QuestionForm(self.anon)) self.assertEqual(None, ResponseForm(self.anon, self.question)) ############# flip setting ############## settings.ALLOW_ANONYMOUS = not settings.ALLOW_ANONYMOUS ############# flip setting ############## self.assertNotEqual(None, QuestionForm(self.anon)) self.assertNotEqual(None, ResponseForm(self.anon, self.question)) form = QuestionForm(self.anon) self.assertNotIn("status", form.fields.keys()) # missing the name/email... QUESTION_POST = {"title": "This is a title friend!", "body": "This is the body friend!"} form = QuestionForm(self.anon, QUESTION_POST) self.assertFalse(form.is_valid()) QUESTION_POST = { "name": "Test Guy", "email": "*****@*****.**", "title": "This is a title friend!", "body": "This is the body friend!", } form = QuestionForm(self.anon, QUESTION_POST) self.assertTrue(form.is_valid()) question = form.save() # question has no user and is public by default self.assertFalse(question.user) self.assertEquals(question.name, "Test Guy") self.assertEquals(question.email, "*****@*****.**") self.assertEquals(question.status, "public") ############# flip setting ############## settings.ALLOW_ANONYMOUS = not settings.ALLOW_ANONYMOUS
def test_form_saving(self): QUESTION_POST = { 'user': self.joe.pk, 'title': 'This is a title friend!', 'body': 'This is the body friend!', 'status': 'draft', 'categories': '' } form = QuestionForm(self.joe, QUESTION_POST) self.assertTrue(form.is_valid()) question = form.save() self.assertEquals(question.status, 'draft') self.assertEquals(question.name, None) self.assertEquals(question.email, None) self.assertEquals(question.title, 'This is a title friend!') self.assertEquals(question.body, 'This is the body friend!') self.assertEquals(question.user, self.joe) RESPONSE_POST = { 'body': 'This is the response body friend!' } form = ResponseForm(self.joe, question, RESPONSE_POST) self.assertTrue(form.is_valid()) response = form.save() self.assertEquals(response.status, 'inherit') self.assertEquals(response.name, None) self.assertEquals(response.email, None) self.assertEquals(response.body, 'This is the response body friend!') self.assertEquals(response.user, self.joe)
def test_ALLOW_ANONYMOUS(self): self.assertFalse(settings.ALLOW_ANONYMOUS) self.assertEqual(None, QuestionForm(self.anon)) self.assertEqual(None, ResponseForm(self.anon, self.question)) ############# flip setting ############## settings.ALLOW_ANONYMOUS = not settings.ALLOW_ANONYMOUS ############# flip setting ############## self.assertNotEqual(None, QuestionForm(self.anon)) self.assertNotEqual(None, ResponseForm(self.anon, self.question)) form = QuestionForm(self.anon) self.assertNotIn('status', form.fields.keys()) # missing the name/email... QUESTION_POST = { 'title': 'This is a title friend!', 'body': 'This is the body friend!' } form = QuestionForm(self.anon, QUESTION_POST) self.assertFalse(form.is_valid()) QUESTION_POST = { 'name': 'Test Guy', 'email': '*****@*****.**', 'title': 'This is a title friend!', 'body': 'This is the body friend!' } form = QuestionForm(self.anon, QUESTION_POST) self.assertTrue(form.is_valid()) question = form.save() # question has no user and is public by default self.assertFalse(question.user) self.assertEquals(question.name, 'Test Guy') self.assertEquals(question.email, '*****@*****.**') self.assertEquals(question.status, 'public') ############# flip setting ############## settings.ALLOW_ANONYMOUS = not settings.ALLOW_ANONYMOUS
def knowledge_ask(request, page='asc', template='django_knowledge/ask.html', Form=QuestionForm, forms=None, curent_cat=1): logger.debug("knowledge_ask page: {0}".format(page)) fiber_page = Page.objects.get(url__exact=page) fiber_current_pages = [fiber_page] # fiber_current_pages = [] # fiber_current_pages.append(fiber_page) cat = Category.objects.all() cur_cat = Category.objects.get(pk=curent_cat) if settings.LOGIN_REQUIRED and not request.user.is_authenticated(): return HttpResponseRedirect(settings.LOGIN_URL + "?next=%s" % request.path) # TODO: добавить при отправке сплывающая подсказка вам отправлено сообщение.... if request.method == 'POST': if forms == 'QuestionAskForm': form = QuestionAskForm(request.user, request.POST, initial={ 'title': cur_cat.title, 'categories': curent_cat }) elif forms == 'ShopAskForm': form = ShopAskForm(request.user, request.POST, initial={ 'title': cur_cat.title, 'categories': curent_cat }) elif forms == 'OfficesAskForm': form = OfficesAskForm(request.user, request.POST, initial={ 'title': cur_cat.title, 'categories': curent_cat }) # form.cleaned_data['title'] = cur_cat.title # form.categories = curent_cat # logger.debug("ShopAskForm: {0}".format(form)) # form.save() else: form = QuestionForm(request.user, request.POST) #form = Form(request.user, request.POST) if form.errors: logger.debug("{1} ERRKR: {0}".format(form.errors, forms)) if form and form.is_valid(): form.cleaned_data['title'] = cur_cat.title form.cleaned_data['categories'] = curent_cat logger.debug("!!!!!!!! cleaned_data: {0}".format( form.cleaned_data)) if request.user.is_authenticated(): logger.debug("FORM SEND authenticated user") # TODO: отправляем полностью письмо send_mail_full(form.cleaned_data, tpl_subject=cur_cat.tpl_subject, tpl_message=cur_cat.tpl_message, tpl_message_html=cur_cat.tpl_message_html) # question = form.save() form = None # return redirect(question.get_absolute_url()) else: logger.debug("FORM SEND is anonymous") send_mail_full(form.cleaned_data, tpl_subject=cur_cat.tpl_subject, tpl_message=cur_cat.tpl_message, tpl_message_html=cur_cat.tpl_message_html) # question = form.save() form = None # return redirect(settings.KN_REDIRECT_PATH) # else: # logger.debug("FORM ERROR: {0}".format(form)) else: if forms == 'QuestionAskForm': form = QuestionAskForm(request.user, initial={ 'title': cur_cat.title, 'categories': curent_cat }) elif forms == 'ShopAskForm': form = ShopAskForm(request.user, initial={ 'title': cur_cat.title, 'categories': curent_cat }) elif forms == 'OfficesAskForm': form = OfficesAskForm(request.user, initial={ 'title': cur_cat.title, 'categories': curent_cat }) else: form = QuestionForm(request.user) #form = Form(request.user) return render( request, template, { 'request': request, 'page': page, 'fiber_page': fiber_page, 'fiber_current_pages': fiber_current_pages, 'my_questions': get_my_questions(request), 'form': form, 'categories': cat, 'cur_cat': cur_cat })