Verified Commit 501d2c09 authored by Agate's avatar Agate 💬

Added filter and stats endpoint in API

parent 62a99d46
......@@ -3,3 +3,60 @@
!.env
!.pylintrc
*.pyc
### OSX ###
.DS_Store
.AppleDouble
.LSOverride
### SublimeText ###
# cache files for sublime text
*.tmlanguage.cache
*.tmPreferences.cache
*.stTheme.cache
# workspace files are user-specific
*.sublime-workspace
# project files should be checked into the repository, unless a significant
# proportion of contributors will probably not be using SublimeText
# *.sublime-project
# sftp configuration file
sftp-config.json
# Basics
*.py[cod]
__pycache__
# Logs
*.log
api/pip-log.txt
# Unit test / coverage reports
.coverage
.tox
nosetests.xml
htmlcov
# Translations
*.mo
*.pot
# Pycharm
.idea
# Vim
*~
*.swp
*.swo
# npm
front/node_modules/
# Compass
.sass-cache
# virtual environments
.env
......@@ -84,6 +84,7 @@ THIRD_PARTY_APPS = [
"allauth.socialaccount",
"rest_framework",
"corsheaders",
"django_filters",
]
LOCAL_APPS = [
"contributions.users.apps.UsersAppConfig",
......
from django_filters import rest_framework as filters
from . import models
class ContributionFilter(filters.FilterSet):
class Meta:
model = models.Contribution
fields = ["type", "contributor"]
from rest_framework import mixins, viewsets
from rest_framework import mixins, response, viewsets
from rest_framework.decorators import action
from django.db.models import Count
from django_filters import rest_framework as rest_framework_filters
from . import models, serializers
from . import filters, models, serializers
def get_stats(queryset):
by_types = (
models.Contribution.objects.all()
.values("type")
.annotate(total=Count("type"))
.order_by("total")
)
stats = {"types": {d["type"]: d["total"] for d in by_types}}
return stats
class ContributionViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
......@@ -15,3 +30,10 @@ class ContributionViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
)
permission_classes = []
authentication_classes = []
filter_backends = (rest_framework_filters.DjangoFilterBackend,)
filterset_class = filters.ContributionFilter
@action(detail=False, methods=["get"])
def stats(self, request, *args, **kwargs):
stats = get_stats(self.get_queryset())
return response.Response(stats)
......@@ -15,3 +15,4 @@ psycopg2==2.7.4 --no-binary psycopg2 # https://github.com/psycopg/psycopg2
requests
pendulum
django-cors-headers
django-filter
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment