-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/questions' into practice_app
- Loading branch information
Showing
11 changed files
with
209 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{% extends "main/base.html" %} | ||
|
||
{% block content %} | ||
<h3>Here you can search for unanswered questions in Stackexchange. (with answer count=0).</h3> | ||
<p>Enter the tag you want to search for.</p> | ||
<p>If you don't have a specific topic in mind, type "all" to get all the unanswered questions.</p> | ||
<p>Tip: If you want to get the reply in pure json format, extend the result url with ?format=json</p> | ||
<form action="{% url 'questions_form_main' %}" method="post", enctype="multipart/form-data"> | ||
{% csrf_token %} | ||
{{ form }} | ||
<input type="submit" value="Get questions"> | ||
</form> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
from urllib import response | ||
from django.shortcuts import render | ||
import requests | ||
import json | ||
from django.http import HttpResponse, JsonResponse | ||
from rest_framework.response import Response | ||
from rest_framework import status | ||
from rest_framework.decorators import api_view | ||
|
||
# this view uses the stackexchange api (given below) to get unanswered questions (with answercount 0) and depending on the given keyword tag | ||
#either returns all the unanswered questions if tag == "all" | ||
# or returns unanswered questions tagged with the given tag | ||
#the Response uses default django html template, extend the url with <?format=json> to get pure json | ||
|
||
|
||
@api_view(['GET']) | ||
def getquestions(request, tag, format=None ): | ||
response = requests.get('http://api.stackexchange.com/2.3/questions/unanswered?order=desc&sort=activity&site=stackoverflow') | ||
|
||
unansweredquestions = {} | ||
#unansweredquestions['allquestions']=[] | ||
allquestions = [] | ||
|
||
if tag == "all": | ||
for question in response.json()['items']: | ||
if question['answer_count']==0 : | ||
questiondata = {} | ||
questiondata['title'] = question['title'] | ||
questiondata['link'] = question['link'] | ||
questiondata['tags'] = question['tags'] | ||
allquestions.append(questiondata) | ||
|
||
#print(question['title']) | ||
#print(question['link']) | ||
#else: | ||
# print("skipped") | ||
#print() | ||
|
||
if allquestions == []: | ||
unansweredquestions["message"] = "Sorry there are no unanswered questions at the moment, please try again later." | ||
return Response(unansweredquestions, status= status.HTTP_404_NOT_FOUND) | ||
|
||
unansweredquestions['allquestions']= allquestions | ||
|
||
#return JsonResponse(unansweredquestions) | ||
return Response(unansweredquestions, status= status.HTTP_200_OK) | ||
|
||
if response.json() == {}: | ||
#print(response.json()) | ||
unansweredquestions["message"] = "Sorry there are no unanswered questions with the given tag at the moment, please try again later." | ||
return Response(unansweredquestions, status= status.HTTP_404_NOT_FOUND) | ||
|
||
|
||
|
||
for question in response.json()['items']: | ||
if question['answer_count']==0 and tag in question['tags']: #and "shell" in question['tags'] | ||
questiondata = {} | ||
questiondata['title'] = question['title'] | ||
questiondata['link'] = question['link'] | ||
questiondata['tags'] = question['tags'] | ||
allquestions.append(questiondata) | ||
|
||
#print(question['title']) | ||
#print(question['link']) | ||
#else: | ||
# print("skipped") | ||
#print() | ||
|
||
if allquestions == []: | ||
unansweredquestions["message"] = "Sorry there are no unanswered questions with the given tag at the moment, please try again later." | ||
return Response(unansweredquestions, status= status.HTTP_404_NOT_FOUND) | ||
|
||
unansweredquestions['allquestions']= allquestions | ||
|
||
#return JsonResponse(unansweredquestions) | ||
return Response(unansweredquestions, status= status.HTTP_200_OK) | ||
#return HttpResponse("Bravo!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
from django import forms | ||
|
||
class TagForm(forms.Form): | ||
tag=forms.CharField(widget=forms.TextInput(attrs={'placeholder':'Enter tag name'}), max_length=100) | ||
tag=forms.CharField(widget=forms.TextInput(attrs={'placeholder':'Enter tag name'}), max_length=100) | ||
|
||
class QuestionTagForm(forms.Form): | ||
tag=forms.CharField(widget=forms.TextInput(attrs={'placeholder':'Question tag'}), max_length=100) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from urllib import response | ||
from django.urls import reverse | ||
from rest_framework import status | ||
from rest_framework.test import APITestCase | ||
from django.test import TestCase, Client | ||
from api.serializers import myUserSerializer, ArtItemSerializer, CommentSerializer, TagSerializer | ||
from api.models import ArtItem, Tag, myUser | ||
from django.contrib.auth.models import User | ||
import json | ||
from faker import Faker | ||
|
||
class TestQuestions(APITestCase): | ||
|
||
def setUp(self): | ||
# setting up for the test | ||
print("TestQuestions:setUp_:begin") | ||
|
||
##### Mock up data ##### | ||
|
||
faker = Faker() | ||
self.tagname = faker.word() | ||
|
||
print("TestQuestions:setUp_:end") | ||
|
||
|
||
|
||
def tearDown(self): | ||
# cleaning up after the test | ||
print("TestQuestions:setUp_:begin") | ||
|
||
# do something | ||
print("TestQuestions:setUp_:end") | ||
|
||
|
||
def test_questions_all(self): | ||
response = self.client.get('/api/v1/questions/all') | ||
unansweredquestions = {} | ||
unansweredquestions["message"] = "Sorry there are no unanswered questions at the moment, please try again later." | ||
|
||
if response.status_code == 404: | ||
self.assertEqual(response.json(), unansweredquestions ) # check if the error message is correct | ||
|
||
else: | ||
self.assertEqual(response.status_code, 200 ) | ||
self.assertNotEqual(response.json(), {}) | ||
self.assertNotEqual(response.json(), unansweredquestions) | ||
|
||
def test_questions_tag(self): | ||
response = self.client.get('/api/v1/questions/{}'.format(self.tagname)) | ||
unansweredquestions = {} | ||
unansweredquestions["message"] = "Sorry there are no unanswered questions with the given tag at the moment, please try again later." | ||
|
||
if response.status_code == 404: | ||
self.assertEqual(response.json(), unansweredquestions ) # check if the error message is correct | ||
|
||
else: | ||
self.assertEqual(response.status_code, 200 ) | ||
self.assertNotEqual(response.json(), {}) | ||
self.assertNotEqual(response.json(), unansweredquestions) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters