def test_operator_cannot_be_set(operator_api_client, operator, operator_2, new_parking_data, updated_parking_data): new_parking_data['operator'] = str(operator_2.id) create_payment_zone(id=new_parking_data['zone'], code=str(new_parking_data['zone']), number=new_parking_data['zone'], domain=EnforcementDomain.get_default_domain()) # POST response_parking_data = post(operator_api_client, list_url, new_parking_data) new_parking = Parking.objects.get(id=response_parking_data['id']) assert new_parking.operator == operator create_payment_zone(id=updated_parking_data['zone'], code=str(updated_parking_data['zone']), number=updated_parking_data['zone'], domain=EnforcementDomain.get_default_domain()) # PUT detail_url = get_detail_url(new_parking) put(operator_api_client, detail_url, updated_parking_data) new_parking.refresh_from_db() assert new_parking.operator == operator # PATCH patch(operator_api_client, detail_url, {'operator': str(operator_2.id)}) new_parking.refresh_from_db() assert new_parking.operator == operator
def test_default_enforcement_domain_is_used_on_parking_creation_if_not_specified( operator_api_client, new_parking_data): create_payment_zone(id=new_parking_data['zone'], code=str(new_parking_data['zone']), number=new_parking_data['zone'], domain=EnforcementDomain.get_default_domain()) response_parking_data = post(operator_api_client, list_url, new_parking_data) assert response_parking_data[ 'domain'] == EnforcementDomain.get_default_domain().code
def test_time_start_cannot_be_after_time_end(operator_api_client, parking, new_parking_data): create_payment_zone(id=new_parking_data['zone'], code=str(new_parking_data['zone']), number=new_parking_data['zone'], domain=EnforcementDomain.get_default_domain()) new_parking_data['time_start'] = '2116-12-10T23:33:29Z' detail_url = get_detail_url(parking) error_message = '"time_start" cannot be after "time_end".' # POST error_data = post(operator_api_client, list_url, new_parking_data, status_code=400) assert error_message in error_data['non_field_errors'] # PUT error_data = put(operator_api_client, detail_url, new_parking_data, status_code=400) assert error_message in error_data['non_field_errors'] # PATCH patch_data = {'time_start': '2116-12-10T23:33:29Z'} error_data = patch(operator_api_client, detail_url, patch_data, status_code=400) assert error_message in error_data['non_field_errors']
def _create_parking_area(self, area_dict): parking_area = ParkingArea( domain=EnforcementDomain.get_default_domain(), origin_id=area_dict['origin_id'], geom=area_dict['geom'], capacity_estimate=area_dict['capacity_estimate'], ) self.created += 1 return parking_area
def test_post_zone_as_string_or_integer(operator_api_client, new_parking_data): new_parking_data['zone'] = '2' create_payment_zone(code=str(new_parking_data['zone']), domain=EnforcementDomain.get_default_domain()) response_parking_data = post(operator_api_client, list_url, new_parking_data) assert response_parking_data['zone'] == 2 new_parking_data['zone'] = 5 create_payment_zone(code=str(new_parking_data['zone']), domain=EnforcementDomain.get_default_domain()) response_parking_data = post(operator_api_client, list_url, new_parking_data) assert response_parking_data['zone'] == 5 new_parking_data['zone'] = '5A' create_payment_zone(code=str(new_parking_data['zone']), domain=EnforcementDomain.get_default_domain()) response_parking_data = post(operator_api_client, list_url, new_parking_data) assert response_parking_data['zone'] == '5A'
def test_post_parking_optional_fields_null(operator_api_client, new_parking_data): new_parking_data['time_end'] = None new_parking_data['location'] = None create_payment_zone(code=str(new_parking_data['zone']), number=new_parking_data['zone'], domain=EnforcementDomain.get_default_domain()) response_parking_data = post(operator_api_client, list_url, new_parking_data) check_response_parking_data(new_parking_data, response_parking_data) new_parking = Parking.objects.get(id=response_parking_data['id']) check_parking_data_matches_parking_object(new_parking_data, new_parking)
def test_put_parking(operator_api_client, parking, updated_parking_data): detail_url = get_detail_url(parking) create_payment_zone(code='2', number=2, domain=EnforcementDomain.get_default_domain()) response_parking_data = put(operator_api_client, detail_url, updated_parking_data) # check data in the response check_response_parking_data(updated_parking_data, response_parking_data) # check the actual object parking.refresh_from_db() check_parking_data_matches_parking_object(updated_parking_data, parking)
def _save_payment_zones(self, payment_zone_dicts): logger.info('Saving payment zones.') count = 0 payment_zone_ids = [] for payment_dict in payment_zone_dicts: payment_zone, _ = PaymentZone.objects.update_or_create( domain=EnforcementDomain.get_default_domain(), code=payment_dict['number'], number=payment_dict['number'], defaults=payment_dict) payment_zone_ids.append(payment_zone.pk) count += 1 PaymentZone.objects.exclude(pk__in=payment_zone_ids).delete() return count
def create_permit_area(identifier, domain=None, permitted_user=None): geom = generate_multi_polygon() if permitted_user is None: permitted_user = get_user_model().objects.get_or_create( username='******', defaults={'is_staff': True})[0] if domain is None: domain = EnforcementDomain.get_default_domain() PermitArea.objects.get_or_create(identifier=identifier, domain=domain, defaults={ 'name': "Kamppi", 'geom': geom, 'permitted_user': permitted_user, })
def test_parking_registration_number_special_chars(operator_api_client, new_parking_data): create_payment_zone(id=new_parking_data['zone'], code=str(new_parking_data['zone']), number=new_parking_data['zone'], domain=EnforcementDomain.get_default_domain()) new_parking_data['registration_number'] = 'ÅÄÖÆØ-:' response_parking_data = post(operator_api_client, list_url, new_parking_data) check_response_parking_data(new_parking_data, response_parking_data) new_parking = Parking.objects.get(id=response_parking_data['id']) check_parking_data_matches_parking_object(new_parking_data, new_parking)
def test_put_parking_optional_fields_null(operator_api_client, parking, updated_parking_data): detail_url = get_detail_url(parking) create_payment_zone(code='2', number=2, domain=EnforcementDomain.get_default_domain()) updated_parking_data['time_end'] = None updated_parking_data['location'] = None response_parking_data = put(operator_api_client, detail_url, updated_parking_data) check_response_parking_data(updated_parking_data, response_parking_data) parking.refresh_from_db() check_parking_data_matches_parking_object(updated_parking_data, parking)
def test_enforcement_domain_can_be_specified_on_parking_creation( operator_api_client, new_parking_data): enforcement_domain = EnforcementDomain.objects.create(code='ESP', name='Espoo') new_parking_data.update(domain=enforcement_domain.code) create_payment_zone(id=new_parking_data['zone'], code=str(new_parking_data['zone']), number=new_parking_data['zone'], domain=enforcement_domain) response_parking_data = post(operator_api_client, list_url, new_parking_data) assert not response_parking_data[ 'domain'] == EnforcementDomain.get_default_domain().code assert response_parking_data['domain'] == enforcement_domain.code
def test_patch_parking(operator_api_client, parking): detail_url = get_detail_url(parking) new_zone_number = parking.zone.number % 3 + 1 new_zone = create_payment_zone( code=str(new_zone_number), number=new_zone_number, id=new_zone_number, domain=EnforcementDomain.get_default_domain()) response_parking_data = patch(operator_api_client, detail_url, {'zone': new_zone.id}) # check data in the response check_response_parking_data({'zone': new_zone.id}, response_parking_data) # check the actual object parking.refresh_from_db() assert parking.zone.number == new_zone.number assert parking.domain.code == response_parking_data['domain']
def test_post_parking(operator_api_client, operator, new_parking_data): create_payment_zone(code=str(new_parking_data['zone']), number=new_parking_data['zone'], domain=EnforcementDomain.get_default_domain()) response_parking_data = post(operator_api_client, list_url, new_parking_data) # check data in the response check_response_parking_data(new_parking_data, response_parking_data) # check the actual object new_parking = Parking.objects.get(id=response_parking_data['id']) check_parking_data_matches_parking_object(new_parking_data, new_parking) # operator should be autopopulated assert new_parking.operator == operator assert new_parking.domain.code == response_parking_data['domain']
def create_parkings_and_regions(parking_count=100, region_count=20): domain = EnforcementDomain.get_default_domain() regions = RegionFactory.create_batch(region_count, domain=domain) parkings = ParkingFactory.create_batch(parking_count, domain=domain) centroids = [region.geom.centroid for region in regions] touching_points = [p for p in centroids if intersects_with_any(p, regions)] # Make sure that some of the parkings are inside the regions for (point, parking) in zip(touching_points, parkings): parking.location = point parking.save() for parking in parkings: # pragma: no cover if intersects_with_any(parking.location, regions): assert parking.region assert intersects(parking.location, parking.region) else: assert parking.region is None return (parkings, regions)
def test_cannot_modify_parking_after_modify_period(operator_api_client, new_parking_data, updated_parking_data): for data in [new_parking_data, updated_parking_data]: create_payment_zone(id=data['zone'], code=str(data['zone']), number=data['zone'], domain=EnforcementDomain.get_default_domain()) start_time = datetime.datetime(2010, 1, 1, 12, 00) error_message = 'Grace period has passed. Only "time_end" can be updated via PATCH.' error_code = 'grace_period_over' with freeze_time(start_time): response_parking_data = post(operator_api_client, list_url, new_parking_data) new_parking = Parking.objects.get(id=response_parking_data['id']) end_time = start_time + settings.PARKKIHUBI_TIME_PARKINGS_EDITABLE + datetime.timedelta( minutes=1) with freeze_time(end_time): # PUT error_data = put(operator_api_client, get_detail_url(new_parking), updated_parking_data, 403) assert error_message in error_data['detail'] assert error_data['code'] == error_code # PATCH other fields than 'time_end' for field_name in updated_parking_data: if field_name == 'time_end': continue parking_data = {field_name: updated_parking_data[field_name]} error_data = patch(operator_api_client, get_detail_url(new_parking), parking_data, 403) assert error_message in error_data['detail'] assert error_data['code'] == error_code
def test_can_modify_time_end_after_modify_period(operator_api_client, new_parking_data): create_payment_zone(id=new_parking_data['zone'], code=str(new_parking_data['zone']), number=new_parking_data['zone'], domain=EnforcementDomain.get_default_domain()) start_time = datetime.datetime(2010, 1, 1, 12, 00) with freeze_time(start_time): response_parking_data = post(operator_api_client, list_url, new_parking_data) new_parking = Parking.objects.get(id=response_parking_data['id']) end_time = start_time + settings.PARKKIHUBI_TIME_PARKINGS_EDITABLE + datetime.timedelta( minutes=1) with freeze_time(end_time): parking_data = {'time_end': '2016-12-12T23:33:29Z'} patch(operator_api_client, get_detail_url(new_parking), parking_data, 200) new_parking.refresh_from_db() assert new_parking.time_end.day == 12 # old day was 10
def test_zone_in_correct_domain_is_set_for_parking(operator_api_client, new_parking_data): domain_1 = EnforcementDomain.get_default_domain() domain_2 = EnforcementDomain.objects.create(code='ESP', name='Espoo') zone_1 = create_payment_zone(code=str(new_parking_data['zone']), number=new_parking_data['zone'], domain=domain_1) zone_2 = create_payment_zone(code=str(new_parking_data['zone']), number=new_parking_data['zone'], domain=domain_2) response_parking_data_1 = post(operator_api_client, list_url, new_parking_data) new_parking_data['domain'] = domain_2.code response_parking_data_2 = post(operator_api_client, list_url, new_parking_data) new_parking_1 = Parking.objects.get(id=response_parking_data_1['id']) new_parking_2 = Parking.objects.get(id=response_parking_data_2['id']) assert new_parking_1.zone == zone_1 assert new_parking_2.zone == zone_2
def save(self, *args, **kwargs): self.capacity_estimate = self.calculate_capacity_estimate() if not self.domain_id: self.domain = EnforcementDomain.get_default_domain() super().save(*args, **kwargs)
import pytz from django.conf import settings from django.utils.timezone import now from django.utils.translation import ugettext_lazy as _ from rest_framework import mixins, serializers, viewsets from parkings.models import EnforcementDomain, Parking, PaymentZone from ..common import ParkingException from .permissions import IsOperator DEFAULT_DOMAIN_CODE = EnforcementDomain.get_default_domain_code() class OperatorAPIParkingSerializer(serializers.ModelSerializer): status = serializers.ReadOnlyField(source='get_state') domain = serializers.SlugRelatedField( slug_field='code', queryset=EnforcementDomain.objects.all(), default=EnforcementDomain.get_default_domain) zone = serializers.SlugRelatedField( slug_field='code', queryset=PaymentZone.objects.all()) class Meta: model = Parking fields = ( 'id', 'created_at', 'modified_at', 'location', 'terminal_number', 'registration_number', 'time_start', 'time_end', 'zone', 'status', 'is_disc_parking',