Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fields in table representation #172

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ Finally include the `rest_framework_docs` urls in your `urls.py`:
You can find detailed information about the package's settings at [the docs](http://drfdocs.com/settings/).

REST_FRAMEWORK_DOCS = {
'HIDE_DOCS': True # Default: False
'HIDE_DOCS': True, # Default: False
'DESC_TABLE': True # Default: False
}


Expand Down
14 changes: 10 additions & 4 deletions docs/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,17 @@ You can use hidden to prevent your docs from showing up in different environment
'HIDE_DOCS': os.environ.get('HIDE_DRFDOCS', False)
}

##### DESC_TABLE
You can use Table representation to make your fields list in the documentation clear. So We can change representation by adding single line inside the `REST_FRAMEWORK_DOCS` in `settings.py`.

'DESC_TABLE': os.environ.get('DESCRIBE_TABLE', False)


Then set the value of the environment variable `HIDE_DRFDOCS` for each environment (ie. Use `.env` files)

### List of Settings

| Setting | Type | Options | Default |
|---------|---------|-----------------|---------|
|HIDE_DOCS| Boolean | `True`, `False` | `False` |
| | | | |
| Setting | Type | Options | Default |
|----------|---------|-----------------|---------|
|HIDE_DOCS | Boolean | `True`, `False` | `False` |
|DESC_TABLE| Boolean | `True`, `False` | `False` |
3 changes: 2 additions & 1 deletion rest_framework_docs/api_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ def __get_serializer_fields__(self, serializer):
"type": str(field.__class__.__name__),
"sub_fields": sub_fields,
"required": field.required,
"to_many_relation": to_many_relation
"to_many_relation": to_many_relation,
"max_length": (field.__dict__.get('max_length', '-') if field.__dict__.get('max_length', '-') != None else '-')
Copy link

@soraphis soraphis Jan 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change:

if field.__dict__.get('max_length', '-') != None else '-')

to

if field.__dict__.get('max_length', '-') is not None else '-')

(to fulfill flake8 requirements)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Committed the changes for flake8 requirements

})
# FIXME:
# Show more attibutes of `field`?
Expand Down
3 changes: 2 additions & 1 deletion rest_framework_docs/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ class DRFSettings(object):

def __init__(self):
self.drf_settings = {
"HIDE_DOCS": self.get_setting("HIDE_DOCS") or False
"HIDE_DOCS": self.get_setting("HIDE_DOCS") or False,
"DESC_TABLE": self.get_setting("DESC_TABLE") or False
}

def get_setting(self, name):
Expand Down
43 changes: 43 additions & 0 deletions rest_framework_docs/templates/rest_framework_docs/home.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
<!DOCTYPE html>
<html>
<head>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}

td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}

tr:nth-child(even) {
background-color: #ECF0F1;
}
</style>
</head>
{% extends "rest_framework_docs/docs.html" %}
{% load drfdocs_filters %}

Expand Down Expand Up @@ -65,8 +86,29 @@ <h4 class="panel-title title">
{% endif %}

{% if endpoint.fields %}
{% if desc_table %}
<p class="fields-desc">Fields details:</p>
<table>
<tr>
<th> Field Name </th>
<th> Field Type </th>
<th> Max Length </th>
<th> Is Required </th>
</tr>
{% for field in endpoint.fields %}
<tr>
<td> {{ field.name }} </td>
<td> {{ field.type }} </td>
<td> {{ field.max_length }} </td>
<td> {{ field.required }} </td>
</tr>
{% endfor %}
</table>
{% else %}

<p class="fields-desc">Fields:</p>
{%include "rest_framework_docs/components/fields_list.html" with fields=endpoint.fields %}
{% endif %}
{% elif not endpoint.errors %}
<p>No fields.</p>
{% endif %}
Expand Down Expand Up @@ -99,3 +141,4 @@ <h4 class="modal-title">Live API Endpoints <span class="label label-info">Beta</
</div>

{% endblock %}
</html>
1 change: 1 addition & 0 deletions rest_framework_docs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ def get_context_data(self, **kwargs):

context['query'] = query
context['endpoints'] = endpoints
context['desc_table'] = settings['DESC_TABLE']
return context