Skip to content

Commit

Permalink
Merge pull request #178 from countable-web/master
Browse files Browse the repository at this point in the history
Backend maintenance updates
  • Loading branch information
jrcarretas authored Sep 13, 2024
2 parents faf2298 + 0941df1 commit 2eb6d68
Show file tree
Hide file tree
Showing 36 changed files with 264 additions and 44,624 deletions.
2 changes: 1 addition & 1 deletion web/.pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ indent-after-paren=4
indent-string=' '

# Maximum number of characters on a single line.
max-line-length=120
max-line-length=140

# Maximum number of lines in a module.
max-module-lines=1000
Expand Down
10 changes: 3 additions & 7 deletions web/grants/management/commands/geocode_grants.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


class Command(BaseCommand):
help = "Load grants from old arts database."
help = "Add point to a grant based on the address."

def add_arguments(self, parser):
parser.add_argument("--starting_id", type=int)
Expand Down Expand Up @@ -39,13 +39,9 @@ def geocode_grants(starting_id, google_api_key):

full_address_string = ", ".join(full_address).replace("#", "").replace("&", " ")

google_api_base_url = "https://maps.googleapis.com/maps/api/geocode/json/"
url = "{google_api_base_url}?address={address}&sensor=false&key={google_api_key}".format(
google_api_base_url=google_api_base_url,
address=full_address_string,
google_api_key=google_api_key,
r = requests.get(
f"https://maps.googleapis.com/maps/api/geocode/json?address={full_address_string}&sensor=false&key={google_api_key}"
)
r = requests.get(url)

data = r.json()
response_status = data.get("status")
Expand Down
41 changes: 40 additions & 1 deletion web/grants/management/commands/load_grants.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,51 @@
from django.core.management.base import BaseCommand
import pandas as pd

from grants.utils import load_grants_from_spreadsheet

from grants.models import Grant, GrantCategory


class Command(BaseCommand):
help = "Load grants from an excel spreadsheet. Run migrate_manytomany_fields.py and geocode_grants.py after."

def handle(self, *args, **options):
load_grants_from_spreadsheet(options["file_name"], options["test"])

def add_arguments(self, parser):
parser.add_argument("--file_name", type=str)
parser.add_argument("--test", type=bool)


def load_grants_from_spreadsheet(file_name, test=False):
# Convert excel into dataframe
grants_dataframe = pd.read_excel(file_name, sheet_name=0)

# Replace NaN values with empty string
grants_dataframe.fillna("", inplace=True)

for _, sheet_row in grants_dataframe.iterrows():
category_abbreviation = sheet_row["Grant"].strip().split(" ")[0]

grant_category = GrantCategory.objects.get(abbreviation=category_abbreviation)

if test:
print(sheet_row["Grant"])
continue

print("Saving ", sheet_row["Grant"])

Grant.objects.create(
grant=sheet_row["Grant"],
language=sheet_row["Language"],
year=int(sheet_row["Year"]) if sheet_row["Year"] else None,
recipient=sheet_row["Recipient"],
community_affiliation=sheet_row["Community/Affiliation"],
title=sheet_row["Title"],
project_brief=sheet_row["Project Brief"],
amount=sheet_row["Amount"],
address=sheet_row["Address"],
city=sheet_row["City"],
province=sheet_row["Province"],
postal_code=sheet_row["Postal Code"],
grant_category=grant_category,
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


class Command(BaseCommand):
help = ""
help = "Map grants to their language, community, and placename."

def handle(self, *args, **options):
migrate_manytomany_fields(options["start_id"])
Expand Down
40 changes: 0 additions & 40 deletions web/grants/utils.py

This file was deleted.

2 changes: 1 addition & 1 deletion web/grants/views/grant.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
GrantDetailSerializer,
GrantCategorySerializer,
)
from .base import BaseGenericViewSet
from grants.views.base import BaseGenericViewSet


class GrantViewSet(
Expand Down
4 changes: 2 additions & 2 deletions web/language/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.contrib import admin
from django.contrib.gis.db import models as geomodels

from .models import (
from language.models import (
Language,
LanguageLink,
LanguageFamily,
Expand All @@ -23,7 +23,7 @@
PlaceNameTaxonomy,
RelatedData,
)
from .widgets import LatLongWidget, GeoJSONFeatureCollectionWidget
from language.widgets import LatLongWidget, GeoJSONFeatureCollectionWidget


# INLINES
Expand Down
9 changes: 5 additions & 4 deletions web/language/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

class ListFilter(Filter):
def filter(self, qs, value):
if value not in (None, ''):
strings = [v for v in value.split(',')]
print(strings)
return qs.filter(**{'%s__%s' % (self.field_name, self.lookup_expr): strings})
if value not in (None, ""):
strings = list(value.split(","))
return qs.filter(
**{"%s__%s" % (self.field_name, self.lookup_expr): strings}
)
return qs
Loading

0 comments on commit 2eb6d68

Please sign in to comment.