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 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 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 init_dump(): """initializes the dump file.""" # # makes the directory. # dumpDir = os.path.join( dsh_django_config.lookup('PHONE_DATA_DJANGO_PATH'), dsh_django_config.lookup('DB_DUMP_DIR')) if not dsh_utils.try_mkdir_ifdoesnt_exist( dumpDir, 'dsh_dump.init: ' + dumpDir): dsh_django_utils.error_event( 'dsh_dump.init: failed to create dump dir: ' + dumpDir, errorLevel='CRT') return None # # determine the file name and open it. # randomName = dsh_utils.date_time_rand() fileName = randomName + '.py' tarName = randomName + '.tar' fullName = os.path.join(dumpDir, fileName) fullTarName = os.path.join(dumpDir, tarName) try: dumpFile = open(fullName, 'w') dumpFile.write('# -*- coding: latin-1 -*-\n') dumpFile.write('import datetime\n\n') except: dsh_django_utils.error_event( 'dsh_dump.init: failed to open file: ' + fullName, errorLevel='CRT') return None return (dumpFile, fullName, fullTarName)
def select_one(request, whatKind, dshUid): """this is called by the javascript responding to clicking on a selection box. the javascript expects to see the string of either 'True' or 'False' if 'True', the javascript changes the icon to a checkmark. 'whatKind' is like 'item' or 'person'. """ # # I'm going to allow non-root to select. # they can even show selections. # but they can't dump. # #if dsh_django_request.deny_it(request): # return HttpResponse('False') dsh_django_utils.debug_event( 'views.select_one: entered: ' + whatKind + ' ' + dshUid, 12) if not dsh_dump.allDbTables.has_key(whatKind): dsh_django_utils.error_event( 'views.select_one: wrong kind: ' + repr(whatKind), errorLevel='CRT') return HttpResponse('False') dbTable = dsh_dump.allDbTables[whatKind][0] if dsh_dump.select_box(dbTable, dshUid): return HttpResponse('True') return HttpResponse('False')
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 write_dump_file(dumpFile, dumpPath, content): try: content = smart_str(content) dumpFile.write(content) return True except: dsh_django_utils.error_event( 'dsh_dump.write_dump_file: failed to write file: ' + dumpPath, errorLevel='CRT') return False
def mark_selected_foreign(thisTable, foreignKeyFieldName, kind='single'): """thisTable is something like Person. foreignKeyFieldName is 'organization'. this function will mark all the parent organizations of the selected persons selected as well. kind is either 'single' or 'many'. 'single for single foreign keys. 'many' for many-to-many relationships. """ selectedObjs = thisTable.objects.filter(u17=True) if not selectedObjs: return for obj in selectedObjs: try: fk = getattr(obj, foreignKeyFieldName) except: dsh_django_utils.error_event( 'dsh_dump.mark_selected_foreign: getattr failed on: ' + \ obj.dsh_uid, errorLevel='CRT') continue if not fk: # # maybe the foreign key is null, that could be ok sometimes. # dsh_django_utils.debug_event( 'dsh_dump.mark_selected_foreign: no fk.', 11) continue # # finally mark the foreign key object(s) selected. # if kind == 'single': if fk.u17: continue fk.u17 = True fk.save() dsh_django_utils.debug_event( 'dsh_dump.mark_selected_foreign: marking active: ' + fk.dsh_uid, 11) continue if kind == 'many': objs = fk.all() for obj in objs: if obj.u17: continue obj.u17 = True obj.save() continue
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 get(field): config = config_obj() if not config: dsh_django_utils.error_event("dsh_db_config.get: no config obj", errorLevel="CRT") return None try: value = getattr(config, field) except: dsh_django_utils.error_event("dsh_db_config.get: failed to get this field: " + field, errorLevel="CRT") return None return value
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 set(field, value): config = config_obj() if not config: dsh_django_utils.error_event("dsh_db_config.set: no config obj", errorLevel="CRT") return try: oldVal = getattr(config, field) if oldVal == value: return setattr(config, field, value) config.save() except: dsh_django_utils.error_event( "dsh_db_config.set: failed to get this field: " + field + " <- " + repr(value), errorLevel="CRT" ) return
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 remove_from_current_dial_if_no_message( caller, itemTable, keyWordTable, eventTable, noLogging=False, sessionID=''): """ called by somewhere from the signal handler in dsh_django2.py. see note100331. """ dsh_common_db.init_log(quiet=True) if not caller.current_dial: dsh_utils.db_print('remove_from_current_dial_if_no_message: ' +\ 'not in CDS.', 165) return hasMessage,whatStr = person_has_any_message_at_all( caller, itemTable, keyWordTable, eventTable, noLogging=noLogging, sessionID=sessionID) if hasMessage: dsh_utils.db_print('remove_from_current_dial_if_no_message: ' +\ 'has messages: ' + whatStr, 165) return caller.current_dial = False try: caller.save(noLogging=noLogging) except: message = 'dsh_common_agi.remove_from_current_dial_if_no_message: ' +\ 'failed to save person: ' + caller.dsh_uid dsh_django_utils.error_event(message, item=person, errorLevel='CRT') dsh_utils.give_bad_news(message, reportLevel='CRT') return message = 'dsh_common_agi.remove_from_current_dial_if_no_message: ' +\ 'removed from current dial: ' + caller.__unicode__() dsh_agi.report_event( message, action='RCDS', owner=caller, sessionID=sessionID) dsh_utils.give_news(message, logging.info)
def add_to_tar(fileToAdd, tarFilePath): """adds fileToAdd to tarFilePath.""" tarBin = dsh_django_config.lookup('TAR_PATH') mediaDir = dsh_config.lookup('MEDIA_DIR') try: os.chdir(mediaDir) if not dsh_utils.is_valid_file(fileToAdd, silent=True): message = 'dsh_dump.add_to_tar: invalid file: ' + fileToAdd tarMsg = dsh_utils.red_error_break_msg(message) dsh_django_utils.error_event(message, errorLevel='ERR') return (False, tarMsg) command = tarBin + ' rf ' + tarFilePath + ' ' + fileToAdd dsh_django_utils.debug_event( 'dsh_dump.add_to_tar: command: ' + command, 8) result = dsh_utils.try_execute(command) if result == None: message = 'dsh_dump.add_to_tar: tar problem: ' + command dsh_django_utils.error_event(message, errorLevel='CRT') return (False, dsh_utils.red_error_break_msg(message)) return (True, '') except: message = 'dsh_dump.add_to_tar: unknown tar problem: ' + command dsh_django_utils.error_event(message, errorLevel='CRT') return (False, dsh_utils.red_error_break_msg(message))
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 red5_frame_page(request, dict): """frames the record and play pages from red5. called by red5_record() and red5_play(). the input dict contains keys for: 'title', 'url', 'width', 'height'. """ #if dsh_django_request.deny_it(request): # return please_log_in() response = page_header(dict['title']) response += '\n' hostPort = dsh_django_utils.get_host_port(request) if hostPort == None: message = 'unable to determine host name.' dsh_django_utils.error_event(message, errorLevel='CRT') response += dsh_utils.red_error_break_msg(message) response += page_footer() return HttpResponse(response) dict['hostname'] = hostPort[0] response += """ <table cellpadding=0 cellspacing=0 height=%(height)s width=%(width)s border=0> <tr height="1%%"> <td style="top:0;width:100%%"> <iframe scrolling=auto id=rf src="http://%(hostname)s:5080/%(url)s" frameborder=0 allowtransparency=true style="width:100%%;height:100%%"> </iframe> </td></tr></table> """ % dict response += page_footer() return HttpResponse(response)
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 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 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
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 demographics(request, dshUid, personTable): """ 10/03/13: called by views.demographics(). """ if dsh_django_request.deny_it(request): return views.please_log_in() response = views.page_header('demographics', includeMp3Player=True) person = dsh_django_utils.get_foreign_key(personTable, dshUid) if not person: message = 'dsh_common_views.demographics: no person found: ' + dshUid response += dsh_utils.red_error_break_msg(message) dsh_django_utils.error_event(message, errorLevel='ERR') response += page_footer() return HttpResponse(response) personEditUrl = dsh_django_config.lookup('PERSON_DETAIL_URL') + \ str(person.id) thumb = dsh_django_utils.thumbnail( person, person.mugshot, noCenter=True) url = '<a href=%s title="edit person details">%s</a>' % \ (personEditUrl, thumb) response += url response += '<BR><BR>' response += '<TABLE BORDER=1>' name = person.__unicode__() if name: response += '<TR><TD>name</TD><TD>%s</TD></TR>' % (name,) if person.spoken_name: spokenName = person.spoken_name_display_field() if spokenName: response += '<TR><TD>spoken name</TD><TD>%s</TD></TR>' % \ (spokenName) response += '<TR><TD>dsh uid</TD><TD>%s</TD></TR>' % (person.dsh_uid,) if person.phone_number: response += '<TR><TD>phone</TD><TD>%s</TD></TR>' % \ (person.phone_number,) if person.organization and person.organization.alias: response += '<TR><TD>org.</TD><TD>%s</TD></TR>' % \ (person.organization.alias,) if person.ptype: response += '<TR><TD>type</TD><TD>%s</TD></TR>' % (person.ptype,) if person.gender: response += '<TR><TD>gender</TD><TD>%s</TD></TR>' % (person.gender,) if person.date_birth: if person.birth_date_approximate: dateStr = str(person.date_birth.year) else: dateStr = person.date_birth.isoformat() response += '<TR><TD>birth date</TD><TD>%s</TD></TR>' % (dateStr,) if person.birth_date_approximate: response += '<TR><TD>birth date approximate</TD><TD>True</TD></TR>' if person.date_birth: years = dsh_common_db.calculate_age(person) if years: response += '<TR><TD>age</TD><TD>%s</TD></TR>' % (str(years),) if person.modify_datetime: timeStr = person.modify_datetime.strftime( '%#Y-%#m-%#d %#H:%#M:%#S') response += '<TR><TD>modify time</TD><TD>%s</TD></TR>' % (timeStr,) response += '</TABLE>' response += views.mp3_widget_control() response += views.page_footer() return HttpResponse(response)