Skip to content

Commit

Permalink
Update to use drf-yasg
Browse files Browse the repository at this point in the history
  • Loading branch information
norkans7 committed Oct 23, 2024
1 parent e95567a commit 4b1039c
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 300 deletions.
352 changes: 60 additions & 292 deletions poetry.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ rapidpro-dash = "~1.16.0"
colorama = "^0.4.3"
celery = "^5.1"
django-compressor = "^4.0"
django-debug-toolbar = "3.2.4"
django-debug-toolbar = "^4.4.6"
django-digest = "^1.13"
django-rosetta = "^0.8.1"
django-storages = "^1.13.2"
django-rest-swagger = "^2.2.0"
djangorestframework = "~3.15.2"
dj-database-url = "^0.5.0"
Pillow = "^10.2.0"
Expand All @@ -37,6 +36,7 @@ iso8601 = "^0.1.12"
sorl-thumbnail = "^12.9.0"
stop_words = "^2018.7.23"
boto3 = "^1.27.1"
drf-yasg = "^1.21.8"


[tool.poetry.dev-dependencies]
Expand Down
102 changes: 102 additions & 0 deletions templates/drf-yasg/swagger-ui.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
{% load static %}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>{% block title %}{{ title }}{% endblock %}</title>

{% block extra_head %}
{# -- Add any extra HTML heads tags here - except scripts and styles -- #}
{% endblock %}

{% block favicon %}
<link rel="icon" type="image/x-icon" href="{{ STATIC_URL }}img/favicon.png">
{% endblock %}

{% block main_styles %}
<link rel="stylesheet" type="text/css" href="{% static 'drf-yasg/style.css' %}"/>
<link rel="stylesheet" type="text/css" href="{% static 'drf-yasg/swagger-ui-dist/swagger-ui.css' %}">
<style>
.topbar {
display: none;
}

.scheme-container {
display: none;
}

section.models {
display: none;
}
</style>
{% endblock %}
{% block extra_styles %}
{# -- Add any additional CSS scripts here -- #}
{% endblock %}
</head>

<body class="swagger-body">

{% block extra_body %}
{# -- Add any header/body markup here (rendered BEFORE the swagger-ui/redoc element) -- #}
{% endblock %}

<div id="swagger-ui"></div>

{% block footer %}
{# -- Add any footer markup here (rendered AFTER the swagger-ui/redoc element) -- #}
{% endblock %}

<script id="swagger-settings" type="application/json">{{ swagger_settings | safe }}</script>
<script id="oauth2-config" type="application/json">{{ oauth2_config | safe }}</script>

{% block main_scripts %}
<script src="{% static 'drf-yasg/swagger-ui-dist/swagger-ui-bundle.js' %}"></script>
<script src="{% static 'drf-yasg/swagger-ui-dist/swagger-ui-standalone-preset.js' %}"></script>
<script src="{% static 'drf-yasg/insQ.min.js' %}"></script>
<script src="{% static 'drf-yasg/immutable.min.js' %}"></script>
<script src="{% static 'drf-yasg/swagger-ui-init.js' %}"></script>
{% endblock %}
{% block extra_scripts %}
{# -- Add any additional scripts here -- #}
{% endblock %}

<a id="oauth2-redirect-url" href="{% static 'drf-yasg/swagger-ui-dist/oauth2-redirect.html' %}" class="hidden"></a>

{% if USE_SESSION_AUTH %}
<div id="django-session-auth" class="hidden">
{% block session_auth_button %}
{% csrf_token %}

{% block user_context_message %}
{% if request.user.is_authenticated %}
<div class="hello">
<span class="django-session">Django</span> <span
class="label label-primary">{{ request.user }}</span>
</div>
{% endif %}
{% endblock %}

{% if request.user.is_authenticated %}
<div class='btn authorize'>
<a id="auth" class="header__btn" href="{{ LOGOUT_URL }}?next={{ request.path }}" data-sw-translate>
{% block django_logout_message %}
Django Logout
{% endblock %}
</a>
</div>
{% else %}
<div class='btn authorize'>
<a id="auth" class="header__btn" href="{{ LOGIN_URL }}?next={{ request.path }}" data-sw-translate>
{% block django_login_message %}
Django Login
{% endblock %}
</a>
</div>
{% endif %}
{% endblock %}
</div>
{% endif %}
</body>

</html>
24 changes: 20 additions & 4 deletions ureport/api/urls.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals

from rest_framework_swagger.views import get_swagger_view

from django.urls import re_path
from django.views.generic import RedirectView

from rest_framework import permissions
from drf_yasg.views import get_schema_view
from drf_yasg import openapi

from ureport.api.views import (
DashBlockDetails,
DashBlockList,
Expand All @@ -24,12 +27,25 @@
VideoList,
)

schema_view = get_swagger_view(title="API")

schema_view = get_schema_view(
openapi.Info(
title="U-Report API",
default_version='v1',
description="U-Report API",
x_logo={
"url": "",
"backgroundColor": "#f1f1f1",
"href": "/"
},
),
public=True,
permission_classes=(permissions.AllowAny,),
)

urlpatterns = [
re_path(r"^$", RedirectView.as_view(pattern_name="api.v1.docs", permanent=False), name="api.v1"),
re_path(r"^docs/", schema_view, name="api.v1.docs"),
re_path(r"^docs/", schema_view.with_ui('swagger', cache_timeout=0), name="api.v1.docs"),

re_path(r"^orgs/$", OrgList.as_view(), name="api.v1.org_list"),
re_path(r"^orgs/(?P<pk>[\d]+)/$", OrgDetails.as_view(), name="api.v1.org_details"),
re_path(r"^polls/org/(?P<org>[\d]+)/$", PollList.as_view(), name="api.v1.org_poll_list"),
Expand Down
1 change: 1 addition & 0 deletions ureport/polls/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ def get_valid_polls(cls, org):

@classmethod
def get_main_poll(cls, org):
org = Org.objects.get(pk=1)
cached_value = cache.get(Poll.ORG_MAIN_POLL_ID % org.id, None)
main_poll = None
if cached_value:
Expand Down
2 changes: 2 additions & 0 deletions ureport/public/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ def get_context_data(self, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs)

org = self.request.org
org = Org.objects.get(pk=1)
context["org"] = org


latest_poll = Poll.get_main_poll(org)
context["latest_poll"] = latest_poll
Expand Down
12 changes: 10 additions & 2 deletions ureport/settings_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@
"ureport.stats",
"django_countries",
"rest_framework",
"rest_framework_swagger",
"drf_yasg",
)

# A sample logging configuration. The only tangible logging
Expand Down Expand Up @@ -1009,7 +1009,7 @@
# Debug Toolbar
# -----------------------------------------------------------------------------------

INTERNAL_IPS = ("127.0.0.1",)
INTERNAL_IPS = ("127.0.0.1", "0.0.0.0:8000")

# -----------------------------------------------------------------------------------
# Crontab Settings
Expand Down Expand Up @@ -2058,3 +2058,11 @@
},
},
}

SWAGGER_SETTINGS = {
'SECURITY_DEFINITIONS': {
'basic': {
'type': 'basic'
}
},
}

0 comments on commit 4b1039c

Please sign in to comment.