From a534f28b1308e6f87da6489beb714b4829efc0dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C4=83t=C4=83lin=20Jitea?= Date: Thu, 20 Jun 2019 15:10:59 +0300 Subject: [PATCH 1/3] [refs #106035] Split cars and vans groups --- notifications/__init__.py | 4 +++- notifications/fixtures/companiesgroups.json | 16 +++++++++---- notifications/management/commands/fetch.py | 7 ++++-- .../management/commands/fetch_bdr.py | 22 ++++++++++-------- .../management/commands/fetch_ecr.py | 1 - notifications/managers.py | 7 +++++- notifications/models.py | 9 ++++---- .../templates/notifications/companies.html | 23 +++++++++++++++---- notifications/views/actions.py | 10 ++++---- notifications/views/base.py | 4 ++-- 10 files changed, 69 insertions(+), 34 deletions(-) diff --git a/notifications/__init__.py b/notifications/__init__.py index 881dc08..3d78aef 100644 --- a/notifications/__init__.py +++ b/notifications/__init__.py @@ -6,7 +6,9 @@ ODS_GROUP_CODE = 'ods' ECR_GROUP_CODES = [ODS_GROUP_CODE, FGASES_EU_GROUP_CODE, FGASES_NONEU_GROUP_CODE] -BDR_GROUP_CODE = 'cars-vans' +CARS_GROUP_CODE = 'cars' +VANS_GROUP_CODE = 'vans' +BDR_GROUP_CODES = [CARS_GROUP_CODE, VANS_GROUP_CODE] ACCEPTED_PARAMS = { 'EXTERNAL_ID': 'company.external_id', diff --git a/notifications/fixtures/companiesgroups.json b/notifications/fixtures/companiesgroups.json index 4c45feb..924a630 100644 --- a/notifications/fixtures/companiesgroups.json +++ b/notifications/fixtures/companiesgroups.json @@ -3,13 +3,21 @@ "model": "notifications.companiesgroup", "pk": 1, "fields": { - "title": "Cars/Vans", - "code": "cars-vans" + "title": "Cars", + "code": "cars" } }, { "model": "notifications.companiesgroup", "pk": 2, + "fields": { + "title": "Vans", + "code": "vans" + } + }, + { + "model": "notifications.companiesgroup", + "pk": 3, "fields": { "title": "F-gases EU", "code": "f-gases-eu" @@ -17,7 +25,7 @@ }, { "model": "notifications.companiesgroup", - "pk": 3, + "pk": 4, "fields": { "title": "F-gases NONEU", "code": "f-gases-noneu" @@ -25,7 +33,7 @@ }, { "model": "notifications.companiesgroup", - "pk": 4, + "pk": 5, "fields": { "title": "ODS", "code": "ods" diff --git a/notifications/management/commands/fetch.py b/notifications/management/commands/fetch.py index 9b7af5c..e558ecb 100644 --- a/notifications/management/commands/fetch.py +++ b/notifications/management/commands/fetch.py @@ -38,6 +38,7 @@ def cleanup(self, code): def create_company(self, **kwargs): """ Create or update a company. """ + name = kwargs['name'] external_id = kwargs['external_id'] @@ -106,8 +107,10 @@ def get_group(self, company): def fetch_companies(self, registry): company_count = 0 for item in registry.get_companies(): - self.create_company(**self.parse_company_data(item)) - company_count += 1 + parsed_company_data = self.parse_company_data(item) + if parsed_company_data: + self.create_company(**parsed_company_data) + company_count += 1 return company_count def fetch_persons(self, registry): diff --git a/notifications/management/commands/fetch_bdr.py b/notifications/management/commands/fetch_bdr.py index 5e22678..de62c7f 100644 --- a/notifications/management/commands/fetch_bdr.py +++ b/notifications/management/commands/fetch_bdr.py @@ -3,7 +3,7 @@ from django.core.management.base import BaseCommand from django.db import IntegrityError -from notifications import BDR_GROUP_CODE +from notifications import BDR_GROUP_CODES from notifications.management.commands.fetch import BaseFetchCommand from notifications.models import CompaniesGroup, Company from notifications.registries import BDRRegistry @@ -23,19 +23,21 @@ class Command(BaseFetchCommand, BaseCommand): def __init__(self): super(Command, self).__init__() - self.group = CompaniesGroup.objects.get(code=BDR_GROUP_CODE) def get_group(self, company): - return self.group + return CompaniesGroup.objects.get(code=company['obligation']) def parse_company_data(self, company): - return dict( - external_id=company['userid'], - name=company['name'], - vat=company['vat_number'], - country=company['country_name'], - group=self.get_group(company) - ) + if company['obligation'] in BDR_GROUP_CODES: + return dict( + external_id=company['userid'], + name=company['name'], + vat=company['vat_number'], + country=company['country_name'], + group=self.get_group(company) + ) + else: + return dict() def parse_person_data(self, person): for key in person.keys(): diff --git a/notifications/management/commands/fetch_ecr.py b/notifications/management/commands/fetch_ecr.py index 6ec5596..c92fe78 100644 --- a/notifications/management/commands/fetch_ecr.py +++ b/notifications/management/commands/fetch_ecr.py @@ -39,7 +39,6 @@ def get_group(self, company): elif company['address']['country']['type'] == FGASES_NONEU: return self.group_noneu - def parse_company_data(self, company): representative_name = '' representative_vat = '' diff --git a/notifications/managers.py b/notifications/managers.py index f93f5cd..df4e952 100644 --- a/notifications/managers.py +++ b/notifications/managers.py @@ -4,11 +4,16 @@ from bdr.settings import ECR_ACCEPTED_COMPANIES_STATUS + class GetOrNoneManager(models.Manager): """ Adds get_or_none method to objects. """ def get_queryset(self): - return QuerySet(self.model).filter(Q(group_id=1) | Q(status__in=ECR_ACCEPTED_COMPANIES_STATUS)) + return QuerySet(self.model).filter( + Q(group__code='cars') + | Q(group__code='vans') + | Q(status__in=ECR_ACCEPTED_COMPANIES_STATUS) + ) def really_all(self): return QuerySet(self.model).all() diff --git a/notifications/models.py b/notifications/models.py index 4a1a760..c86c023 100644 --- a/notifications/models.py +++ b/notifications/models.py @@ -46,10 +46,11 @@ def __unicode__(self): class CompaniesGroup(models.Model): """ Base class for the 3 groups of companies: - 1 - Cars/Vans - 2 - F-gases EU - 3 - F-gases NONEU - 4 - ODS + 1 - Cars + 2 - Vans + 3 - F-gases EU + 4 - F-gases NONEU + 5 - ODS """ title = models.CharField(max_length=256) code = models.SlugField(max_length=100, unique=True) diff --git a/notifications/templates/notifications/companies.html b/notifications/templates/notifications/companies.html index 2297240..4dd55dc 100644 --- a/notifications/templates/notifications/companies.html +++ b/notifications/templates/notifications/companies.html @@ -64,7 +64,8 @@

{{ group.title }} obligation