Skip to content

Commit

Permalink
Merge pull request #318 from uktrade/release/kachasu
Browse files Browse the repository at this point in the history
Kachasu Release
  • Loading branch information
canni authored Jul 20, 2017
2 parents 59b2744 + 0e2ab12 commit 935a4e9
Show file tree
Hide file tree
Showing 73 changed files with 1,670 additions and 1,148 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ nosetests.xml
coverage.xml
*,cover
.hypothesis/
/codecov.sh

# Translations
*.mo
Expand Down
43 changes: 35 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,32 @@ Leeloo uses Docker compose to setup and run all the necessary components. The do
docker-compose run leeloo python manage.py loaddata /app/fixtures/datahub_businesstypes.yaml
docker-compose run leeloo python manage.py createinitialrevisions
```
4. Optionally, you can load some test data and update elasticsearch:

4. Create a superuser:
```shell
docker-compose run leeloo python manage.py loaddata /app/fixtures/test_data.yaml
docker-compose run leeloo python manage.py sync_es
```

5. Create a superuser:

```shell
docker-compose run leeloo python manage.py createsuperuser
```

5. Run the services:
6. Run the services:

```shell
docker-compose up
```

6. To set up the [data hub frontend app](https://github.com/uktrade/data-hub-fe-beta2), log into the [django admin](http://localhost:8000/admin/oauth2_provider/application/) and add a new oauth application with:
7. To set up the [data hub frontend app](https://github.com/uktrade/data-hub-frontend), log into the [django admin](http://localhost:8000/admin/oauth2_provider/application/) and add a new oauth application with:

- Client type: Confidential
- Authorization grant type: Resource owner password-based

7. Add the client id / client secret to the frontend .env file
8. Add the client id / client secret to the frontend .env file

Local development with Docker
-----------------------------
Expand Down Expand Up @@ -117,7 +124,13 @@ Dependencies:
create database datahub;
```

8. Configure and populate the db:
8. Make sure you have elasticsearch running locally. If you don't, you can run one in docker:
```shell
docker run -p 9200:9200 -e "http.host=0.0.0.0" -e "transport.host=127.0.0.1" elasticsearch:2.3
```
9. Configure and populate the db:
```shell
./manage.py migrate
Expand All @@ -128,18 +141,26 @@ Dependencies:
./manage.py createinitialrevisions
```
9. Start the server:
10. Optionally, you can load some test data and update elasticsearch:
```shell
./manage.py loaddata /app/fixtures/test_data.yaml
./manage.py sync_es
```
11. Start the server:
```shell
./manage.py runserver
```
10. To set up the [data hub frontend app](https://github.com/uktrade/data-hub-fe-beta2), log into the [django admin](http://localhost:8000/admin/oauth2_provider/application/) and add a new oauth application with:
12. To set up the [data hub frontend app](https://github.com/uktrade/data-hub-frontend), log into the [django admin](http://localhost:8000/admin/oauth2_provider/application/) and add a new oauth application with:
- Client type: Confidential
- Authorization grant type: Resource owner password-based
11. Add the client id / client secret to the frontend .env file
13. Add the client id / client secret to the frontend .env file
Local development (without Docker)
----------------------------------
Expand Down Expand Up @@ -209,6 +230,12 @@ docker-compose run leeloo python manage.py loaddata /app/fixtures/metadata.yaml
docker-compose run leeloo python manage.py loaddata /app/fixtures/datahub_businesstypes.yaml
```
Update Elasticsearch:
```shell
docker-compose run leeloo python manage.py sync_es
```
Dependencies
============
Expand Down
8 changes: 5 additions & 3 deletions config/api_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
from django.conf.urls import include, url
from rest_framework import routers

from datahub.company import views as company_views
from datahub.company import urls as company_urls
from datahub.investment import urls as investment_urls
from datahub.company import views as company_views
from datahub.interaction import views as interaction_views
from datahub.investment import urls as investment_urls
from datahub.leads import urls as leads_urls
from datahub.omis import urls as omis_urls
from datahub.search import urls as search_urls
from datahub.v2.urls import urlpatterns as v2_urlpatterns

Expand Down Expand Up @@ -36,5 +37,6 @@
url(r'^', include((company_urls.contact_urls, 'contact'), namespace='contact')),
url(r'^', include((company_urls.company_urls, 'company'), namespace='company')),
url(r'^', include((company_urls.ch_company_urls, 'ch-company'), namespace='ch-company')),
url(r'^', include((search_urls, 'search'), namespace='search'))
url(r'^', include((search_urls, 'search'), namespace='search')),
url(r'^omis/', include((omis_urls, 'omis'), namespace='omis'))
]
1 change: 1 addition & 0 deletions config/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
'datahub.search.apps.SearchConfig',
'datahub.user',
'datahub.korben',
'datahub.omis.order'
]

INSTALLED_APPS = DJANGO_APPS + THIRD_PARTY_APPS + LOCAL_APPS
Expand Down
10 changes: 7 additions & 3 deletions datahub/company/management/commands/sync_ch.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ def filter_irrelevant_ch_columns(row):

@contextmanager
def open_ch_zipped_csv(fp):
"""Enclose all the complicated logic of on-the-fly unzip->csv read in a nice context manager."""
"""Enclose all the complicated logic of on-the-fly unzip->csv read in a nice context manager.
"""
with zipfile.ZipFile(fp) as zf:
# get the first file from zip, assuming it's the only one from CH
csv_name = zf.filelist[0].filename
Expand All @@ -86,9 +87,12 @@ def iter_ch_csv_from_url(url, tmp_file_creator):
def sync_ch(tmp_file_creator, endpoint=None, truncate_first=False):
"""Do the sync.
We are batching the records instead of letting bulk_create doing it because Django casts the objects into a list
We are batching the records instead of letting bulk_create doing it because Django casts
the objects into a list:
https://github.com/django/django/blob/master/django/db/models/query.py#L420
this would create a list with millions of objects, that will try to be saved in batches in a single transaction
This would create a list with millions of objects, that will try to be saved in batches
in a single transaction.
"""
logger.info('Starting CH load...')
count = 0
Expand Down
16 changes: 11 additions & 5 deletions datahub/company/models/company.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ class Company(ArchivableModel, CompanyAbstract):

company_number = models.CharField(max_length=MAX_LENGTH, blank=True, null=True)
id = models.UUIDField(primary_key=True, db_index=True, default=uuid.uuid4)
alias = models.CharField(max_length=MAX_LENGTH, blank=True, null=True, help_text='Trading name')
alias = models.CharField(
max_length=MAX_LENGTH, blank=True, null=True, help_text='Trading name'
)
business_type = models.ForeignKey(
metadata_models.BusinessType, blank=True, null=True,
on_delete=models.SET_NULL
Expand Down Expand Up @@ -86,7 +88,9 @@ class Company(ArchivableModel, CompanyAbstract):
related_name='company_future_interest_countries'
)
description = models.TextField(blank=True, null=True)
website = models.CharField(max_length=MAX_LENGTH, validators=[RelaxedURLValidator], blank=True, null=True)
website = models.CharField(
max_length=MAX_LENGTH, validators=[RelaxedURLValidator], blank=True, null=True
)
uk_region = models.ForeignKey(
metadata_models.UKRegion, blank=True, null=True,
on_delete=models.SET_NULL
Expand Down Expand Up @@ -158,15 +162,17 @@ def _validate_trading_address(self):
self.trading_address_postcode,
self.trading_address_country
))
all_required_trading_address_fields = all(getattr(self, field)
for field in self.REQUIRED_TRADING_ADDRESS_FIELDS)
all_required_trading_address_fields = all(
getattr(self, field) for field in self.REQUIRED_TRADING_ADDRESS_FIELDS
)
if any_trading_address_fields and not all_required_trading_address_fields:
return False
return True

def _generate_trading_address_errors(self):
"""Generate per field error."""
empty_fields = [field for field in self.REQUIRED_TRADING_ADDRESS_FIELDS if not getattr(self, field)]
empty_fields = [field for field in self.REQUIRED_TRADING_ADDRESS_FIELDS
if not getattr(self, field)]
return {field: ['This field may not be null.'] for field in empty_fields}

def _validate_uk_region(self):
Expand Down
13 changes: 9 additions & 4 deletions datahub/company/models/contact.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ def __str__(self):

def _generate_address_errors(self):
"""Generate per field error."""
empty_fields = [field for field in self.REQUIRED_ADDRESS_FIELDS if not getattr(self, field)]
empty_fields = [field for field in self.REQUIRED_ADDRESS_FIELDS
if not getattr(self, field)]
return {field: ['This field may not be null.'] for field in empty_fields}

def validate_contact_preferences(self):
Expand All @@ -92,15 +93,19 @@ def validate_address(self):
self.address_postcode,
self.address_country
))
all_required_fields_existence = all(getattr(self, field) for field in self.REQUIRED_ADDRESS_FIELDS)
all_required_fields_existence = all(
getattr(self, field) for field in self.REQUIRED_ADDRESS_FIELDS
)
if self.address_same_as_company and some_address_fields_existence:
error_message = 'Please select either address_same_as_company or enter an address manually, not both!'
error_message = ('Please select either address_same_as_company or enter an address '
'manually, not both!')
raise ValidationError({'address_same_as_company': error_message})
if not self.address_same_as_company:
if some_address_fields_existence and not all_required_fields_existence:
raise ValidationError(self._generate_address_errors())
elif not some_address_fields_existence:
error_message = 'Please select either address_same_as_company or enter an address manually.'
error_message = ('Please select either address_same_as_company or enter an '
'address manually.')
raise ValidationError({'address_same_as_company': error_message})

def clean(self):
Expand Down
4 changes: 2 additions & 2 deletions datahub/company/test/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ class ContactFactory(factory.django.DjangoModelFactory):

id = factory.Sequence(lambda _: str(uuid.uuid4()))
title_id = constants.Title.wing_commander.value.id
first_name = factory.Sequence(lambda n: 'name {n}')
last_name = factory.Sequence(lambda n: 'surname {n}')
first_name = factory.Sequence(lambda n: f'name {n}')
last_name = factory.Sequence(lambda n: f'surname {n}')
company = factory.SubFactory(CompanyFactory)
email = '[email protected]'
primary = True
Expand Down
4 changes: 2 additions & 2 deletions datahub/company/test/test_advisor_views.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from rest_framework import status
from rest_framework.reverse import reverse

from datahub.core.test_utils import LeelooTestCase
from datahub.core.test_utils import APITestMixin
from .factories import AdviserFactory


class AdviserTestCase(LeelooTestCase):
class TestAdviser(APITestMixin):
"""Adviser test case."""

def test_adviser_list_view(self):
Expand Down
Loading

0 comments on commit 935a4e9

Please sign in to comment.