Skip to content

Commit

Permalink
set url from url_for with config
Browse files Browse the repository at this point in the history
  • Loading branch information
TheoLechemia committed Aug 9, 2023
1 parent 29c288a commit f1f56c6
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 56 deletions.
14 changes: 11 additions & 3 deletions apptax/admin/admin_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def index(self):
return self.render("admin/login.html", form=form)

def render(self, template, **kwargs):
self.extra_js = [url_for("configs.get_config"), url_for(".static", filename="js/login.js")]
self.extra_js = [url_for(".static", filename="js/login.js")]
self._template_args["RETURN_URL"] = get_mdict_item_or_list(request.args, "redirect")
self._template_args["IP_APP"] = get_current_app_id()
return super(LoginView, self).render(template, **kwargs)
Expand Down Expand Up @@ -179,7 +179,11 @@ def on_model_change(self, form, model, is_created):

def render(self, template, **kwargs):
self.extra_js = [
url_for("configs.get_config"),
url_for(
"configs.get_config",
variable_name="URL_GROUP_REGNE",
str_endpoint="taxref.get_regneGroup2Inpn_taxref",
),
url_for(".static", filename="js/regne_group2_inpn.js"),
]

Expand Down Expand Up @@ -508,7 +512,11 @@ def can_delete(self):

def render(self, template, **kwargs):
self.extra_js = [
url_for("configs.get_config"),
url_for(
"configs.get_config",
variable_name="URL_GROUP_REGNE",
str_endpoint="taxref.get_regneGroup2Inpn_taxref",
),
url_for(".static", filename="js/regne_group2_inpn.js"),
]

Expand Down
54 changes: 25 additions & 29 deletions apptax/admin/static/js/login.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,33 @@

$(document).ready(function() {
$("#login-submit").click(function() {
$(document).ready(function () {
$("#login-submit").click(function () {
const login_data = {
login: $("#identifiant").val(),
login: $("#identifiant").val(),
password: $("#password").val(),
id_application: $("#id-app").val()
id_application: $("#id-app").val(),
};

let url = (APPLICATION_ROOT + "/api/auth/login").replace("//", "/");

fetch(url, {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
method: "POST",
body: JSON.stringify(login_data)
}).then(
(response) => {
$('#login-error').hide();
if (!response.ok) {
return Promise.reject(response.json());
}

return response.json()
fetch(URL_LOGIN, {
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
method: "POST",
body: JSON.stringify(login_data),
})
.then((response) => {
$("#login-error").hide();
if (!response.ok) {
return Promise.reject(response.json());
}
)

return response.json();
})
.then((data) => {
location.href = $("#return-url").val()
location.href = $("#return-url").val();
})
.catch(error => {
console.error('There was an error!', error);
$('#login-error').show();
.catch((error) => {
console.error("There was an error!", error);
$("#login-error").show();
});
})
});
});
});
42 changes: 19 additions & 23 deletions apptax/admin/static/js/regne_group2_inpn.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,31 @@

$(document).ready(function() {
$(document).ready(function () {
// Get value of regne and group_inpn
let regne_group2 = {}

let url = (APPLICATION_ROOT + "/api/taxref/regnewithgroupe2").replace("//", "/");

fetch(url)
.then((response) => response.json())
.then((data) => {
regne_group2 = data;

// Populate value of regne
let $dropdown = $("#regne");
$('#regne').empty();
$dropdown.append($("<option />").val("").text("---"));
$.each(regne_group2, function(key, value) {
$dropdown.append($("<option />").val(key).text(key));
let regne_group2 = {};

fetch(URL_GROUP_REGNE)
.then((response) => response.json())
.then((data) => {
regne_group2 = data;

// Populate value of regne
let $dropdown = $("#regne");
$("#regne").empty();
$dropdown.append($("<option />").val("").text("---"));
$.each(regne_group2, function (key, value) {
$dropdown.append($("<option />").val(key).text(key));
});
});

});

$('#regne').on('change', function() {
$("#regne").on("change", function () {
let $dropdown = $("#group2_inpn");

// Clear group2_inpn
$('#group2_inpn').empty();
$("#group2_inpn").empty();
$("#group2_inpn").val("");

// Populate with regne selected value
$.each(regne_group2[$('#regne').val()], function() {
$.each(regne_group2[$("#regne").val()], function () {
$dropdown.append($("<option />").val(this).text(this));
});
});
});
});
1 change: 1 addition & 0 deletions apptax/admin/templates/admin/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{% import 'admin/lib.html' as lib with context %}

{% block body %}
<script> var URL_LOGIN = "{{url_for('auth.login', _external=True)}}" </script>

<h1>Login</h1>
<div id="login-error" class="alert alert-danger" role="alert" style="display: none;">
Expand Down
15 changes: 14 additions & 1 deletion apptax/utils/routesconfig.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
from flask import Blueprint, current_app, Response
from flask import Blueprint, current_app, Response, url_for

adresses = Blueprint("configs", __name__)


@adresses.route("<variable_name>/<str_endpoint>", methods=["GET"])
def get_config(variable_name, str_endpoint):
"""
Route générant la configuration utile au frontend
"""
url = url_for(str_endpoint, _external=True)
js = f"const {variable_name}='{url}'"

resp = Response(response=js, status=200, mimetype="application/javascript")
resp.headers["Access-Control-Allow-Origin"] = "*"
return resp

0 comments on commit f1f56c6

Please sign in to comment.