""" Project: PARTS Website Author: Edward Middleton-Smith Precision And Research Technology Systems Limited Technology: App Routing Feature: Dog - Command Routes Description: Contact Page Controller. """ # IMPORTS # internal from dog_training.business_objects.api import API from dog_training.business_objects.dog.command_category import Command_Category from dog_training.datastores.datastore_dog import DataStore_Dog from dog_training.forms.dog.command_category import Filters_Command_Category from dog_training.helpers.helper_app import Helper_App from dog_training.models.model_view_dog_command_category import Model_View_Dog_Command_Category from dog_training.models.model_view_home import Model_View_Home import dog_training.lib.argument_validation as av # external from flask import Flask, render_template, jsonify, request, render_template_string, send_from_directory, redirect, url_for, session, Blueprint, current_app, flash from flask_mail import Mail, Message from dog_training.extensions import db, oauth, mail from urllib.parse import quote_plus, urlencode from authlib.integrations.flask_client import OAuth from authlib.integrations.base_client import OAuthError from urllib.parse import quote, urlparse, parse_qs import json import base64 import hmac import hashlib import datetime from altcha import ChallengeOptions, create_challenge, verify_solution routes_dog_command_category = Blueprint('routes_dog_command_category', __name__) @routes_dog_command_category.route(Model_View_Dog_Command_Category.HASH_PAGE_DOG_COMMAND_CATEGORIES, methods=['GET']) def command_categories(): Helper_App.console_log('command categories') Helper_App.console_log(f'request_args: {request.args}') try: form_filters = Filters_Command_Category.from_json(request.args) except Exception as e: Helper_App.console_log(f'Error: {e}') form_filters = Filters_Command_Category() Helper_App.console_log(f'form_filters={form_filters}') model = Model_View_Dog_Command_Category(form_filters_old = form_filters) if not model.is_user_logged_in: return redirect(url_for('routes_core_home.home')) Helper_App.console_log(f'form_filters={form_filters}') return render_template('pages/dog/_command_categories.html', model = model) @routes_dog_command_category.route(Model_View_Dog_Command_Category.HASH_SAVE_DOG_COMMAND_CATEGORY, methods=['POST']) def save_command_category(): data = Helper_App.get_request_data(request) try: form_filters = Filters_Command_Category.from_json(data[Model_View_Dog_Command_Category.FLAG_FORM_FILTERS]) if not form_filters.validate_on_submit(): return jsonify({ Model_View_Dog_Command_Category.FLAG_STATUS: Model_View_Dog_Command_Category.FLAG_FAILURE, Model_View_Dog_Command_Category.FLAG_MESSAGE: f'Filters form invalid.\n{form_filters.errors}' }) model_return = Model_View_Dog_Command_Category(form_filters_old=form_filters) if not model_return.is_user_logged_in: raise Exception('User not logged in') command_categories = data[Model_View_Dog_Command_Category.FLAG_COMMAND_CATEGORY] if len(command_categories) == 0: return jsonify({ Model_View_Dog_Command_Category.FLAG_STATUS: Model_View_Dog_Command_Category.FLAG_FAILURE, Model_View_Dog_Command_Category.FLAG_MESSAGE: f'No command categories.' }) objs_command_category = [] for command_category in command_categories: objs_command_category.append(Command_Category.from_json(command_category)) Helper_App.console_log(f'objs_command_category={objs_command_category}') errors = DataStore_Dog.save_command_categories(data.get('comment', 'No comment'), objs_command_category) if (len(errors) > 0): return jsonify({ Model_View_Dog_Command_Category.FLAG_STATUS: Model_View_Dog_Command_Category.FLAG_FAILURE, Model_View_Dog_Command_Category.FLAG_MESSAGE: f'Error saving command categories.\n{model_return.convert_list_objects_to_json(errors)}' }) return jsonify({ Model_View_Dog_Command_Category.FLAG_STATUS: Model_View_Dog_Command_Category.FLAG_SUCCESS, Model_View_Dog_Command_Category.FLAG_DATA: Model_View_Dog_Command_Category.convert_list_objects_to_json(model_return.command_categories) }) except Exception as e: return jsonify({ Model_View_Dog_Command_Category.FLAG_STATUS: Model_View_Dog_Command_Category.FLAG_FAILURE, Model_View_Dog_Command_Category.FLAG_MESSAGE: f'Bad data received by controller.\n{e}' })