major claude changes
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
import logging
|
||||
from flask import request, jsonify
|
||||
from flask_login import login_required
|
||||
from geopy.geocoders import Nominatim
|
||||
from app.customer import customer
|
||||
from app import db
|
||||
from app.common.decorators import login_required as common_login_required
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
from datetime import datetime
|
||||
from app.classes.cards import Card_Card
|
||||
from app.classes.customer import \
|
||||
@@ -16,6 +20,7 @@ from app.classes.service import Service_Parts
|
||||
from app.classes.admin import Admin_Company
|
||||
from app.classes.auto import Auto_Delivery,Auto_Delivery_schema
|
||||
from app.classes.stats_customer import Stats_Customer
|
||||
from app.schemas import CreateCustomerSchema, UpdateCustomerSchema, validate_request
|
||||
import string
|
||||
import random
|
||||
|
||||
@@ -33,8 +38,9 @@ def generate_random_number_string(length):
|
||||
|
||||
|
||||
@customer.route("/all", methods=["GET"])
|
||||
|
||||
@common_login_required
|
||||
def all_customers_around():
|
||||
logger.info("GET /customer/all - Fetching all customers")
|
||||
customer_list = db.session \
|
||||
.query(Customer_Customer) \
|
||||
.all()
|
||||
@@ -43,12 +49,12 @@ def all_customers_around():
|
||||
|
||||
|
||||
@customer.route("/all/<int:page>", methods=["GET"])
|
||||
|
||||
@common_login_required
|
||||
def all_customers(page):
|
||||
"""
|
||||
pagination all customers
|
||||
"""
|
||||
|
||||
logger.info(f"GET /customer/all/{page} - Fetching customers page {page}")
|
||||
per_page_amount = 100
|
||||
if page is None:
|
||||
offset_limit = 0
|
||||
@@ -67,9 +73,11 @@ def all_customers(page):
|
||||
|
||||
|
||||
@customer.route("/<int:customer_id>", methods=["GET"])
|
||||
@common_login_required
|
||||
def get_a_customer(customer_id):
|
||||
"""
|
||||
"""
|
||||
logger.info(f"GET /customer/{customer_id} - Fetching customer")
|
||||
get_customer = (db.session
|
||||
.query(Customer_Customer)
|
||||
.filter(Customer_Customer.id == customer_id)
|
||||
@@ -79,10 +87,12 @@ def get_a_customer(customer_id):
|
||||
|
||||
|
||||
@customer.route("/description/<int:customer_id>", methods=["GET"])
|
||||
@common_login_required
|
||||
def get_a_customer_description(customer_id):
|
||||
"""
|
||||
|
||||
|
||||
"""
|
||||
logger.info(f"GET /customer/description/{customer_id} - Fetching customer description")
|
||||
get_customer_description = (db.session
|
||||
.query(Customer_Description)
|
||||
.filter(Customer_Description.customer_id == customer_id)
|
||||
@@ -112,10 +122,12 @@ def get_a_customer_description(customer_id):
|
||||
|
||||
|
||||
@customer.route("/tank/<int:customer_id>", methods=["GET"])
|
||||
@common_login_required
|
||||
def get_a_customer_tank(customer_id):
|
||||
"""
|
||||
|
||||
|
||||
"""
|
||||
logger.info(f"GET /customer/tank/{customer_id} - Fetching customer tank info")
|
||||
get_customer_tank = (db.session
|
||||
.query(Customer_Tank_Inspection)
|
||||
.filter(Customer_Tank_Inspection.customer_id == customer_id)
|
||||
@@ -142,56 +154,50 @@ def get_a_customer_tank(customer_id):
|
||||
|
||||
|
||||
@customer.route("/create", methods=["POST"])
|
||||
|
||||
@validate_request(CreateCustomerSchema)
|
||||
@common_login_required
|
||||
def create_customer():
|
||||
"""
|
||||
Create a new customer with validated input data.
|
||||
"""
|
||||
logger.info("POST /customer/create - Creating new customer")
|
||||
# Get validated data from request
|
||||
data = request.validated_data
|
||||
|
||||
now = datetime.utcnow()
|
||||
get_company = (db.session
|
||||
.query(Admin_Company)
|
||||
.filter(Admin_Company.id == 1)
|
||||
.first())
|
||||
|
||||
get_company = (db.session
|
||||
.query(Admin_Company)
|
||||
.filter(Admin_Company.id == 1)
|
||||
.first())
|
||||
|
||||
|
||||
random_string = generate_random_number_string(6)
|
||||
|
||||
|
||||
made_account_number = str(get_company.account_prefix) + '-' + str(random_string)
|
||||
see_if_exists = (db.session.query(Customer_Customer).filter(Customer_Customer.account_number == made_account_number).first())
|
||||
|
||||
if see_if_exists is not None:
|
||||
|
||||
random_string = generate_random_number_string(10)
|
||||
|
||||
made_account_number = str(get_company.account_prefix) + '-' + str(random_string)
|
||||
see_if_exists = (db.session.query(Customer_Customer).filter(Customer_Customer.account_number == made_account_number).first())
|
||||
|
||||
if see_if_exists is not None:
|
||||
|
||||
random_string = generate_random_number_string(10)
|
||||
|
||||
made_account_number = str(get_company.account_prefix) + '-' + str(random_string)
|
||||
see_if_exists = (db.session.query(Customer_Customer).filter(Customer_Customer.account_number == made_account_number).first())
|
||||
|
||||
response_customer_last_name = request.json["customer_last_name"]
|
||||
response_customer_first_name = request.json["customer_first_name"]
|
||||
response_customer_town = request.json["customer_town"]
|
||||
response_customer_state = request.json["customer_state"]
|
||||
response_customer_zip = request.json["customer_zip"]
|
||||
response_customer_email = request.json["customer_email"]
|
||||
response_customer_home_type = request.json["customer_home_type"]
|
||||
customer_phone_number = request.json["customer_phone_number"]
|
||||
customer_address = request.json["customer_address"]
|
||||
customer_apt = request.json["customer_apt"]
|
||||
customer_description_msg = request.json["customer_description"]
|
||||
|
||||
# Use validated data instead of direct request.json access
|
||||
response_customer_last_name = data["customer_last_name"]
|
||||
response_customer_first_name = data["customer_first_name"]
|
||||
response_customer_town = data["customer_town"]
|
||||
response_customer_state = data["customer_state"]
|
||||
response_customer_zip = str(data["customer_zip"])
|
||||
response_customer_email = data.get("customer_email")
|
||||
response_customer_home_type = data["customer_home_type"]
|
||||
customer_phone_number = data.get("customer_phone_number")
|
||||
customer_address = data["customer_address"]
|
||||
customer_apt = data.get("customer_apt")
|
||||
customer_description_msg = data.get("customer_description")
|
||||
|
||||
int_customer_home_type = int(response_customer_home_type)
|
||||
response_customer_zip = str(response_customer_zip)
|
||||
response_customer_state = int(response_customer_state)
|
||||
|
||||
|
||||
@@ -204,51 +210,18 @@ def create_customer():
|
||||
else:
|
||||
the_state = 'MA'
|
||||
|
||||
# if response_customer_town == 0:
|
||||
# the_town = 'Auburn'
|
||||
# elif response_customer_town == 1:
|
||||
# the_town = 'Charlton'
|
||||
# elif response_customer_town == 2:
|
||||
# the_town = 'Cherry Valley'
|
||||
# elif response_customer_town == 3:
|
||||
# the_town = 'Dudley'
|
||||
# elif response_customer_town == 4:
|
||||
# the_town = 'Grafton'
|
||||
# elif response_customer_town == 5:
|
||||
# the_town = 'Leicester'
|
||||
# elif response_customer_town == 6:
|
||||
# the_town = 'Millbury'
|
||||
# elif response_customer_town == 7:
|
||||
# the_town = 'N Oxford'
|
||||
# elif response_customer_town == 8:
|
||||
# the_town = 'Oxford'
|
||||
# elif response_customer_town == 9:
|
||||
# the_town = 'Rochdale'
|
||||
# elif response_customer_town == 10:
|
||||
# the_town = 'Shrewsbury'
|
||||
# elif response_customer_town == 11:
|
||||
# the_town = 'Southbridge'
|
||||
# elif response_customer_town == 12:
|
||||
# the_town = 'Spencer'
|
||||
# elif response_customer_town == 13:
|
||||
# the_town = 'Sturbridge'
|
||||
# elif response_customer_town == 14:
|
||||
# the_town = 'Webster'
|
||||
# elif response_customer_town == 15:
|
||||
# the_town = 'Worcester'
|
||||
# else:
|
||||
# the_town = 'NA'
|
||||
|
||||
|
||||
|
||||
geolocator = Nominatim(user_agent="auburnoil")
|
||||
address_string = customer_address + ' ' + response_customer_town+ ' ' + the_state
|
||||
try:
|
||||
location = geolocator.geocode(address_string)
|
||||
user_lat =location.latitude
|
||||
user_lat = location.latitude
|
||||
user_long = location.longitude
|
||||
cor_ad = True
|
||||
except:
|
||||
user_lat =None
|
||||
except Exception:
|
||||
user_lat = None
|
||||
user_long = None
|
||||
cor_ad = False
|
||||
|
||||
@@ -323,70 +296,95 @@ def create_customer():
|
||||
|
||||
@customer.route("/edit/<int:customer_id>", methods=["PUT"])
|
||||
@login_required
|
||||
@validate_request(UpdateCustomerSchema)
|
||||
def edit_customer(customer_id):
|
||||
"""
|
||||
"""
|
||||
logger.info(f"PUT /customer/edit/{customer_id} - Editing customer")
|
||||
get_customer = (db.session
|
||||
.query(Customer_Customer)
|
||||
.filter(Customer_Customer.id == customer_id)
|
||||
.first())
|
||||
|
||||
if not get_customer:
|
||||
return jsonify({"error": "Customer not found"}), 404
|
||||
|
||||
get_customer_description = (db.session
|
||||
.query(Customer_Description)
|
||||
.filter(Customer_Description.customer_id == customer_id)
|
||||
.first())
|
||||
response_customer_last_name = request.json["customer_last_name"]
|
||||
response_customer_first_name = request.json["customer_first_name"]
|
||||
response_customer_town = request.json["customer_town"]
|
||||
response_customer_state = request.json["customer_state"]
|
||||
response_customer_zip = request.json["customer_zip"]
|
||||
response_customer_phone_number = request.json["customer_phone_number"]
|
||||
response_customer_email = request.json["customer_email"]
|
||||
response_customer_home_type = request.json["customer_home_type"]
|
||||
response_customer_address = request.json["customer_address"]
|
||||
response_customer_apt = request.json["customer_apt"]
|
||||
response_customer_description = request.json["customer_description"]
|
||||
|
||||
response_customer_fill_location = request.json["customer_fill_location"]
|
||||
data = request.validated_data
|
||||
response_customer_last_name = data.get("customer_last_name")
|
||||
response_customer_first_name = data.get("customer_first_name")
|
||||
response_customer_town = data.get("customer_town")
|
||||
response_customer_state = data.get("customer_state")
|
||||
response_customer_zip = data.get("customer_zip")
|
||||
response_customer_phone_number = data.get("customer_phone_number")
|
||||
response_customer_email = data.get("customer_email")
|
||||
response_customer_home_type = data.get("customer_home_type")
|
||||
response_customer_address = data.get("customer_address")
|
||||
response_customer_apt = data.get("customer_apt")
|
||||
response_customer_description = data.get("customer_description")
|
||||
response_customer_fill_location = data.get("customer_fill_location")
|
||||
|
||||
|
||||
# Update description if provided
|
||||
if get_customer_description is not None:
|
||||
get_customer_description.description = response_customer_description
|
||||
get_customer_description.fill_location = response_customer_fill_location
|
||||
if response_customer_description is not None:
|
||||
get_customer_description.description = response_customer_description
|
||||
if response_customer_fill_location is not None:
|
||||
get_customer_description.fill_location = response_customer_fill_location
|
||||
db.session.add(get_customer_description)
|
||||
|
||||
if response_customer_state == 0:
|
||||
the_state = 'MA'
|
||||
elif response_customer_state == 1:
|
||||
the_state = 'RI'
|
||||
elif response_customer_state == 1:
|
||||
the_state = 'NH'
|
||||
else:
|
||||
the_state = 'MA'
|
||||
# Only update fields that were provided in the request
|
||||
if response_customer_last_name is not None:
|
||||
get_customer.customer_last_name = response_customer_last_name
|
||||
if response_customer_first_name is not None:
|
||||
get_customer.customer_first_name = response_customer_first_name
|
||||
if response_customer_town is not None:
|
||||
get_customer.customer_town = response_customer_town
|
||||
if response_customer_state is not None:
|
||||
get_customer.customer_state = response_customer_state
|
||||
if response_customer_zip is not None:
|
||||
get_customer.customer_zip = response_customer_zip
|
||||
if response_customer_phone_number is not None:
|
||||
get_customer.customer_phone_number = response_customer_phone_number
|
||||
if response_customer_email is not None:
|
||||
get_customer.customer_email = response_customer_email
|
||||
if response_customer_home_type is not None:
|
||||
get_customer.customer_home_type = response_customer_home_type
|
||||
if response_customer_apt is not None:
|
||||
get_customer.customer_apt = response_customer_apt
|
||||
|
||||
geolocator = Nominatim(user_agent="auburnoil")
|
||||
address_string = response_customer_address + ' ' + response_customer_town+ ' ' + the_state
|
||||
try:
|
||||
location = geolocator.geocode(address_string, timeout=10)
|
||||
get_customer.customer_latitude = location.latitude
|
||||
get_customer.customer_longitude = location.longitude
|
||||
cor_ad = True
|
||||
except:
|
||||
get_customer.customer_latitude = None
|
||||
get_customer.customer_longitude = None
|
||||
cor_ad = False
|
||||
|
||||
# Re-geocode if address fields changed
|
||||
if response_customer_address is not None or response_customer_town is not None or response_customer_state is not None:
|
||||
get_customer.customer_address = response_customer_address if response_customer_address is not None else get_customer.customer_address
|
||||
|
||||
get_customer.customer_address = response_customer_address
|
||||
get_customer.customer_home_type = response_customer_home_type
|
||||
get_customer.customer_phone_number = response_customer_phone_number
|
||||
get_customer.customer_last_name = response_customer_last_name
|
||||
get_customer.customer_first_name = response_customer_first_name
|
||||
get_customer.customer_town = response_customer_town
|
||||
get_customer.customer_state = response_customer_state
|
||||
get_customer.customer_zip = response_customer_zip
|
||||
get_customer.customer_email = response_customer_email
|
||||
get_customer.customer_apt = response_customer_apt
|
||||
get_customer.correct_address = cor_ad
|
||||
state_code = response_customer_state if response_customer_state is not None else get_customer.customer_state
|
||||
if state_code == 0:
|
||||
the_state = 'MA'
|
||||
elif state_code == 1:
|
||||
the_state = 'RI'
|
||||
elif state_code == 2:
|
||||
the_state = 'NH'
|
||||
else:
|
||||
the_state = 'MA'
|
||||
|
||||
town = response_customer_town if response_customer_town is not None else get_customer.customer_town
|
||||
address = get_customer.customer_address
|
||||
|
||||
geolocator = Nominatim(user_agent="auburnoil")
|
||||
address_string = address + ' ' + town + ' ' + the_state
|
||||
try:
|
||||
location = geolocator.geocode(address_string, timeout=10)
|
||||
get_customer.customer_latitude = location.latitude
|
||||
get_customer.customer_longitude = location.longitude
|
||||
get_customer.correct_address = True
|
||||
except Exception:
|
||||
get_customer.customer_latitude = None
|
||||
get_customer.customer_longitude = None
|
||||
get_customer.correct_address = False
|
||||
|
||||
|
||||
db.session.add(get_customer)
|
||||
@@ -407,6 +405,7 @@ def edit_customer(customer_id):
|
||||
def delete_customer(customer_id):
|
||||
"""
|
||||
"""
|
||||
logger.info(f"DELETE /customer/delete/{customer_id} - Deleting customer")
|
||||
get_customer = (db.session
|
||||
.query(Customer_Customer)
|
||||
.filter(Customer_Customer.id == customer_id)
|
||||
@@ -436,6 +435,7 @@ def delete_customer(customer_id):
|
||||
def customer_count():
|
||||
"""
|
||||
"""
|
||||
logger.info("GET /customer/count - Getting customer count")
|
||||
get_customer = (db.session
|
||||
.query(Customer_Customer)
|
||||
.count())
|
||||
@@ -451,6 +451,7 @@ def customer_count():
|
||||
def customer_automatic_status(customer_id):
|
||||
"""
|
||||
"""
|
||||
logger.info(f"GET /customer/automatic/status/{customer_id} - Checking auto delivery status")
|
||||
get_customer = (db.session
|
||||
.query(Customer_Customer)
|
||||
.filter(Customer_Customer.id == customer_id)
|
||||
@@ -475,7 +476,7 @@ def get_all_automatic_deliveries():
|
||||
"""
|
||||
Get all automatic deliveries for the table.
|
||||
"""
|
||||
|
||||
logger.info("GET /customer/automatic/deliveries - Fetching all auto deliveries")
|
||||
try:
|
||||
deliveries = Auto_Delivery.query.all()
|
||||
schema = Auto_Delivery_schema(many=True)
|
||||
@@ -491,6 +492,7 @@ def get_all_automatic_deliveries():
|
||||
def customer_automatic_assignment(customer_id):
|
||||
"""
|
||||
"""
|
||||
logger.info(f"GET /customer/automatic/assign/{customer_id} - Toggling auto delivery assignment")
|
||||
get_customer = (db.session
|
||||
.query(Customer_Customer)
|
||||
.filter(Customer_Customer.id == customer_id)
|
||||
@@ -578,6 +580,7 @@ def edit_customer_tank(customer_id):
|
||||
"""
|
||||
Safely edits or creates tank and description details for a customer.
|
||||
"""
|
||||
logger.info(f"PUT /customer/edit/tank/{customer_id} - Editing customer tank info")
|
||||
get_customer = db.session.query(Customer_Customer).filter(Customer_Customer.id == customer_id).one_or_none()
|
||||
if not get_customer:
|
||||
return jsonify({"ok": False, "error": "Customer not found"}), 404
|
||||
|
||||
Reference in New Issue
Block a user