Skip to content

Commit

Permalink
Merge pull request #19 from eea/split-cars-vans
Browse files Browse the repository at this point in the history
Split cars and vans groups
  • Loading branch information
dianaboiangiu authored Jun 20, 2019
2 parents 3c158af + 74c6e79 commit 52207f5
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 36 deletions.
4 changes: 3 additions & 1 deletion notifications/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
24 changes: 16 additions & 8 deletions notifications/fixtures/companiesgroups.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
[
{
"model": "notifications.companiesgroup",
"pk": 1,
"fields": {
"title": "Cars/Vans",
"code": "cars-vans"
}
},
{
"model": "notifications.companiesgroup",
"pk": 2,
Expand All @@ -30,5 +22,21 @@
"title": "ODS",
"code": "ods"
}
},
{
"model": "notifications.companiesgroup",
"pk": 5,
"fields": {
"title": "Cars",
"code": "cars"
}
},
{
"model": "notifications.companiesgroup",
"pk": 6,
"fields": {
"title": "Vans",
"code": "vans"
}
}
]
7 changes: 5 additions & 2 deletions notifications/management/commands/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']

Expand Down Expand Up @@ -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):
Expand Down
22 changes: 12 additions & 10 deletions notifications/management/commands/fetch_bdr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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():
Expand Down
1 change: 0 additions & 1 deletion notifications/management/commands/fetch_ecr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ''
Expand Down
7 changes: 6 additions & 1 deletion notifications/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion notifications/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 - Cars
6 - Vans
"""
title = models.CharField(max_length=256)
code = models.SlugField(max_length=100, unique=True)
Expand Down
25 changes: 19 additions & 6 deletions notifications/templates/notifications/companies.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ <h2 id="{{ group.code }}">{{ group.title }} obligation</h2>

<script>
$(document).ready(function () {
var cars_table = $('#cars-vans-table').DataTable();
var cars_table = $('#cars-table').DataTable();
var vans_table = $('#vans-table').DataTable();
var ods_table = $('#ods-table').DataTable();
var fgas_eu_table = $('#f-gases-eu-table').DataTable();
var fgas_noneu_table = $('#f-gases-noneu-table').DataTable();
Expand All @@ -75,7 +76,8 @@ <h2 id="{{ group.code }}">{{ group.title }} obligation</h2>
fgas_noneu_table.search($(this).val()).draw();
});

$('#f-gases-eu').hide();
$('#vans').hide();
$('#cars').hide();
$('#f-gases-noneu').hide();
$('#ods').hide();

Expand All @@ -85,25 +87,36 @@ <h2 id="{{ group.code }}">{{ group.title }} obligation</h2>
switch (selectedOption) {
case 'F-gases EU':
$('#f-gases-eu').show();
$('#cars-vans').hide();
$('#cars').hide();
$('#vans').hide();
$('#ods').hide();
$('#f-gases-noneu').hide();
break;
case 'F-gases NONEU':
$('#f-gases-noneu').show();
$('#ods').hide();
$('#cars-vans').hide();
$('#cars').hide();
$('#vans').hide();
$('#f-gases-eu').hide();
break;
case 'ODS':
$('#ods').show();
$('#cars-vans').hide();
$('#cars').hide();
$('#vans').hide();
$('#f-gases-eu').hide();
$('#f-gases-noneu').hide();
break;
case 'Cars':
$('#ods').hide();
$('#cars').show();
$('#vans').hide();
$('#f-gases-eu').hide();
$('#f-gases-noneu').hide();
break;
default:
$('#ods').hide();
$('#cars-vans').show();
$('#vans').show();
$('#cars').hide();
$('#f-gases-eu').hide();
$('#f-gases-noneu').hide();
}
Expand Down
10 changes: 6 additions & 4 deletions notifications/views/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
FGASES_EU_GROUP_CODE,
FGASES_NONEU_GROUP_CODE,
FGASES_EU,
BDR_GROUP_CODE,
BDR_GROUP_CODES,
ODS_GROUP_CODE, FGASES_NONEU)
from notifications.models import Company, Person, CompaniesGroup
from notifications.registries import EuropeanCacheRegistry, BDRRegistry
Expand Down Expand Up @@ -177,10 +177,10 @@ def cleanup(self):
""" Delete all persons and companies fetched from BDR registry.
"""
Person.objects.filter(
company__group__code=BDR_GROUP_CODE
company__group__code__in=BDR_GROUP_CODES
).delete()
Company.objects.filter(
group__code=BDR_GROUP_CODE
group__code__in=BDR_GROUP_CODES
).delete()

def get(self, request, *args, **kwargs):
Expand All @@ -189,7 +189,9 @@ def get(self, request, *args, **kwargs):
else:
registry = BDRRegistry()

group = CompaniesGroup.objects.get(code=BDR_GROUP_CODE)
# TODO This view has not usage, so it will be deleted in the future.
# For now, use cars group.
group = CompaniesGroup.objects.get(code='cars')

# fetch companies
company_count = 0
Expand Down
4 changes: 2 additions & 2 deletions notifications/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.urls import reverse
from django.views import generic

from notifications import BDR_GROUP_CODE, ECR_GROUP_CODES
from notifications import BDR_GROUP_CODES, ECR_GROUP_CODES
from notifications.models import Cycle, CompaniesGroup, Person, Company
from notifications.views.breadcrumb import NotificationsBaseView, Breadcrumb

Expand Down Expand Up @@ -65,7 +65,7 @@ def get_bdr(self):
page_bdr = self.request.GET.get('page_bdr', 1)
return self.get_current_page(
data=Person.objects
.filter(company__group__code=BDR_GROUP_CODE)
.filter(company__group__code__in=BDR_GROUP_CODES)
.prefetch_related('company')
.order_by('name')
.distinct(),
Expand Down

0 comments on commit 52207f5

Please sign in to comment.