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 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 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 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_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_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 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 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 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 process_data(self, **data): question = Question(author=self.user, **self.create_revision_data(True, **data)) question.save() self.node = question