Skip to content

Commit

Permalink
move from flake8 to ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
SamDudley committed Mar 28, 2024
1 parent 6b98b44 commit 0829354
Show file tree
Hide file tree
Showing 46 changed files with 244 additions and 254 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ jobs:
- name: Collect static
run: make collectstatic

- name: Run Flake8
run: make flake8
- name: Run ruff
run: make ruff

- name: Run isort
run: make isort-check
Expand Down
27 changes: 18 additions & 9 deletions core/import_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ def add_position(d, h):
dictionary inside"""
c = {}
for k, v in d.items():
if type(v) is dict:
if isinstance(v, dict):
c[k] = add_position(v, h)
else:
if type(v) is str:
if isinstance(v, str):
v = v.lower()
if v in h:
c[k] = h[v]
Expand Down Expand Up @@ -145,7 +145,7 @@ def read_csv_from_dict(d, row, year):

default_list = {}
for k, v in d[IMPORT_CSV_FIELDLIST_KEY].items():
if type(v) is dict:
if isinstance(v, dict):
default_list[k], errormsg = read_csv_from_dict(v, row, year)
else:
default_list[k] = get_value_from_field(
Expand Down Expand Up @@ -185,8 +185,8 @@ def get_col_from_obj_key(obj_key):
if IMPORT_CSV_IS_FK in obj_key:
header_list.append(obj_key[IMPORT_CSV_IS_FK])
if IMPORT_CSV_FIELDLIST_KEY in obj_key:
for k, v in obj_key[IMPORT_CSV_FIELDLIST_KEY].items():
if type(v) is dict:
for _, v in obj_key[IMPORT_CSV_FIELDLIST_KEY].items():
if isinstance(v, dict):
header_list = header_list + get_col_from_obj_key(v)
else:
header_list.append(v)
Expand Down Expand Up @@ -254,13 +254,22 @@ class ImportInfo:

def __init__(
self,
key={},
key=None,
title="",
h_list=[],
h_list=None,
special_import_func=None,
filter=[],
filter=None,
extra_func=None,
):
if key is None:
key = {}

if h_list is None:
h_list = []

if filter is None:
filter = []

self.key = key
self.special_func = special_import_func
if bool(key):
Expand Down Expand Up @@ -328,7 +337,7 @@ def get_field_name(obj_key, prefix):
field_list.append(prefix + model._meta.pk.name)
if IMPORT_CSV_FIELDLIST_KEY in obj_key:
for k, v in obj_key[IMPORT_CSV_FIELDLIST_KEY].items():
if type(v) is dict:
if isinstance(v, dict):
field_list = field_list + get_field_name(v, prefix + k + "__")
else:
field_list.append(prefix + k)
Expand Down
4 changes: 2 additions & 2 deletions core/management/commands/create_stub_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def create_departmental_group(
)
self.counter += 1
directorate_code_base = int(group_code[0:4]) * 10
for i in range(howmany_directorate):
for _ in range(howmany_directorate):
self.create_directorate(
departmental_group,
"{}A".format(directorate_code_base),
Expand All @@ -47,7 +47,7 @@ def create_directorate(
group=departmental_group,
)
self.counter += 1
for i in range(howmany_cost_centres):
for _ in range(howmany_cost_centres):
CostCentre.objects.create(
cost_centre_code=888810 + self.counter,
active=True,
Expand Down
2 changes: 1 addition & 1 deletion core/remove_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def bulk_delete_users(file_obj: FileUpload) -> int:
else:
# needed to avoid processing empty rows at the end of the file
break
workbook.close
workbook.close()

final_status = FileUpload.PROCESSED
if error_found:
Expand Down
1 change: 0 additions & 1 deletion core/test/factories.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import factory
import factory
from django.contrib.auth import get_user_model
from faker import Faker

Expand Down
1 change: 0 additions & 1 deletion data_lake/test/test_actual_split.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ def setUp(self):
natural_account_code=nac_obj,
project_code=project_obj,
)
self.financial_code_obj.save
financial_period_queryset = FinancialPeriod.objects.filter(
financial_period_code__lt=4
)
Expand Down
25 changes: 11 additions & 14 deletions download_file/test/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,23 @@ def setUp(self):
natural_account_code=nac_obj,
project_code=project_obj,
)
financial_code_obj.save
apr_figure = ForecastMonthlyFigure.objects.create(
# apr figure
ForecastMonthlyFigure.objects.create(
financial_period=FinancialPeriod.objects.get(financial_period_code=1),
financial_code=financial_code_obj,
financial_year=year_obj,
amount=self.amount_apr,
)
apr_figure.save
may_figure = ForecastMonthlyFigure.objects.create(
# may figure
ForecastMonthlyFigure.objects.create(
financial_period=FinancialPeriod.objects.get(
financial_period_code=2,
),
amount=self.amount_may,
financial_code=financial_code_obj,
financial_year=year_obj,
)
may_figure.save

self.year_total = self.amount_apr + self.amount_may

def test_download_mi_view(self):
Expand Down Expand Up @@ -146,40 +146,37 @@ def setUp(self):
natural_account_code=nac_obj,
project_code=project_obj,
)
financial_code_obj.save
apr_figure = BudgetMonthlyFigure.objects.create(
# apr figure
BudgetMonthlyFigure.objects.create(
financial_period=FinancialPeriod.objects.get(financial_period_code=1),
financial_code=financial_code_obj,
financial_year=year_obj,
amount=self.amount_apr,
)
apr_figure.save

may_figure = BudgetMonthlyFigure.objects.create(
# may figure
BudgetMonthlyFigure.objects.create(
financial_period=FinancialPeriod.objects.get(
financial_period_code=2,
),
amount=self.amount_may,
financial_code=financial_code_obj,
financial_year=year_obj,
)
may_figure.save

financial_code_obj1 = FinancialCode.objects.create(
programme=self.programme_obj,
cost_centre=cost_centre,
natural_account_code=nac_obj,
)

may_figure1 = BudgetMonthlyFigure.objects.create(
# another may figure
BudgetMonthlyFigure.objects.create(
financial_period=FinancialPeriod.objects.get(
financial_period_code=2,
),
amount=self.amount_may,
financial_code=financial_code_obj1,
financial_year=year_obj,
)
may_figure1.save

self.year_total = self.amount_apr + self.amount_may

Expand Down
3 changes: 1 addition & 2 deletions end_of_month/test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ def __init__(self, year=0):
natural_account_code=nac_obj,
project_code=project_obj,
)
self.financial_code_obj.save

if year == 0:
year = get_current_financial_year()
Expand Down Expand Up @@ -166,7 +165,7 @@ def __init__(self, last_archived_period=16, year=0):
self.setup_forecast()
self.setup_budget()
# prepares the lists used to store the totals
for period in range(0, last_archived_period):
for _ in range(0, last_archived_period):
self.archived_forecast.append(0)
self.archived_budget.append(0)
self.set_archive_period(last_archived_period)
Expand Down
6 changes: 4 additions & 2 deletions forecast/import_actuals.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ def validate_trial_balance_report(file_upload, month_number, year):
str(ex),
str(ex),
)
workbook.close()
raise ex

try:
Expand All @@ -222,8 +223,9 @@ def validate_trial_balance_report(file_upload, month_number, year):
str(ex),
str(ex),
)
workbook.close
workbook.close()
raise ex

return workbook, worksheet


Expand Down Expand Up @@ -284,7 +286,7 @@ def upload_trial_balance_report(file_upload, month_number, financial_year):
else:
# needed to avoid processing empty rows at the end of the file
break
workbook.close
workbook.close()

final_status = FileUpload.PROCESSED
if check_financial_code.error_found:
Expand Down
21 changes: 15 additions & 6 deletions forecast/import_budget_or_forecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def upload_figures(uploadmodel, data_row, year_obj, financialcode_obj, month_dic
if period_figure is None:
period_figure = 0
# We import from Excel, and the user may have entered spaces in an empty cell.
if type(period_figure) == str:
if isinstance(period_figure, str):
period_figure = period_figure.strip()
if period_figure == "-":
# we accept the '-' as it is a recognised value in Finance for 0
Expand All @@ -99,7 +99,10 @@ def upload_figures(uploadmodel, data_row, year_obj, financialcode_obj, month_dic
f"Non-numeric value in {data_row[month_idx].coordinate}:{period_figure}" # noqa
)
if period_figure:
(figure_obj, created,) = uploadmodel.objects.get_or_create(
(
figure_obj,
created,
) = uploadmodel.objects.get_or_create(
financial_year=year_obj,
financial_code=financialcode_obj,
financial_period=period_obj,
Expand Down Expand Up @@ -215,6 +218,7 @@ def upload_figure_from_file(file_upload, year):
title = "Budgets"
else:
title = "Forecasts"

try:
workbook, worksheet = validate_excel_file(file_upload, title)
except UploadFileFormatError as ex:
Expand All @@ -223,8 +227,11 @@ def upload_figure_from_file(file_upload, year):
str(ex),
str(ex),
)
workbook.close()
raise ex

header_dict = xslx_header_to_dict(worksheet[1])

try:
check_header(header_dict, EXPECTED_FIGURE_HEADERS)
except UploadFileFormatError as ex:
Expand All @@ -233,19 +240,21 @@ def upload_figure_from_file(file_upload, year):
str(ex),
str(ex),
)
workbook.close
workbook.close()
raise ex

try:
upload_financial_figures(worksheet, year, header_dict, file_upload)
except (UploadFileDataError) as ex:
except UploadFileDataError as ex:
set_file_upload_fatal_error(
file_upload,
str(ex),
str(ex),
)
workbook.close
workbook.close()
raise ex
workbook.close

workbook.close()


def upload_budget_from_file(file_upload, year):
Expand Down
2 changes: 1 addition & 1 deletion forecast/management/commands/new_financial_year.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def run_command(self, message, command_name, *arg, **options):
)
self.stdout.write(self.style.ERROR(full_error_message))
raise CommandError(full_error_message)
return False

return True

def handle_user(self, *args, **options):
Expand Down
38 changes: 27 additions & 11 deletions forecast/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,7 @@ def adj_periods(self):
)

def reset_actuals(self):
self.get_queryset().filter(actual_loaded=True,).update(
actual_loaded=False,
)
self.get_queryset().filter(actual_loaded=True).update(actual_loaded=False)

def get_max_period(self):
return self.get_queryset().order_by("-financial_period_code").first()
Expand Down Expand Up @@ -574,9 +572,9 @@ def do_output_subtotal(self, current_row):
if self.output_subtotal[column]:
subtotal_row = self.subtotals[column].copy()
level = self.subtotal_columns.index(column)
subtotal_row[
self.display_total_column
] = f"Total {self.previous_values[column]}"
subtotal_row[self.display_total_column] = (
f"Total {self.previous_values[column]}"
)
show_class = TOTAL_CLASS
for out_total in self.subtotal_columns[level + 1 :]:
subtotal_row[self.display_total_column] = (
Expand Down Expand Up @@ -671,7 +669,7 @@ def calculate_subtotal_data(
self.output_subtotal[column] = True
if subtotal_time:
self.do_output_subtotal(current_row)
for k, totals in self.subtotals.items():
for _, totals in self.subtotals.items():
self.add_row_to_subtotal(current_row, totals)
self.output_row_to_table(current_row, "")

Expand Down Expand Up @@ -701,7 +699,13 @@ def calculate_subtotal_data(
class PivotManager(models.Manager):
"""Managers returning the data in Monthly figures pivoted"""

def pivot_data(self, columns, filter_dict={}, year=0, order_list=[]):
def pivot_data(self, columns, filter_dict=None, year=0, order_list=None):
if filter_dict is None:
filter_dict = {}

if order_list is None:
order_list = []

if year == 0:
year = get_current_financial_year()

Expand All @@ -728,11 +732,17 @@ def subtotal_data(
display_total_column,
subtotal_columns,
data_columns,
filter_dict={},
filter_dict=None,
year=0,
order_list=[],
order_list=None,
show_grand_total=True,
):
if filter_dict is None:
filter_dict = {}

if order_list is None:
order_list = []

# If requesting a subtotal, the
# list of columns must be specified
if not subtotal_columns:
Expand Down Expand Up @@ -768,7 +778,13 @@ def subtotal_data(
show_grand_total,
)

def raw_data_annotated(self, columns, filter_dict={}, year=0, order_list=[]):
def raw_data_annotated(self, columns, filter_dict=None, year=0, order_list=None):
if filter_dict is None:
filter_dict = {}

if order_list is None:
order_list = []

annotations = {
budget_field: Sum("budget"),
"Apr": Sum("apr"),
Expand Down
Loading

0 comments on commit 0829354

Please sign in to comment.