Beispiel #1
0
def activities():
    from TalentUsers import domain_from_id
    from TalentActivityAPI import TalentActivityAPI

    domain = domain_from_id(auth.user.domainId)
    api = TalentActivityAPI()
    user_ids = user_ids_in_domain(auth.user.domainId)
    return_activities = api.get(user_ids=user_ids, limit=25)
    return dict(domain=domain, activities=return_activities)
Beispiel #2
0
def resume():
    email_campaign = db.email_campaign(request.vars.id)
    if auth.user.id != email_campaign.userId: return dict(success=0)
    scheduler_task_ids = email_campaign.schedulerTaskIds
    db(db.scheduler_task.id.belongs(scheduler_task_ids)).update(enabled='T')

    # Add activity
    activity_api = TalentActivityAPI()
    activity_api.create(auth.user.id,
                        activity_api.CAMPAIGN_RESUME,
                        source_table='email_campaign',
                        source_id=email_campaign.id,
                        params=dict(id=email_campaign.id,
                                    name=email_campaign.name))

    return dict(success=1)
Beispiel #3
0
def delete():
    campaign_ids = request.vars.ids
    if not verify_campaigns_owner(campaign_ids, auth.user.domainId):
        return dict()
    db(db.email_campaign.id.belongs(campaign_ids)).update(isHidden=1)

    # Add activity
    activity_api = TalentActivityAPI()
    email_campaigns = db(db.email_campaign.id.belongs(campaign_ids)).select(
        db.email_campaign.id, db.email_campaign.name)
    for campaign_id in campaign_ids:
        campaign_name = email_campaigns.find(
            lambda row: row.id == campaign_id).first().name
        activity_api.create(auth.user.id,
                            activity_api.CAMPAIGN_DELETE,
                            source_table='email_campaign',
                            source_id=campaign_id,
                            params=dict(id=campaign_id, name=campaign_name))

    return dict()
Beispiel #4
0
def builder():
    from TalentEmailMarketing import schedule_email_campaign_sends
    # if not request.vars.sendTime: request.vars.sendTime = request.now.strftime("%Y-%m-%d %H:%M:%S")
    form = SQLFORM(db.email_campaign)

    if request.post_vars:
        # Set the campaign's user ID
        request.vars['userId'] = auth.user.id

        # Set the campaign's custom parameter JSON, if any
        if request.vars['customSrc']:
            import simplejson

            request.vars['customUrlParamsJson'] = simplejson.dumps(
                dict(src=request.vars.customSrc))

        # If there is an HTML email but not plaintext, automatically set plaintext
        if not request.vars.emailBodyText and request.vars.emailBodyHtml:
            request.vars['emailBodyText'] = _html_to_text(
                request.vars.emailBodyHtml)

        # get html text from saved template table,
        # @TODO reply on user_email_template to get email content from, and connect it to email_campaign, no need for duplicate data
        if not request.post_vars.SelectedTemplateId:
            request.post_vars['emailBodyHtml'] = request.vars.emailBodyHtml
            request.post_vars['emailBodyText'] = _html_to_text(
                request.vars.emailBodyHtml)
        else:
            html_email = db(db.user_email_template.id ==
                            request.post_vars.SelectedTemplateId).select(
                                db.user_email_template.emailBodyHtml,
                                db.user_email_template.emailBodyText)
            request.post_vars['emailBodyHtml'] = html_email[0].emailBodyHtml
            request.post_vars['emailBodyText'] = html_email[
                0].emailBodyText or _html_to_text(request.vars.emailBodyHtml)

        current.email_campaign_validation_error = ''
        if form.process(session=None,
                        formname=None,
                        onvalidation=lambda (frm): email_campaign_validation(
                            frm, request.vars, auth.user.domainId)).accepted:
            # Add activity
            activity_api = TalentActivityAPI()
            activity_api.create(auth.user.id,
                                activity_api.CAMPAIGN_CREATE,
                                source_table='email_campaign',
                                source_id=form.vars.id,
                                params=dict(id=form.vars.id,
                                            name=form.vars.name))

            # Make email_campaign_smart_list records
            campaign = db.email_campaign(form.vars.id)
            TalentSmartListAPI.create_email_campaign_smart_lists(
                request.vars.smart_list_ids, campaign.id, auth.user_id)

            # Schedule the sending of emails
            schedule_email_campaign_sends(campaign, auth.user)
            session.flash = "Campaign %s successfully started" % form.vars.name
            redirect(URL('campaign', 'index'))
            return
        elif form.errors:
            logger.error("Received error saving email campaign form: %s",
                         form.errors)
            if current.email_campaign_validation_error:
                response.flash = current.email_campaign_validation_error
            else:
                response.flash = "%s: %s" % (
                    "Enter a value for",
                    db.email_campaign[form.errors.keys()[0]].label)

    frequencies = db(db.frequency.id > 0).select()

    smart_lists = TalentSmartListAPI.get_in_domain(auth.user.domainId,
                                                   order=True,
                                                   get_candidate_count=False)
    # Filter out smart lists that have 0 recipients
    # smart_lists = filter(lambda row: row.candidateCount > 0, smart_lists)

    send_once_frequency_id = [freq.id for freq in frequencies][0]

    # Templates
    templates = db((db.user_email_template.type == TEMPLATE_EMAIL_MARKETING)
                   & (db.user_email_template.userId == db.user.id)
                   & (db.user.domainId == auth.user.domainId)).select(
                       db.user_email_template.ALL)

    # templates = templates.find(lambda row: row.isImmutable == 1)
    # mails = templates.find(lambda row: not row.isImmutable)

    # Set up folder_id_to_mails_dicts, which is like: [{1: mails}, {2: mails}, ..]
    folders = db(
        db.email_template_folder.domainId == auth.user.domainId).select()
    folder_id_to_mails_dicts = [{folder.id: []} for folder in folders]
    folder_id_to_mails_dicts.append({0: []})  # Uncategorized: No folder ID
    for template in templates:
        for folder_id_to_mails in folder_id_to_mails_dicts:  # Get the dict of folder ID to mails
            folder_id = folder_id_to_mails.keys()[0]
            if folder_id == (template.emailTemplateFolderId
                             or 0):  # 0 is Uncategorized
                folder_id_to_mails[template.emailTemplateFolderId
                                   or 0].append(template)

    # print 'folder_id_to_mails_dicts keys before swapping', [e.keys() for e in folder_id_to_mails_dicts]

    # Make 'Uncategorized' folder last, and 'Templates' folder first if it exists
    uncategorized_index = -1
    folder_called_templates_index = -1
    folder_called_templates = folders.find(
        lambda row: row.name == 'Templates').first()
    for i, folder_id_to_mails in enumerate(folder_id_to_mails_dicts):
        folder_id = folder_id_to_mails.keys()[0]
        if folder_id == 0:
            uncategorized_index = i
        elif folder_called_templates and folder_id == folder_called_templates.id:
            folder_called_templates_index = i
    if uncategorized_index != -1:  # Swap 'Uncategorized' and the last
        last = folder_id_to_mails_dicts[-1]
        folder_id_to_mails_dicts[-1] = folder_id_to_mails_dicts[
            uncategorized_index]
        folder_id_to_mails_dicts[uncategorized_index] = last
    if folder_called_templates_index != -1:  # Swap 'Templates' and the first
        first = folder_id_to_mails_dicts[0]
        folder_id_to_mails_dicts[0] = folder_id_to_mails_dicts[
            folder_called_templates_index]
        folder_id_to_mails_dicts[folder_called_templates_index] = first

    # print 'folder_id_to_mails_dicts keys after swapping', [e.keys() for e in folder_id_to_mails_dicts]

    # Domain-wide default tracking code & from name
    domain = db.domain(auth.user.domainId)
    tracking_code = domain.defaultTrackingCode
    default_from_name = domain.defaultFromName

    # Show/Hide Generate Report button
    domain_settings_dict = get_domain_settings_dict(
        db.domain(auth.user.domainId))
    hidden_fields = get_hidden_fields_from_domain_settings_dict(
        'builder', domain_settings_dict)
    layout_mode = get_hidden_fields_from_domain_settings_dict(
        'layoutMode', domain_settings_dict)

    # If domain is Kaiser Corporate, hide Job Alert smartlists for non-admin users
    if is_kaiser_domain(auth.user.domainId) and not is_current_user_admin(
            user_id=auth.user_id):
        smart_lists = filter(lambda row: 'Job Alerts' not in row.name,
                             smart_lists)

    return dict(user=db.user(auth.user_id),
                smart_lists=smart_lists,
                frequencies=frequencies,
                send_once_frequency_id=send_once_frequency_id,
                templates=templates,
                folders=folders,
                folder_id_to_mails_dicts=folder_id_to_mails_dicts,
                DEFAULT_FIRST_NAME_MERGETAG=DEFAULT_FIRST_NAME_MERGETAG,
                DEFAULT_LAST_NAME_MERGETAG=DEFAULT_LAST_NAME_MERGETAG,
                tracking_code=tracking_code,
                default_from_name=default_from_name,
                hidden_fields=hidden_fields,
                is_current_user_admin=is_current_user_admin(),
                layout_mode=layout_mode)
Beispiel #5
0
def url_redirect():
    if not URL.verify(request, hmac_key=HMAC_KEY):
        raise HTTP(403)
    # Update hitcount
    url_conversion_id = request.args[0]
    url_conversion = db.url_conversion(url_conversion_id)
    try:
        # Increment hit count for email marketing
        new_hit_count = (url_conversion.hitCount or 0) + 1
        url_conversion.update_record(hitCount=new_hit_count,
                                     lastHitTime=request.now)

        row = db(db.email_campaign_send.id ==
                 db.email_campaign_send_url_conversion.emailCampaignSendId)(
                     db.email_campaign_send_url_conversion.urlConversionId ==
                     url_conversion_id).select(cache=(cache.ram, 300)).first()
        email_campaign_send, email_campaign_send_url_conversion = row.email_campaign_send, row.email_campaign_send_url_conversion
        email_campaign = db(db.email_campaign.id == email_campaign_send.
                            emailCampaignId).select().first()
        is_open = email_campaign_send_url_conversion.type == TRACKING_URL_TYPE
        candidate = db(db.candidate.id ==
                       email_campaign_send.candidateId).select().first()
        if candidate:  # If candidate has been deleted, don't make the activity
            # Add activity
            from TalentActivityAPI import TalentActivityAPI

            activity_api = TalentActivityAPI()
            activity_type = activity_api.CAMPAIGN_EMAIL_OPEN if is_open else activity_api.CAMPAIGN_EMAIL_CLICK
            activity_api.create(
                email_campaign.userId,
                activity_type,
                source_table='email_campaign_send',
                source_id=email_campaign_send.id,
                params=dict(candidateId=email_campaign_send.candidateId,
                            campaign_name=email_campaign.name,
                            candidate_name=candidate.name()))
        else:
            logger.info(
                "Tried performing URL redirect for nonexistent candidate: %s. email_campaign_send: %s",
                email_campaign_send.candidateId, email_campaign_send.id)

        # Update email_campaign_blast entry only if it's a new hit
        if new_hit_count == 1:
            email_campaign_blast = db(
                db.email_campaign_blast.sentTime ==
                email_campaign_send.sentTime).select().first()
            if email_campaign_blast:
                if is_open:
                    email_campaign_blast.update_record(
                        opens=email_campaign_blast.opens + 1)
                else:
                    email_campaign_blast.update_record(
                        htmlClicks=email_campaign_blast.htmlClicks + 1)

                if candidate:
                    flush_campaign_send_statistics_cache(
                        db.user(candidate.ownerUserId).domainId)
                flush_campaign_blast_cache(
                    email_campaign_blast.emailCampaignId)
            else:
                logger.error(
                    "URL redirect: No email_campaign_blast found matching email_campaign_send.sentTime %s, campaign_id=%s, so creating"
                    % (email_campaign_send.sentTime, email_campaign.id))
    except Exception:
        logger.exception(
            "Received exception doing url_redirect (url_conversion_id=%s)",
            url_conversion_id)

    response.title = "getTalent.com: Redirecting to %s" % url_conversion.destinationUrl
    if "www." == url_conversion.destinationUrl[0:4]:
        url_conversion.destinationUrl = "http://" + url_conversion.destinationUrl

    if url_conversion.destinationUrl == '#':
        redirect(HOST_NAME + str(URL(a='web', c='dashboard', f='index')))
    else:
        redirect(url_conversion.destinationUrl)
Beispiel #6
0
def _activities_in_layout():
    from TalentActivityAPI import TalentActivityAPI

    activity_api = TalentActivityAPI()
    recent_activities = activity_api.get_recent_readable(user_ids_in_domain(auth.user.domainId), limit=5)
    return dict(recent_activities=recent_activities)