def edit_contact():
    obj_account = account()
    obj_contact = contact()
    obj_tenant = tenant()
    obj_session = app_session()

    if obj_session.check_session() == True:
        obj_account.set_account(session['session_id'])
    else:
        return redirect(link('login'))

    # selecting which tenant to view
    if 'tenant' not in session or session['tenant'] is None or session[
            'tenant'] == '':
        return redirect(link('tenant'))

    if request.method == 'POST':

        obj_account.update_account(session['account']['account_id'],
                                   request.form['lang'])

        return redirect(link('contact'))

    contacts = obj_contact.get_contacts_by_tenant(session['tenant'])
    list_accounts = []
    for contact1 in contacts:
        account_ele = obj_account.get_account(contact1['contact_account'])
        account_ele['contact'] = contact1['contact_contact']
        account_ele['renew'] = contact1['contact_renew']
        account_ele['emergency'] = contact1['contact_emergency']
        list_accounts.append(account_ele)

    context = {
        'resources_path': obj_config.params['resources_path'],
        'title': obj_config.params['app_name'],
        'link_home': link(''),
        'lang_name': lang('Name'),
        'lang_email': lang('Email'),
        'lang_phone': lang('Phone'),
        'lang_normal': lang('Normal'),
        'lang_renewal': lang('Renewal'),
        'lang_emergency': lang('Emergency'),
        'lang_cancel': lang('Cancel'),
        'lang_save': lang('Save'),
        'val_accounts': list_accounts
    }
    context_header = header_template()
    context = {**context, **context_header}

    return render_template('contact.html', context=context)
def select_tenant():
    obj_account = account()
    obj_contact = contact()
    obj_tenant = tenant()
    obj_session = app_session()

    if obj_session.check_session() == True:
        obj_account.set_account(session['session_id'])
    else:
        return redirect(link('login'))

    if request.method == 'POST':
        if request.form['tenant'] != '':
            list_tenants = obj_contact.get_contacts_by_account()
            if len(list_tenants) > 0:
                for tenant1 in list_tenants:
                    if tenant1['contact_tenant'] == request.form['tenant']:
                        session['contact'] = tenant1
                        session['tenant'] = tenant1['contact_tenant']
                        return redirect(link(''))

    # selecting which tenant to view
    list_tenants = obj_contact.get_contacts_by_account()
    if len(list_tenants) == 1:
        session['contact'] = list_tenants[0]
        session['tenant'] = list_tenants[0]['contact_tenant']
        return redirect(link(''))

    context = {
        'resources_path': obj_config.params['resources_path'],
        'title': obj_config.params['app_name'],
        'lang_tenant': lang('Select Tenant'),
        'lang_select': lang('Select'),
        'lang_save': lang('Save'),
        'val_tenants': list_tenants,
        'val_lang': session['lang']
    }
    context_header = header_template()
    context = {**context, **context_header}

    return render_template('tenant.html', context=context)
def sync():
    obj_account = account()
    obj_tenant = tenant()
    obj_contact = contact()
    obj_session = app_session()
    obj_sfdc = sfdc()
    obj_one_garden = one_garden()

    if obj_sfdc.login() == True:
        # for adding sfdc tenant to tenants in db
        tenants = obj_sfdc.list_tenants()
        local_tenants = obj_tenant.list_tenants()
        exist_tenants = []
        for tenant1 in tenants:
            exist_tenants.append(tenant1['Name'])
            if tenant1['Name'] not in local_tenants:
                id = tenant1['attributes']['url'].split('/')[-1]
                obj_tenant.add_tenant(tenant1['Name'], id,
                                      tenant1['main_domain__c'],
                                      tenant1['Account_Name__c'],
                                      tenant1['environment_status__c'])

            # get other domains via one_garden to db
            domains = obj_one_garden.get_domains(tenant1['Name'])
            if domains != False:
                domains.remove(tenant1['main_domain__c'])
                obj_tenant.update_tenant_domains(tenant1['Name'], domains)

        # for deleting tenants in db not in sfdc
        for tenant1 in local_tenants:
            if tenant1 not in exist_tenants:
                obj_tenant.delete_tenant(tenant1)

        # get contract info via sfdc to db
        contracts = obj_sfdc.list_contracts()
        unique_contracts = {}
        for contract1 in contracts:
            if contract1['OneTenant__c'] in unique_contracts:
                if int(
                        datetime.strptime(
                            unique_contracts[contract1['OneTenant__c']]
                            ['contract_end_date__c'],
                            '%Y-%m-%d').timestamp()) < int(
                                datetime.strptime(
                                    contract1['contract_end_date__c'],
                                    '%Y-%m-%d').timestamp()):
                    unique_contracts[contract1['OneTenant__c']] = contract1
            else:
                unique_contracts[contract1['OneTenant__c']] = contract1

        for contract1 in unique_contracts:
            tenant_info = obj_tenant.get_tenant_by_sfdc(contract1)
            if tenant_info != False:
                contract_data = {
                    'end_date':
                    unique_contracts[contract1]['contract_end_date__c'],
                    'price':
                    unique_contracts[contract1]['sub_total_price_sum__c'],
                    'hac': unique_contracts[contract1]['hac_sum__c'],
                    'hsb': unique_contracts[contract1]['hsb_sum__c'],
                    'hdc': unique_contracts[contract1]['hdc_sum__c'],
                    'dlp': unique_contracts[contract1]['dlp_sum__c'],
                    'arc': unique_contracts[contract1]['archive_sum__c'],
                    'hos': unique_contracts[contract1]['hos_sum__c']
                }
                obj_tenant.update_tenant_licenses(tenant_info['tenant_id'],
                                                  contract_data)

        # for adding sfdc contacts to accounts in db
        contacts = obj_sfdc.list_contacts()
        local_contacts = obj_account.list_accounts()
        exist_contacts = []
        for contact1 in contacts:
            exist_contacts.append(contact1['Email'])
            id = contact1['attributes']['url'].split('/')[-1]
            if contact1['Email'] not in local_contacts:
                account_id = obj_account.add_account(id, contact1['Name'],
                                                     contact1['Email'])
                account_data = {'phone': contact1['Phone']}
                obj_account.update_account(account_id, '', account_data)
            else:
                account_info = obj_account.get_account_by_email(
                    contact1['Email'])
                account_data = {'phone': contact1['Phone']}
                obj_account.update_account(account_info['account_id'], '',
                                           account_data)

        # for deleting accounts in db not in sfdc contacts
        for contact1 in local_contacts:
            if contact1 not in exist_contacts:
                obj_account.delete_account(contact1)

        # for adding sfdc connects to contacts in db
        connects = obj_sfdc.list_connects()
        local_contacts = obj_account.list_accounts('sfdc')
        local_connects = obj_contact.list_contacts()
        exist_connects = []
        for connect in connects:
            if connect['Contact__c'] in local_contacts:
                account_info = local_contacts[connect['Contact__c']]
                connect_id = connect['attributes']['url'].split('/')[-1]
                exist_connects.append(connect_id)
                if connect_id in local_connects:
                    tenant1 = obj_tenant.get_tenant_by_sfdc(
                        connect['OneTenant__c'])
                    contact_id = obj_contact.update_contact(
                        connect_id, account_info['account_id'],
                        tenant1['tenant_id'],
                        connect['Distribution_Channel__c'],
                        connect['support_flag__c'], connect['renewal_flag__c'],
                        connect['emergency_flag__c'])
                else:
                    tenant1 = obj_tenant.get_tenant_by_sfdc(
                        connect['OneTenant__c'])
                    contact_id = obj_contact.add_contact(
                        connect_id, account_info['account_id'],
                        tenant1['tenant_id'],
                        connect['Distribution_Channel__c'],
                        connect['support_flag__c'], connect['renewal_flag__c'],
                        connect['emergency_flag__c'])

        # for deleting contacts in db not in sfdc connects
        for connect1 in local_connects:
            if connect1 not in exist_connects:
                obj_contact.delete_contact(connect1)

        return 'sync finished'
def index():
    obj_account = account()
    obj_tenant = tenant()
    obj_contact = contact()
    obj_session = app_session()

    if obj_session.check_session() == True:
        obj_account.set_account(session['session_id'])
    else:
        return redirect(link('login'))

    # selecting which tenant to view
    if 'tenant' not in session or session['tenant'] is None or session[
            'tenant'] == '':
        return redirect(link('tenant'))

    contacts = obj_contact.get_contacts_by_tenant(session['tenant'])
    list_accounts = []
    for contact1 in contacts:
        account_ele = obj_account.get_account(contact1['contact_account'])
        account_ele['contact'] = contact1['contact_contact']
        account_ele['renew'] = contact1['contact_renew']
        account_ele['emergency'] = contact1['contact_emergency']
        list_accounts.append(account_ele)
    tenant_data = obj_tenant.get_tenant(session['tenant'])
    list_domains = [{
        'label': lang('Main'),
        'value': tenant_data['tenant_domain']
    }]
    for domain in tenant_data['tenant_domains']:
        domain_ele = {'label': '', 'value': domain}
        list_domains.append(domain_ele)
    services = {'hac': 0, 'hsb': 0, 'hdc': 0, 'dlp': 0, 'arc': 0, 'hos': 0}
    for service in services:
        if service in tenant_data['tenant_licenses']:
            services[service] = int(tenant_data['tenant_licenses'][service])
    if 'end_date' in tenant_data['tenant_licenses']:
        end_date = tenant_data['tenant_licenses']['end_date']
    else:
        end_date = ''
    if 'price' in tenant_data['tenant_licenses']:
        total = tenant_data['tenant_licenses']['price']
    else:
        total = ''

    context = {
        'resources_path': obj_config.params['resources_path'],
        'title': obj_config.params['app_name'],
        'link_contact': link('contact'),
        'lang_name': lang('Name'),
        'lang_email': lang('Email'),
        'lang_phone': lang('Phone'),
        'lang_normal': lang('Normal'),
        'lang_renewal': lang('Renewal'),
        'lang_emergency': lang('Emergency'),
        'lang_domain': lang('Domain'),
        'lang_service': lang('Service'),
        'lang_licenses': lang('Licenses'),
        'lang_hac': lang('HAC'),
        'lang_hsb': lang('HSB'),
        'lang_hdc': lang('HDC'),
        'lang_dlp': lang('DLP'),
        'lang_arc': lang('ARC'),
        'lang_hos': lang('HOS'),
        'lang_company_name': lang('Company Name'),
        'lang_contract_status': lang('Contract Status'),
        'lang_contract_valid': lang('Contract Valid'),
        'lang_edit': lang('Edit'),
        'lang_total': lang('Total'),
        'val_hac': services['hac'],
        'val_hsb': services['hsb'],
        'val_hdc': services['hdc'],
        'val_dlp': services['dlp'],
        'val_arc': services['arc'],
        'val_hos': services['hos'],
        'val_accounts': list_accounts,
        'val_domains': list_domains,
        'val_name': tenant_data['tenant_name'],
        'val_status': tenant_data['tenant_status'],
        'val_enddate': display_datetime(end_date),
        'show_total': obj_contact.check_contact_view_price(),
        'val_total': display_number(total)
    }
    context_header = header_template()
    context = {**context, **context_header}

    return render_template('index.html', context=context)