def schedule_one_callee(request, personDshUid): """triggerred by clicking the little clock icon on the Person page. for scheduling auto-dialed call for one person.""" if dsh_django_request.deny_it(request): return please_log_in() response = page_header('schedule auto-dialed call for one callee') response += '\n' disabled = dsh_db_config.get('auto_dial_disable') if disabled: response += dsh_utils.black_break_msg( 'auto-dial is currently <font color=red><b>disabled</b></font>.') response += page_footer() return HttpResponse(response) persons = dvoice.db.models.Person.objects.filter(dsh_uid=personDshUid) if not persons or len(persons) > 1: message = 'views.schedule_one_callee: bad DshUid: ' + personDshUid response += dsh_utils.red_error_break_msg(message) dsh_django_utils.error_event(message, errorLevel='CRT') response += page_footer() return HttpResponse(response) person = persons[0] dsh_django_utils.check_auto_timed_calls_for_person(person) response += dsh_utils.black_break_msg('done.') response += dsh_utils.black_break_msg( 'see the <a href=/scheduled>schedule</a>.') response += page_footer() return HttpResponse(response)
def schedule_outgoing_calls(request): if dsh_django_request.deny_it(request): return please_log_in() response = page_header('schedule auto-dialed outgoing calls') #response += dsh_utils.black_break_msg('generating...') dsh_django_utils.check_auto_timed_calls_for_all_persons() response += dsh_utils.black_break_msg('done.') response += dsh_utils.black_break_msg( 'see the <a href=/scheduled>schedule</a>.') response += page_footer() return HttpResponse(response)
def auto_schedule_delete_all(force=False, sessionID=None): """ moved from dsh_django_utils.py called by views.schedule_del_all(). force=True when initiated by views.schedule_delete_all(). """ disableWipe = dsh_db_config.get('reschedule_wipe_disable') if not force and disableWipe: message = 'dsh_common_agi.auto_schedule_delete_all: ' +\ 'note: wiping of existing schedule is disabled.' dsh_agi.report_event(message, sessionID=sessionID) return dsh_utils.black_break_msg(message) spoolDir = dsh_common_config.lookup('ASTERISK_DOT_CALL_DIR') if not dsh_utils.is_valid_dir(spoolDir, silence=True): message = 'dsh_common_agi.auto_schedule_delete_all: ' + \ 'spool directory invalid: ' + spoolDir dsh_agi.report_event(message, reportLevel = 'CRT', sessionID=sessionID) return dsh_utils.red_error_break_msg(message) message = 'dsh_common_agi.auto_schedule_delete_all: ' +\ 'listdir() failed: ' + spoolDir try: listing = os.listdir(spoolDir) listing = dsh_common_db.filter_listdir_with_dbname(listing) except: dsh_agi.report_event(message, reportLevel = 'CRT', sessionID=sessionID) return dsh_utils.red_error_break_msg(message) message = dsh_utils.black_break_msg('deleting...') for one in listing: full = os.path.join(spoolDir, one) if not one: continue if os.path.isdir(full): continue if not dsh_utils.is_valid_file(full): continue if not one.endswith('.call'): continue dsh_utils.cleanup_path( full, 'dsh_common_agi.auto_schedule_delete_all: ') message += dsh_utils.black_break_msg(full) message += dsh_utils.black_break_msg('done.') return message
def auto_dial_disable(request): """10/03/19: moved from view.py""" if dsh_django_request.deny_it(request): return views.please_log_in() response = views.page_header('disable auto-dial') dsh_db_config.set('auto_dial_disable', True) disabled = dsh_db_config.get('auto_dial_disable') if not disabled: message = 'views.auto_dial_disable: unexpected error.' response += dsh_utils.red_error_break_msg(message) dsh_django_utils.error_event(message, errorLevel='CRT') response += views.page_footer() return HttpResponse(response) response += spool_wipe_status_message() dsh_django_utils.auto_schedule_delete_all() response += dsh_utils.black_break_msg( 'now <font color=red><b>disabled</b></font>. ' +\ 'wanna <a href="/autodialenable">re-enable</a>?') response += views.page_footer() dsh_agi.report_event('auto-dial disabled.') return HttpResponse(response)
def clear_current_dial_set(personTable): """ 10/03/22: called by dsh_common_views.clear_current_dial_set(). """ response = '' people = personTable.objects.filter(current_dial=True) count = 0 for person in people: person.current_dial = False try: person.save(noLogging=True) count += 1 except: message = 'dsh_common_selection.clear_current_dial_set: ' +\ 'failed to save person: ' + person.dsh_uid dsh_django_utils.error_event(message, item=person, errorLevel='CRT') response += dsh_utils.red_error_break_msg(message) return response response += dsh_utils.black_break_msg( 'number of people removed from the current dial set: ' +\ str(count) + '.') return response
def dump_one_table(dumpFile, dumpPath, tarPath, whatKind, dumpDefinition, dumpedVarName, dumpAll=False): """called by dump_selected(). whatKind is something like db.models.KeyWord. dumpDefinition is something like the union of: dsh_dump_models.DshObjectDef and dsh_dump_models.KeyWordDef. dumpedVarName is something like 'SelectedKeyWords', the global variable name of the dictionary used in the dumped file. """ if dumpAll: selectedObjs = whatKind.objects.all() else: selectedObjs = whatKind.objects.filter(u17=True) ans = dumpedVarName + ' = [\n' errorMsg = '' for obj in selectedObjs: dumpStr,errStr = dump_one(obj, dumpDefinition, tarPath) ans += dumpStr ans += ',\n' errorMsg += errStr ans += ']\n' dumpSuccess = write_dump_file(dumpFile, dumpPath, ans) if dumpSuccess: result = 'success' else: result = 'failure' errorMsg += dsh_utils.black_break_msg( result + ': ' + str(len(selectedObjs)) + ' objects dumped.') errorMsg += '<br>\n' return (dumpSuccess, errorMsg)
def select_starred(): """called by views.select_starred().""" response = '' starred = dv2.db.models.Item.objects.filter(starred=True) if not starred: response += dsh_utils.red_error_break_msg( 'no starred item.') return response count = 0 for item in starred: if item.u17: continue item.u17 = True try: item.save(noLogging=True) count += 1 except: message = 'dsh_selection.select_starred: failed to save: ' +\ item.dsh_uid dsh_django_utils.error_event(message, errorLevel='CRT') response += dsh_utils.red_error_break_msg(message) return response message = '%s starred item(s) selected.' % (str(count),) response += dsh_utils.black_break_msg(message) dsh_agi.report_event(message) return response
def deselect_all(): messages = '' for tableName,tableInfo in allDbTables.iteritems(): thisTable = tableInfo[0] tableStr = tableInfo[1] selectedObjs = thisTable.objects.filter(u17=True) if not selectedObjs: continue howMany = len(selectedObjs) messages += dsh_utils.black_break_msg( 'de-selecting ' + str(howMany) + ' ' + tableStr + '...') for obj in selectedObjs: obj.u17 = False obj.save(noLogging=True) messages += dsh_utils.black_break_msg('done.') return messages
def spool_wipe_status_message(): """called by reschedule(), auto_dial_enable(), and auto_dial_disable(). displays the current status of spool directory wiping.""" response = '' disableWipe = dsh_db_config.get('reschedule_wipe_disable') if disableWipe: response += dsh_utils.black_break_msg( 'note: wiping of existing schedule is disabled.') return response
def select_current_dial_set(personTable, action='set'): """ 10/03/22. called by dsh_common_views.select_current_dial_set(). add/remove the people in the current dial set to/from the current selection. basically copied from process_selected_people_current_dial(). """ response = '' people = personTable.objects.filter(current_dial=True) count = 0 for person in people: if action == 'set': if person.u17: continue person.u17 = True elif action == 'clear': if not person.u17: continue person.u17 = False else: break try: person.save(noLogging=True) count += 1 except: message = 'dsh_common_selection.' +\ 'select_current_dial_set: ' +\ 'failed to save person: ' + person.dsh_uid dsh_django_utils.error_event(message, item=person, errorLevel='CRT') response += dsh_utils.red_error_break_msg(message) return response if action == 'set': message = 'People added to the current selection: ' elif action == 'clear': message = 'People removed from the current selection: ' else: message = '' if message: message += str(count) + '.' response += dsh_utils.black_break_msg(message) dsh_agi.report_event(message) return response
def auto_dial_status(request): """ 10/03/19: moved from views.py""" response = views.page_header('auto-dial status') response += spool_wipe_status_message() disabled = dsh_db_config.get('auto_dial_disable') if disabled: response += dsh_utils.black_break_msg( 'auto-dial is currently <font color=red><b>disabled</b></font>.') response += dsh_utils.black_break_msg( '<a href="/autodialenable">enable</a>?') else: response += dsh_utils.black_break_msg( 'auto-dial is currently <font color=green><b>enabled</b></font>.') response += dsh_utils.black_break_msg( '<a href="/autodialdisable">disable</a>?') response += views.page_footer() return HttpResponse(response)
def lookup_phone_number(request, personDshUid): if dsh_django_request.deny_it(request): return please_log_in() response = page_header('lookup a phone number') response += '\n' persons = dv2.db.models.Person.objects.filter(dsh_uid=personDshUid) if not persons or len(persons) > 1: message = 'views.lookup_phone_number: bad DshUid: ' + personDshUid response += dsh_utils.red_error_break_msg(message) dsh_django_utils.error_event(message, errorLevel='CRT') response += page_footer() return HttpResponse(response) person = persons[0] phoneNumber = person.phone_number message = '<BR><B><FONT SIZE=3>Phone number: %s</FONT></B>' % \ (phoneNumber,) response += dsh_utils.black_break_msg(message) response += '<BR>' response += dsh_utils.black_break_msg('People who share this number are:') response += '<BR>' response += """ <table cellpadding=0 cellspacing=0 border=0 height=600 width=1000> <tr height="1%%"> <td style="top:0;width:100%%"> <iframe scrolling=auto id=rf src="http:/admin/db/person/?phone_number=%s" frameborder=0 allowtransparency=true style="width:100%%;height:100%%"> </iframe> </td></tr></table> """ % (phoneNumber,) response += page_footer() return HttpResponse(response)
def schedule_one_callee(request, personDshUid): """triggerred by clicking the little clock icon on the Person page. for scheduling auto-dialed call for one person.""" if dsh_django_request.deny_it(request): return please_log_in() response = page_header('schedule auto-dialed call for one callee') response += '\n' disabled = dsh_db_config.get('auto_dial_disable') if disabled: response += dsh_utils.black_break_msg( 'auto-dial is currently <font color=red><b>disabled</b></font>.') #response += page_footer() #return HttpResponse(response) # # 10/03/18: # I'm going to schedule a call anyways. # for sending out doctor replies even though global auto-dial # has been turned off. # response += dsh_utils.black_break_msg( "but we are scheduling a call anyhow.") persons = dv2.db.models.Person.objects.filter(dsh_uid=personDshUid) if not persons or len(persons) > 1: message = 'views.schedule_one_callee: bad DshUid: ' + personDshUid response += dsh_utils.red_error_break_msg(message) dsh_django_utils.error_event(message, errorLevel='CRT') response += page_footer() return HttpResponse(response) person = persons[0] scheduled,respStr = dsh_django_utils.check_auto_timed_calls_for_person( person) response += dsh_utils.black_break_msg(respStr) if scheduled: response += dsh_utils.black_break_msg('scheduled.') else: response += dsh_utils.black_break_msg('not scheduled.') response += dsh_utils.black_break_msg('done.') response += dsh_utils.black_break_msg( 'see the <a href=/scheduled>schedule</a>.') response += page_footer() return HttpResponse(response)
def star(action='add'): """star the selection.""" response = '' selectedItems = dv2.db.models.Item.objects.filter(u17=True) if not selectedItems: response += dsh_utils.red_error_break_msg( 'no item selected currently.') return response count = 0 for item in selectedItems: if action == 'add': if item.starred: continue item.starred = True else: if not item.starred: continue item.starred = False try: item.save(noLogging=True) count += 1 except: message = 'dsh_selection.star: failed to save: ' +\ item.dsh_uid dsh_django_utils.error_event(message, errorLevel='CRT') response += dsh_utils.red_error_break_msg(message) return response if action == 'add': actDone = 'starred' else: actDone = 'de-starred' message = '%s item(s) %s.' % (str(count), actDone) response += dsh_utils.black_break_msg(message) dsh_agi.report_event(message, action='STAR') return response
def red5_clear(request): if dsh_django_request.deny_it(request): return please_log_in() response = page_header('clear files in red5 scratch space') keepList = ['9.flv', '9.flv.meta', 'toystory3.flv', 'toystory3.flv.meta', 'test_randy.flv', 'test_randy.flv.meta', ] red5Dir = dsh_config.lookup('RED5_STREAMS_DIR') red5TmpDir = os.path.join(red5Dir, 'tmp') dsh_utils.empty_dir(red5Dir, keepList) dsh_utils.empty_dir(red5TmpDir) response += dsh_utils.black_break_msg('Cleared.') response += page_footer() return HttpResponse(response)
def reschedule_current_dial_set(personTable, tinyResponse=False, sessionID=None): """ 10/03/22. called by dsh_common_views.reschedule(). and dsh_reschedule.py tinyResponse==True when called by dsh_reschedule.py. it just returns the number of people scheduled in that case. """ response = '' people = personTable.objects.filter(current_dial=True) if not people: response += dsh_utils.black_break_msg( 'No person in the current dial set.') if tinyResponse: return 0 return response dsh_utils.black_break_msg('Processing the following people...') count = 0 for person in people: calleeInfo = '<span style="white-space: nowrap;">' +\ dsh_django_utils.callee_info(person) + '</span>' response += dsh_utils.black_break_msg(calleeInfo) scheduled,strMsg = dsh_django_utils.check_auto_timed_calls_for_person( person, noLogging=True) if scheduled: strMsg = ' scheduled: ' + strMsg else: strMsg = ' not scheduled: ' + strMsg response += dsh_utils.black_break_msg(strMsg) count += 1 countMsg = dsh_utils.black_break_msg( 'number of people in the dial set processed: ' +\ str(count) + '.') response += countMsg dsh_agi.report_event(countMsg, sessionID=sessionID) if tinyResponse: return count return response
def make_dot_call_file(callee, sessionID=''): """ 10/04/10: called by dsh_common_db.attempt_demo_reply_now() and dsh_django2.dial_now(). """ now = datetime.datetime.now() wait = dsh_common_config.lookup2('DEMO_REPLY_DELAY') delta = datetime.timedelta(seconds=wait) callTime = now + delta success = dsh_django_utils.generate_dot_call_file( callee, callTime, None, dialNow=True, sessionID=sessionID) if success: response = dsh_utils.black_break_msg( 'Calling %s @ %s...' % (callee.__unicode__(), callee.phone_number)) else: response = dsh_utils.red_error_break_msg('Something wrong happened.') return (success, response)
def read_back_caller_number(item, fromPhone=False, sessionID=''): """ 10/04/09: called by demo_reply(). read back the phone number of the most recent caller. """ response = '' funcName = 'dsh_common_agi.read_back_caller_number:' caller = item.owner phoneNumber = caller.phone_number if not phoneNumber: message = '%s no phone number: %s.' % (funcName, caller.dsh_uid) response += dsh_utils.red_error_break_msg(message) dsh_agi.report_event(message, reportLevel='ERR', sessionID=sessionID) dsh_utils.give_bad_news(message, logging.error) return (False, response) message = 'Send an immediate reply to %s @ %s? ' %\ (caller.__unicode__(), phoneNumber) message += '<a href="/senddemoreplyconfirmed/%s">Confirm</a>?' %\ (item.dsh_uid,) response += dsh_utils.black_break_msg(message) if fromPhone: promptDir = dsh_common_config.lookup('DSH_PROMPT_DIR') last4 = dsh_common_config.lookup('DSH_PROMPT_DEMO_LAST4') dsh_agi.say_it(os.path.join(promptDir, last4)) howManyDigits = dsh_config.lookup('HIDE_PHONE_DIGITS') lastDigits = phoneNumber[-howManyDigits:] dsh_utils.give_news( 'demo to phone: %s, last 4 digits: %s.' %\ (phoneNumber, lastDigits), logging.info) say_digits(lastDigits) return (True, response)
def reschedule(request, personTable): """ 10/03/19: moved from views.py a combination of delete all and active.""" if dsh_django_request.deny_it(request): return views.please_log_in() response = views.page_header('reschedule auto-dialed calls') dsh_common_agi.auto_schedule_delete_all() response += spool_wipe_status_message() disabled = dsh_db_config.get('auto_dial_disable') if disabled: response += dsh_utils.black_break_msg( 'global auto-dial is currently ' +\ '<font color=red><b>disabled</b></font>.') response += dsh_utils.black_break_msg( 'processing people in the current dial set...') response += dsh_common_selection.reschedule_current_dial_set( personTable) else: response += dsh_utils.black_break_msg( 'global auto-dial is currently ' +\ '<font color=green><b>enabled</b></font>.') response += dsh_utils.black_break_msg( 'processing all people...') dsh_django_utils.check_auto_timed_calls_for_all_persons(noLogging=True) response += dsh_utils.black_break_msg('done.') response += dsh_utils.black_break_msg( 'see the <a href=/scheduled>schedule</a>.') response += views.page_footer() dsh_agi.report_event( 'reschedule triggered by web interface.', action='RESC') 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
def keyword_add_del(dshUid, action='add'): """called by views.keyword_add() and views.keyword_del().""" errorMsg = '' keyWord = dsh_dump.get_foreign_key(dv2.db.models.KeyWord, dshUid) if not keyWord: message = 'dsh_selection.keyword_add_del: bad key word dshUid: ' +\ repr(dshUid) dsh_django_utils.error_event(message, errorLevel='ERR') errorMsg += dsh_utils.red_error_break_msg(message) return errorMsg selectedItems = dv2.db.models.Item.objects.filter(u17=True) if not selectedItems: errorMsg += dsh_utils.red_error_break_msg( 'no item selected currently.') return errorMsg count = 0 for item in selectedItems: kwList = item.key_words.all() if action == 'add': if keyWord in kwList: continue try: item.key_words.add(keyWord) item.save(noLogging=True) count += 1 except: message = 'dsh_selection.key_word_add_del: key word add ' +\ 'failed: ' + repr(dshUid) dsh_django_utils.error_event(message, errorLevel='CRT') errorMsg += dsh_utils.red_error_break_msg(message) return errorMsg elif action == 'del': if not (keyWord in kwList): continue try: item.key_words.remove(keyWord) item.save(noLogging=True) count += 1 except: message = 'dsh_selection.key_word_add_del: key word del ' +\ 'failed: ' + repr(dshUid) dsh_django_utils.error_event(message, errorLevel='CRT') errorMsg += dsh_utils.red_error_break_msg(message) return errorMsg else: return dsh_utils.red_error_break_msg('bad action: ' + action) if action == 'add': actDone = 'added to' logAction = 'KADD' elif action == 'del': actDone = 'removed from' logAction = 'KDEL' else: return dsh_utils.red_error_break_msg('bad action: ' + action) keyStr = keyWord.key_word message = 'key word "%s" %s %s items.' % (keyStr, actDone, str(count)) errorMsg += dsh_utils.black_break_msg(message) dsh_agi.report_event( message, action=logAction, item=keyWord) return errorMsg
def select_keyed_persons(personTable, keyWordTable, dshUid, action='set'): """ 10/03/22: modeled after keyword_select(). could've combined them but I'm lazy. called by dsh_common_views.select_keyed_persons(). add the keyworded persons to the current selection. returns (success, response) """ response = '' keyword = dsh_django_utils.get_foreign_key(keyWordTable, dshUid) if not keyword: message = 'dsh_common_selection.select_keyed_persons: ' +\ 'no such dsh_uid: ' + dshUid response += dsh_utils.red_error_break_msg(message) dsh_django_utils.error_event(message, errorLevel='ERR') return (False, response) response += dsh_utils.black_break_msg( 'The key word is: "' + keyword.key_word + '".') persons = personTable.objects.filter( person_key_words__dsh_uid__exact=dshUid) if not persons: message = 'dsh_common_selection.select_keyed_persons: ' +\ 'no person with this key word.' response += dsh_utils.red_error_break_msg(message) return (False, response) response += dsh_utils.black_break_msg( 'Number of persons with this key word: ' + str(len(persons)) + '.') # # the stuff below is copied from dsh_selection.keyword_select_starred(). # count = 0 for person in persons: if action == 'clear': if not person.u17: continue person.u17 = False else: if person.u17: continue person.u17 = True try: person.save(noLogging=True) count += 1 except: message = 'dsh_common_selection.select_keyed_persons: ' +\ 'failed to save: '+ item.dsh_uid dsh_django_utils.error_event(message, errorLevel='CRT') response += dsh_utils.red_error_break_msg(message) return (False, response) if action == 'clear': message = 'Items removed from the selection: ' else: message = 'Items added to the selection: ' message += str(count) + '.' response += dsh_utils.black_break_msg(message) dsh_agi.report_event(message) return (True, response)
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 + \ ' « ' + \ 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)
def dump_selected(dumpFile, dumpPath, tarPath, dumpAll=False, dumpPersons=False): """called by views.dump(). dumps selected items from each table. if dumpPersons is True, we're dumping all persons and organizations, nothing else.""" totalSuccess = True errorMsg = '' if not dumpAll: propagate_selections() # # key words. # errorMsg += dsh_utils.black_break_msg('dumping KeyWord table...') keyWordDef = dsh_utils.union_dict( dsh_dump_models.DshObjectDef, dsh_dump_models.KeyWordDef) success,errMsg = dump_one_table(dumpFile, dumpPath, tarPath, dvoice.db.models.KeyWord, keyWordDef, 'SelectedKeyWords', dumpAll=dumpAll) totalSuccess = totalSuccess and success errorMsg += errMsg # # organizations. # errorMsg += dsh_utils.black_break_msg('dumping Organization table...') orgDef = dsh_utils.union_dict( dsh_dump_models.DshObjectDef, dsh_dump_models.OrgDef) success,errMsg = dump_one_table(dumpFile, dumpPath, tarPath, dvoice.db.models.Organization, orgDef, 'SelectedOrgs', dumpAll=dumpAll or dumpPersons) totalSuccess = totalSuccess and success errorMsg += errMsg # # persons. # errorMsg += dsh_utils.black_break_msg('dumping Person table...') personDef = dsh_utils.union_dict( dsh_dump_models.DshObjectDef, dsh_dump_models.PersonDef) success,errMsg = dump_one_table(dumpFile, dumpPath, tarPath, dvoice.db.models.Person, personDef, 'SelectedPersons', dumpAll=dumpAll or dumpPersons) totalSuccess = totalSuccess and success errorMsg += errMsg # # items. # errorMsg += dsh_utils.black_break_msg('dumping Item table...') itemDef = dsh_utils.union_dict( dsh_dump_models.DshObjectDef, dsh_dump_models.ItemDef) success,errMsg = dump_one_table(dumpFile, dumpPath, tarPath, dvoice.db.models.Item, itemDef, 'SelectedItems', dumpAll=dumpAll) totalSuccess = totalSuccess and success errorMsg += errMsg # # events. # errorMsg += dsh_utils.black_break_msg('dumping Event table...') eventDef = dsh_utils.union_dict( dsh_dump_models.DshObjectDef, dsh_dump_models.EventDef) success,errMsg = dump_one_table(dumpFile, dumpPath, tarPath, dvoice.db.models.Event, eventDef, 'SelectedEvents', dumpAll=dumpAll) totalSuccess = totalSuccess and success errorMsg += errMsg return (totalSuccess, errorMsg)
def add_person_key_word(personTable, keyWordTable, dshUid, action='set'): """ 10/03/22: copied from dsh_selection.keyword_add_del() called by dsh_common_view.add_person_keyword()""" errorMsg = '' keyWord = dsh_django_utils.get_foreign_key(keyWordTable, dshUid) if not keyWord: message = 'dsh_common_selection.add_person_key_word: ' +\ 'bad key word dshUid: ' + repr(dshUid) dsh_django_utils.error_event(message, errorLevel='ERR') errorMsg += dsh_utils.red_error_break_msg(message) return errorMsg selectedPersons = personTable.objects.filter(u17=True) if not selectedPersons: errorMsg += dsh_utils.red_error_break_msg( 'no person selected currently.') return errorMsg count = 0 for person in selectedPersons: kwList = person.person_key_words.all() if action == 'set': if keyWord in kwList: continue try: person.person_key_words.add(keyWord) person.save(noLogging=True) count += 1 except: message = 'dsh_common_selection.add_person_key_word: ' +\ 'key word add ' +\ 'failed: ' + repr(dshUid) dsh_django_utils.error_event(message, errorLevel='CRT') errorMsg += dsh_utils.red_error_break_msg(message) return errorMsg elif action == 'clear': if not (keyWord in kwList): continue try: person.person_key_words.remove(keyWord) person.save(noLogging=True) count += 1 except: message = 'dsh_common_selection.add_person_key_word: ' +\ 'key word del ' +\ 'failed: ' + repr(dshUid) dsh_django_utils.error_event(message, errorLevel='CRT') errorMsg += dsh_utils.red_error_break_msg(message) return errorMsg else: return dsh_utils.red_error_break_msg('bad action: ' + action) if action == 'set': actDone = 'added to' logAction = 'KADD' elif action == 'clear': actDone = 'removed from' logAction = 'KDEL' else: return dsh_utils.red_error_break_msg('bad action: ' + action) keyStr = keyWord.key_word message = 'key word "%s" %s %s persons.' % (keyStr, actDone, str(count)) errorMsg += dsh_utils.black_break_msg(message) dsh_agi.report_event( message, action=logAction, item=keyWord) return errorMsg
def stats_calculate(sortBy=None, logAction='STT2', textLog=False): """called by views.stats(). possible values of sortedBy are: 'recorded' 'items' 'callsnorec' 'calls' 'starred' 'recdur' 'calldur' returns (success,response,totals) when triggered from daily cron job in dsh_stats_log, logAction=='STT1' and textLog=True. otherwise, the default value here. """ # # fill the tuple list of people and their counts. # people = dvoice.db.models.Person.objects.all() tupleList = [] for person in people: tuple = person_tuple_list(person) tupleList.append(tuple) # # what key do we sort it by? and sort it! # if not sortBy: sortBy = dsh_config.lookup('STATS_SORT_BY') sortKey = sort_key(sortBy) if not sortKey: resp = dsh_utils.red_error_break_msg('invalid sort field: ' + repr(sortBy)) return (False, resp, None) sortedTupleList = sorted(tupleList, key=sortKey, reverse=True) # # just prints a message at the beginning of the page. # sortStr = sortBy if sortBy == 'callsnorec': sortStr = 'calls without recording' elif sortBy == 'recdur': sortStr = 'record duration' elif sortBy == 'calldur': sortStr = 'call duration' response = '' response += dsh_utils.black_break_msg( 'click the column headings to sort by different criteria.') response += dsh_utils.black_break_msg( 'currently sorted by <b><i>%s</i></b>:' % (sortStr,)) response += '<BR>' # # table header. # i = 1 response += '<TABLE border=1>\n' response += table_header() # # to be replaced. # response += '<!-- total row -->' # # the body of the table. # totals = (0, 0, 0, 0, 0, 0, 0) for tuple in sortedTupleList: oneRow,totals = one_row(tuple, i, totals) response += oneRow i += 1 totalRow = total_row(totals) response += totalRow response += table_header() response += '</TABLE>' response = response.replace('<!-- total row -->', totalRow) log_event(totals, logAction=logAction, textLog=textLog) return (True, response, totals)
def keyword_select(dshUid, keyWordTable, itemTable, action='select'): """ 10/03/14: called by dsh_common_views.keyword_select(). add the keyworded items to the current selection. returns (success, response) """ response = '' keyword = dsh_django_utils.get_foreign_key(keyWordTable, dshUid) if not keyword: message = 'dsh_common_selection.keyword_select: no such dsh_uid: ' +\ dshUid response += dsh_utils.red_error_break_msg(message) dsh_django_utils.error_event(message, errorLevel='ERR') return (False, response) response += dsh_utils.black_break_msg( 'The key word is: "' + keyword.key_word + '".') items = itemTable.objects.filter(key_words__dsh_uid__exact=dshUid) if not items: message = 'dsh_common_selection.keyword_select: ' +\ 'no messages with this key word.' response += dsh_utils.red_error_break_msg(message) return (False, response) response += dsh_utils.black_break_msg( 'Number of messages with this key word: ' + str(len(items)) + '.') # # the stuff below is copied from dsh_selection.keyword_select_starred(). # count = 0 for item in items: if action == 'deselect': if not item.u17: continue item.u17 = False else: if item.u17: continue item.u17 = True try: item.save(noLogging=True) count += 1 except: message = 'dsh_common_selection.keyword_select: failed to save: '+\ item.dsh_uid dsh_django_utils.error_event(message, errorLevel='CRT') response += dsh_utils.red_error_break_msg(message) return (False, response) if action == 'deselect': message = 'Items removed from the selection: ' else: message = 'Items added to the selection: ' message += str(count) + '.' response += dsh_utils.black_break_msg(message) dsh_agi.report_event(message) return (True, response)
def process_selection(selectedItems, fieldStr, action='set'): """ called by dsh_selection.process_selection(). initially modeled after dsh_selection.star(). selectedItems is a list of selected items. passed in as arguments because we are selecting different types of objects. filedStr is something like "starred" or "active" or "peer_shared". they are fields defined in db/models.py. action is either "set" or "clear". returns the html response string to be displayed. """ response = '' if not selectedItems: response += dsh_utils.red_error_break_msg( 'no item selected currently.') return response count = 0 changedIds = [] for item in selectedItems: try: valCurr = getattr(item, fieldStr) except: message = 'dsh_common_selection.process_selection(): ' + \ 'getattr() failed: ' + item.dsh_uid + ', fieldStr: ' +\ fieldStr dsh_django_utils.error_event(message, errorLevel='CRT') response += dsh_utils.red_error_break_msg(message) return response valChange = False if action == 'set': if valCurr: continue valNew = True valChange = True else: # # it's 'clear'. # if not valCurr: continue valNew = False valChange = True if not valChange: # # not really necessary... # continue try: setattr(item, fieldStr, valNew) item.save(noLogging=True) count += 1 changedIds.append(item.dsh_uid) except: message = 'dsh_common_selection.process_selection: ' +\ 'failed to save: ' +\ item.dsh_uid + ', fieldStr: ' + fieldStr dsh_django_utils.error_event(message, errorLevel='CRT') response += dsh_utils.red_error_break_msg(message) return response if action == 'set': actDone = 'set' else: actDone = 'cleared' message = '%s item(s) %s. ' % (str(count), actDone) messageLog = 'dsh_common_selection.process_selection(): ' + message messageLog += 'fieldStr: ' + fieldStr + '. ' if count: messageLog += 'items: ' + repr(changedIds) response += dsh_utils.black_break_msg(message) dsh_agi.report_event(messageLog) return response