{% extends "base.html" %}
{% load static %}

{% block css_extra %}
  {{ block.super }}
  <link href="{% static 'rest_framework_swagger/bundles/vendors.bundle.css' %}" rel="stylesheet" type="text/css">
  <link href="{% static 'rest_framework_swagger/bundles/app.bundle.css' %}" rel="stylesheet" type="text/css">
  <style>
    #rest-swagger-ui table th,
    #rest-swagger-ui table td {
      text-align: left ! important;
      width: inherit;
    }
    #rest-swagger-ui .scheme-container,
    #rest-swagger-ui .information-container {
      display: none;
    }
    #api-help {
      margin-top: 20px;
    }
    #api-help .btn-link:hover,
    #api-help .btn-link:focus{
      color: #1ABC9C;
      text-decoration: none;
    }
    #api-help table th {
      font-weight: bold;
    }
    #api-help table tr td:first-child,
    #api-help table tr td:last-child {
      text-align: left ! important;
    }

  </style>
{% endblock %}

{% block bodyclass %}{% endblock %}
{% block page_header %}Contraxsuite API{% endblock %}

{% block project_selection %}{% endblock %}

{% block content %}
  <div id="swagger-header">
    <h5>
      Schema: coreapi-2.0
      <span><a href="{% url "swagger" "app" %}? format=openapi" download="contraxsuite-coreapi.json">JSON</a></span>
    </h5>
    <h5>
      <a href="{% url "swagger-ui" %}">OpenAPI Swagger UI</a>
    </h5>

  </div>

  <div class="pull-right wrapper">
    <span class="btn-group">
      <a class="btn btn-u btn-xs btn-s" href="{% url "swagger" group_by="app" %}">App</a>
      <a class="btn btn-u btn-xs btn-p" href="{% url "swagger" group_by="version" %}">Ver</a>
    </span>
    <a class="btn btn-u btn-xs btn-i" href="{% url "swagger" %}? download=">Download</a>
  </div>
  <div class="swagger-section">
    <div id="message-bar" class="swagger-ui-wrap" data-sw-translate>&nbsp;</div>
    <div id="api-help" class="container">
      <div class="card">
        <div class="card-header" id="headingOne">
          <h5 class="mb-0">
            <button class="btn btn-link" data-toggle="collapse" data-target="#collapseOne" aria-expanded="false" aria-controls="collapseOne">
              API Help
            </button>
          </h5>
        </div>

        <div id="collapseOne" class="collapse hide" aria-labelledby="headingOne" data-parent="#accordion">
          <div class="card-body">
            <div>
              <div>
                <p>To use swagger page for API calls, you have to authenticate first, go to rest-auth > login,
                  fill in password and username, send request and ensure that response has auth_token (key) in reponse.</p>
                <p>And then you will be able to use other API calls without any modifications and injecting auth_token,
                  because auth_token will exist in cookies, so no need to care about auth_token after login</p>
                <p>For API calls f.e.  via "requests" lib:
                  <ul>
                    <li><b>Method #1</b>: add "? auth_token=..." to *query string*, *not to request's data*, they are different things
                      so the right way to do API calls is:
                      <pre>
import json, requests

headers = {'Accept': "application/json", 'Content-Type': "application/json"}
url =  'https://some.site.com/api/v1/project/projects/201/?auth_token=c607f555bf71f0ff37d61622be01a2a280bddb95'
data = json.dumps({"description": "new"})
resp = requests.request("PATCH", url, headers=headers,  data=data)
                      </pre>
                    </li>
                    <li><b>Method #2</b>: send authentication token in request headers:
                      <pre>
import json, requests

headers = {'Accept': "application/json", 'Content-Type': "application/json", 'AUTHORIZATION': "Token c607f555bf71f0ff37d61622be01a2a280bddb95"}
url =  'https://some.site.com/api/v1/project/projects/201/'
data = json.dumps({"description": "new"})
resp = requests.request("PATCH", url, headers=headers,  data=data)
                      </pre>
                    </li>
                    <li><b>Method #3</b>: use cookies to send authentication token:
                      <pre>
import json, requests

headers = {'Accept': "application/json", 'Content-Type': "application/json"}
url =  'https://some.site.com/api/v1/project/projects/201/'
data = json.dumps({"description": "new"})
cookies = {"auth_token": "Token c607f555bf71f0ff37d61622be01a2a280bddb95"}
resp = requests.request("PATCH", url, headers=headers,  data=data, cookies=cookies)
                      </pre>
                    </li>
                <p>Note that there  is no need to add "Token " before actual token for auth_token in query string,
                  but token format *Token ACTUAL_TOKEN_UID* is required for authentication via headers and cookies.</p>
              </div>
              <table class="table table-bordered table-striped">
                <tr>
                  <th>Parameter</th>
                  <th>Type</th>
                  <th>Default</th>
                  <th>Notes</th>
                </tr>
                <tr>
                  <th colspan="4">Pagination</th>
                </tr>
                <tr>
                  <td>enable_pagination</td>
                  <td>bool</td>
                  <td>false</td>
                  <td>You can skip it if disable pagination</td>
                </tr>
                <tr>
                  <td>pagesize</td>
                  <td>int</td>
                  <td></td>
                  <td>You can skip it if enable_pagination=false</td>
                </tr>
                <tr>
                  <td>pagenum</td>
                  <td>int</td>
                  <td></td>
                  <td>Starts with 0.  You can skip it if enable_pagination=false</td>
                </tr>
                <tr>
                  <th colspan="4">Sorting</th>
                </tr>
                <tr>
                  <td>sortdatafield</td>
                  <td>str</td>
                  <td></td>
                  <td>Column name</td>
                </tr>
                <tr>
                  <td>sortdatafield</td>
                  <td>str</td>
                  <td></td>
                  <td>Column name.  Sort by 2 or more columns is not implemented.  Skip sort params to not sort at all.</td>
                </tr>
                <tr>
                  <td>sortorder</td>
                  <td>asc/desc</td>
                  <td></td>
                  <td>Sort order</td>
                </tr>
                <tr>
                  <th colspan="4">Filtering</th>
                </tr>
                <tr>
                  <td>filterscount</td>
                  <td>int</td>
                  <td></td>
                  <td>Number of filters to apply. 
                    For each filter params below you should add filter number starting with 0
                    i.e. if you have two filters, parameters should be filterscount=2,
                    filterdatafield0, filtervalue0, filtercondition0, filteroperator0 for first filter
                    and filterdatafield1, filtervalue1, filtercondition1, filteroperator1 for second filter. 
                  </td>
                </tr>
                <tr>
                  <td>filterdatafield</td>
                  <td>str</td>
                  <td></td>
                  <td>Column Name to apply filter.  One field can have 2 conditions maximum.</td>
                </tr>
                <tr>
                  <td>filtervalue</td>
                  <td>str/int/float</td>
                  <td></td>
                  <td>Value to filter on</td>
                </tr>
                <tr>
                  <td>filteroperator</td>
                  <td>0/1</td>
                  <td>0</td>
                  <td>0 - AND, 1 - OR</td>
                </tr>
                <tr>
                  <td>filtercondition</td>
                  <td>str</td>
                  <td></td>
                  <td>
                    <pre>
                    EQUAL - for all data types
                    EQUAL_CASE_SENSITIVE - for string
                    CONTAINS - for string
                    CONTAINS_CASE_SENSITIVE - for string
                    LESS_THAN - for date, int, float
                    LESS_THAN_OR_EQUAL - for date, int, float
                    GREATER_THAN - for date, int, float
                    GREATER_THAN_OR_EQUAL - for date, int, float
                    STARTS_WITH - for string
                    ENDS_WITH - for string
                    STARTS_WITH_CASE_SENSITIVE - for string
                    ENDS_WITH_CASE_SENSITIVE - for string
                    EMPTY - for strings, x == "", filtervalue doesn't matter
                    NULL - nulls, filtervalue doesn't matter
                    NOT_NULL - not nulls, filtervalue doesn't matter
                    DOES_NOT_CONTAIN - for string
                    DOES_NOT_CONTAIN_CASE_SENSITIVE - for string
                    NOT_EQUAL - for all data types
                    </pre>
                  </td>
                </tr>
                <tr>
                  <th colspan="4">Others</th>
                </tr>
                <tr>
                  <td>total_records</td>
                  <td>bool</td>
                  <td>false</td>
                  <td>Return total records like {data: [...], total_records: N}</td>
                </tr>
                <tr>
                  <td>URL Example</td>
                  <td colspan="3">
                    <pre>
                    https://dev.alpha.contraxsuite.com/api/v1/document/project/582/documents/
                    ? enable_pagination=true
                    &pagesize=10
                    &pagenum=0
                    &sortdatafield=df250888_2ec8_4d8c_a291_5b75185e1b60
                    &sortorder=desc
                    &filterscount=2
                    &filterdatafield0=4dafbc9d-cf81-47d7-8b31-9a4ab292e58f
                    &filtervalue0=1997-09-17
                    &filteroperator0=0
                    &filtercondition0=LESS_THAN_OR_EQUAL
                    &filterdatafield1=4dafbc9d-cf81-47d7-8b31-9a4ab292e58f
                    &filteroperator1=0
                    &filtercondition1=NOT_NULL
                    &total_records=true
                    </pre>
                  </td>
                </tr>
              </table>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div id="rest-swagger-ui"></div>
    {% csrf_token %}
  </div>
{% endblock %}

{% block js %}
  <script>
    window.drsSettings = {{ drs_settings|safe }};
    window.drsSpec = {{ spec|safe }};
  </script>
  <script src="{% static 'rest_framework_swagger/bundles/vendors.bundle.js' %}"></script>
  <script src="{% static 'rest_framework_swagger/bundles/app.bundle.js' %}"></script>
  {{ block.super }}
{% endblock %}
