added method Routing

This commit is contained in:
hongwei1 2019-08-11 11:38:18 +02:00
parent 3c191c7672
commit a271b017bb
11 changed files with 201 additions and 0 deletions

View File

@ -57,6 +57,7 @@ INSTALLED_APPS = [
'metrics',
'config',
'webui',
'methodrouting',
]
MIDDLEWARE = [

View File

@ -36,4 +36,5 @@ urlpatterns = [
url(r'^metrics/', include('metrics.urls')),
url(r'^config/', include('config.urls')),
url(r'^webui/', include('webui.urls')),
url(r'^methodrouting/', include('methodrouting.urls')),
]

View File

@ -64,6 +64,8 @@
<li{% ifequal request.path config_index_url %} class="active" {% endifequal %}><a href="{{ config_index_url }}">Config</a></li>
{% url "webui-index" as webui_props_index_url %}
<li{% ifequal request.path webui_props_index_url %} class="active" {% endifequal %}><a href="{{ webui_props_index_url }}">Webui Props</a></li>
{% url "methodrouting-index" as methodrouting_index_url %}
<li{% ifequal request.path methodrouting_index_url %} class="active" {% endifequal %}><a href="{{ methodrouting_index_url }}">Method Routings</a></li>
{% if API_TESTER_URL %}
<li>
<p class="navbar-btn"><a href="{{ API_TESTER_URL }}" class="btn btn-default">Go to API Tester</a></p>

View File

View File

@ -0,0 +1,11 @@
# -*- coding: utf-8 -*-
"""
App config for config app
"""
from django.apps import AppConfig
class MethodRoutingConfig(AppConfig):
"""Config for methodrouting"""
name = 'methodrouting'

View File

@ -0,0 +1,13 @@
from django import forms
class MethodRoutingForm(forms.Form):
method_routing_body = forms.CharField(
label='Method Routing Body',
widget=forms.Textarea(
attrs={
'class': 'form-control',
}
),
required=False
)

View File

@ -0,0 +1,10 @@
#config pre {
overflow: auto;
word-wrap: normal;
white-space: pre;
}
#config .string { color: green; }
#config .number { color: darkorange; }
#config .boolean { color: blue; }
#config .null { color: magenta; }
#config .key { color: red; }

View File

@ -0,0 +1,25 @@
$(document).ready(function($) {
function syntaxHighlight(json) {
if (typeof json != 'string') {
json = JSON.stringify(json, undefined, 2);
}
json = json.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
var cls = 'number';
if (/^"/.test(match)) {
if (/:$/.test(match)) {
cls = 'key';
} else {
cls = 'string';
}
} else if (/true|false/.test(match)) {
cls = 'boolean';
} else if (/null/.test(match)) {
cls = 'null';
}
return '<span class="' + cls + '">' + match + '</span>';
});
}
$('#config-json').html((syntaxHighlight(ConfigJson)));
});

View File

@ -0,0 +1,49 @@
{% extends 'base.html' %}
{% load static %}
{% block page_title %}{{ block.super }} / Users{% endblock page_title %}
{% block content %}
<div id="methodrouting">
<div id="methodrouting_list">
<h1>Method Routing</h1>
<form method="post">
{% csrf_token %}
{% if form.non_field_errors %}
<div class="alert alert-danger">
{{ form.non_field_errors }}
</div>
{% endif %}
<div class="row">
<div class="col-xs-12 col-sm-4">
{% if form.method_routing_body.errors %}<div class="alert alert-danger">{{ form.method_routing_body.errors }}</div>{% endif %}
<div class="form-group">
{{ form.method_routing_body.label_tag }}
{{ form.method_routing_body }}
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12 hidden-xs">
<div class="form-group">
<button type="submit" class="btn btn-primary btn-green">Submit</button>
</div>
</div>
</div>
</form>
</div>
</div>
{% endblock %}
{% block extrajs %}
<script type="text/javascript" src="{% static 'methodrouting/js/methodrouting.js' %}"></script>
{% endblock extrajs %}
{% block extracss %}
<link href="{% static 'methodrouting/css/methodrouting.css' %}" rel="stylesheet">
{% endblock extracss %}

View File

@ -0,0 +1,14 @@
# -*- coding: utf-8 -*-
"""
URLs for config app
"""
from django.conf.urls import url
from .views import IndexView
urlpatterns = [
url(r'^$',
IndexView.as_view(),
name='methodrouting-index'),
]

View File

@ -0,0 +1,75 @@
# -*- coding: utf-8 -*-
"""
Views of config app
"""
import json
from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import FormView
from obp.api import API, APIError
from .forms import MethodRoutingForm
from django.urls import reverse_lazy
def error_once_only(request, err):
"""
Just add the error once
:param request:
:param err:
:return:
"""
storage = messages.get_messages(request)
if str(err) not in [str(m.message) for m in storage]:
messages.error(request, err)
class IndexView(LoginRequiredMixin, FormView):
"""Index view for config"""
template_name = "methodrouting/index.html"
form_class = MethodRoutingForm
success_url = reverse_lazy('methodrouting-index')
def dispatch(self, request, *args, **kwargs):
self.api = API(request.session.get('obp'))
return super(IndexView, self).dispatch(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs)
return context
def get_form(self, *args, **kwargs):
form = super(IndexView, self).get_form(*args, **kwargs)
# Cannot add api in constructor: super complains about unknown kwarg
fields = form.fields
form.api = self.api
try:
fields['method_routing_body'].initial = ""
except APIError as err:
messages.error(self.request, APIError(Exception("OBP-API server is not running or do not response properly. "
"Please check OBP-API server. Details: " + str(err))))
except Exception as err:
messages.error(self.request, "Unknown Error. Details: "+ str(err))
return form
def form_valid(self, form):
try:
data = form.cleaned_data
urlpath = '/management/method_routings'
payload = json.loads(data["method_routing_body"])
result = self.api.post(urlpath, payload=payload)
except APIError as err:
error_once_only(self.request, APIError(Exception("OBP-API server is not running or do not response properly. "
"Please check OBP-API server. Details: " + str(err))))
return super(IndexView, self).form_invalid(form)
except Exception as err:
error_once_only(self.request, "Unknown Error. Details: "+ str(err))
return super(IndexView, self).form_invalid(form)
if 'code' in result and result['code']>=400:
error_once_only(self.request, result['message'])
return super(IndexView, self).form_valid(form)
msg = 'Submission successfully!'
messages.success(self.request, msg)
return super(IndexView, self).form_valid(form)