Skip to content

Commit

Permalink
810 - Carbon Calculator actions update - part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
BradHN1 committed Oct 12, 2023
1 parent 077ac1c commit 113bfd2
Show file tree
Hide file tree
Showing 8 changed files with 1,468 additions and 50 deletions.
2 changes: 0 additions & 2 deletions src/_main_/utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import pytz
from django.utils import timezone
from datetime import datetime, timedelta
#import cv2
from datetime import datetime
from dateutil import tz
from sentry_sdk import capture_message
import base64
Expand Down
9 changes: 6 additions & 3 deletions src/carbon_calculator/carbonCalculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
#imports
import os
import pytz
from datetime import datetime
from dateutil import tz
from datetime import datetime, date
from .models import Action, Question, CarbonCalculatorMedia, CalcDefault, Version
#from django.utils import timezone
from _main_.settings import BASE_DIR
Expand Down Expand Up @@ -60,14 +61,15 @@ def versionCheck():
fileDateTime(ACTIONS_DATA),
fileDateTime(DEFAULTS_DATA))

today = str(date.today())
if version.version != CALCULATOR_VERSION:
version.version = CALCULATOR_VERSION
version.note = "Calculator version update on "+str(datetime.today())
version.note = "Calculator version update on "+today
print(version.note)
version.save()
return False # reload data
elif version.updated_on < files_updated:
version.note = "Calculator data update on "+str(datetime.today())
version.note = "Calculator data update on "+today
print(version.note)
version.save()
return False # reload data
Expand Down Expand Up @@ -400,6 +402,7 @@ def ImportActions(self, actionsFile):
continue
avg_points = item[t["Avg points"]]
defaults = {
'title':item[t["Title"]],
'description':item[t["Description"]],
'helptext':item[t["Helptext"]],
'category':item[t["Category"]],
Expand Down
1,333 changes: 1,295 additions & 38 deletions src/carbon_calculator/content/all-actions-update.csv

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions src/carbon_calculator/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#from django.shortcuts import render
from django.http import JsonResponse
from database.utils.json_response_wrapper import Json
#from django.views.decorators.csrf import csrf_exempt
from database.utils.common import get_request_contents
from _main_.utils.massenergize_response import MassenergizeResponse
from _main_.utils.massenergize_errors import NotAuthorizedError
Expand Down
6 changes: 3 additions & 3 deletions src/database/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,7 @@ def summary(self):
done_points = 0
for actionRel in done_actions:
if actionRel.action and actionRel.action.calculator_action:
done_points += actionRel.action.calculator_action.average_points
done_points += AverageImpact(actionRel.action.calculator_action, actionRel.date_completed)
else:
done_points += actionRel.carbon_impact

Expand All @@ -950,7 +950,7 @@ def summary(self):
todo_points = 0
for actionRel in todo_actions:
if actionRel.action and actionRel.action.calculator_action:
todo_points += actionRel.action.calculator_action.average_points
todo_points += AverageImpact(actionRel.action.calculator_action, actionRel.date_completed)
else:
todo_points += actionRel.carbon_impact

Expand Down Expand Up @@ -1949,7 +1949,7 @@ def full_json(self):
"name": u.real_estate_unit.name if u.real_estate_unit else None,
},
"date_completed": u.date_completed,
"carbon_impact": u.action.calculator_action.average_points if u.action.calculator_action else None,
"carbon_impact": AverageImpact(u.action.calculator_action, u.date_completed) if u.action.calculator_action else None,
"recorded_at": u.updated_at,

}
Expand Down
160 changes: 160 additions & 0 deletions src/task_queue/database_tasks/update_actions_content.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
import csv
import datetime
from django.apps import apps
from sentry_sdk import capture_message
from _main_.utils.emailer.send_email import send_massenergize_email_with_attachments
from api.utils.constants import DATA_DOWNLOAD_TEMPLATE
from database.models import Community, Action, FeatureFlag
from carbon_calculator.models import Action as CCAction
from django.http import HttpResponse
from _main_.settings import BASE_DIR
import re


FEATURE_FLAG_KEY = "update-actions-content-feature-flag"
ACTIONS_UPDATE_FILE = BASE_DIR + "/carbon_calculator/content/all-actions-update.csv"

"""
This task is used to update Action content in the database for one or more communities.
There are two parts to this:
1. Generate a report of the contents that need to be fixed with the following information
a. Community: the community the Action belongs to
b. Action name: the name or title of the content
c. Carbon Calculator action and old carbon calculator action, if differnt
d. Impact, cost or category tag and old impact/cost/category tag, if different
2. Update the link between Action and CCAction, to be correct in all cases.
3. As appropriate - delete a number of garbage actions with no redeeming features
"""

def write_to_csv(data):
response = HttpResponse(content_type="text/csv")
writer = csv.DictWriter(response, fieldnames=["Community", "Action Name", "CCAction", "Impact", "Cost"])
writer.writeheader()
for row in data:
writer.writerow(row)
return response.content


#def get_community(instance):
# if instance and hasattr(instance, "community"):
# return instance.community.name if instance.community else ""
# return "N/A"

#def get_model_instances(model_name, app_label):
# model = apps.get_model(app_label=app_label, model_name=model_name)
# filter_args = {} if model_name == "PastEvent" else {"is_deleted": False}
# model_instances = model.objects.filter(**filter_args)
# return model_instances

#def is_feature_enabled(instance):
# communities = db_models.Community.objects.filter(is_deleted=False)
# flag = db_models.FeatureFlag.objects.filter(key=FEATURE_FLAG_KEY).first()
# if not flag or not flag.enabled():
# return False
# enabled_communities = flag.enabled_communities(communities)
# if hasattr(instance, "community"):
# if not instance.community or instance.community in enabled_communities:
# return True
# elif hasattr(instance, "primary_community"):
# if not instance.primary_community or instance.primary_community in enabled_communities:
# return True
# return False


def update_actions_content(task=None):
try:
data = []
communities = Community.objects.filter(is_deleted=False)

# check if update feature enabled, otherwise will just report
flag = FeatureFlag.objects.filter(key=FEATURE_FLAG_KEY).first()
if not flag or not flag.enabled():
update_enabled = False
else:
update_enabled = True
enabled_communities = flag.enabled_communities(communities)

# ccActions = CCAction.objects.filter(is_deleted=False) - when we implement ccAction deletion
ccActions = CCAction.objects.all()

# open the all-actions-content.csv file which will drive the updates
with open(ACTIONS_UPDATE_FILE, newline='') as csvfile:
inputlist = csv.reader(csvfile)
first = True
num = 0

# loop over lines in the file
for item in inputlist:
if first: # header line
t = {}
for i in range(len(item)):
if i==0:
item[i] = 'Name'
t[item[i]] = i
first = False
else:
community_name = item[0]
community = None
if community_name != '':
community = communities.filter(name=community_name)
if not community:
continue # commmunity not in this database
community = community.first()



action_title = item[t["Action"]]
impact = item[t["Impact"]]
cost = item[t["Cost"]]
category = item[t["Category"]]
ccActionName = item[t["Carbon Calculator Action"]]

# locate the Action from title and community
action = Action.objects.filter(title=action_title, community=community)
if action:
action = action.first()

# check whether action has correct calculator_action, impact and cost
if not action.calculator_action or action.calculator_action.name != ccActionName:

# add line to report
line = {
"Community": community_name,
"Action Name": action_title,
"CCAction": ccActionName,
"Impact": impact,
"Cost": cost,
}
print(line)
data.append(line)

if update_enabled:
if not community or community in enabled_communities:
if ccActionName == "DELETE":
# if calculator_action is DELETE, mark for deletion
action.is_deleted = True
else:
# set calculator_action,
ccAction = ccActions.filter(name=ccActionName)
if not ccAction:
print("Carbon calculator action '"+ccActionName+"' does not exist")
continue
action.calculator_action = ccAction.first()

action.save()


if len(data) > 0:
report = write_to_csv(data)
temp_data = {'data_type': "Content Spacing", "name":task.creator.full_name if task.creator else "admin"}
file_name = "Content-Spacing-Report-{}.csv".format(datetime.datetime.now().strftime("%Y-%m-%d"))
send_massenergize_email_with_attachments(DATA_DOWNLOAD_TEMPLATE,temp_data,[task.creator.email], report, file_name)

return True
except Exception as e:
print(str(e))
capture_message(str(e), level="error")
return False



2 changes: 2 additions & 0 deletions src/task_queue/jobs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

from task_queue.database_tasks.contents_spacing_correction import process_spacing_data
from task_queue.database_tasks.update_actions_content import update_actions_content
from task_queue.nudges.cadmin_events_nudge import send_events_nudge
from task_queue.nudges.user_event_nudge import prepare_user_events_nudge
from task_queue.nudges.postmark_sender_signature import collect_and_create_signatures
Expand All @@ -22,4 +23,5 @@
"Create Community Snapshots": create_snapshots,
"Postmark Sender Signature": collect_and_create_signatures,
"Process Content Spacing": process_spacing_data,
"Update Action Content": update_actions_content,
}
4 changes: 2 additions & 2 deletions src/task_queue/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from django.db.models import Count
from django.db.models import Q
from django.core.exceptions import ObjectDoesNotExist

from carbon_calculator.carbonCalculator import AverageImpact

today = datetime.datetime.utcnow().replace(tzinfo=utc)
one_week_ago = today - timezone.timedelta(days=7)
Expand Down Expand Up @@ -253,7 +253,7 @@ def _get_user_reported_info(community, users):

carbon_user_reported = sum(
[
action_rel.action.calculator_action.average_points
AverageImpact(action_rel.action.calculator_action, action_rel.date_completed)
if action_rel.action.calculator_action
else 0
for action_rel in done_action_rels
Expand Down

0 comments on commit 113bfd2

Please sign in to comment.