97 lines
4.1 KiB
Python
97 lines
4.1 KiB
Python
"""
|
|
Project: PARTS Website
|
|
Author: Edward Middleton-Smith
|
|
Precision And Research Technology Systems Limited
|
|
|
|
Technology: App Routing
|
|
Feature: Dog - Location Routes
|
|
|
|
Description:
|
|
Contact Page Controller.
|
|
"""
|
|
|
|
# IMPORTS
|
|
# internal
|
|
from business_objects.api import API
|
|
from business_objects.dog.location import Location
|
|
from datastores.datastore_dog import DataStore_Dog
|
|
from forms.dog.location import Filters_Location
|
|
from helpers.helper_app import Helper_App
|
|
from models.model_view_dog_location import Model_View_Dog_Location
|
|
from models.model_view_home import Model_View_Home
|
|
import 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 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_location = Blueprint('routes_dog_location', __name__)
|
|
|
|
|
|
@routes_dog_location.route(Model_View_Dog_Location.HASH_PAGE_DOG_LOCATIONS, methods=['GET'])
|
|
def locations():
|
|
Helper_App.console_log('locations')
|
|
Helper_App.console_log(f'request_args: {request.args}')
|
|
try:
|
|
form_filters = Filters_Location.from_json(request.args)
|
|
except Exception as e:
|
|
Helper_App.console_log(f'Error: {e}')
|
|
form_filters = Filters_Location()
|
|
Helper_App.console_log(f'form_filters={form_filters}')
|
|
model = Model_View_Dog_Location(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/_locations.html', model = model)
|
|
|
|
@routes_dog_location.route(Model_View_Dog_Location.HASH_SAVE_DOG_LOCATION, methods=['POST'])
|
|
def save_location():
|
|
data = Helper_App.get_request_data(request)
|
|
try:
|
|
form_filters = Filters_Location.from_json(data[Model_View_Dog_Location.FLAG_FORM_FILTERS])
|
|
if not form_filters.validate_on_submit():
|
|
return jsonify({
|
|
Model_View_Dog_Location.FLAG_STATUS: Model_View_Dog_Location.FLAG_FAILURE,
|
|
Model_View_Dog_Location.FLAG_MESSAGE: f'Filters form invalid.\n{form_filters.errors}'
|
|
})
|
|
model_return = Model_View_Dog_Location(form_filters_old=form_filters)
|
|
if not model_return.is_user_logged_in:
|
|
raise Exception('User not logged in')
|
|
|
|
locations = data[Model_View_Dog_Location.FLAG_LOCATION]
|
|
if len(locations) == 0:
|
|
return jsonify({
|
|
Model_View_Dog_Location.FLAG_STATUS: Model_View_Dog_Location.FLAG_FAILURE,
|
|
Model_View_Dog_Location.FLAG_MESSAGE: f'No locations.'
|
|
})
|
|
objs_location = []
|
|
for location in locations:
|
|
objs_location.append(Location.from_json(location))
|
|
Helper_App.console_log(f'objs_location={objs_location}')
|
|
errors = DataStore_Dog.save_locations(data.get('comment', 'No comment'), objs_location)
|
|
|
|
if (len(errors) > 0):
|
|
return jsonify({
|
|
Model_View_Dog_Location.FLAG_STATUS: Model_View_Dog_Location.FLAG_FAILURE,
|
|
Model_View_Dog_Location.FLAG_MESSAGE: f'Error saving locations.\n{model_return.convert_list_objects_to_json(errors)}'
|
|
})
|
|
return jsonify({
|
|
Model_View_Dog_Location.FLAG_STATUS: Model_View_Dog_Location.FLAG_SUCCESS,
|
|
Model_View_Dog_Location.FLAG_DATA: Model_View_Dog_Location.convert_list_objects_to_json(model_return.locations)
|
|
})
|
|
except Exception as e:
|
|
return jsonify({
|
|
Model_View_Dog_Location.FLAG_STATUS: Model_View_Dog_Location.FLAG_FAILURE,
|
|
Model_View_Dog_Location.FLAG_MESSAGE: f'Bad data received by controller.\n{e}'
|
|
}) |