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 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 person_tuple_list(person): """called by stats() below. makes a tuple for each person for sorting.""" # # count of items longer than 10 seconds. # recThresh = dsh_django_config.lookup("RECORDED_THRESH") recCount = dv2.db.models.Item.objects.filter(owner=person, rec_duration__gt=recThresh).count() # # count of total items. # itemCount = dv2.db.models.Item.objects.filter(owner=person).count() # # count of calls without recording. # basically copied from dsh_django2.py # callsNoRec = dv2.db.models.Event.objects.filter( owner=person, phone_number=person.phone_number, action="CALL", etype="INF" ).count() # # total number of calls is recorded items plus calls without recording. # callCount = itemCount + callsNoRec # # starred item count. # starCount = dv2.db.models.Item.objects.filter(starred=True, owner=person).count() # # record duration. # recDur = 0 callDur = 0 items = dv2.db.models.Item.objects.filter(owner=person) for item in items: recDur += item.rec_duration callDur += item.call_duration # # total call duration. # events = dv2.db.models.Event.objects.filter( owner=person, phone_number=person.phone_number, action="CALL", etype="INF" ) for event in events: callDur += event.call_duration return (person, recCount, itemCount, callsNoRec, callCount, starCount, recDur, callDur)
def page_footer(): response = """ <br class="clear" /> </div> <!-- END content --> <div id="footer"></div> </div> <!-- END container --> """ response += dsh_django_config.lookup('VIEW_FOOTER') response += """ </body> """ return response
def is_triggered_by_dial_now(dshUid): """ the indicator is like: '__DIAL_NOW__' if this is triggered as "dial now," the indicator is postfixed by the real dsh_uid. """ dialNowIndicator = dsh_django_config.lookup("DIAL_NOW_INDICATOR") indicLen = len(dialNowIndicator) prefix = dshUid[:indicLen] postfix = dshUid[indicLen:] if prefix == dialNowIndicator: # # this dot call is triggered as a result of hitting the # dial-now icon. we're not re-arming. # message = "dsh_failed.is_triggered_by_dial_now: " + postfix dsh_agi.report_event(message, sessionID=get_session()) dsh_utils.give_news(message, logging.info) return True return False
# # usage: dsh_load.py [--overwrite] # # this file will look for a file named 'dumpfile.py' # in the current directory. # this dumpfile is generated by the http://localhost:8080/dump/ script. # this dump script may also generated a .tar file of the /media files. # the .tar file must be untarred first before dsh_load.py is run. # import sys,os,subprocess,logging import dsh_django_config,dsh_django_utils2 dsh_django_utils2.append_to_sys_path( dsh_django_config.lookup('DSH_VOICE_CODE_DIR')) import dsh_utils,dsh_config,dsh_agi os.environ['DJANGO_SETTINGS_MODULE'] = dsh_config.lookup( 'DJANGO_SETTINGS_MODULE') dsh_django_utils2.add_to_sys_path(dsh_config.lookup('django_sys_paths')) from dv2.db.models import Person,Organization,Item,Event,KeyWord,ZObject01,ZObject02,ZObject03,ZObject04,ZObject05,ZObject06,ZObject07,ZObject08 import dsh_django_utils,dsh_dump_models,dsh_dump try: import dumpfile except: print 'failed to find dumpfile.py'
# Django settings for mysite project. import os.path import dsh_django_config dshDjangoDataRoot = dsh_django_config.lookup('PHONE_DATA_DJANGO_PATH') dshTemplateDir = dsh_django_config.lookup('TEMPLATE_DIR') import dsh_django_utils2 dsh_django_utils2.add_to_sys_path(dsh_django_config.lookup('COMMON_CODE_DIR')) DEBUG = True TEMPLATE_DEBUG = DEBUG ADMINS = ( # ('Your Name', '*****@*****.**'), ) MANAGERS = ADMINS DATABASE_ENGINE = 'sqlite3' # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'. #DATABASE_NAME = os.path.join(os.path.dirname(__file__), 'database/datarepo.db').replace('\\','/') # Or path to database file if using sqlite3. DATABASE_NAME = os.path.join(dshDjangoDataRoot, 'database') DATABASE_USER = '' # Not used with sqlite3. DATABASE_PASSWORD = '' # Not used with sqlite3. DATABASE_HOST = '' # Set to empty string for localhost. Not used with sqlite3. DATABASE_PORT = '' # Set to empty string for default. Not used with sqlite3. #
def one_row(tuple, i, totals): """called by stats() below. prints one row of the stats table. accumulates the total as well. """ response = '' person,recCount,itemCount,callsNoRec,calls,starCount,recDur,callDur = tuple totalCalls,totalRecorded,totalItems,totalListen,totalStars,\ totalRecDur,totalCallDur = totals # # mugshot column # thumb = dsh_django_utils.thumbnail(person, person.mugshot) href = dsh_django_config.lookup('PERSON_DETAIL_URL') + str(person.id) mugLink = '<a href="%s">%s</a>' % (href, thumb) # # who column # name = person.__unicode__() href = dsh_django_config.lookup('PERSON_URL') + str(person.id) nameLink = '<a href="%s">%s</a>' % (href, name) org = person.organization orgName = org.alias href = dsh_django_config.lookup('ORG_URL') + str(org.id) orgLink = '<a href="%s">%s</a>' % (href, orgName) messagesIcon = person.person_item_link() whoLinks = nameLink + '<BR><BR>' + messagesIcon + '<BR><BR>' + orgLink # # all columns of one row. # response += '<TR>\n' response += '<TD><p align=center>%s</p></TD>\n' % (str(i),) response += '<TD>%s</TD>\n' % (mugLink,) response += '<TD>%s</TD>\n' % (whoLinks) # # total number of calls. # response += '<TD><p align=right>%s</p></TD>\n' % (str(calls),) totalCalls += calls # # long recorded items. # eg.: # /admin/db/item/?owner__id__exact=51&rec_duration__gt=10 # href = '/admin/db/item/?rec_duration__gt=%s&owner__id__exact=%s' %\ (dsh_django_config.lookup('RECORDED_THRESH'), str(person.id)) recLink = '<a href="%s" title="long recorded items">%s</a>' %\ (href, str(recCount)) response += '<TD><p align=right>%s</p></TD>\n' % (recLink,) totalRecorded += recCount # # total count of items. # eg.: # /admin/db/item/?owner__id__exact=16 # href = '/admin/db/item/?owner__id__exact=' href += str(person.id) itemLink = '<a href="%s" title="all recorded items">%s</a>' %\ (href, str(itemCount)) response += '<TD><p align=right>%s</p></TD>\n' % (itemLink,) totalItems += itemCount # # calls without recording. # eg.: # /admin/db/event/?owner__id__exact=3&action__exact=CALL # href = '/admin/db/event/?action__exact=CALL&owner__id__exact=' href += str(person.id) listenLink = '<a href="%s" title="listen without recording">%s</a>' %\ (href, str(callsNoRec)) response += '<TD><p align=right>%s</p></TD>\n' % (listenLink,) totalListen += callsNoRec # # starred. # eg.: # /admin/db/item/?starred__exact=1&owner__id__exact=16 # href = '/admin/db/item/?starred__exact=1&owner__id__exact=' href += str(person.id) starLink = '<a href="%s" title="starred items">%s</a>' %\ (href, str(starCount)) response += '<TD><p align=right>%s</p></TD>\n' % (starLink,) totalStars += starCount # # record duration. # response += '<TD><p align=right>%s</p></TD>\n' % (str(recDur),) totalRecDur += recDur # # call duration. # response += '<TD><p align=right>%s</p></TD>\n' % (str(callDur),) totalCallDur += callDur response += '</TR>\n' totals = totalCalls,totalRecorded,totalItems,totalListen,totalStars,\ totalRecDur,totalCallDur return (response,totals)
import sys, os, subprocess, logging import dv2.db.models # from dv2.db.models import Organization, Person, Item, KeyWord, Event import dsh_django_config, dsh_django_utils, dsh_dump_models, dsh_django_utils2 dsh_django_utils2.append_to_sys_path(dsh_django_config.lookup("DSH_VOICE_CODE_DIR")) import dsh_utils, dsh_config, dsh_agi from django.utils.encoding import smart_str, smart_unicode def config_obj(): """we need just one object in ZObject01. if it's not there, make one.""" objs = dv2.db.models.ZObject01.objects.all() if len(objs) > 0: return objs[0] configObj = dv2.db.models.ZObject01() configObj.save() return configObj 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
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)