Esempio n. 1
0
def email_selection(request):
    """emails the selected items."""

    if dsh_django_request.deny_it(request):
        return please_log_in()
    
    response = page_header('email the selected items')

    if not 'email_addresses' in request.POST or \
       not request.POST['email_addresses']:
        response += dsh_utils.red_error_break_msg('no email address given.')
        response += page_footer()
        return HttpResponse(response)    

    response += dsh_utils.black_break_msg_debug(
        'emails: ' + repr(request.POST['email_addresses']), 117)

    if ('comments' in request.POST) and request.POST['comments']:
        comments = request.POST['comments']
    else:
        comments = ''        

    attachVoice = False
    if 'attach_voice' in request.POST:
        response += dsh_utils.black_break_msg_debug(
            'attach: ' + repr(request.POST['attach_voice']), 117)
        if request.POST['attach_voice']:
            attachVoice = True
    else:
        response += dsh_utils.black_break_msg_debug('attach: not found.', 117)


    fields = {'dsh_url': False, 'lan_url': False, 'vpn_url': False}
    for varStr,varVal in fields.iteritems():
        if varStr in request.POST and request.POST[varStr]:
            fields[varStr] = True
    

    returnMsg,emailAddrs = dsh_django_utils.check_valid_email_addresses(
        request.POST['email_addresses'])
    if not emailAddrs:
        response += returnMsg
        response += page_footer()
        return HttpResponse(response)
    else:
        response += dsh_utils.black_break_msg_debug(
            'email addresses are: ' + repr(emailAddrs), 117)

    response += dsh_selection.email_selections(emailAddrs, attachVoice,
                                               comments, urlFields=fields)

    response += page_footer()
    return HttpResponse(response)    
def email_selections(emailAddrs, attach, comments='', urlFields=None):
    """called by views.email_selection()."""

    returnMsg = ''


    #
    # display the email addresses.
    #
    returnMsg = dsh_utils.black_break_msg('<i><u>email(s) sent to:</u></i>\n')
    for email in emailAddrs:
        returnMsg += dsh_utils.black_break_msg(email)
    returnMsg += '<BR>\n'


    #
    # deal with comments: display them, include them.
    #
    emailText = ''
    if comments:
        returnMsg += '<i><u>comments:</u></i><br>\n'
        returnMsg += comments
        returnMsg += '<br><br>'
        emailText = '[comments:]\n\n%s\n' % (comments,)
        emailText += '\n---------------\n\n'


    #
    # now loop through all the selected items.
    #
    returnMsg += dsh_utils.black_break_msg('<i><u>items sent:</u></i><br>\n')
    
    selectedItems = dv2.db.models.Item.objects.filter(u17=True)
    if not selectedItems:
        returnMsg += dsh_utils.red_error_break_msg(
            'no item selected currently.')
        return returnMsg

    #
    # 09/11/14:
    # reverse it so it's in reverse chronological order.
    # for some reason, this didn't work...
    #
    #selectedItems.reverse()
    

    count = 0
    displayText = '<TABLE border=1>\n'
    fileList = []
    attachedFileList = []
    

    #
    # 09/11/14:
    # reverse it so it's in reverse chronological order.
    #
    for item in selectedItems[::-1]:

        emailText += smart_unicode(item.email_text(attach=attach,
                                                   urlFields=urlFields))
        emailText += '\n---------------\n\n'

        
        displayText += '<TR><TD>%s</TD></TR>\n' % \
                       (item.email_text(br=True, attach=attach,
                                        urlFields=urlFields,
                                        allowTags=True),)

        if attach:
            file = item.full_file_path()
            attachedFile = item.attachment_file_name()
        
            if file and attachedFile:
                fileList.append(file)
                attachedFileList.append(attachedFile)
            
    displayText += '</TABLE>\n'
    returnMsg += smart_unicode(displayText)
    
    returnMsg += dsh_utils.black_break_msg_debug('', 118)
    returnMsg += dsh_utils.black_break_msg_debug(repr(fileList), 118)
    returnMsg += dsh_utils.black_break_msg_debug('', 118)
    returnMsg += dsh_utils.black_break_msg_debug(repr(attachedFileList), 118)

    emailText = smart_unicode(emailText)
    emailText = smart_str(emailText)


    #
    # got everything.  ready to actually call gmail_send().
    #
    try:
        success,errorMsg = dsh_utils.gmail_send(
            dsh_config.lookup('GMAIL_SENDER_ADDRESS'),
            dsh_config.lookup('GMAIL_SENDER_PASSWORD'),
            emailAddrs,
            dsh_config.lookup('GMAIL_SENDER_SUBJECT'),
            emailText,
            attachedFiles=fileList,
            attachmentNames=attachedFileList)
        if success:
            message = 'selection emailed to: %s.  comments: %s.' %\
                      (', '.join(emailAddrs), comments)
            dsh_agi.report_event(message, action='EMAI')
            returnMsg += '<br>' + dsh_utils.black_break_msg('Message sent.')
        else:
            dsh_django_utils.error_event(errorMsg, errorLevel='ERR')
            returnMsg += '<br>' + dsh_utils.red_error_break_msg(errorMsg)
    except:
        message = 'dsh_selection.email_selections: gmail_send() failed. ' +\
                  'Unexpected error.  Tell Randy about this page.'
        dsh_django_utils.error_event(message, errorLevel='ERR')
        returnMsg += '<br>' + dsh_utils.red_error_break_msg(message)
        
    return returnMsg
Esempio n. 3
0
def dump_one(obj, define, tarPath):
    """called by dump_selected() below, dumps one django object into a
    dump file.
    obj is an object retrieved from django db.
    each dumped object is a dictionary.  of the form:
    FieldName: [FieldType, FieldValue],
    'define' is the object definition coming from dsh_dump_models.py.
    it's a dictionary, of the form:
    FieldName: [FieldType],
    """

    errorMsg = ''
    ans = '{\n'
    for fieldName,specs in define.iteritems():
        fieldType = specs[0]

        errorMsg += dsh_utils.black_break_msg_debug(
            'loop iteration: ' + fieldName + ' - ' + fieldType, 124)
        
        try:
            value = getattr(obj, fieldName)
        except:
            message = 'dsh_dump.dump_one: exception: ' + fieldName + '.  '
            dsh_django_utils.error_event(message, errorLevel='CRT')
            errorMsg += dsh_utils.red_error_break_msg(message)
            continue

        if fieldType == 'StrType':
            if not value:
                continue
            #
            # escape the double quote character.
            #
            value = value.replace('"', '\\"')
            oneField = "    '%s': ['%s', \"\"\"%s\"\"\"],\n" % \
                       (fieldName, fieldType, value)
            ans += oneField
            continue
            
        if fieldType == 'BoolType':
            oneField = "    '%s': ['%s', %s],\n" % \
                       (fieldName, fieldType, repr(value))
            ans += oneField
            continue

        if fieldType == 'IntType':
            oneField = "    '%s': ['%s', %s],\n" % \
                       (fieldName, fieldType, str(value))
            ans += oneField
            continue

        if fieldType == 'DateType':
            if not value:
                continue
            oneField = "    '%s': ['%s', %s],\n" % \
                       (fieldName, fieldType, repr(value))
            ans += oneField
            continue

        if fieldType == 'FileType':
            if not value:
                continue
            dumpFileName = dsh_agi.abs_url_to_relative(value.url)
            dsh_django_utils.debug_event(
                'dsh_dump.dump_one: url: ' + dumpFileName, 8)
            dsh_django_utils.debug_event(
                'dsh_dump.dump_one: tarPath: ' + tarPath, 8)

            success,tarMsg = add_to_tar(dumpFileName, tarPath)
            errorMsg += tarMsg
            if success:
                errorMsg += dsh_utils.black_break_msg(
                    'tar: ' + tarPath + \
                    '&nbsp;&nbsp;&nbsp;&#171;&nbsp;&nbsp;&nbsp;' + \
                    dumpFileName)
            else:
                errorMsg += dsh_utils.red_error_break_msg(
                    'tar command failed on: ' + dumpFileName)
                continue
            
            oneField = "    '%s': ['%s', \"\"\"%s\"\"\"],\n" % \
                       (fieldName, fieldType, dumpFileName)
            ans += oneField
            continue

        if fieldType == 'RequiredForeignOrgType' or \
               fieldType == 'RequiredForeignPersonType':
            if not value:
                errorMsg += dsh_utils.red_error_break_msg(
                    'missing required foreign org. or person.')
                continue
            foreignDshUid = value.dsh_uid

            oneField = "    '%s': ['%s', \"\"\"%s\"\"\"],\n" % \
                       (fieldName, fieldType, foreignDshUid)
            ans += oneField
            continue

        if fieldType == 'OptionalFollowUpsType' or \
               fieldType == 'OptionalOwnerType':
            #
            # like the above, except it's optional.
            # so if it's blank, we don't freak out.
            #
            if not value:
                continue
            foreignDshUid = value.dsh_uid

            oneField = "    '%s': ['%s', \"\"\"%s\"\"\"],\n" % \
                       (fieldName, fieldType, foreignDshUid)
            ans += oneField
            continue

        if fieldType == 'OptionalKeyWordsType' or \
           fieldType == 'OptionalPersonsType':
            #
            # the intended_audience field can be dealt with like this too.
            #
            if not value:
                continue
            dshUidList = get_dsh_uid_list(value)
            if not dshUidList:
                continue
            oneField = "    '%s': ['%s', %s],\n" % \
                       (fieldName, fieldType, repr(dshUidList))
            ans += oneField
            continue

        errorMsg += dsh_utils.red_error_break_msg(
            'unknown field type: ' + fieldType)
        

    ans += '}'
    return (ans, errorMsg)