Skip to content
2 changes: 1 addition & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ celery-remote:
celery -A _main_.celery.app worker -l info

full-celery-local:
make -j 2 start celery
DJANGO_ENV="local" make -j 2 start celery
.PHONY: full-celery-local

beat-remote:
Expand Down
2 changes: 0 additions & 2 deletions src/_main_/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
import os
import firebase_admin
from firebase_admin import credentials
from dotenv import load_dotenv
from pathlib import Path # python3 only
import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration
from sentry_sdk.integrations.celery import CeleryIntegration
Expand Down
10 changes: 1 addition & 9 deletions src/api/handlers/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def registerRoutes(self) -> None:
self.add("/menus.remake", self.remake_navigation_menu)
self.add("/menus.list", self.navigation_menu_list)
self.add("/user.portal.menu.list", self.load_menu_items)
self.add("/data.backfill", self.backfill)
#make this a task: self.add("/data.backfill", self.backfill)
self.add("/data.carbonEquivalency.create", self.create_carbon_equivalency)
self.add("/data.carbonEquivalency.update", self.update_carbon_equivalency)
self.add("/data.carbonEquivalency.get", self.get_carbon_equivalencies)
Expand Down Expand Up @@ -64,14 +64,6 @@ def navigation_menu_list(self, request):
return err
return MassenergizeResponse(data=goal_info)

def backfill(self, request):
context: Context = request.context
args: dict = context.args
goal_info, err = self.service.backfill(context, args)
if err:
return err
return MassenergizeResponse(data=goal_info)

def actions_report(self, request):
context: Context = request.context
args: dict = context.args
Expand Down
5 changes: 0 additions & 5 deletions src/api/services/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ def navigation_menu_list(
return None, err
return serialize_all(main_menu_items), None

def backfill(self, context: Context, args) -> Tuple[dict, MassEnergizeAPIError]:
result, err = self.store.backfill(context, args)
if err:
return None, err
return result, None
def actions_report(self, context: Context, args) -> Tuple[dict, MassEnergizeAPIError]:
result, err = self.store.actions_report(context, args)
if err:
Expand Down
4 changes: 2 additions & 2 deletions src/api/store/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
from xhtml2pdf import pisa
import pytz
from _main_.utils.utils import Console
from api.store.utils import getCarbonScoreFromActionRel
from database.models import Community, CommunityAdminGroup, Event, Media, Team, UserActionRel
from carbon_calculator.carbonCalculator import getCarbonImpact
from django.db.models import Q, Model
from django.utils import timezone
import boto3
Expand Down Expand Up @@ -107,7 +107,7 @@ def count_action_completed_and_todos(**kwargs):
for completed_action in completed_actions:
action_id = completed_action.action.id
action_name = completed_action.action.title
action_carbon = getCarbonScoreFromActionRel(completed_action)
action_carbon = getCarbonImpact(completed_action)
done = 1 if completed_action.status == "DONE" else 0
todo = 1 if completed_action.status == "TODO" else 0

Expand Down
98 changes: 23 additions & 75 deletions src/api/store/community.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,23 +212,21 @@ def _update_locations(self, geography_type, locations, community):
zipcode = zipcodes.matching(location)
if len(zipcode) > 0:
city = zipcode[0].get("city", None)
county = zipcode[0].get("county", None)
state = zipcode[0].get("state", None)
else:
raise Exception("No zip code entry found for zip=" + location)

# get_or_create gives an error if multiple such locations exist (which can happen)
# loc, created = Location.objects.get_or_create(location_type='ZIP_CODE_ONLY', zipcode=location, city=city)
loc = Location.objects.filter(
location_type="ZIP_CODE_ONLY", zipcode=location, city=city
location_type="FULL_ADDRESS", zipcode=location, city=city, county=county, state=state
)
if not loc:
loc = Location.objects.create(
location_type="ZIP_CODE_ONLY", zipcode=location, city=city
location_type="FULL_ADDRESS", zipcode=location, city=city, county=county, state=state
)
print("Zipcode " + location + " created for town " + city)
else:
loc = loc.first()
print("Zipcode " + location + " found for town " + city)

self._check_geography_unique(community, geography_type, location)

else:
Expand All @@ -243,47 +241,28 @@ def _update_locations(self, geography_type, locations, community):
zips = zipcodes.filter_by(
city=town, state=state, zip_code_type="STANDARD"
)
print("Number of zipcodes = " + str(len(zips)))
if len(zips) > 0:
for zip in zips:
print(zip)
zipcode = zip["zip_code"]
zipcode = zip.get("zip_code")
county = zip.get("county")

# get_or_create gives an error if multiple such locations exist (which can happen)
# loc, created = Location.objects.get_or_create(location_type='ZIP_CODE_ONLY', zipcode=location, city=city)
loc = Location.objects.filter(
location_type="ZIP_CODE_ONLY",
zipcode=location,
city=town,
location_type="FULL_ADDRESS", zipcode=zipcode, city=town, county=county, state=state
)
if not loc:
loc = Location.objects.create(
location_type="ZIP_CODE_ONLY",
zipcode=location,
city=town,
location_type="FULL_ADDRESS", zipcode=location, city=city, county=county, state=state
)
print("Zipcode " + zipcode + " created")
else:
loc = loc.first()
print("Zipcode " + zipcode + " found")

self._check_geography_unique(
community, geography_type, zipcode
)
self._check_geography_unique(community, geography_type, zipcode)

else:
print(
"No zipcodes found corresponding to town "
+ town
+ ", "
+ state
)
raise Exception(
"No zipcodes found corresponding to city "
+ town
+ ", "
+ state
)
msg = "No zipcodes found corresponding to town " + town + ", " + state
raise Exception(msg)

elif geography_type == "CITY":
# check that this city is found in the zipcodes list
ss = location.split("-")
Expand All @@ -296,29 +275,21 @@ def _update_locations(self, geography_type, locations, community):
zips = zipcodes.filter_by(
city=city, state=state, zip_code_type="STANDARD"
)
print("Number of zipcodes = " + str(len(zips)))
if len(zips) > 0:
# get_or_create gives an error if multiple such locations exist (which can happen)
# loc, created = Location.objects.get_or_create(location_type='ZIP_CODE_ONLY', zipcode=location, city=city)
county = zips[0].get("county")
loc = Location.objects.filter(
location_type="CITY_ONLY", city=city, state=state
location_type="FULL_ADDRESS", zipcode=location, city=city, county=county, state=state
)
if not loc:
loc = Location.objects.create(
location_type="CITY_ONLY", city=city, state=state
location_type="FULL_ADDRESS", zipcode=location, city=city, county=county, state=state
)
print("City " + city + " created")
else:
loc = loc.first()
print("City " + city + " found")

else:
print(
"No zipcodes found corresponding to city " + city + ", " + state
)
raise Exception(
"No zipcodes found corresponding to city " + city + ", " + state
)
msg = "No zipcodes found corresponding to city " + city + ", " + state
raise Exception(msg)

self._check_geography_unique(community, geography_type, city)
elif geography_type == "COUNTY":
Expand All @@ -333,35 +304,21 @@ def _update_locations(self, geography_type, locations, community):
zips = zipcodes.filter_by(
county=county, state=state, zip_code_type="STANDARD"
)
print("Number of zipcodes = " + str(len(zips)))
if len(zips) > 0:
# get_or_create gives an error if multiple such locations exist (which can happen)
# loc, created = Location.objects.get_or_create(location_type='ZIP_CODE_ONLY', zipcode=location, city=city)
loc = Location.objects.filter(
location_type="COUNTY_ONLY", county=county, state=state
)
if not loc:
loc = Location.objects.create(
location_type="COUNTY_ONLY", county=county, state=state
)
print("County " + county + " created")
else:
loc = loc.first()
print("County " + county + " found")

else:
print(
"No zipcodes found corresponding to county "
+ county
+ ", "
+ state
)
raise Exception(
"No zipcodes found corresponding to county "
+ county
+ ", "
+ state
)
msg = "No zipcodes found corresponding to county " + county + ", " + state
raise Exception(msg)

self._check_geography_unique(community, geography_type, county)

Expand All @@ -380,23 +337,18 @@ def _update_locations(self, geography_type, locations, community):
loc = Location.objects.create(
location_type="STATE_ONLY", state=state
)
print("State " + state + " created")
else:
loc = loc.first()
print("State " + state + " found")
else:
print("No zipcodes found corresponding to state " + location)
raise Exception(
"No zipcodes found corresponding to state " + location
)
msg = "No zipcodes found corresponding to state " + location
raise Exception(msg)

self._check_geography_unique(community, geography_type, location)

elif geography_type == "COUNTRY":
# check that this state is found in the zipcodes list
country = location
zips = zipcodes.filter_by(country=country, zip_code_type="STANDARD")
print("Number of zipcodes = " + str(len(zips)))
if len(zips) > 0:
# get_or_create gives an error if multiple such locations exist (which can happen)
# loc, created = Location.objects.get_or_create(location_type='ZIP_CODE_ONLY', zipcode=location, city=city)
Expand All @@ -407,15 +359,11 @@ def _update_locations(self, geography_type, locations, community):
loc = Location.objects.create(
location_type="COUNTRY_ONLY", country=country
)
print("Country " + country + " created")
else:
loc = loc.first()
print("Country " + country + " found")
else:
print("No zipcodes found corresponding to country " + location)
raise Exception(
"No zipcodes found corresponding to country " + location
)
msg = "No zipcodes found corresponding to country " + location
raise Exception(msg)

self._check_geography_unique(community, geography_type, location)

Expand Down
25 changes: 6 additions & 19 deletions src/api/store/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
InvalidResourceError,
CustomMassenergizeError,
)
from _main_.utils.massenergize_response import MassenergizeResponse
from _main_.utils.context import Context
from collections import Counter
from api.store.utils import get_human_readable_date
Expand Down Expand Up @@ -44,18 +43,15 @@
from api.constants import STANDARD_USER, GUEST_USER
from _main_.utils.constants import ADMIN_URL_ROOT, COMMUNITY_URL_ROOT
from api.store.tag_collection import TagCollectionStore
from api.store.deviceprofile import DeviceStore
from django.db.models import Q
from sentry_sdk import capture_message
from typing import Tuple

from django.utils import timezone
import datetime
from django.utils.timezone import utc
from carbon_calculator.carbonCalculator import AverageImpact
from django.db.models import Count, Sum
from uuid import UUID

from carbon_calculator.carbonCalculator import getCarbonImpact

EMPTY_DOWNLOAD = (None, None)

Expand Down Expand Up @@ -439,13 +435,8 @@ def _get_last_30_days_count(self, action):

#Gets row information for the All Actions CSV and the All Communities and Actions CSV
def _get_action_info_cells(self, action):
average_carbon_points = (
AverageImpact(action.calculator_action)
if action.calculator_action
else int(action.average_carbon_score)
if action.average_carbon_score.isdigit()
else 0
)

average_carbon_points = getCarbonImpact(action)

is_published = "Yes" if action.is_published else "No"

Expand Down Expand Up @@ -566,10 +557,8 @@ def _get_team_info_cells(self, team):
done_actions_count += actions.filter(status= "DONE").count()

for done_action in done_actions:
if done_action.action and done_action.action.calculator_action:
total_carbon_points += (
AverageImpact(done_action.action.calculator_action, done_action.date_completed)
)
total_carbon_points += getCarbonImpact(done_action)

total_carbon_points = str(total_carbon_points)

trending_actions = self._get_last_30_days_list(members)
Expand Down Expand Up @@ -729,9 +718,7 @@ def _get_community_info_cells(self, community, prim_comm_dict):

carbon_user_reported = sum(
[
AverageImpact(action_rel.action.calculator_action)
if action_rel.action.calculator_action
else 0
getCarbonImpact(action_rel)
for action_rel in done_action_rels
]
)
Expand Down
9 changes: 4 additions & 5 deletions src/api/store/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from typing import Tuple
from api.services.utils import send_slack_message
from _main_.settings import SLACK_SUPER_ADMINS_WEBHOOK_URL, RUN_SERVER_LOCALLY, IS_PROD, IS_CANARY
from carbon_calculator.carbonCalculator import AverageImpact
from carbon_calculator.carbonCalculator import getCarbonImpact

def get_households_engaged(community: Community):

Expand All @@ -24,7 +24,7 @@ def get_households_engaged(community: Community):
# loop over actions completed
for actionRel in done_actions:
if actionRel.action and actionRel.action.calculator_action :
points = AverageImpact(actionRel.action.calculator_action, actionRel.date_completed)
points = getCarbonImpact(actionRel)
carbon_footprint_reduction += points

return {"community": {"id": community.id, "name": community.name},
Expand All @@ -38,9 +38,8 @@ def get_all_households_engaged():
actions_completed = done_actions.count()
carbon_footprint_reduction = 0
for actionRel in done_actions:
if actionRel.action and actionRel.action.calculator_action :
carbon_footprint_reduction += AverageImpact(actionRel.action.calculator_action, actionRel.date_completed)

carbon_footprint_reduction += getCarbonImpact(actionRel)

return {"community": {"id": 0, "name": 'Other'},
"actions_completed": actions_completed, "households_engaged": households_engaged,
"carbon_footprint_reduction": carbon_footprint_reduction}
Expand Down
Loading