Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add github action for black and flake8 #2074

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/workflows/black.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Black Check

on:
pull_request:
push:
branches:
- develop

jobs:
check:
name: Black Check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Install black
run: pip install black
- name: Run black
uses: rickstaa/action-black@v1
with:
black_args: ". --check"
25 changes: 25 additions & 0 deletions .github/workflows/flake8.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Flake8 check.

on:
pull_request:
push:
branches:
- develop

jobs:
check:
name: Flake8 check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

# Flake8 check
- name: Install flake8
run: pip install flake8
- name: Run flake8
uses: suo/flake8-github-action@releases/v1
with:
# NOTE: this needs to be the same as the job name
checkName: 'Flake8 check'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3 changes: 3 additions & 0 deletions api/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import csv
import time

from django.contrib.gis import admin as geoadmin
from django.contrib import admin, messages
from django.urls import reverse
Expand All @@ -11,6 +12,7 @@
from django.http import HttpResponse, HttpResponseRedirect
from django.conf import settings
from django_admin_listfilter_dropdown.filters import RelatedDropdownFilter

from rest_framework.authtoken.admin import TokenAdmin
from rest_framework.authtoken.models import Token
from reversion_compare.admin import CompareVersionAdmin
Expand Down Expand Up @@ -510,6 +512,7 @@ class RegionLinkInline(admin.TabularInline):
class CountryContactInline(admin.TabularInline):
model = models.CountryContact


class CountryKeyDocumentInline(admin.TabularInline):
model = models.CountryKeyDocument

Expand Down
19 changes: 10 additions & 9 deletions api/drf_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
Subquery,
Avg
)
from django.db.models.functions import TruncMonth, TruncYear, Coalesce
from django.db.models.functions import TruncMonth, Coalesce
from django.db.models.fields import IntegerField
from django.utils import timezone
from drf_spectacular.utils import extend_schema, extend_schema_view
Expand Down Expand Up @@ -150,7 +150,6 @@
CountryKeyDocumentFilter,
CountrySupportingPartnerFilter
)
from api.utils import bad_request
from api.visibility_class import ReadOnlyVisibilityViewsetMixin


Expand Down Expand Up @@ -271,7 +270,7 @@ def get_databank(self, request, pk):
def get_country_figure(self, request, pk):
country = self.get_object()
end_date = timezone.now()
start_date = end_date + timedelta(days=-2*365)
start_date = end_date + timedelta(days=-2 * 365)
start_date = request.GET.get("start_date", start_date)
end_date = request.GET.get("end_date", end_date)
appeal_conditions = (
Expand All @@ -280,7 +279,7 @@ def get_country_figure(self, request, pk):

all_appealhistory = AppealHistory.objects.select_related("appeal").filter(appeal__code__isnull=False)
if start_date and end_date:
all_appealhistory =all_appealhistory.filter(
all_appealhistory = all_appealhistory.filter(
start_date__lte=end_date, end_date__gte=start_date
)

Expand All @@ -304,7 +303,8 @@ def get_country_figure(self, request, pk):
),
amount_requested_without_dref=Case(When(appeal_conditions, then=F("amount_requested")), output_field=IntegerField()),
amount_requested_dref=Case(
When(Q(end_date__gte=start_date) & Q(start_date__lte=end_date), then=F("amount_requested")), output_field=IntegerField()
When(Q(end_date__gte=start_date) & Q(start_date__lte=end_date), then=F("amount_requested")),
output_field=IntegerField()
),
amount_funded_without_dref=Case(When(appeal_conditions, then=F("amount_funded")), output_field=IntegerField()),
emergencies_count=Count(F("appeal__event"), distinct=True)
Expand Down Expand Up @@ -338,7 +338,7 @@ def get_country_figure(self, request, pk):
def get_country_disaster_count(self, request, pk):
country = self.get_object()
end_date = timezone.now()
start_date = end_date + timedelta(days=-2*365)
start_date = end_date + timedelta(days=-2 * 365)
start_date = request.GET.get("start_date", start_date)
end_date = request.GET.get("end_date", end_date)

Expand Down Expand Up @@ -378,10 +378,10 @@ def get_country_disaster_count(self, request, pk):
def get_country_disaster_monthly_count(self, request, pk):
country = self.get_object()
end_date = timezone.now()
start_date = end_date + timedelta(days=-2*365)
start_date = end_date + timedelta(days=-2 * 365)
start_date = request.GET.get("start_date", start_date)
end_date = request.GET.get("end_date", end_date)
queryset = Event.objects.filter(
queryset = Event.objects.filter(
countries__in=[country.id],
dtype__isnull=False,
).annotate(
Expand Down Expand Up @@ -420,7 +420,7 @@ def get_country_disaster_monthly_count(self, request, pk):
def get_country_historical_disaster(self, request, pk):
country = self.get_object()
end_date = timezone.now()
start_date = end_date + timedelta(days=-2*365)
start_date = end_date + timedelta(days=-2 * 365)
start_date = request.GET.get("start_date", start_date)
end_date = request.GET.get("end_date", end_date)
dtype = request.GET.get("dtype", None)
Expand Down Expand Up @@ -463,6 +463,7 @@ def get_country_historical_disaster(self, request, pk):
).data
)


class CountryRMDViewset(viewsets.ReadOnlyModelViewSet):
queryset = Country.objects.filter(is_deprecated=False).filter(iso3__isnull=False).exclude(iso3="")
filterset_class = CountryFilterRMD
Expand Down
1 change: 1 addition & 0 deletions api/factories/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,6 @@ class AppealFactory(factory.django.DjangoModelFactory):
amount_funded = fuzzy.FuzzyInteger(0)
event = factory.SubFactory(EventFactory)
country = factory.SubFactory(CountryFactory)

class Meta:
model = Appeal
7 changes: 4 additions & 3 deletions api/filter_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class CountryKeyDocumentFilter(filters.FilterSet):

class Meta:
model = CountryKeyDocument
fields= ()
fields = ()


class DistrictRMDFilter(filters.FilterSet):
Expand Down Expand Up @@ -250,13 +250,13 @@ class AppealHistoryFilter(filters.FilterSet):
appeal_id = filters.NumberFilter(
field_name="appeal_id", lookup_expr="exact", help_text="Use this (or code) for appeal identification."
)
district = filters.ModelMultipleChoiceFilter (
district = filters.ModelMultipleChoiceFilter(
field_name="country__district",
queryset=District.objects.all(),
label="district",
method="get_country_district"
)
admin2 = filters.ModelMultipleChoiceFilter(
admin2 = filters.ModelMultipleChoiceFilter(
field_name="country__district__admin2",
queryset=Admin2.objects.all(),
label="admin2",
Expand Down Expand Up @@ -284,6 +284,7 @@ def get_country_admin2(self, qs, name, value):
return qs.filter(country__district__admin2=value).distinct()
return qs


class AppealDocumentFilter(filters.FilterSet):
appeal = filters.ModelMultipleChoiceFilter(
method='get_appeal_filter',
Expand Down
55 changes: 45 additions & 10 deletions api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ class Country(models.Model):
""" A country """

name = models.CharField(verbose_name=_('name'), max_length=100)
record_type = models.IntegerField(choices=CountryType.choices, verbose_name=_('type'), default=1, help_text=_('Type of entity'))
record_type = models.IntegerField(
choices=CountryType.choices,
verbose_name=_('type'), default=1,
help_text=_('Type of entity')
)
iso = models.CharField(
verbose_name=_('ISO'), max_length=2, null=True, blank=True, unique=True,
validators=[RegexValidator('^[A-Z]*$', 'ISO must be uppercase')])
Expand Down Expand Up @@ -372,6 +376,7 @@ class SupportingPartnerType(models.IntegerChoices):
def __str__(self):
return f'{self.country.name} - {self.first_name} - {self.last_name}'


class CountryKeyDocument(models.Model):
country = models.ForeignKey(
Country,
Expand All @@ -386,7 +391,7 @@ class CountryKeyDocument(models.Model):
verbose_name=_('Url'),
max_length=255
)
thumbnail= models.CharField(
thumbnail = models.CharField(
verbose_name=_('Thumbnail'),
max_length=255
)
Expand Down Expand Up @@ -894,7 +899,7 @@ def end_date(self):

def indexing(self):
countries = [c.name for c in self.countries.all()]
ns = [c.id for c in self.countries.all()]
ns = [c.id for c in self.countries.all()]
return {
'id': self.id,
'event_id': self.id,
Expand Down Expand Up @@ -1054,7 +1059,11 @@ class SituationReport(models.Model):
type = models.ForeignKey(
SituationReportType, verbose_name=_('type'), related_name='situation_reports', null=True, on_delete=models.SET_NULL
)
visibility = models.IntegerField(choices=VisibilityChoices.choices, verbose_name=_('visibility'), default=VisibilityChoices.MEMBERSHIP)
visibility = models.IntegerField(
choices=VisibilityChoices.choices,
verbose_name=_('visibility'),
default=VisibilityChoices.MEMBERSHIP
)
is_pinned = models.BooleanField(verbose_name=_('is pinned?'), default=False, help_text=_('pin this report at the top'))

class Meta:
Expand Down Expand Up @@ -1297,7 +1306,13 @@ class AppealDocument(models.Model):
appeal = models.ForeignKey(Appeal, verbose_name=_('appeal'), on_delete=models.CASCADE)
type = models.ForeignKey(AppealDocumentType, verbose_name=_('type'), null=True, on_delete=models.SET_NULL)
description = models.CharField(verbose_name=_('description'), max_length=200, null=True, blank=True)
iso = models.ForeignKey(Country, to_field="iso", db_column="iso", verbose_name=_('location'), null=True, on_delete=models.SET_NULL)
iso = models.ForeignKey(
Country, to_field="iso",
db_column="iso",
verbose_name=_('location'),
null=True,
on_delete=models.SET_NULL
)

class Meta:
verbose_name = _('appeal document')
Expand Down Expand Up @@ -1656,22 +1671,42 @@ class RecentAffected(models.IntegerChoices):
eru_logistics = models.IntegerField(choices=RequestChoices.choices, verbose_name=_('ERU logistics'), default=0)
eru_logistics_units = models.IntegerField(verbose_name=_('ERU logistics units'), null=True, blank=True)

eru_deployment_hospital = models.IntegerField(choices=RequestChoices.choices, verbose_name=_('ERU deployment hospital'), default=0)
eru_deployment_hospital = models.IntegerField(
choices=RequestChoices.choices,
verbose_name=_('ERU deployment hospital'),
default=0
)
eru_deployment_hospital_units = models.IntegerField(verbose_name=_('ERU deployment hospital units'), null=True, blank=True)

eru_referral_hospital = models.IntegerField(choices=RequestChoices.choices, verbose_name=_('ERU referral hospital'), default=0)
eru_referral_hospital = models.IntegerField(
choices=RequestChoices.choices,
verbose_name=_('ERU referral hospital'),
default=0
)
eru_referral_hospital_units = models.IntegerField(verbose_name=_('ERU referral hospital units'), null=True, blank=True)

eru_relief = models.IntegerField(choices=RequestChoices.choices, verbose_name=_('ERU relief'), default=0)
eru_relief_units = models.IntegerField(null=True, verbose_name=_('ERU relief units'), blank=True)

eru_water_sanitation_15 = models.IntegerField(choices=RequestChoices.choices, verbose_name=_('ERU water sanitation M15'), default=0)
eru_water_sanitation_15 = models.IntegerField(
choices=RequestChoices.choices,
verbose_name=_('ERU water sanitation M15'),
default=0
)
eru_water_sanitation_15_units = models.IntegerField(verbose_name=_('ERU water sanitation M15 units'), null=True, blank=True)

eru_water_sanitation_40 = models.IntegerField(choices=RequestChoices.choices, verbose_name=_('ERU water sanitation M40'), default=0)
eru_water_sanitation_40 = models.IntegerField(
choices=RequestChoices.choices,
verbose_name=_('ERU water sanitation M40'),
default=0
)
eru_water_sanitation_40_units = models.IntegerField(verbose_name=_('ERU water sanitation M40 units'), null=True, blank=True)

eru_water_sanitation_20 = models.IntegerField(choices=RequestChoices.choices, verbose_name=_('ERU water sanitation MSM20'), default=0)
eru_water_sanitation_20 = models.IntegerField(
choices=RequestChoices.choices,
verbose_name=_('ERU water sanitation MSM20'),
default=0
)
eru_water_sanitation_20_units = models.IntegerField(verbose_name=_('ERU water sanitation MSM20 units'), null=True, blank=True)

# Ugly solution to a design problem with handling Actions
Expand Down
5 changes: 3 additions & 2 deletions api/molnix_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ def get_deployments(self):
def get_position(self, id):
try:
return self.call_api(path='positions/%d' % id)
except:
except Exception as e:
print(f"Exception {e}")
return None

def get_countries(self):
Expand All @@ -98,4 +99,4 @@ def get_countries(self):

def logout(self):
self.call_api('logout')
return True
return True
8 changes: 6 additions & 2 deletions api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
# from api.utils import pdf_exporter
from api.tasks import generate_url
from drf_spectacular.utils import extend_schema_field
from dref.models import(
from dref.models import (
Dref,
DrefOperationalUpdate,
DrefFinalReport
Expand Down Expand Up @@ -598,7 +598,7 @@ class RegionRelationSerializer(ModelSerializer):
country_cluster_count = serializers.SerializerMethodField()
country_plan_count = serializers.IntegerField(read_only=True)
bbox = serializers.SerializerMethodField()
#centroid = serializers.SerializerMethodField()
# centroid = serializers.SerializerMethodField()

class Meta:
model = Region
Expand Down Expand Up @@ -632,6 +632,7 @@ def get_country_cluster_count(obj) -> int:
def get_bbox(region) -> dict:
return region.bbox and json.loads(region.bbox.geojson)


class CountryDirectorySerializer(ModelSerializer):
class Meta:
model = CountryDirectory
Expand Down Expand Up @@ -778,6 +779,7 @@ def get_country_delegation(self, country):

class CountryKeyDocumentSerializer(ModelSerializer):
country_details = MiniCountrySerializer(source='country', read_only=True)

class Meta:
model = CountryKeyDocument
fields = "__all__"
Expand Down Expand Up @@ -2296,11 +2298,13 @@ class AggregateByDtypeSerializer(serializers.Serializer):
dtype = serializers.IntegerField()
count = serializers.IntegerField()


class CountryKeyFigureInputSerializer(serializers.Serializer):
start_date = serializers.DateField(required=False)
end_date = serializers.DateField(required=False)
dtype = serializers.IntegerField(required=False)


class CountryKeyClimateInputSerializer(serializers.Serializer):
year = serializers.IntegerField(required=False)

Expand Down
Loading
Loading