Skip to content

Commit

Permalink
app.js is now application.py and form filling on app.js seems fixed b…
Browse files Browse the repository at this point in the history
…y now.

Signed-off-by: Ignacio de Miguel Díaz <[email protected]>
  • Loading branch information
imigueldiaz committed Sep 3, 2023
1 parent f063627 commit 10d0d11
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 46 deletions.
4 changes: 2 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Create a .env file with your CSRF secret key, you can generate it with any passwords application
SECRET_KEY=your-secret-key
FLASK_APP=src/app/app.py
FLASK_DEBUG=1
FLASK_APP=src/app/appplication.py
FLASK_DEBUG=0
PYTHONPATH=./src
5 changes: 5 additions & 0 deletions .idea/astro-shoots.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/jsLibraryMappings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions src/app/app.py → src/app/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
SECRET_KEY = config["SECRET_KEY"]
STATIC_URL_PATH = config["STATIC_URL_PATH"]
DEBUG = config["DEBUG"]
pass


def format_float(value, format_spec=".2f"):
Expand Down Expand Up @@ -52,9 +51,6 @@ def create_app():
app.config["SECRET_KEY"] = SECRET_KEY
app.config["DEBUG"] = DEBUG

app.dsoDict = {}
app.jsonObjectList = []

init_logging(app)
init_limiter(app)
init_talisman(app)
Expand Down
1 change: 1 addition & 0 deletions src/app/routes/search_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def search_objects():
"text": f"<strong>{obj['name']}</strong> - <em>{common_name}</em> <small>({object_type})</small>",
"value": obj["name"],
"type": object_type,
"object_id": obj["name"],
}
)

Expand Down
34 changes: 5 additions & 29 deletions src/app/static/scripts/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,6 @@ function fillFormFieldsFromLocalStorage() {
*/
function initializeSelect2(selector, route, minInputLength, grouping = null) {
const selectElement = $(selector);

selectElement.select2({
minimumInputLength: minInputLength,
ajax: {
Expand All @@ -269,36 +268,17 @@ function initializeSelect2(selector, route, minInputLength, grouping = null) {
if (!groupedData[obj[grouping]]) {
groupedData[obj[grouping]] = [];
}
groupedData[obj[grouping]].push({
id: obj.value,
text: obj.text
});
groupedData[obj[grouping]].push({id: obj.value, text: obj.text, data: obj});
});

const finalGroupedData = Object.keys(groupedData).map(key => {
return {
text: key,
children: groupedData[key]
};
});

return {results: finalGroupedData};
} else {
return {
results: data.map(function (obj) {
let extraData = Object.entries(obj).reduce(function (acc, [key, value]) {
if (key !== 'value' && key !== 'text') {
acc['data-' + key] = value;
}
return acc;
}, {});
return {
id: obj.value,
text: obj.text,
...extraData
};
})
};
return {results: data.map(obj => ({id: obj.value, text: obj.text, data: obj}))};
}
}
},
Expand All @@ -313,14 +293,10 @@ function initializeSelect2(selector, route, minInputLength, grouping = null) {
return $('<span>').html(data.text);
}
}).on('select2:select', function (e) {
const selectedData = e.params.data;
const selectedValue = selectedData.id;
selectElement.attr('data-selected-value', selectedValue);

// Update related input fields based on the selected value
const selectedData = e.params.data.data;
for (let key in selectedData) {
if (key.startsWith('data-')) {
const relatedInputId = key.substring(5);
if (key !== 'value' && key !== 'text') {
const relatedInputId = key.replace('data-', '');
const relatedElement = $('#' + relatedInputId);
if (relatedElement.length && relatedElement.is('input')) {
relatedElement.val(selectedData[key]);
Expand Down
17 changes: 10 additions & 7 deletions src/app/utils/initialize.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
# initialize.py
import logging
from logging.handlers import RotatingFileHandler

from flask import Flask
from flask_limiter import Limiter
from flask_talisman import Talisman
from flask_wtf.csrf import CSRFProtect


def init_logging(app):
def init_logging(app: Flask):
"""
Initializes logging for the application.
Args:
app (object): The application object.
app (Flask): The Flask application object.
Returns:
None
"""
handler = logging.FileHandler("app.log")
handler.setLevel(logging.DEBUG)
handler = RotatingFileHandler("app.log", maxBytes=10000, backupCount=3)
handler.setLevel(logging.ERROR)
app.logger.addHandler(handler)


Expand All @@ -34,7 +36,7 @@ def init_limiter(app):
return Limiter(app, default_limits=["10000 per day", "2000 per hour"])


def init_talisman(app):
def init_talisman(app: Flask):
"""
Initializes the Talisman middleware for the Flask application.
Expand All @@ -51,7 +53,8 @@ def init_talisman(app):
"'self'",
"'unsafe-inline'",
"https://cdn.jsdelivr.net", # For Bootstrap, Fork-Awesome and Select2
"https://code.jquery.com", # For jQuery
"https://code.jquery.com",
"https://cdnjs.cloudflare.com", # For RxJS
],
"style-src": [
"'self'",
Expand All @@ -63,7 +66,7 @@ def init_talisman(app):
return Talisman(app, content_security_policy=csp)


def init_csrf(app):
def init_csrf(app: Flask):
"""
Initializes Cross-Site Request Forgery (CSRF) protection for the given Flask app.
Expand Down
3 changes: 0 additions & 3 deletions src/app/utils/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ def load_config():
"""
Loads the configuration settings from the "config.ini" file and environment variables.
Parameters:
None
Returns:
None
"""
Expand Down

0 comments on commit 10d0d11

Please sign in to comment.