Skip to content

Commit

Permalink
Tracking pull request to bring release-1.22.0 to master - Sprint 32 (…
Browse files Browse the repository at this point in the history
…Franklin) (#608)
  • Loading branch information
kuanfandevops authored Mar 24, 2021
1 parent 8bf2f8f commit 8493985
Show file tree
Hide file tree
Showing 63 changed files with 1,965 additions and 552 deletions.
4 changes: 2 additions & 2 deletions backend/.s2i/bin/assemble
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ if [[ ! -z "$ENABLE_PIPENV" ]]; then
pipenv check
elif [[ -f requirements.txt ]]; then
echo "---> Installing dependencies ..."
pip install --upgrade pip
pip install -r requirements.txt
pip install -i https://$ARTIFACTORY_USER:$ARTIFACTORY_PASSWORD@artifacts.developer.gov.bc.ca/artifactory/api/pypi/pypi-remote/simple --upgrade pip
pip install -i https://$ARTIFACTORY_USER:$ARTIFACTORY_PASSWORD@artifacts.developer.gov.bc.ca/artifactory/api/pypi/pypi-remote/simple -r requirements.txt
elif [[ -f setup.py ]]; then
echo "---> Installing application ..."
python setup.py develop
Expand Down
70 changes: 70 additions & 0 deletions backend/api/fixtures/operational/0024_update_roles_permissions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from django.db import transaction

from api.management.data_script import OperationalDataScript
from api.models.permission import Permission
from api.models.role import Role
from api.models.role_permission import RolePermission
from api.authorities import REQUIRED_AUTHORITIES


class UpdateRolesPermissions(OperationalDataScript):
"""
Adds the Roles and Permissions
"""
is_revertable = False
comment = 'Adds new permission'

def check_run_preconditions(self):
return True

def update_permissions(self):
Permission.objects.get_or_create(
permission_code="VIEW_BCEID_NOTIFICATIONS",
defaults={
'name': "View BceID Notifications",
'description': "can view notification list for BCEID"
}
)

def update_signing_authority(self):
permissions_to_be_added = [
'VIEW_BCEID_NOTIFICATIONS'
]

role = Role.objects.get(
role_code="Signing Authority"
)

for permission_code in permissions_to_be_added:
permission = Permission.objects.get(permission_code=permission_code)
RolePermission.objects.get_or_create(
permission=permission,
role=role
)

def update_zeva_user(self):
permissions_to_be_added = [
'VIEW_BCEID_NOTIFICATIONS'
]

role = Role.objects.get(
role_code="ZEVA User"
)

for permission_code in permissions_to_be_added:
permission = Permission.objects.get(permission_code=permission_code)
RolePermission.objects.get_or_create(
permission=permission,
role=role
)

@transaction.atomic
def run(self):
self.update_permissions()
self.update_zeva_user()
self.update_signing_authority()

print('Updated Permissions')


script_class = UpdateRolesPermissions
55 changes: 55 additions & 0 deletions backend/api/fixtures/operational/0025_update_notifications.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from django.db import transaction

from api.management.data_script import OperationalDataScript
from api.models.notification import Notification
from api.models.permission import Permission


class UpdateNotifications(OperationalDataScript):
"""
Update notifications permission
"""
is_revertable = False
comment = 'Update notifications permission'

def check_run_preconditions(self):
return True
def update_notifications(self):
permission = Permission.objects.get(permission_code="VIEW_BCEID_NOTIFICATIONS")
notification = Notification.objects.get(notification_code="CREDIT_APPLICATION_ISSUED")
notification.permission = permission
notification.save()
notification = Notification.objects.get(notification_code="CREDIT_TRANSFER_APPROVED_PARTNER")
notification.permission = permission
notification.save()
notification = Notification.objects.get(notification_code="CREDIT_TRANSFER_RECORDED")
notification.permission = permission
notification.save()
notification = Notification.objects.get(notification_code="CREDIT_TRANSFER_REJECTED_PARTNER")
notification.permission = permission
notification.save()
notification = Notification.objects.get(notification_code="CREDIT_TRANSFER_REJECTED")
notification.permission = permission
notification.save()
notification = Notification.objects.get(notification_code="CREDIT_TRANSFER_RESCINDED_PARTNER")
notification.permission = permission
notification.save()
notification = Notification.objects.get(notification_code="CREDIT_TRANSFER_SUBMITTED")
notification.permission = permission
notification.save()
notification = Notification.objects.get(notification_code="ZEV_MODEL_REJECTED")
notification.permission = permission
notification.save()
notification = Notification.objects.get(notification_code="ZEV_MODEL_RANGE_REPORT_TEST_RESULT_REQUESTED")
notification.permission = permission
notification.save()
notification = Notification.objects.get(notification_code="ZEV_MODEL_VALIDATED")
notification.permission = permission
notification.save()

@transaction.atomic
def run(self):
self.update_notifications()


script_class = UpdateNotifications
34 changes: 34 additions & 0 deletions backend/api/fixtures/operational/0026_update_notifications.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from django.db import transaction

from api.management.data_script import OperationalDataScript
from api.models.notification import Notification
from api.models.permission import Permission


class UpdateNotifications(OperationalDataScript):
"""
Update notifications permission
"""
is_revertable = False
comment = 'Update notifications permission'

def check_run_preconditions(self):
return True
def update_notifications(self):
permission = Permission.objects.get(permission_code="EDIT_ZEV")
notification = Notification.objects.get(notification_code="ZEV_MODEL_REJECTED")
notification.permission = permission
notification.save()
notification = Notification.objects.get(notification_code="ZEV_MODEL_RANGE_REPORT_TEST_RESULT_REQUESTED")
notification.permission = permission
notification.save()
notification = Notification.objects.get(notification_code="ZEV_MODEL_VALIDATED")
notification.permission = permission
notification.save()

@transaction.atomic
def run(self):
self.update_notifications()


script_class = UpdateNotifications
140 changes: 140 additions & 0 deletions backend/api/serializers/compliance_obligation_activity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
from datetime import date
from django.db.models import Sum, Value, Q
from rest_framework import serializers
from api.serializers.credit_transaction import CreditTransactionListSerializer, \
CreditTransactionBalanceSerializer, CreditTransactionSerializer
from api.models.account_balance import AccountBalance
from api.models.vehicle import Vehicle
from api.models.sales_submission import SalesSubmission
from api.models.vehicle_statuses import VehicleDefinitionStatuses
from api.models.credit_transaction import CreditTransaction
from api.serializers.credit_transaction import CreditClassSerializer, CreditTransactionObligationActivitySerializer
from api.serializers.account_balance import AccountBalanceSerializer
from api.serializers.organization import OrganizationSerializer
from api.serializers.sales_submission import SalesSubmissionListSerializer, SalesSubmissionObligationActivitySerializer


class ComplianceObligationActivityDetailsSerializer(serializers.ModelSerializer):
"""
"""
prior_year_balance = serializers.SerializerMethodField()
report_year_balance = serializers.SerializerMethodField()
report_year_transactions = serializers.SerializerMethodField()
pending_balance = serializers.SerializerMethodField()

def retrieve_balance(self, year, credit_type):
request = self.context.get('request')
balance = AccountBalance.objects.filter(
organization=request.user.organization,
effective_date__lte=date(year, 9, 30),
credit_class_id=credit_type
).order_by('-effective_date').first()
if balance:
return balance.balance
else:
return 0

def get_prior_year_balance(self, obj, *args, **kwargs):
kwargs = self.context.get('kwargs')
report_year = int(kwargs.get('year'))
prior_year = report_year-1
prior_year_balance_a = self.retrieve_balance(prior_year, 1)
prior_year_balance_b = self.retrieve_balance(prior_year, 2)
return {'year': prior_year, 'a': prior_year_balance_a, 'b': prior_year_balance_b}

def get_report_year_balance(self, obj, *args, **kwargs):
kwargs = self.context.get('kwargs')
report_year = int(kwargs.get('year'))
report_year_balance_a = self.retrieve_balance(report_year, 1)
report_year_balance_b = self.retrieve_balance(report_year, 2)
return {'year': report_year, 'a': report_year_balance_a, 'b': report_year_balance_b}

def get_report_year_transactions(self, obj, *args, **kwargs):
request = self.context.get('request')
kwargs = self.context.get('kwargs')
report_year = int(kwargs.get('year'))
transfers_in = CreditTransaction.objects.filter(
credit_to=request.user.organization,
transaction_type__transaction_type='Credit Transfer',
transaction_timestamp__lte=date(report_year, 9, 30),
transaction_timestamp__gte=date(report_year-1, 10, 1),
).values(
'credit_class_id', 'model_year_id'
).annotate(
total_value=Sum('total_value')
).order_by(
'credit_class_id', 'model_year_id'
)

transfers_out = CreditTransaction.objects.filter(
debit_from=request.user.organization,
transaction_type__transaction_type='Credit Transfer',
transaction_timestamp__lte=date(report_year, 9, 30),
transaction_timestamp__gte=date(report_year-1, 10, 1),
).values(
'credit_class_id', 'model_year_id'
).annotate(total_value=Sum(
'total_value')
).order_by(
'credit_class_id', 'model_year_id'
)

credits_issued_sales = CreditTransaction.objects.filter(
credit_to=request.user.organization,
transaction_type__transaction_type='Validation',
transaction_timestamp__lte=date(report_year, 9, 30),
transaction_timestamp__gte=date(report_year-1, 10, 1),
).values(
'credit_class_id', 'model_year_id'
).annotate(
total_value=Sum('total_value')
).order_by(
'credit_class_id', 'model_year_id'
)

transfers_in_serializer = CreditTransactionObligationActivitySerializer(transfers_in, read_only=True, many=True)
transfers_out_serializer = CreditTransactionObligationActivitySerializer(transfers_out, read_only=True, many=True)
credit_sales_serializer = CreditTransactionObligationActivitySerializer(credits_issued_sales, read_only=True, many=True)
content = {
'transfers_in': transfers_in_serializer.data,
'transfers_out': transfers_out_serializer.data,
'credits_issued_sales': credit_sales_serializer.data
}
return content

def get_pending_balance(self, obj, *args, **kwargs):
kwargs = self.context.get('kwargs')
report_year = int(kwargs.get('year'))
request = self.context.get('request')
pending_sales_submissions = SalesSubmission.objects.filter(
organization=request.user.organization,
validation_status__in=['SUBMITTED', 'RECOMMEND_APPROVAL', 'RECOMMEND_REJECTION', 'CHECKED'],
submission_date__lte=date(report_year, 9, 30),
submission_date__gte=date(report_year-1, 10, 1),
)
totals = {}
for obj in pending_sales_submissions:
for record in obj.get_content_totals_by_vehicles():
try:
model_year = float(record['xls_model_year'])
except ValueError:
continue
vehicle = Vehicle.objects.filter(
make__iexact=record['xls_make'],
model_name=record['xls_model'],
model_year__name=int(model_year),
validation_status=VehicleDefinitionStatuses.VALIDATED,
).first()
if vehicle:
model_year_str = str(int(model_year))
if model_year_str not in totals.keys():
totals[model_year_str] = {'A': 0, 'B': 0}
totals[model_year_str][vehicle.get_credit_class()] += vehicle.get_credit_value() * record['num_vins']
return totals

class Meta:
model = CreditTransaction
fields = (
'report_year_balance', 'pending_balance',
'prior_year_balance', 'report_year_transactions'
)
24 changes: 24 additions & 0 deletions backend/api/serializers/credit_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,30 @@ class Meta:
'total_value', 'credit_class', 'model_year', 'weight_class',
)

class CreditTransactionObligationActivitySerializer(ModelSerializer):
"""
Serializer for credit transactions
"""
credit_class = SerializerMethodField()
model_year = SerializerMethodField()

def get_credit_class(self, obj):
credit_class = CreditClass.objects.get(id=obj.get('credit_class_id'))

serializer = CreditClassSerializer(credit_class, read_only=True)
return serializer.data

def get_model_year(self, obj):
model_year = ModelYear.objects.get(id=obj.get('model_year_id'))

serializer = ModelYearSerializer(model_year, read_only=True)
return serializer.data

class Meta:
model = CreditTransaction
fields = (
'total_value', 'credit_class', 'model_year',
)

class CreditTransactionSaveSerializer(ModelSerializer):
"""
Expand Down
3 changes: 1 addition & 2 deletions backend/api/serializers/model_year_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def get_obligation_credits(self, obj):
class Meta:
model = ModelYearReport
fields = (
'model_year', 'validation_status', 'ldv_sales', 'supplier_class',
'id','model_year', 'validation_status', 'ldv_sales', 'supplier_class',
'compliant', 'obligation_total', 'obligation_credits',
)

Expand Down Expand Up @@ -72,7 +72,6 @@ def create(self, validated_data):
model_year_id=model_year.id,
organization_id=organization.id,
organization_name=organization.name,
supplier_class="M",
**validated_data,
create_user=request.user.username,
update_user=request.user.username,
Expand Down
Loading

0 comments on commit 8493985

Please sign in to comment.