from marshmallow import ValidationError import logging from app.api.integrations import KafkaIntegration from app.config import Config from app.models import MembersWebfiles logger = logging.getLogger(__name__) from app.api.integrations import StripeIntegration import datetime import jwt import requests import redis import json from app.notifications.mail_factory import send_email_factory class BaseService: TRANSACTION_TYPE = None JWT_SECRET_KEY = Config.JWT_SECRET_KEY SEND_EMAIL_FROM = Config.SEND_EMAIL_FROM SEND_EMAIL_PASS = Config.SEND_EMAIL_PASS THIS_SITE_URL = Config.THIS_SITE_URL CACHE_SERVER = Config.CACHE_SERVER CACHE_PORT = Config.CACHE_PORT CACHE_PASSWORD = Config.CACHE_PASSWORD CACHE_DEFAULT_EXPIRE = Config.CACHE_DEFAULT_EXPIRE MEDIA_SERVER = Config.MEDIA_SERVER @staticmethod def get_profile_picture_url(profile_uid): file_url = '' if profile_uid is None or profile_uid == '': return file_url selectedFile = MembersWebfiles.get_member_webfiles_by_file_uid(profile_uid) if selectedFile: file_url = ( BaseService.MEDIA_SERVER + "/" + selectedFile.file_group + "/" + str( selectedFile.uid) + "/" + selectedFile.filename).lower() return file_url @staticmethod def addStripeCustomer(customerData): customer_data = { "email": customerData["email"], "name": customerData["name"], } stripe_customer = StripeIntegration.create_customer(customer_data) logger.info(f"Stripe_Customer ===== : {stripe_customer}") return stripe_customer @staticmethod def send_completepass_mail(signup_email, pending_uid, pending_id, firstname, lastname): # html_body = f"""\ # #
# #
#
# |
#
| # Hello {firstname}! # | #
|
# Password Reset Completed # |
#
|
# For any support # Reach Out # support@mermsemr.com # https://www.mermsemr.com/ # |
#
| Hello {firstname}! |
|
Password Reset Completed |
#
# |
#
| # Hello {firstname}! # | #
|
# You received this message for account reset password # |
#
|
# Follow the link: link to reset your password. # |
#
|
# For any support # Reach Out # support@mermsemr.com # https://www.mermsemr.com/ # |
#
| Hello {firstname}! |
|
You received this message for account reset password |
|
Follow the link: link to reset your password. |
You have received this message for your account verification.
Follow the link: link to complete the verification process.
""" send_email_factory( to_email=receiver_email, subject=subject, html_content=html_body ) @classmethod def validate_data(cls, data, schema): """ Validate input data based on the provided schema. """ logger.info(f"Processing {cls.TRANSACTION_TYPE} request") return schema.load(data) @classmethod def async_send_settings_refresh_to_kafka(cls, settings_data, subscription_uid, topic): KafkaIntegration.send_setting_refresh_request(settings_data=settings_data, subscription_uid=subscription_uid, topic=topic) KafkaIntegration.flush() @classmethod def async_send_to_kafka(cls, loan_data, request_id, topic): KafkaIntegration.send_loan_request(loan_data=loan_data, request_id=request_id, topic=topic) KafkaIntegration.flush() @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}") else: res_data={} response_data = { "provision_uid": str(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 @staticmethod def write_cache_data(cacheSection, cacheId, cacheData): try: cacheKey = "MERMS-" + cacheSection + '-' + cacheId # note theh use of - logger.info(f"write_cache_data () key {cacheKey}", exc_info=True) # Define connection parameters and connect r = redis.Redis(host=BaseService.CACHE_SERVER, port=BaseService.CACHE_PORT, password=BaseService.CACHE_PASSWORD, decode_responses=True) if r.exists(cacheKey): r.unlink(cacheKey) # Set a key 'foo' with value 'bar' json_string = json.dumps(cacheData, indent=4) r.set(cacheKey, json_string, ex=BaseService.CACHE_DEFAULT_EXPIRE) # Verify by getting the value value = r.get(cacheKey) print(f"Value of {cacheKey}: {value}") # Output: Value of 'foo': bar response_data = { "session_details": value, # "product_id": product_id, } return response_data except Exception as e: logger.error(f"An error occurred while write_cache_data data: {str(e)}", exc_info=True) return None