def create_question(request): if request.method == 'POST': form = CreateQuestion(request.POST) if form.is_valid(): cd = form.cleaned_data question = cd['question'] tags = cd ['tags'] question_tlt = cd['question_title'] q = Question(question_text=question, question_title=question_tlt,pub_date=timezone.now(), creator=request.user, tags_list=tags) q.save() for t in tags.split(','): if len(t) > 0: try: print "Text : " + t print len(t) my_tag = Tags.objects.get(tag_text=t) except (KeyError, Tags.DoesNotExist): tag_create = Tags(tag_text=t) tag_create.save() q.tags.add(tag_create) else: q.tags.add(my_tag) return HttpResponseRedirect('/forum/') else: form = CreateQuestion() return render(request, 'forum/create_question.html', {'form': form})
def test_question_form(user): from forum.forms import QuestionForm from forum.models import Question instance = Question() instance.course = mommy.make('Course', slug='dbsql') instance.user = user data = { 'title': 'Test Title', 'text': 'asljf asdfhuas dfasdflashfdlusafdlsafdlsa filasdflisalfdiayslfdnsalfdyaslifd', } form = QuestionForm(instance=instance, data=data) assert form.is_valid() is True, form.errors form.save() assert Question.objects.filter(slug='test-title').exists() data = { 'title': '', 'text': 'asljf asdfhuas dfasdflashfdlusafdlsafdlsa filasdflisalfdiayslfdnsalfdyaslifd', } form = QuestionForm(data=data) assert form.is_valid() is False data = { 'title': 'Test Title', 'text': '', } form = QuestionForm(data=data) assert form.is_valid() is False
def create_question(request): if request.method == 'POST': form = CreateQuestion(request.POST) if form.is_valid(): cd = form.cleaned_data question = cd['question'] tags = cd['tags'] question_tlt = cd['question_title'] q = Question(question_text=question, question_title=question_tlt, pub_date=timezone.now(), creator=request.user, tags_list=tags) q.save() for t in tags.split(','): if len(t) > 0: try: print "Text : " + t print len(t) my_tag = Tags.objects.get(tag_text=t) except (KeyError, Tags.DoesNotExist): tag_create = Tags(tag_text=t) tag_create.save() q.tags.add(tag_create) else: q.tags.add(my_tag) return HttpResponseRedirect('/forum/') else: form = CreateQuestion() return render(request, 'forum/create_question.html', {'form': form})
def process_data(self, **data): processed_data = self.create_revision_data(True, **data) if 'added_at' in data: processed_data['added_at'] = data['added_at'] question = Question(author=self.user, **processed_data) question.save() self.node = question
def process_data(self, **data): processed_data = self.create_revision_data(True, **data) if 'added_at' in data: processed_data['added_at'] = data['added_at'] question = Question(author=self.user, **processed_data) question.save() self.node = question messages.info(REQUEST_HOLDER.request, self.describe(self.user))
def add_question(request, course_id): if request.method == 'POST': question_title = request.POST['question_title'] question_content = request.POST['question_content'] new_question = Question(title=question_title, content=question_content, course=Course.objects.get(id=course_id), user=request.user, time=datetime.datetime.now()) new_question.save() return HttpResponseRedirect("/course/" + str(course_id))
def post(self, request, *args, **kwargs): new_question = Question() course = get_object_or_404(Course, slug=self.kwargs['course_slug']) new_question.course = course new_question.user = request.user form = QuestionForm(instance=new_question, **self.get_form_kwargs()) if form.is_valid(): form.save() self.success_url = reverse_lazy('forum_question', kwargs={'slug': new_question.slug}) return self.form_valid(form) else: return self.form_invalid(form)
def create(request: HttpRequest): """ Create the question endpoint. Request content: {"subject": "My Subject", "body": "Text of my question", "user_id": 1} """ if not request.user.is_authenticated: return HttpResponseForbidden(json.dumps( {'message': 'User must be authenticated'}), content_type='application/json') data = json.loads(request.body.decode('utf-8')) question = Question() if 'subject' in data: question.subject = data['subject'] if 'body' in data: question.body = encrypt(data['body'], settings.TEXT_SECRET_CODE) question.user_id = request.user.id question.status = Question.TO_BE_APPROVED question.save() return HttpResponse(json.dumps({ 'subject': question.subject, 'body': decrypt(question.body, settings.TEXT_SECRET_CODE), 'user__first_name': request.user.first_name, 'user__last_name': request.user.last_name, 'answers': [], 'id': question.id }), content_type='application/json')
def post_question(request): if not request.user.is_authenticated: return redirect("/login/") if request.method=="POST": text = request.POST.get('editordata') cat = request.POST.get('category') question = Question(question = text, category = cat, author=request.user) mp.track(request.user.username, 'New question', {"Link":"http://127.0.0.1:8000/", "Question": f"{question.question}"}) question.save() return redirect("/discussion/") question = Question.objects.all().order_by('-datetime') return render(request, "forum/discussion.html", {"question" : question})
def test_search(self): user = User.objects.create(username='******', email='*****@*****.**') Question.objects.create(subject='test', body='test', user=user) Question.objects.create(subject='egg test', body='egg test', user=user) Question.objects.create(subject='test spam', body='test spam', user=user) Question.objects.create(subject='spam egg', body='spam egg', user=user) query_test = Question.search('test') self.assertEqual(query_test.count(), 3) query_test = Question.search('egg') self.assertEqual(query_test.count(), 2) query_test = Question.search('spam') self.assertEqual(query_test.count(), 2)
def test_basic_addition(self): user = User.objects.all()[0] point = Point() point.fromuser = user point.save() question = Question(); question.user = user question.title = "hi" question.slug = 'something' question.text = 'else' question.save() question.points.add(point) question.save() self.assertEquals(1, question.total_points())
def post(self, request, **kwargs): form = AskQuestionForm(data=request.POST, instance=Question(author=request.user)) if form.is_valid(): question = form.save() return redirect('forum:question', question_id=question.id) kwargs['form'] = form return super().get(request, **kwargs)
def process_data(self, **data): processed_data = self.create_revision_data(True, **data) if 'added_at' in data: processed_data['added_at'] = data['added_at'] question = Question(author=self.user, **processed_data) question.save() #BEGIN #Linking OSQA node with Magnate zinnia entry from zinnia.models.entry import Entry try: entry_id = int(data['entry_id']) # not processed_data question.about_entries.create(entry=Entry.objects.get(pk=entry_id)) except (KeyError, ValueError, Entry.DoesNotExist) as e: pass #END self.node = question messages.info(REQUEST_HOLDER.request, self.describe(self.user))
def process_data(self, **data): processed_data = self.create_revision_data(True, **data) if 'added_at' in data: processed_data['added_at'] = data['added_at'] question = Question(author=self.user, **processed_data) question.save() self.node = question makers = list(data['maker']) # Add makers for maker in data['maker']: if not (PromiseMaker.objects.filter(maker=maker,promise=question).exists()): temp = PromiseMaker(maker=maker,promise=question) temp.save() # Delete removed makers for pm in PromiseMaker.objects.filter(promise=question): if not (pm.maker in makers): maker.delete()
def process_data(self, **data): processed_data = self.create_revision_data(True, **data) if 'added_at' in data: processed_data['added_at'] = data['added_at'] question = Question(author=self.user, **processed_data) question.save() if 'invites' in data: question.invites = data['invites'].strip() for inviter_name in question.invites.split(): try: inviter = User.objects.get(username=inviter_name) except User.DoesNotExist: if isidvalid(inviter_name): inviter = User(username=userid, email=(userid+ u'@synopsys.com'), real_name=userid, email_isvalid=True) inviter.save() else: continue question.whitelist.add(inviter) self.node = question messages.info(REQUEST_HOLDER.request, self.describe(self.user))
def add_question(request): group_id = request.POST.get('group_id', False); #attempt to create question using parameters from POST try: q = Question( question_text=request.POST['question'], body = request.POST['body'], pub_date=timezone.now(), user=request.user, subject=request.POST['subject'] ) if group_id: q.group = Group.objects.get(pk=group_id) except(KeyError, Question.DoesNotExist): return render(request, 'forum/index', {'error_message': "Question must not be empty!"}) else: #allow for insertion of HTML in questions q.body = q.body.replace("\n", "<br/>") q.body = q.body.replace(" ", " ") q.save() if group_id: return HttpResponseRedirect(reverse('groups:detail', args=(group_id))) return HttpResponseRedirect(reverse('forum:index'))
def get_list(request): """ Get the JSON response of list of questions. """ if not request.user.is_authenticated: return HttpResponseForbidden(json.dumps( {'message': 'User must be authenticated'}), content_type='application/json') iterator = Question.objects.filter(status=Question.PUBLIC) \ if not request.GET.get('q') else Question.search(request.GET.get('q')) data = [{ 'subject': question.subject, 'body': decrypt(question.body, settings.TEXT_SECRET_CODE), 'id': question.id, 'user__first_name': question.user.first_name, 'user__last_name': question.user.last_name, } for question in iterator] return HttpResponse(json.dumps(data), content_type='application/json')
def test_add_point_to_question(self): self.client.login(username='******', password='******') user = User.objects.all()[0] question = Question(); question.user = user question.title = "hi" question.slug = 'something' question.text = 'else' question.save() response = self.client.post('/ajax/forum/upvote_question.json', {'question_pk': question.pk}) json_response = json.loads(response.content) self.assertTrue(json_response['success']) self.assertTrue(json_response['data']['delta'], 1) self.assertEquals(1, Question.objects.all().count()) response = self.client.post('/ajax/forum/upvote_question.json', {'question_pk': question.pk}) json_response = json.loads(response.content) self.assertTrue(json_response['success']) self.assertTrue(json_response['data']['delta'], -1) print Point.objects.all() self.assertEquals(0, Point.objects.all().count())
def test_user_points(self): josh = User.objects.all()[0] question = Question(); question.user = josh question.title = "hi" question.slug = 'something' question.text = 'else' question.save() point = Point() point.fromuser = josh point.save() question.points.add(point) user = User() user.save() point = Point() point.fromuser = user point.save() question.points.add(point) self.assertEquals(2, userPoint(josh))
def process_data(self, **data): question = Question(author=self.user, **self.create_revision_data(True, **data)) question.save() self.node = question
from forum.models import Question from django.conf import settings from djangosphinx.models import SphinxSearch Question.add_to_class('search', SphinxSearch( index=' '.join(settings.SPHINX_SEARCH_INDICES), mode='SPH_MATCH_BOOLEAN', ) )
from forum.models import Question from django.conf import settings from djangosphinx.manager import SphinxSearch from djangosphinx.models import SphinxSearch Question.add_to_class("search", SphinxSearch(index=" ".join(settings.SPHINX_SEARCH_INDICES), mode="SPH_MATCH_ALL"))
### -*- coding: utf-8 -*- ## from django.db import models from django.utils.translation import ugettext_lazy as _ from muaccounts.models import MUAccount from forum.models import Question, Answer Question.add_to_class('muaccount', models.ForeignKey(MUAccount, related_name="questions")) Question.add_to_class('bounty_points', models.PositiveIntegerField(_('bounty points'), default=0)) class PointAward(models.Model): date = models.DateTimeField(auto_now_add=True) answer = models.OneToOneField(Answer, related_name='point_award') points = models.PositiveIntegerField(default=0) class Meta: ordering = ('-date',)
from forum.models import Question from django.conf import settings from djangosphinx.manager import SphinxSearch from djangosphinx.models import SphinxSearch Question.add_to_class( 'search', SphinxSearch( index=' '.join(settings.SPHINX_SEARCH_INDICES), mode='SPH_MATCH_ALL', ))