Skip to content

Commit

Permalink
add pre-commit-hooks - black, isort, django-migrations-check
Browse files Browse the repository at this point in the history
  • Loading branch information
anroopak committed Mar 23, 2020
1 parent ecff77c commit cd256f5
Show file tree
Hide file tree
Showing 45 changed files with 209 additions and 397 deletions.
35 changes: 33 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,37 @@ default_stages: [commit]
fail_fast: true

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: master
hooks:
- id: trailing-whitespace
files: (^|/)a/.+\.(py|html|sh|css|js)$
- id: check-merge-conflict
name: check for merge conflict
description: Prevent accidentally commiting files with merge conflicts.
- id: end-of-file-fixer
name: fix end of files.
description: Ensures that a file is either empty, or ends with one newline.

- repo: local

- repo: https://github.com/asottile/seed-isort-config
rev: v2.1.0
hooks:
- id: seed-isort-config

- repo: https://github.com/pre-commit/mirrors-isort
rev: v4.3.21
hooks:
- id: isort

- repo: https://github.com/ambv/black
rev: stable
hooks:
- id: black
args: [--line-length=120, --safe]
language_version: python3.7

- repo: local
hooks:
- id: flake8
name: flake8
Expand All @@ -18,3 +42,10 @@ repos:
types: [python]
args: ['--config=setup.cfg']

- id: pre-commit-django-migrations
name: Check django migrations
entry: python manage.py makemigrations --check
language: system
types: [python]
pass_filenames: false
require_serial: true
1 change: 0 additions & 1 deletion CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

1 change: 0 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

1 change: 0 additions & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
web: gunicorn config.wsgi:application
release: python manage.py collectstatic --noinput && python manage.py migrate

7 changes: 1 addition & 6 deletions care/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
__version__ = "0.1.0"
__version_info__ = tuple(
[
int(num) if num.isdigit() else num
for num in __version__.replace("-", ".", 1).split(".")
]
)
__version_info__ = tuple([int(num) if num.isdigit() else num for num in __version__.replace("-", ".", 1).split(".")])
21 changes: 12 additions & 9 deletions care/facility/admin.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
from django.contrib import admin

from .models import (
Ambulance,
AmbulanceDriver,
Building,
Facility,
FacilityStaff,
FacilityCapacity,
FacilityStaff,
FacilityVolunteer,
Building,
Room,
StaffRoomAllocation,
InventoryItem,
Inventory,
InventoryItem,
InventoryLog,
AmbulanceDriver,
Ambulance)
Room,
StaffRoomAllocation,
)


class BuildingAdmin(admin.ModelAdmin):
Expand Down Expand Up @@ -59,11 +60,13 @@ class AmbulanceDriverInline(admin.TabularInline):

class AmbulanceAdmin(admin.ModelAdmin):
search_fields = ["vehicle_number"]
inlines = [AmbulanceDriverInline, ]
inlines = [
AmbulanceDriverInline,
]


class AmbulanceDriverAdmin(admin.ModelAdmin):
autocomplete_fields = ['ambulance']
autocomplete_fields = ["ambulance"]


admin.site.register(Facility, FacilityAdmin)
Expand Down
20 changes: 11 additions & 9 deletions care/facility/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
from drf_extra_fields.geo_fields import PointField
from rest_framework import serializers

from care.facility.models import FACILITY_TYPES, AmbulanceDriver
from care.facility.models import Facility, Ambulance
from care.facility.models import FACILITY_TYPES, Ambulance, AmbulanceDriver, Facility
from config.serializers import ChoiceField

User = get_user_model()

TIMESTAMP_FIELDS = ('created_date', 'modified_date', 'deleted',)
TIMESTAMP_FIELDS = (
"created_date",
"modified_date",
"deleted",
)


class FacilitySerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -40,27 +43,26 @@ class Meta:
class AmbulanceDriverSerializer(serializers.ModelSerializer):
class Meta:
model = AmbulanceDriver
exclude = TIMESTAMP_FIELDS + ('ambulance',)
exclude = TIMESTAMP_FIELDS + ("ambulance",)


class AmbulanceSerializer(serializers.ModelSerializer):
drivers = serializers.ListSerializer(
child=AmbulanceDriverSerializer())
drivers = serializers.ListSerializer(child=AmbulanceDriverSerializer())

class Meta:
model = Ambulance
exclude = TIMESTAMP_FIELDS

def create(self, validated_data):
with transaction.atomic():
drivers = validated_data.pop('drivers', [])
drivers = validated_data.pop("drivers", [])
ambulance = super(AmbulanceSerializer, self).create(validated_data)
for d in drivers:
d['ambulance'] = ambulance
d["ambulance"] = ambulance
AmbulanceDriverSerializer().create(d)
return ambulance

def update(self, instance, validated_data):
validated_data.pop('drivers', [])
validated_data.pop("drivers", [])
ambulance = super(AmbulanceSerializer, self).update(instance, validated_data)
return ambulance
26 changes: 15 additions & 11 deletions care/facility/api/views.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
from django_filters import rest_framework as filters
from rest_framework import status, serializers
from rest_framework import serializers, status
from rest_framework.decorators import action
from rest_framework.mixins import (
CreateModelMixin,
DestroyModelMixin,
ListModelMixin,
RetrieveModelMixin,
CreateModelMixin,
UpdateModelMixin,
DestroyModelMixin)
)
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.viewsets import GenericViewSet

from care.facility.api.serializers import FacilitySerializer, AmbulanceSerializer, AmbulanceDriverSerializer
from care.facility.models import Facility, Ambulance
from care.facility.api.serializers import (
AmbulanceDriverSerializer,
AmbulanceSerializer,
FacilitySerializer,
)
from care.facility.models import Ambulance, Facility


class FacilityBaseViewset(CreateModelMixin, RetrieveModelMixin,
UpdateModelMixin, DestroyModelMixin, GenericViewSet):
class FacilityBaseViewset(CreateModelMixin, RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin, GenericViewSet):
"""Base class for all endpoints related to Faclity model."""

permission_classes = (IsAuthenticated,)
Expand All @@ -42,7 +46,7 @@ def perform_update(self, serializer):


class AmbulanceFilterSet(filters.FilterSet):
vehicle_numbers = filters.BaseInFilter(field_name='vehicle_number')
vehicle_numbers = filters.BaseInFilter(field_name="vehicle_number")


class AmbulanceViewSet(FacilityBaseViewset, ListModelMixin):
Expand All @@ -51,7 +55,7 @@ class AmbulanceViewSet(FacilityBaseViewset, ListModelMixin):
filter_backends = (filters.DjangoFilterBackend,)
filterset_class = AmbulanceFilterSet

@action(methods=['POST'], detail=True)
@action(methods=["POST"], detail=True)
def add_driver(self, request):
ambulance = self.get_object()
serializer = AmbulanceDriverSerializer(data=request.data)
Expand All @@ -60,7 +64,7 @@ def add_driver(self, request):
driver = ambulance.ambulancedriver_set.create(**serializer.validated_data)
return Response(data=AmbulanceDriverSerializer(driver).data, status=status.HTTP_201_CREATED)

@action(methods=['DELETE'], detail=True)
@action(methods=["DELETE"], detail=True)
def remove_driver(self, request):
class DeleteDriverSerializer(serializers.Serializer):
driver_id = serializers.IntegerField()
Expand All @@ -75,7 +79,7 @@ def create(self, validated_data):
serializer = DeleteDriverSerializer(data=request.data)
serializer.is_valid(raise_exception=True)

driver = ambulance.ambulancedriver_set.filter(id=serializer.validated_data['driver_id']).first()
driver = ambulance.ambulancedriver_set.filter(id=serializer.validated_data["driver_id"]).first()
if not driver:
raise serializers.ValidationError({"driver_id": "Detail not found"})

Expand Down
13 changes: 8 additions & 5 deletions care/facility/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@ class FacilityCreationForm(ModelForm):
class Meta:
model = Facility
fields = ["name", "district", "address", "oxygen_capacity", "phone_number"]
labels = {"name": "Enter the name of your hospital", "district": "Pick your District",
"address": "Enter Hospital Address",
"phone_number": "Enter emergency contact number of your hospital","oxygen_capacity":"Enter the total oxygen capacity of your hospital (in litres)"}
labels = {
"name": "Enter the name of your hospital",
"district": "Pick your District",
"address": "Enter Hospital Address",
"phone_number": "Enter emergency contact number of your hospital",
"oxygen_capacity": "Enter the total oxygen capacity of your hospital (in litres)",
}


class FacilityCapacityCreationForm(ModelForm):
class Meta:
model = FacilityCapacity
fields = ["room_type", "total_capacity", "current_capacity"]
labels = {"room_type": "Bed Type", "total_capacity": "Total Capacity",
"current_capacity": "Currently Occupied"}
labels = {"room_type": "Bed Type", "total_capacity": "Total Capacity", "current_capacity": "Currently Occupied"}


class DoctorsCountCreationForm(ModelForm):
Expand Down
Loading

0 comments on commit cd256f5

Please sign in to comment.