def populate_chart_data_structures(survey_type_title, teams, team_history): #Populate GVIS History Chart Data Structures #In: # teams: team list # bvc_data['team_history']: query set of team or teams history temp data to chart #Out: # historical_options # json_history_chart_table # timezone.activate(pytz.timezone('Australia/Queensland')) num_rows = 0 team_index = 0 history_chart_schema = {"archive_date": ("datetime", "Archive_Date")} history_chart_columns = ('archive_date',) average_index = None for team in teams: history_chart_schema.update({team['team_name'] : ("number",team['team_name'].replace("_", " "))}) history_chart_columns = history_chart_columns + (team['team_name'],) if team['team_name'] == 'Average': average_index = team_index team_index += 1 history_chart_data = [] row = None for survey_summary in team_history: if row == None: row = {} row['archive_date'] = timezone.localtime(survey_summary.archive_date) elif row['archive_date'] != timezone.localtime(survey_summary.archive_date): history_chart_data.append(row) row = {} row['archive_date'] = timezone.localtime(survey_summary.archive_date) row[survey_summary.team_name] = (float(survey_summary.average_score), str(float(survey_summary.average_score)) + " (" + str(survey_summary.responder_count) + " Responses)") history_chart_data.append(row) # Loading it into gviz_api.DataTable history_chart_table = gviz_api.DataTable(history_chart_schema) history_chart_table.LoadData(history_chart_data) # Creating a JSon string json_history_chart_table = history_chart_table.ToJSon(columns_order=(history_chart_columns)) historical_options = { 'legendPosition': 'newRow', 'title' : survey_type_title + ' by Team', 'vAxis': {'title': survey_type_title}, 'hAxis': {'title': "Month"}, 'seriesType': "bars", 'vAxis': { 'ticks': [1,2,3,4,5,6,7,8,9,10] }, 'max': 12, 'min': 0, 'focusTarget': 'category', 'tooltip': { 'trigger': 'selection', 'isHtml': 'true' }, } if average_index != None: historical_options.update({'series': {average_index: {'type': "line"}}}) return historical_options, json_history_chart_table, team_index
def admin(request, survey_id, team_name=''): survey = get_object_or_404(TeamTemperature, pk=survey_id) timezone.activate(pytz.timezone(survey.default_tz or 'UTC')) # if valid session token or valid password render results page password = None user = None survey_teams=[] next_archive_date = timezone.now() if request.method == 'POST': form = ResultsPasswordForm(request.POST, error_class=ErrorBox) if form.is_valid(): rpf = form.cleaned_data password = rpf['password'].encode('utf-8') else: try: userid = request.session.get('userid', '__nothing__') user = User.objects.get(id=userid) except User.DoesNotExist: return render(request, 'password.html', {'form': ResultsPasswordForm()}) if user and survey.creator.id == user.id or check_password(password, survey.password): request.session['userid'] = survey.creator.id teamtemp = TeamTemperature.objects.get(pk=survey_id) survey_type = teamtemp.survey_type if team_name != '': team_found = teamtemp.teams_set.filter(team_name = team_name).count() if team_found == 0 and survey_type != 'DEPT-REGION-SITE': TeamDetails = Teams(request = survey,team_name = team_name) TeamDetails.save() results = teamtemp.temperatureresponse_set.filter(team_name = team_name, archived = False) else: results = teamtemp.temperatureresponse_set.filter(archived = False) survey_teams = teamtemp.teams_set.all() if team_name != '': stats = survey.team_stats(team_name=team_name) else: stats = survey.stats() if survey.archive_schedule > 0: next_archive_date = timezone.localtime(survey.archive_date) + timedelta(days=(survey.archive_schedule)) if next_archive_date < timezone.localtime(timezone.now()): next_archive_date = timezone.localtime(timezone.now() + timedelta(days=1)) return render(request, 'results.html', { 'id': survey_id, 'stats': stats, 'results': results, 'team_name':team_name, 'pretty_team_name':team_name.replace("_", " "),'survey_teams':survey_teams, 'archive_schedule' : survey.archive_schedule, 'next_archive_date' : next_archive_date.strftime("%A %d %B %Y") } ) else: return render(request, 'password.html', {'form': ResultsPasswordForm()})
def auto_archive_surveys(request): timezone.activate(pytz.timezone('UTC')) print >>sys.stderr,"auto_archive_surveys: Start at " + str(timezone.localtime(timezone.now())) + " UTC" teamtemps = TeamTemperature.objects.filter(archive_schedule__gt=0) nowstamp = timezone.now() data = {'archive_date': nowstamp} for teamtemp in teamtemps: print >>sys.stderr,"auto_archive_surveys: Survey " + teamtemp.id print >>sys.stderr,"auto_archive_surveys: Comparing " + str(timezone.localtime(timezone.now()).date()) + " >= " + str(timezone.localtime( teamtemp.archive_date + timedelta(days=teamtemp.archive_schedule) ).date()) print >>sys.stderr,"auto_archive_surveys: Comparing " + str(timezone.localtime(timezone.now())) + " >= " + str(timezone.localtime( teamtemp.archive_date + timedelta(days=teamtemp.archive_schedule) )) print >>sys.stderr,"auto_archive_surveys: Comparison returns: " + str( timezone.localtime(timezone.now()).date() >= timezone.localtime( teamtemp.archive_date + timedelta(days=teamtemp.archive_schedule) ).date() ) if teamtemp.archive_date is None or (timezone.localtime( timezone.now() ).date() >= (timezone.localtime( teamtemp.archive_date + timedelta(days=teamtemp.archive_schedule) ).date() ) ): scheduled_archive(request, teamtemp.id) TeamTemperature.objects.filter(pk=teamtemp.id).update(**data) print >>sys.stderr,"Archiving: " + " " + teamtemp.id + " at " + str(nowstamp) + " UTC " + str(timezone.localtime(timezone.now())) + " UTC" print >>sys.stderr,"auto_archive_surveys: Stop at " + str(timezone.localtime(timezone.now())) + " UTC"
def populate_chart_data_structures(survey_type_title, teams, team_history, tz='UTC'): #Populate GVIS History Chart Data Structures #In: # teams: team list # bvc_data['team_history']: query set of team or teams history temp data to chart #Out: # historical_options # json_history_chart_table # timezone.activate(pytz.timezone(tz)) num_rows = 0 team_index = 0 history_chart_schema = {"archive_date": ("datetime", "Archive_Date")} history_chart_columns = ('archive_date',) average_index = None for team in teams: if team['team_name'] != 'Average': history_chart_schema.update({team['team_name'] : ("number",team['team_name'].replace("_", " "))}) history_chart_columns = history_chart_columns + (team['team_name'],) team_index += 1 #Add average heading if not already added for adhoc filtering #if team_index > 1: average_index = team_index history_chart_schema.update({'Average' : ("number",'Average')}) history_chart_columns = history_chart_columns + ('Average',) history_chart_data = [] row = None num_scores = 0 score_sum = 0 responder_sum = 0 for survey_summary in team_history: if survey_summary.team_name != 'Average': if row == None: row = {} row['archive_date'] = timezone.localtime(survey_summary.archive_date) elif row['archive_date'] != timezone.localtime(survey_summary.archive_date): #TODO can it recalculate the average here for adhoc filtering if num_scores > 0: average_score = score_sum / num_scores row['Average'] = (float(average_score), str("%.2f" % float(average_score)) + " (" + str(responder_sum) + " Responses)") score_sum = 0 num_scores = 0 responder_sum = 0 history_chart_data.append(row) row = {} row['archive_date'] = timezone.localtime(survey_summary.archive_date) #Accumulate for average calc score_sum = score_sum + survey_summary.average_score num_scores = num_scores + 1 responder_sum = responder_sum + survey_summary.responder_count row[survey_summary.team_name] = (float(survey_summary.average_score), str("%.2f" % float(survey_summary.average_score)) + " (" + str(survey_summary.responder_count) + " Responses)") average_score = score_sum / num_scores row['Average'] = (float(average_score), str("%.2f" % float(average_score)) + " (" + str(responder_sum) + " Responses)") history_chart_data.append(row) # Loading it into gviz_api.DataTable history_chart_table = gviz_api.DataTable(history_chart_schema) history_chart_table.LoadData(history_chart_data) # Creating a JSon string json_history_chart_table = history_chart_table.ToJSon(columns_order=(history_chart_columns)) historical_options = { 'legendPosition': 'newRow', 'title' : survey_type_title + ' by Team', 'vAxis': {'title': survey_type_title}, 'hAxis': {'title': "Month"}, 'seriesType': "bars", 'vAxis': { 'ticks': [1,2,3,4,5,6,7,8,9,10] }, 'max': 12, 'min': 0, 'focusTarget': 'category', 'tooltip': { 'trigger': 'selection', 'isHtml': 'true' }, } if average_index != None: historical_options.update({'series': {average_index: {'type': "line"}}}) return historical_options, json_history_chart_table, team_index
def bvc(request, survey_id, team_name='', archive_id= '', weeks_to_trend='12', num_iterations='0'): #Check if *any* scheduled archive surveys are overdue for archiving #auto_archive_surveys(request) timezone.activate(pytz.timezone('Australia/Queensland')) survey = get_object_or_404(TeamTemperature, pk=survey_id) password = None user = None num_rows = 0 json_history_chart_table = None historical_options = {} stats_date = '' survey_teams=[] ignore_bvc_auth = False if settings.IGNORE_BVC_AUTH: ignore_bvc_auth = settings.IGNORE_BVC_AUTH #Process POST return results from Password Form Submit: if request.method == 'POST': form = ResultsPasswordForm(request.POST, error_class=ErrorBox) if form.is_valid(): rpf = form.cleaned_data password = rpf['password'].encode('utf-8') if check_password(password, survey.password): request.session['userid'] = survey.creator.id #Retrieve User Token - if user does not exist present password entry form try: userid = request.session.get('userid', '__nothing__') user = User.objects.get(id=userid) except User.DoesNotExist: if not ignore_bvc_auth: return render(request, 'password.html', {'form': ResultsPasswordForm()}) #If authenticated via user token or ignoring auth for bvc rendering if ignore_bvc_auth or (user and survey.creator.id == user.id): survey_type_title = 'Team Temperature' if survey.survey_type == 'CUSTOMERFEEDBACK': survey_type_title = 'Customer Feedback' if not ignore_bvc_auth: request.session['userid'] = survey.creator.id teamtemp = TeamTemperature.objects.get(pk=survey_id) if team_name != '': if archive_id == '': results = teamtemp.temperatureresponse_set.filter(team_name = team_name, archived = False) else: past_date = teamtemp.temperatureresponse_set.filter(id=archive_id).values('archive_date')[0] results = teamtemp.temperatureresponse_set.filter(team_name = team_name, archived = True,archive_date=past_date['archive_date']) stats_date = past_date['archive_date'] team_history = teamtemp.teamresponsehistory_set.filter(team_name = team_name).order_by('archive_date') num_rows = teamtemp.teamresponsehistory_set.filter(team_name = team_name).count() teams = teamtemp.teamresponsehistory_set.filter(team_name = team_name).values('team_name').distinct() archived_dates = teamtemp.temperatureresponse_set.filter(team_name = team_name, archived = True).values('archive_date','id').distinct('archive_date') else: if archive_id == '': results = teamtemp.temperatureresponse_set.filter(archived = False) else: past_date = teamtemp.temperatureresponse_set.filter(id=archive_id).values('archive_date')[0] results = teamtemp.temperatureresponse_set.filter( archive_date=past_date['archive_date'] ) stats_date = past_date['archive_date'] team_history = teamtemp.teamresponsehistory_set.all().order_by('archive_date') num_rows = teamtemp.teamresponsehistory_set.all().count() teams = teamtemp.teamresponsehistory_set.all().values('team_name').distinct() archived_dates = teamtemp.temperatureresponse_set.filter(archived = True).values('archive_date','id').distinct('archive_date') trend_period = timedelta(weeks=int(float(weeks_to_trend))) buffer_max = timedelta(days=3) max_date = timezone.now() + buffer_max min_date = max_date - trend_period - buffer_max team_index = 0 if num_rows > 0: history_chart_schema = {"archive_date": ("datetime", "Archive_Date")} history_chart_columns = ('archive_date',) average_index = None for team in teams: history_chart_schema.update({team['team_name'] : ("number",team['team_name'].replace("_", " "))}) history_chart_columns = history_chart_columns + (team['team_name'],) if team['team_name'] == 'Average': average_index = team_index team_index += 1 history_chart_data = [] row = None for survey_summary in team_history: if row == None: row = {} row['archive_date'] = timezone.localtime(survey_summary.archive_date) elif row['archive_date'] != timezone.localtime(survey_summary.archive_date): history_chart_data.append(row) row = {} row['archive_date'] = timezone.localtime(survey_summary.archive_date) row[survey_summary.team_name] = (float(survey_summary.average_score), str(float(survey_summary.average_score)) + " (" + str(survey_summary.responder_count) + " Responses)") history_chart_data.append(row) # Loading it into gviz_api.DataTable history_chart_table = gviz_api.DataTable(history_chart_schema) history_chart_table.LoadData(history_chart_data) # Creating a JSon string json_history_chart_table = history_chart_table.ToJSon(columns_order=(history_chart_columns)) historical_options = { 'legendPosition': 'newRow', 'title' : survey_type_title + ' by Team', 'vAxis': {'title': survey_type_title}, 'hAxis': {'title': "Month"}, 'seriesType': "bars", 'vAxis': { 'ticks': [1,2,3,4,5,6,7,8,9,10] }, 'max': 12, 'min': 0, 'focusTarget': 'category', 'tooltip': { 'trigger': 'selection', 'isHtml': 'true' }, } if average_index != None: historical_options.update({'series': {average_index: {'type': "line"}}}) if team_name != '' and stats_date == '': stats = survey.team_stats(team_name=team_name) elif team_name == '' and stats_date != '': stats = survey.archive_stats(archive_date=stats_date) elif team_name != '' and stats_date != '': stats = survey.archive_team_stats(team_name=team_name,archive_date=stats_date) else: stats = survey.stats() survey_teams = teamtemp.teams_set.all() if int(float(num_iterations)) > 0: multi_stats = calc_multi_iteration_average(team_name, survey, int(float(num_iterations))) if multi_stats: stats = multi_stats #generate word cloud words = "" word_cloudurl = "" word_count = 0 for word in stats['words']: for i in range(0,word['id__count']): words = words + word['word'] + " " word_count += 1 #TODO Write a better lookup and model to replace this hack word_cloud_index = WordCloudImage.objects.filter(word_list = words) if word_cloud_index: if os.path.isfile(os.path.join(os.path.dirname(os.path.abspath(__file__)), word_cloud_index[0].image_url)): word_cloudurl = word_cloud_index[0].image_url else: #Files have been deleted remove from db and then regenerate WordCloudImage.objects.filter(word_list = words).delete() if word_cloudurl == "" and words != "": word_cloudurl = generate_wordcloud(words) if word_cloudurl: word_cloud = WordCloudImage(creation_date = timezone.now(), word_list = words, image_url = word_cloudurl) word_cloud.save() return render(request, 'bvc.html', { 'id': survey_id, 'stats': stats, 'results': results, 'team_name':team_name, 'archive_date':stats_date, 'pretty_team_name':team_name.replace("_", " "), 'team_history' : team_history , 'json_historical_data' : json_history_chart_table, 'min_date' : min_date, 'max_date' : max_date, 'historical_options' : historical_options, 'archived_dates': archived_dates, 'survey_teams': survey_teams, 'word_cloudurl':word_cloudurl, 'num_iterations':num_iterations, 'team_count' : team_index, 'survey_type_title' : survey_type_title } ) else: return render(request, 'password.html', {'form': ResultsPasswordForm()})