HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //home/arjun/projects/buyercall_forms/buyercall/buyercall/blueprints/partnership/tasks.py
import boto
from boto.s3.key import Key
from flask import current_app
# Create a context for the database connection.
from buyercall.app import create_app

from buyercall.app import create_celery_app
from sqlalchemy import func, Date, cast
from datetime import date, timedelta

from buyercall.extensions import db

celery = create_celery_app(current_app)


@celery.task
def upload_partnership_logo(folder_name, logo_data, **kwargs):
    try:
        s3 = boto.connect_s3(celery.conf['AMAZON_ACCESS_KEY'], celery.conf['AMAZON_SECRET_KEY'])

        # Get a handle to the S3 bucket
        bucket_name = celery.conf['PARTNERSHIP_BUCKET']
        bucket = s3.get_bucket(bucket_name)
        k = Key(bucket)

        # Read the contents of the file
        file_contents = logo_data.read()

        # Use Boto to upload the file to the S3 bucket
        k.key = '{}/{}'.format(folder_name, logo_data.filename)
        k.set_metadata('Content-Type', logo_data.mimetype)
        print("Uploading data to {} with key: {}".format(bucket_name, k.key))
        k.set_contents_from_string(file_contents)
        k.set_acl('public-read')
    except Exception as e:
        print(e.__doc__)
        print(e.message)


@celery.task(soft_time_limit=600)
def daily_partnership_account_counts():
    """
    Count partnership account daily lead count for contacts, phone calls, message and form leads
    and update the db with values to keep track of lead count per account.

    :return: True
    """
    app = current_app or create_app()
    db.app = app
    with app.app_context():
        # Import the leads models
        from buyercall.blueprints.contacts.models import Contact
        from buyercall.blueprints.leads.models import Lead
        from buyercall.blueprints.form_leads.models import FormLead
        from buyercall.blueprints.sms.models import Message
        from buyercall.blueprints.partnership.models import PartnershipAccountCounts
        # Get the contact leads count for each partnership for yesterday
        daily_contacts = Contact.query.filter(cast(Contact.created_on, Date) == date.today() - timedelta(days=1))\
            .with_entities(Contact.partnership_account_id, func.count(Contact.id).label('count'))\
            .group_by(Contact.partnership_account_id).all()
        # Get the call leads count for each partnership
        daily_calls = Lead.query.filter(cast(Lead.created_on, Date) == date.today() - timedelta(days=1))\
            .with_entities(Lead.partnership_account_id, func.count(Lead.id).label('count'))\
            .group_by(Lead.partnership_account_id).all()
        # Get the form leads count for each partnership
        daily_form_leads = FormLead.query.filter(cast(FormLead.created_on, Date) == date.today() - timedelta(days=1))\
            .with_entities(FormLead.partnership_account_id, func.count(FormLead.id).label('count'))\
            .group_by(FormLead.partnership_account_id).all()
        # Get the message leads count for each partnership
        daily_messages = Message.query.filter(cast(Message.created_on, Date) == date.today() - timedelta(days=1))\
            .with_entities(Message.partnership_account_id, func.count(Message.id).label('count')) \
            .group_by(Message.partnership_account_id).all()

        # Set dict for each lead count group
        daily_contacts_dict = {}
        daily_calls_dict = {}
        daily_form_leads_dict = {}
        daily_messages_dict = {}
        # Create list of partnership accounts that had lead types yesterday
        partnership_account_list = []

        for dc in daily_contacts:
            # check if partnership account is in list and if not add it to list
            if dc.partnership_account_id not in partnership_account_list:
                partnership_account_list.append(dc.partnership_account_id)
            # Add contacts count to contacts dict
            daily_contacts_dict[dc.partnership_account_id] = dc.count

        for dcls in daily_calls:
            # check if partnership account is in list and if not add it to list
            if dcls.partnership_account_id not in partnership_account_list:
                partnership_account_list.append(dcls.partnership_account_id)
            # Add calls count to calls dict
            daily_calls_dict[dcls.partnership_account_id] = dcls.count

        for dfl in daily_form_leads:
            # check if partnership account is in list and if not add it to list
            if dfl.partnership_account_id not in partnership_account_list:
                partnership_account_list.append(dfl.partnership_account_id)
            # Add form lead count to form lead dict
            daily_form_leads_dict[dfl.partnership_account_id] = dfl.count

        for dm in daily_messages:
            # check if partnership account is in list and if not add it to list
            if dm.partnership_account_id not in partnership_account_list:
                partnership_account_list.append(dm.partnership_account_id)
            # Add message count to message dict
            daily_messages_dict[dm.partnership_account_id] = dm.count

        # Query the partnership account table to update with new counts
        partnership_account_counts = PartnershipAccountCounts.query\
            .filter(PartnershipAccountCounts.partnership_account_id.in_(partnership_account_list)).all()

        for pa in partnership_account_counts:
            # Check to see if partnership account is in the contact dict
            if pa.partnership_account_id in daily_contacts_dict:
                # Update the partnership account with any new contact lead count
                pa.contact_count = pa.contact_count + daily_contacts_dict[pa.partnership_account_id]
            # Check to see if partnership account is in the calls dict
            if pa.partnership_account_id in daily_calls_dict:
                # Update the partnership account with any new call lead count
                pa.call_count = pa.call_count + daily_calls_dict[pa.partnership_account_id]
            # Check to see if partnership account is in the form lead dict
            if pa.partnership_account_id in daily_form_leads_dict:
                # Update the partnership account with any new form lead count
                pa.form_lead_count = pa.form_lead_count + daily_form_leads_dict[pa.partnership_account_id]
            # Check to see if partnership account is in the message dict
            if pa.partnership_account_id in daily_messages_dict:
                # Update the partnership account with any new message lead count
                pa.message_count = pa.message_count + daily_messages_dict[pa.partnership_account_id]
        db.session.commit()
    return True