Skip to content
This repository has been archived by the owner on Feb 13, 2019. It is now read-only.

Commit

Permalink
Adding actual reporting views
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Sinchok committed Nov 10, 2014
1 parent c6ef945 commit b49757a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 20 deletions.
41 changes: 41 additions & 0 deletions bulbs/accounting/serializers.py
@@ -1,3 +1,7 @@
from bulbs.contributions.models import Contribution

from collections import OrderedDict
from django.utils import timezone
from rest_framework import serializers

from .models import AccountingRule
Expand All @@ -8,3 +12,40 @@ class AccountingRuleSerializer(serializers.ModelSerializer):
class Meta:
model = AccountingRule


class AccountingReportSerializer(serializers.ModelSerializer):

user = serializers.SerializerMethodField("get_user")
content = serializers.SerializerMethodField("get_content")
role = serializers.SerializerMethodField("get_role")
amount = serializers.SerializerMethodField("get_amount")

class Meta:
model = Contribution
fields = ("id", "content", "user", "role", "amount")

def get_amount(self, obj):
rule = AccountingRule.objects.match(obj)
if rule:
return rule.amount
return None

def get_content(self, obj):
return OrderedDict([
("id", obj.content.id),
("title", obj.content.title),
("url", obj.content.get_absolute_url()),
("content_type", obj.content.__class__.__name__),
("feature_type", obj.content.feature_type),
("published", timezone.localtime(obj.content.published))
])

def get_user(self, obj):
return {
"id": obj.contributor.id,
"username": obj.contributor.username,
"full_name": obj.contributor.get_full_name(),
}

def get_role(self, obj):
return obj.role.name
37 changes: 18 additions & 19 deletions bulbs/accounting/views.py
Expand Up @@ -5,25 +5,30 @@
from django.utils import timezone, dateparse

from rest_framework import viewsets, routers
from rest_framework.settings import api_settings
from rest_framework_csv.renderers import CSVRenderer

from bulbs.api.permissions import IsAdminUser
from bulbs.api.mixins import UncachedResponse
from bulbs.content.models import Content
from bulbs.contributions.models import Contribution

from .models import AccountingRule, AccountingOverride
from .serializers import AccountingRuleSerializer
from .serializers import AccountingRuleSerializer, AccountingReportSerializer


class AccountingRuleViewSet(UncachedResponse, viewsets.ModelViewSet):
model = AccountingRule
serializer_class = AccountingRuleSerializer

permission_classes = [IsAdminUser]
# permission_classes = [IsAdminUser]


class AccountingReportViewSet(UncachedResponse, viewsets.GenericViewSet):
class AccountingReportViewSet(UncachedResponse, viewsets.ModelViewSet):

def list(self, request, *args, **kwargs):
renderer_classes = (CSVRenderer, ) + tuple(api_settings.DEFAULT_RENDERER_CLASSES)
serializer_class = AccountingReportSerializer

def get_queryset(self):
now = timezone.now()

start_date = datetime.datetime(
Expand All @@ -40,22 +45,16 @@ def list(self, request, *args, **kwargs):

content = Content.objects.filter(published__range=(start_date, end_date))
content_ids = content.values_list("pk", flat=True)
contributions = Contribution.objects.filter(content__in=content_ids)

rules = AccountingRule.objects.all()
contributions = Contribution.objects.filter(content__in=content_ids).select_related("content")
contribution_ids = contributions.values_list("pk", flat=True)
overrides = AccountingOverride.objects.filter(contribution__in=contribution_ids)

data = []
for contribution in contributions:
for rule in rules:
if rule.matches(content)
data.append({
"author": contribution.contributor,
"content": contribution.content,
"amount":
})
ordering = self.request.GET.get("ordering", "content")
order_options = {
"content": "content__published",
"user": "contributor__id"
}
return contributions.order_by(order_options[ordering])


api_v1_router = routers.DefaultRouter()
api_v1_router.register(r"rule", AccountingRuleViewSet, base_name="rule")
api_v1_router.register(r"accountingreport", AccountingReportViewSet, base_name="accountingreport")
5 changes: 5 additions & 0 deletions bulbs/api/urls.py
Expand Up @@ -19,3 +19,8 @@
urlpatterns += (
url(r"^contributions/", include('bulbs.contributions.urls')),
)

if "bulbs.accounting" in settings.INSTALLED_APPS:
urlpatterns += (
url(r"^accounting/", include('bulbs.accounting.urls')),
)
1 change: 0 additions & 1 deletion tests/contributions/test_contributions_reporting.py
Expand Up @@ -9,7 +9,6 @@
from bulbs.contributions.models import Contribution, ContributorRole

from tests.utils import BaseAPITestCase, make_content
from tests.testcontent.models import TestContentObj

from django.contrib.auth.models import User

Expand Down

0 comments on commit b49757a

Please sign in to comment.