Skip to content

Commit

Permalink
Merge pull request #590 from uktrade/release/zhumir
Browse files Browse the repository at this point in the history
Release Zhumir
  • Loading branch information
marcofucci authored Oct 31, 2017
2 parents ace1cd7 + e6e726c commit a1cf43f
Show file tree
Hide file tree
Showing 26 changed files with 602 additions and 91 deletions.
13 changes: 5 additions & 8 deletions datahub/event/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ class EventSerializer(serializers.ModelSerializer):

default_error_messages = {
'lead_team_not_in_teams': ugettext_lazy('Lead team must be in teams array.'),
'end_date_without_start_date': ugettext_lazy('Cannot have an end date without a start '
'date.'),
'end_date_before_start_date': ugettext_lazy('End date cannot be before start date.'),
'uk_region_non_uk_country': ugettext_lazy('Cannot specify a UK region for a non-UK '
'country.')
}

end_date = serializers.DateField()
event_type = NestedRelatedField('event.EventType')
location_type = NestedRelatedField('event.LocationType', required=False, allow_null=True)
organiser = NestedAdviserField(required=False, allow_null=True)
Expand All @@ -31,6 +29,7 @@ class EventSerializer(serializers.ModelSerializer):
'event.Programme', many=True, required=False, allow_empty=True
)
service = NestedRelatedField('metadata.Service')
start_date = serializers.DateField()

def validate(self, data):
"""Performs cross-field validation."""
Expand Down Expand Up @@ -63,14 +62,12 @@ def _validate_lead_team(self, combiner):

def _validate_dates(self, combiner):
errors = {}

start_date = combiner.get_value('start_date')
end_date = combiner.get_value('end_date')

if end_date:
if not start_date:
errors['end_date'] = self.error_messages['end_date_without_start_date']
elif end_date < start_date:
errors['end_date'] = self.error_messages['end_date_before_start_date']
if start_date and end_date and end_date < start_date:
errors['end_date'] = self.error_messages['end_date_before_start_date']

return errors

Expand Down
1 change: 1 addition & 0 deletions datahub/event/test/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class EventFactory(factory.django.DjangoModelFactory):
name = factory.Faker('text')
event_type_id = EventType.seminar.value.id
start_date = factory.Faker('date')
end_date = factory.LazyAttribute(lambda event: event.start_date)
location_type_id = LocationType.hq.value.id
address_1 = factory.Faker('text')
address_2 = factory.Faker('text')
Expand Down
35 changes: 32 additions & 3 deletions datahub/event/test/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,16 @@ class TestCreateEventView(APITestMixin):
def test_create_minimal_success(self):
"""Tests successfully creating an event with only the required fields."""
url = reverse('api-v3:event:collection')

request_data = {
'name': 'Grand exhibition',
'event_type': EventType.seminar.value.id,
'address_1': 'Grand Court Exhibition Centre',
'address_town': 'New York',
'address_country': Country.united_states.value.id,
'service': Service.trade_enquiry.value.id,
'start_date': '2010-09-12',
'end_date': '2010-09-12',
}
response = self.api_client.post(url, format='json', data=request_data)

Expand All @@ -119,8 +122,8 @@ def test_create_minimal_success(self):
'id': EventType.seminar.value.id,
'name': EventType.seminar.value.name,
},
'start_date': None,
'end_date': None,
'start_date': '2010-09-12',
'end_date': '2010-09-12',
'location_type': None,
'notes': '',
'address_1': 'Grand Court Exhibition Centre',
Expand Down Expand Up @@ -230,6 +233,7 @@ def test_create_lead_team_not_in_teams(self):
url = reverse('api-v3:event:collection')
request_data = {
'name': 'Grand exhibition',
'end_date': '2010-09-12',
'event_type': EventType.seminar.value.id,
'address_1': 'Grand Court Exhibition Centre',
'address_town': 'London',
Expand All @@ -238,6 +242,7 @@ def test_create_lead_team_not_in_teams(self):
'lead_team': Team.crm.value.id,
'teams': [Team.healthcare_uk.value.id],
'service': Service.trade_enquiry.value.id,
'start_date': '2010-09-12',
}
response = self.api_client.post(url, format='json', data=request_data)

Expand All @@ -252,11 +257,13 @@ def test_create_uk_no_uk_region(self):
url = reverse('api-v3:event:collection')
request_data = {
'name': 'Grand exhibition',
'end_date': '2010-09-12',
'event_type': EventType.seminar.value.id,
'address_1': 'Grand Court Exhibition Centre',
'address_town': 'London',
'address_country': Country.united_kingdom.value.id,
'service': Service.trade_enquiry.value.id,
'start_date': '2010-09-12',
}
response = self.api_client.post(url, format='json', data=request_data)

Expand All @@ -272,11 +279,13 @@ def test_create_non_uk_with_uk_region(self):
request_data = {
'name': 'Grand exhibition',
'event_type': EventType.seminar.value.id,
'end_date': '2010-09-12',
'address_1': 'Grand Court Exhibition Centre',
'address_town': 'London',
'address_country': Country.united_states.value.id,
'uk_region': UKRegion.east_of_england.value.id,
'service': Service.trade_enquiry.value.id,
'start_date': '2010-09-12',
}
response = self.api_client.post(url, format='json', data=request_data)

Expand Down Expand Up @@ -304,7 +313,7 @@ def test_create_end_date_without_start_date(self):
assert response.status_code == status.HTTP_400_BAD_REQUEST
response_data = response.json()
assert response_data == {
'end_date': ['Cannot have an end date without a start date.']
'start_date': ['This field is required.']
}

def test_create_end_date_before_start_date(self):
Expand Down Expand Up @@ -341,9 +350,11 @@ def test_create_omitted_failure(self):
'address_1': ['This field is required.'],
'address_country': ['This field is required.'],
'address_town': ['This field is required.'],
'end_date': ['This field is required.'],
'event_type': ['This field is required.'],
'name': ['This field is required.'],
'service': ['This field is required.'],
'start_date': ['This field is required.'],
}

def test_create_blank_failure(self):
Expand All @@ -353,9 +364,11 @@ def test_create_blank_failure(self):
'address_1': '',
'address_country': None,
'address_town': '',
'end_date': None,
'event_type': None,
'name': '',
'service': None,
'start_date': None,
}
response = self.api_client.post(url, format='json', data=request_data)

Expand All @@ -365,9 +378,11 @@ def test_create_blank_failure(self):
'address_1': ['This field may not be blank.'],
'address_country': ['This field may not be null.'],
'address_town': ['This field may not be blank.'],
'end_date': ['This field may not be null.'],
'event_type': ['This field may not be null.'],
'name': ['This field may not be blank.'],
'service': ['This field may not be null.'],
'start_date': ['This field may not be null.'],
}


Expand Down Expand Up @@ -470,6 +485,20 @@ def test_patch_lead_team_success(self):
response_data = _get_canonical_response_data(response)
assert response_data['lead_team']['id'] == Team.healthcare_uk.value.id

def test_patch_null_end_date_failure(self):
"""Test updating an event's end date with null."""
event = EventFactory(end_date=None)
url = reverse('api-v3:event:item', kwargs={'pk': event.pk})

request_data = {
'end_date': None,
}
response = self.api_client.patch(url, request_data, format='json')

response_data = response.json()
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert response_data['end_date'] == ['This field may not be null.']

def test_patch_lead_team_failure(self):
"""Test updating an event's lead team to an invalid team."""
event = EventFactory()
Expand Down
20 changes: 20 additions & 0 deletions datahub/omis/order/migrations/0002_order_paid_on.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2017-10-27 16:09
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('order', '0001_squashed_0030_cancellation'),
]

operations = [
migrations.AddField(
model_name='order',
name='paid_on',
field=models.DateTimeField(blank=True, null=True),
),
]
29 changes: 29 additions & 0 deletions datahub/omis/order/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ class Order(BaseModel):
related_name='+'
)

paid_on = models.DateTimeField(null=True, blank=True)

completed_on = models.DateTimeField(null=True, blank=True)
completed_by = models.ForeignKey(
settings.AUTH_USER_MODEL,
Expand Down Expand Up @@ -425,6 +427,33 @@ def mark_as_paid(self, by, payments_data):
Payment.objects.create_from_order(self, by, data)

self.status = OrderStatus.paid
self.paid_on = max(item['received_on'] for item in payments_data)
self.save()

@transaction.atomic
def complete(self, by):
"""
Complete an order.
:param by: the adviser who marked the order as complete
"""
for order_validator in [
validators.OrderInStatusValidator(
allowed_statuses=(
OrderStatus.paid,
)
)
]:
order_validator.set_instance(self)
order_validator()

for complete_validator in [validators.CompletableOrderValidator()]:
complete_validator.set_order(self)
complete_validator()

self.status = OrderStatus.complete
self.completed_on = now()
self.completed_by = by
self.save()


Expand Down
10 changes: 10 additions & 0 deletions datahub/omis/order/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class Meta:
'billing_address_postcode',
'billing_address_country',
'archived_documents_url_path',
'paid_on',
'completed_by',
'completed_on',
'cancelled_by',
Expand All @@ -115,6 +116,7 @@ class Meta:
'vat_cost',
'total_cost',
'archived_documents_url_path',
'paid_on',
'completed_by',
'completed_on',
'cancelled_by',
Expand Down Expand Up @@ -196,6 +198,13 @@ def validate(self, data):
data = self._reset_vat_fields_if_necessary(data)
return data

def complete(self):
"""Mark an order as complete."""
self.instance.complete(
by=self.context['current_user']
)
return self.instance


class PublicOrderSerializer(serializers.ModelSerializer):
"""DRF serializer for public facing API."""
Expand Down Expand Up @@ -230,6 +239,7 @@ class Meta:
'billing_address_county',
'billing_address_postcode',
'billing_address_country',
'paid_on',
'completed_on',
)
read_only_fields = fields
Expand Down
7 changes: 7 additions & 0 deletions datahub/omis/order/test/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class OrderCancelledFactory(OrderWithAcceptedQuoteFactory):
class OrderPaidFactory(OrderWithAcceptedQuoteFactory):
"""Factory for orders marked as paid."""

paid_on = factory.Faker('date_time')
status = OrderStatus.paid


Expand All @@ -144,6 +145,12 @@ class Meta:
model = 'order.OrderAssignee'


class OrderAssigneeCompleteFactory(OrderAssigneeFactory):
"""Order Assignee factory with actual time set."""

actual_time = factory.Faker('random_int', min=10, max=100)


class HourlyRateFactory(factory.django.DjangoModelFactory):
"""HourlyRate factory."""

Expand Down
Loading

0 comments on commit a1cf43f

Please sign in to comment.