Skip to content

Commit

Permalink
Tracking pull request to merge release-1.27.0 to master (#724)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuanfandevops authored Jun 17, 2021
1 parent 2a9b961 commit e6d1ee7
Show file tree
Hide file tree
Showing 78 changed files with 5,771 additions and 515 deletions.
22 changes: 22 additions & 0 deletions backend.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
DATABASE_NAME=postgres
DATABASE_USER=postgres
DATABASE_PASSWORD=postgres
DATABASE_ENGINE=postgresql
DATABASE_SERVICE_NAME=postgresql
POSTGRESQL_SERVICE_HOST=db
POSTGRESQL_SERVICE_PORT=5432
KEYCLOAK_ENABLED=True
KEYCLOAK_AUDIENCE=zeva-app
KEYCLOAK_CLIENT_ID=zeva-app
KEYCLOAK_REALM=http://keycloak:8080/auth/realms/zeva
KEYCLOAK_ISSUER=http://localhost:8888/auth/realms/zeva
KEYCLOAK_CERTS_URL=http://keycloak:8080/auth/realms/zeva/protocol/openid-connect/certs
KEYCLOAK_SA_BASEURL=http://keycloak:8080
KEYCLOAK_SA_REALM=zeva
KEYCLOAK_SA_CLIENT_ID=zeva-app-sa
KEYCLOAK_SA_CLIENT_SECRET=06dc71d6-1800-4f5d-b7b3-4c4fda226599
SMTP_SERVER_HOST=127.0.0.1
SMTP_SERVER_PORT=2500
EMAIL_SENDING_ENABLED=True
EMAIL_FROM_ADDRESS=[email protected]
DJANGO_DEBUG=True
13 changes: 6 additions & 7 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
FROM python:3.7-stretch
ENV PYTHONUNBUFFERED 1
RUN mkdir /app
WORKDIR /app
ADD requirements.txt .
FROM python:3.7.6

ENV PYTHONUNBUFFERED=1

WORKDIR /api
COPY requirements.txt /api/
RUN pip install -r requirements.txt
ADD . .
CMD python3 manage.py migrate && python3 manage.py createcachetable && python3 manage.py runserver 0.0.0.0:10102
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.0.14 on 2021-06-01 15:55

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('api', '0106_auto_20210531_1443'),
]

operations = [
migrations.AddField(
model_name='modelyearreport',
name='credit_reduction_selection',
field=models.CharField(max_length=1, null=True),
),
]
46 changes: 44 additions & 2 deletions backend/api/models/model_year_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from enumfields import EnumField

from auditable.models import Auditable
from api.models.model_year_report_adjustment import ModelYearReportAdjustment
from api.models.model_year_report_statuses import ModelYearReportStatuses
from api.models.model_year_report_make import ModelYearReportMake
from api.models.model_year_report_ldv_sales import ModelYearReportLDVSales
Expand Down Expand Up @@ -49,6 +50,12 @@ class ModelYearReport(Auditable):
statuses=[c.name for c in ModelYearReportStatuses]
)
)
credit_reduction_selection = models.CharField(
db_comment="Which ZEV credit class to use first for unspecified "
"reductions. (A or B)",
max_length=1,
null=True
)

@property
def makes(self):
Expand All @@ -62,16 +69,51 @@ def makes(self):
def ldv_sales(self):
return self.get_ldv_sales(from_gov=False)

@property
def adjustments(self):
data = ModelYearReportAdjustment.objects.filter(
model_year_report_id=self.id
)

return data

def get_avg_sales(self):
avg_sales = self.organization.get_avg_ldv_sales(
year=self.model_year.name
)

# if this is empty that means we don't have enough ldv_sales to
# get the average. our avg_sales at this point should be from the
# current report ldv_sales
if not avg_sales:
report_ldv_sales = ModelYearReportLDVSales.objects.filter(
model_year_report_id=self.id,
model_year_id=self.model_year_id
).order_by('-update_timestamp').first()

if report_ldv_sales:
avg_sales = report_ldv_sales.ldv_sales

return avg_sales

def get_ldv_sales(self, from_gov=False):
row = ModelYearReportLDVSales.objects.filter(
model_year_id=self.model_year_id,
model_year_report_id=self.id,
from_gov=from_gov
).first()

if row:
return row.ldv_sales

return None

def get_ldv_sales_with_year(self, from_gov=False):
row = ModelYearReportLDVSales.objects.filter(
model_year_id=self.model_year_id,
model_year_report_id=self.id,
from_gov=from_gov
).first()
if row:
return {'sales': row.ldv_sales, 'year': row.model_year.name}
return None

class Meta:
Expand Down
Empty file.
9 changes: 6 additions & 3 deletions backend/api/models/organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ def get_avg_ldv_sales(self, year=None):
if date.today().month < 10:
year -= 1

sales = self.ldv_sales.filter(model_year__name__lte=year).values_list(
sales = self.ldv_sales.filter(
model_year__name__lte=year
).values_list(
'ldv_sales', flat=True
)[:3]

Expand All @@ -144,7 +146,7 @@ def get_avg_ldv_sales(self, year=None):

return sum(list(sales)) / len(sales)

def get_current_class(self, year=None):
def get_current_class(self, year=None, avg_sales=None):
# The logic below means that if we're past october, the past year
# should count the current yer
if not year:
Expand All @@ -153,7 +155,8 @@ def get_current_class(self, year=None):
if date.today().month < 10:
year -= 1

avg_sales = self.get_avg_ldv_sales(year)
if not avg_sales:
avg_sales = self.get_avg_ldv_sales(year)

if not avg_sales:
avg_sales = 0
Expand Down
79 changes: 66 additions & 13 deletions backend/api/serializers/model_year_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from api.models.model_year_report_address import ModelYearReportAddress
from api.models.model_year_report_make import ModelYearReportMake
from api.models.model_year_report_statuses import ModelYearReportStatuses
from api.models.model_year_report_ldv_sales import ModelYearReportLDVSales
from api.serializers.model_year_report_ldv_sales import ModelYearReportLDVSalesSerializer
from api.models.user_profile import UserProfile
from api.serializers.model_year_report_address import \
Expand All @@ -21,13 +20,14 @@
ModelYearReportMakeSerializer
from api.serializers.model_year_report_history import \
ModelYearReportHistorySerializer
from api.serializers.model_year_report_adjustment import \
ModelYearReportAdjustmentSerializer
from api.serializers.user import MemberSerializer
from api.serializers.vehicle import ModelYearSerializer
from api.services.model_year_report import get_model_year_report_statuses


class ModelYearReportSerializer(ModelSerializer):

create_user = SerializerMethodField()
model_year = ModelYearSerializer()
model_year_report_addresses = ModelYearReportAddressSerializer(many=True)
Expand All @@ -36,9 +36,11 @@ class ModelYearReportSerializer(ModelSerializer):
model_year_report_history = ModelYearReportHistorySerializer(many=True)
confirmations = SerializerMethodField()
statuses = SerializerMethodField()
ldv_sales_updated = SerializerMethodField()
ldv_sales = SerializerMethodField()
ldv_sales_previous = SerializerMethodField()
avg_sales = SerializerMethodField()
changelog = SerializerMethodField()
adjustments = ModelYearReportAdjustmentSerializer(many=True)

def get_ldv_sales_previous(self, obj):
year = int(obj.model_year.name)
Expand All @@ -56,7 +58,8 @@ def get_ldv_sales_previous(self, obj):
def get_avg_sales(self, obj):
rows = ModelYearReportLDVSales.objects.filter(
model_year_report_id=obj.id,
from_gov=False
from_gov=False,
model_year__name__lt=obj.model_year.name
).values_list(
'ldv_sales', flat=True
)[:3]
Expand All @@ -65,6 +68,7 @@ def get_avg_sales(self, obj):
if rows.count() < 3:
row = ModelYearReportLDVSales.objects.filter(
model_year_report_id=obj.id,
model_year_id=obj.model_year_id
).first()
if row:
return row.ldv_sales
Expand All @@ -89,14 +93,31 @@ def get_confirmations(self, obj):

return confirmations

def get_ldv_sales_updated(self, obj):
def get_ldv_sales(self, obj):
request = self.context.get('request')

if request.user.is_government:
return obj.get_ldv_sales(from_gov=True) or obj.ldv_sales

return obj.ldv_sales

def get_changelog(self, obj):
request = self.context.get('request')
if request.user.is_government:
from_gov_sales = obj.get_ldv_sales_with_year(from_gov=True)
sales_changes = ''
if from_gov_sales:
not_gov_sales = obj.get_ldv_sales_with_year(from_gov=False)
sales_changes = {'from_gov': from_gov_sales['sales'], 'not_from_gov': not_gov_sales['sales'], 'year': from_gov_sales['year']}

gov_makes = ModelYearReportMake.objects.filter(
model_year_report_id=obj.id,
from_gov=True
)
gov_makes_additions_serializer = ModelYearReportMakeSerializer(gov_makes, many=True)
return {'makes_additions': gov_makes_additions_serializer.data, 'ldv_changes': sales_changes}
return obj.ldv_sales

def get_makes(self, obj):
request = self.context.get('request')

Expand All @@ -122,8 +143,8 @@ class Meta:
'organization_name', 'supplier_class', 'model_year',
'model_year_report_addresses', 'makes', 'validation_status',
'create_user', 'model_year_report_history', 'confirmations',
'statuses', 'ldv_sales_updated', 'statuses',
'ldv_sales_previous', 'avg_sales'
'statuses', 'ldv_sales', 'ldv_sales_previous', 'avg_sales',
'credit_reduction_selection', 'changelog', 'adjustments',
)


Expand Down Expand Up @@ -185,7 +206,7 @@ def create(self, validated_data):
makes = validated_data.pop('makes')
model_year = validated_data.pop('model_year')
confirmations = request.data.get('confirmations')
ldv_sales = request.user.organization.ldv_sales
ldv_sales = request.data.get('ldv_sales')

report = ModelYearReport.objects.create(
model_year_id=model_year.id,
Expand All @@ -197,11 +218,16 @@ def create(self, validated_data):
supplier_class=request.user.organization.supplier_class
)
for each in ldv_sales:
ModelYearReportLDVSales.objects.create(
model_year=each.model_year,
ldv_sales=each.ldv_sales,
model_year_report=report
)
model_year = ModelYear.objects.filter(
name=each.get('model_year')
).first()

if model_year:
ModelYearReportLDVSales.objects.create(
model_year=model_year,
ldv_sales=each.get('ldv_sales'),
model_year_report_id=report.id
)
for confirmation in confirmations:
ModelYearReportConfirmation.objects.create(
create_user=request.user.username,
Expand Down Expand Up @@ -310,6 +336,33 @@ def update(self, instance, validated_data):
other=address.other
)

ldv_sales = request.data.get('ldv_sales', None)

if 'ldv_sales' in request.data:
ModelYearReportLDVSales.objects.filter(
model_year_report_id=instance.id
).exclude(
model_year_id=instance.model_year_id
).delete()

for each in ldv_sales:
model_year = ModelYear.objects.filter(
name=each.get('model_year')
).first()

if model_year:
ModelYearReportLDVSales.objects.create(
model_year_id=model_year.id,
ldv_sales=each.get('ldv_sales'),
model_year_report_id=instance.id
)

if instance.get_avg_sales():
instance.supplier_class = request.user.organization.get_current_class(
avg_sales=instance.get_avg_sales()
)
instance.save()

for confirmation in confirmations:
ModelYearReportConfirmation.objects.update_or_create(
model_year_report=instance,
Expand Down
24 changes: 24 additions & 0 deletions backend/api/serializers/model_year_report_adjustment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from rest_framework.serializers import ModelSerializer, SlugRelatedField
from api.models.model_year import ModelYear
from api.models.credit_class import CreditClass

from api.models.model_year_report_adjustment import \
ModelYearReportAdjustment


class ModelYearReportAdjustmentSerializer(ModelSerializer):
credit_class = SlugRelatedField(
slug_field='credit_class',
queryset=CreditClass.objects.all()
)
model_year = SlugRelatedField(
slug_field='name',
queryset=ModelYear.objects.all()
)

class Meta:
model = ModelYearReportAdjustment
fields = (
'id', 'credit_class', 'model_year', 'is_reduction',
'number_of_credits',
)
Loading

0 comments on commit e6d1ee7

Please sign in to comment.