179 lines
8.8 KiB
Python
179 lines
8.8 KiB
Python
from flask import jsonify
|
||
|
||
from app.api.enums import KafkaMessage
|
||
from app.api.services import MyProductsService
|
||
from app.utils.logger import logger
|
||
from app.api.services.base_service import BaseService
|
||
from sqlalchemy import func, desc
|
||
from app.extensions import db
|
||
from app.models import MembersProductsSettings, MembersProducts, ProductsColorStyle, ProductsTemplates
|
||
from threading import Thread
|
||
import requests
|
||
|
||
|
||
class WebContentsService(BaseService):
|
||
|
||
@staticmethod
|
||
def get_web_contents_data(provision_uid):
|
||
try:
|
||
web_contents_data = {
|
||
"last_update": "0",
|
||
"site_title": "my-site-title",
|
||
"site_description": "This is the site description in the header",
|
||
"site_logo_text": "MY-SITE-LOGO",
|
||
"site_contact_email": "email@email.com",
|
||
"site_contact_phone": "911 111 1111",
|
||
"site_keywords": "Health, Practioner, myPage, telemedicine, Software, Mobile App, Health Specialty, Startup, Creative, Digital Health",
|
||
"facebook": "myface.facebook.com",
|
||
"twitter": "mytwit.twitter.com",
|
||
"youtube": "myyou.youtube.com",
|
||
"banner_description": "We focus on your better health outcomes ",
|
||
"footer_description": "Any text under the logo on the footer",
|
||
"about_description": "Naira",
|
||
"banner_text": "Banner title for your new website. Adjust as needed.",
|
||
"banner_description": "A brief overview of your banner title. Tailor the text to accurately reflect the interests and demographics of your audience.",
|
||
"about_title": "This is about title section",
|
||
"about_description": "We are a team of dedicated general healthcare practitioners committed to providing compassionate, comprehensive care to patients and families in our community.",
|
||
"about_extra_1": "With years of experience in primary medicine, we focus on building lasting relationships with patients while addressing their complete health and wellness needs. ",
|
||
"about_extra_2": "Our practice welcomes patients of all ages and offers a full range of services from preventive care to chronic disease management. ",
|
||
"contact_title": "Questions? Let's Talk",
|
||
"contact_introduction": "Want to learn more about us, or speak with an expert? Let us know what you are looking for, and we’ll get back to you right away.",
|
||
"internal_template_style": ''
|
||
}
|
||
# web_contents_data[""] = ""
|
||
# Do we have this subsction
|
||
membersSubResult = MembersProducts.get_member_product_by_subscription_uid(provision_uid)
|
||
|
||
if membersSubResult:
|
||
member_sub = {
|
||
'id': membersSubResult.id,
|
||
'subscription_uid': str(membersSubResult.uid),
|
||
'member_id': membersSubResult.member_id,
|
||
'product_id': membersSubResult.product_id,
|
||
'internal_url': membersSubResult.internal_url,
|
||
'external_url': membersSubResult.external_url,
|
||
'dns_group': membersSubResult.dns_group,
|
||
'product_template': membersSubResult.product_template,
|
||
'primary_server': membersSubResult.primary_server,
|
||
'provision_port': membersSubResult.provision_port,
|
||
'custom_template': membersSubResult.custom_template,
|
||
'status': membersSubResult.status,
|
||
'updated': membersSubResult.updated,
|
||
"added": membersSubResult.added,
|
||
}
|
||
|
||
selected_flavor = ''
|
||
logger.info(
|
||
f"membersSubResult.product_template *****{membersSubResult.product_template} provision_uid={provision_uid} ")
|
||
if membersSubResult.product_template:
|
||
selectedTemplate = ProductsTemplates.get_template_by_uid(str(membersSubResult.product_template))
|
||
if selectedTemplate:
|
||
selected_flavor = selectedTemplate.flavor
|
||
|
||
|
||
# BaseService.get_site_imges_data(provision_uid, membersSubResult.primary_server,
|
||
# membersSubResult.provision_port, selected_flavor)
|
||
|
||
if membersSubResult.colorstyle is not None and membersSubResult.colorstyle != '':
|
||
# is this a valid olor scheme
|
||
colorStyle = ProductsColorStyle.get_colorstyle_by_product_id_and_uid(membersSubResult.product_id,
|
||
membersSubResult.colorstyle)
|
||
if colorStyle and colorStyle.color_style != '':
|
||
web_contents_data["internal_template_style"] = colorStyle.color_style
|
||
|
||
# web_contents_data ={}
|
||
settings_data_result = MembersProductsSettings.get_product_settings_by_subscription_uid(provision_uid)
|
||
if settings_data_result:
|
||
for t in settings_data_result:
|
||
if t.settings_key.strip() is web_contents_data:
|
||
web_contents_data[t.settings_key.strip()] = t.setting_value.strip()
|
||
else:
|
||
web_contents_data[t.settings_key.strip()] = t.setting_value.strip()
|
||
|
||
# The template set up
|
||
# web_contents_data["web_images"]=[]
|
||
|
||
cacheId = str(provision_uid).replace("-", "")
|
||
BaseService.write_cache_data("WEB_DATA",cacheId, web_contents_data)
|
||
|
||
return web_contents_data
|
||
|
||
except Exception as e:
|
||
logger.error(f"An error occurred while getting dashboard data: {str(e)}", exc_info=True)
|
||
return jsonify({"message": "Internal Server Error"}), 500
|
||
|
||
@staticmethod
|
||
def get_web_trafic_data(data):
|
||
provision_uid = data.get('provision_uid', '')
|
||
country = data.get('country', '')
|
||
region = data.get('region', '')
|
||
city = data.get('city', '')
|
||
latitude = data.get('latitude', '')
|
||
longitude = data.get('longitude', '')
|
||
|
||
membersSubResult = MembersProducts.get_member_product_by_subscription_uid(provision_uid)
|
||
if not membersSubResult:
|
||
return {
|
||
"message": "Invalid Subscription",
|
||
"error_message": "Error - Please select a valid product to view",
|
||
"data": None,
|
||
"error": f"Subscription with UID {provision_uid} does not exist."
|
||
}, 400
|
||
|
||
member_id = membersSubResult.member_id
|
||
product_id = membersSubResult.product_id
|
||
|
||
try:
|
||
response_data = {
|
||
"provision_uid": provision_uid,
|
||
"member_id": member_id,
|
||
"product_id": product_id,
|
||
"country": country,
|
||
"region": region,
|
||
"city": city,
|
||
"latitude": latitude,
|
||
"longitude": longitude,
|
||
}
|
||
|
||
logger.error(f"Going for Thread ******************** ")
|
||
thread = Thread(target=MyProductsService.async_send_settings_refresh_to_kafka,
|
||
args=(response_data, provision_uid, KafkaMessage.SITE_TRAFFIC_DATA))
|
||
thread.start()
|
||
logger.error(f"After the Thread ******************** ")
|
||
|
||
return response_data
|
||
|
||
except Exception as e:
|
||
logger.error(f"An error occurred while getting dashboard data: {str(e)}", exc_info=True)
|
||
return jsonify({"message": "Internal Server Error"}), 500
|
||
|
||
# @staticmethod
|
||
# def get_site_imges_data(provision_uid, primary_server, provision_port, selected_flavor):
|
||
# destination_server = "http://" + str(primary_server) + ":" + str(provision_port)
|
||
# api_url = destination_server + "/api/props"
|
||
# try:
|
||
# payload = {'provision_uid': provision_uid, 'flavor': selected_flavor}
|
||
# logger.info(f"api_url: {str(api_url)}")
|
||
# logger.info(f"selected_flavor: {str(selected_flavor)}")
|
||
#
|
||
# # Make the GET request
|
||
# res_data = []
|
||
# response = requests.get(api_url, params=payload)
|
||
# if response.status_code == 200:
|
||
# logger.info(f"Response Site Images: {res_data}")
|
||
# # Convert the JSON response to a Python dictionary
|
||
# res_data = response.json()
|
||
# logger.info(f"Response Site Images: {res_data}")
|
||
#
|
||
# response_data = {
|
||
# "provision_uid": provision_uid,
|
||
# "data": res_data,
|
||
# # "product_id": product_id,
|
||
# }
|
||
#
|
||
# return response_data
|
||
#
|
||
# except Exception as e:
|
||
# logger.error(f"An error occurred while get_site_imges_data data: {str(e)}", exc_info=True)
|
||
# return None
|