Skip to content

Commit

Permalink
Merge pull request #138 from bounswe/practice-app/enhancement/post-in…
Browse files Browse the repository at this point in the history
…fo-#135

Some requested enhancements in practice-app/post-info
  • Loading branch information
yavuuzsameet authored May 18, 2022
2 parents a9b1e25 + 44a5e33 commit 78d0209
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 12 deletions.
Binary file modified practice-app/post_info/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
48 changes: 48 additions & 0 deletions practice-app/post_info/forms.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{% load crispy_forms_tags %}
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/html">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<title></title>
</head>
<body>

</div>
{% if action_fail %}
<div class="alert alert-danger" role="alert">
<h4 class="alert-heading">Error</h4>
<p>Please enter valid values.</p>
</div>

{% endif %}

</div>
<div class="container">
<div class="d-flex justify-content-around" style="display:flex;">

<form class="form-group" method="GET" novalidate action="{% url 'getPost' %}">
<h2>View a Post</h2>
{{getForm|crispy}}
{% csrf_token %}
<div class="d-flex p-2"></div>
<button type="submit" class="btn btn-info">Submit</button>
</form>
<form class="form-group" method="POST" novalidate action="{% url 'postPost' %}">
<h2>Update a Post</h2>
{{postForm|crispy}}
{% csrf_token %}
<p></p>
<p>Note: Please provide the id of the category, not the name of it.</p>
<div class="d-flex p-2"></div>
<button type="submit" class="btn btn-info">Submit</button>
</form>

</div>
</div>
</body>
</html>
Binary file not shown.
14 changes: 13 additions & 1 deletion practice-app/post_info/templates/forms.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% load crispy_forms_tags %}
<!DOCTYPE html>
<html>
<html xmlns="http://www.w3.org/1999/html">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
Expand All @@ -12,6 +12,16 @@
</head>
<body>

</div>
{% if action_fail %}
<div class="alert alert-danger" role="alert">
<h4 class="alert-heading">Error</h4>
<p>Please enter valid values.</p>
</div>

{% endif %}

</div>
<div class="container">
<div class="d-flex justify-content-around" style="display:flex;">

Expand All @@ -26,6 +36,8 @@ <h2>View a Post</h2>
<h2>Update a Post</h2>
{{postForm|crispy}}
{% csrf_token %}
<p></p>
<p>Note: Please provide the id of the category, not the name of it.</p>
<div class="d-flex p-2"></div>
<button type="submit" class="btn btn-info">Submit</button>
</form>
Expand Down
Binary file not shown.
30 changes: 19 additions & 11 deletions practice-app/post_info/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
def index(request):
getForm = postGetForm()
postForm = postPostForm()
return render(request, "forms.html", {"getForm":getForm, "postForm":postForm})
return render(request, "forms.html", {"getForm":getForm, "postForm":postForm, "action_fail":False})


class PostInfo(APIView):
Expand Down Expand Up @@ -66,16 +66,17 @@ def post(self, request, pk,*args, **kwargs):
)

country = request.data.get('country')
country = country.lower()
country = country.capitalize()

try:
result = requests.get(f"https://coronavirus.m.pipedream.net/").json()
data = result["rawData"]
output_dict = [x for x in data if x['Country_Region'] == country]
result = requests.get(f"https://api.covid19api.com/summary").json()
data = result["Countries"]
output_dict = [x for x in data if x['Country'] == country]
output = output_dict[0]


death = output['Deaths']
case = output['Confirmed']
death = output['NewDeaths']
case = output['NewConfirmed']

covid19cases = {
'death': death,
Expand All @@ -91,7 +92,7 @@ def post(self, request, pk,*args, **kwargs):
'category': request.data.get('category'),
'user': request.user.id,
'timestamp': django.utils.timezone.now(),
'country': request.data.get('country'),
'country': country,
'covid19cases': covid19cases
}

Expand All @@ -101,7 +102,7 @@ def post(self, request, pk,*args, **kwargs):
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)
#print("nooo")

return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


Expand All @@ -122,5 +123,12 @@ def getPost(request):
def postPost(request):
a = PostInfo()
post_id = request.POST["post_id"]
a.post(request=request, pk=post_id)
return HttpResponseRedirect("..?success=true")
response = a.post(request=request, pk=post_id)

if response.status_code == 200:
return HttpResponseRedirect("..?success=true")

else:
getForm = postGetForm()
postForm = postPostForm()
return render(request, "forms.html", {"getForm":getForm, "postForm":postForm, "action_fail":True})

0 comments on commit 78d0209

Please sign in to comment.