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

UI improvements #129

Closed
wants to merge 11 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
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ Run the app with a debugger:
docker-compose run --rm -p 8000:8000 app
```

Load the development data:
```bash
docker-compose run --rm app python manage.py loaddata asset_dashboard/fixtures/data.json
```

### Restore the FPDCC database
Download the database from Dropbox and save the tar file in this repo's root directory.

Expand Down Expand Up @@ -105,7 +110,4 @@ docker-compose run --rm app python manage.py dumpdata \
asset_dashboard auth
```

Load the data:
```bash
docker-compose run --rm app python manage.py loaddata asset_dashboard/fixtures/data.json
```

114 changes: 112 additions & 2 deletions asset_dashboard/endpoints.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,119 @@
from django.contrib.auth.models import User
from django.db.models import Q

from rest_framework import viewsets
from rest_framework.authentication import SessionAuthentication
from rest_framework.permissions import IsAuthenticated

from asset_dashboard.models import Portfolio
from asset_dashboard.serializers import PortfolioSerializer
from asset_dashboard.models import Phase, Portfolio, PortfolioPhase, Project, \
LocalAsset, Buildings, TrailsInfo, PoiInfo, PicnicGroves
from asset_dashboard.serializers import PortfolioSerializer, UserSerializer, \
PortfolioPhaseSerializer, PhaseSerializer, ProjectSerializer, \
BuildingsSerializer, TrailsSerializer, LocalAssetWriteSerializer, LocalAssetReadSerializer, \
PointsOfInterestSerializer, PicnicGrovesSerializer, ParkingLotsSerializer


class PortfolioViewSet(viewsets.ModelViewSet):
queryset = Portfolio.objects.all()
serializer_class = PortfolioSerializer
authentication_classes = [SessionAuthentication]
permission_classes = [IsAuthenticated]


class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
authentication_classes = [SessionAuthentication]
permission_classes = [IsAuthenticated]


class PortfolioPhaseViewSet(viewsets.ModelViewSet):
queryset = PortfolioPhase.objects.all()
serializer_class = PortfolioPhaseSerializer
authentication_classes = [SessionAuthentication]
permission_classes = [IsAuthenticated]


class PhaseViewSet(viewsets.ModelViewSet):
queryset = Phase.objects.all()
serializer_class = PhaseSerializer
authentication_classes = [SessionAuthentication]
permission_classes = [IsAuthenticated]


class ProjectViewSet(viewsets.ModelViewSet):
queryset = Project.objects.all()
serializer_class = ProjectSerializer
authentication_classes = [SessionAuthentication]
permission_classes = [IsAuthenticated]


class AssetViewSet(viewsets.ModelViewSet):
queryset = Buildings.objects.all()
authentication_classes = [SessionAuthentication]
permission_classes = [IsAuthenticated]

@property
def asset_type(self):
return self.request.query_params.get('asset_type', 'buildings')

@property
def model_cls(self):
return {
'buildings': {'model': Buildings},
'trails': {'model': TrailsInfo},
'points_of_interest': {'model': PoiInfo},
'picnic_groves': {'model': PicnicGroves},
'parking_lots': {'model': PoiInfo, 'check_for_not_null': True}
}.get(self.asset_type, Buildings)

@property
def serializer_cls(self):
return {
'buildings': BuildingsSerializer,
'trails': TrailsSerializer,
'points_of_interest': PointsOfInterestSerializer,
'picnic_groves': PicnicGrovesSerializer,
'parking_lots': ParkingLotsSerializer
}.get(self.asset_type, BuildingsSerializer)

def get_serializer_class(self, *args, **kwargs):
return self.serializer_cls

def get_queryset(self, *args, **kwargs):
search_filter = Q()

if query := self.request.query_params.get('q', False):
for field, field_type in self.model_cls.get('model').Search.fields:
try:
field_type(query)
except (ValueError, TypeError):
continue
else:
search_filter |= Q(**{f'{field}__icontains': query})

if self.model_cls.get('check_for_not_null'):
for field in self.model_cls.get('model').Search.not_null_fields:
search_filter &= Q(**{f'{field}__isnull': False})

return self.model_cls.get('model').objects.filter(search_filter)


class LocalAssetViewSet(viewsets.ModelViewSet):
queryset = LocalAsset.objects.all()
authentication_classes = [SessionAuthentication]
permission_classes = [IsAuthenticated]

def get_serializer(self, *args, **kwargs):
if isinstance(kwargs.get('data', {}), list):
kwargs['many'] = True
return super().get_serializer(*args, **kwargs)

def get_serializer_class(self):
"""
Get the appropriate serializer class depending on request type.
See https://testdriven.io/blog/drf-serializers/#different-read-and-write-serializers
"""
if self.action in ['create', 'update', 'partial_update', 'destroy']:
return LocalAssetWriteSerializer
return LocalAssetReadSerializer
Loading