def test_registration_type(self): event = models.Event( organization=self.organization, name="Test Registration Event", pricing=self.pricing, registration_pricing_logic=[ { 'var': 'total', 'exp': { 'if': [ { '==': [ { 'var': 'registration.registration_type' }, 'worktrade', ] }, 100, 200, ] }, }, ], camper_pricing_logic=[], ) # default registration type (None) registration = models.Registration( event=event, attributes={}, ) price_components = pricing.calculate_price(registration, []) self.assertEqual(price_components, { 'campers': [], 'total': 200, }) # 'worktrade' registration type registration = models.Registration( event=event, attributes={}, registration_type=models.RegistrationType( event=event, name='worktrade', label='Work-trade', ), ) price_components = pricing.calculate_price(registration, []) self.assertEqual(price_components, { 'campers': [], 'total': 100, })
def test_campers_and_registration(self): event = models.Event( organization=self.organization, name="Test Camper/Registration Event", pricing=self.pricing, registration_pricing_logic=self.registration_pricing_logic, camper_pricing_logic=self.camper_pricing_logic) registration = models.Registration(event=event, attributes={ "number_of_cabins": 3, "number_of_parking_passes": 2 }) campers = [ models.Camper(attributes={"age": 2}), models.Camper(attributes={"age": 17}), models.Camper(attributes={"age": 18}), models.Camper(attributes={"age": 42}) ] price_components = pricing.calculate_price(registration, campers) self.assertEqual( price_components, { "tuition": 1200, "meals": 700, "cabins": 300, "parking_passes": 100, "total": 2300, })
def test_calculate_campers(self): event = models.Event( organization=self.organization, name="Test Camper Event", pricing=self.pricing, registration_pricing_logic=[], camper_pricing_logic=self.camper_pricing_logic, ) registration = models.Registration(event=event) campers = [ models.Camper(attributes={"age": 2}), models.Camper(attributes={"age": 17}), models.Camper(attributes={"age": 18}), models.Camper(attributes={"age": 42}), ] price_components = pricing.calculate_price(registration, campers) self.assertEqual( price_components, { "is_adult": 2, "tuition": 1200, "meals": 700, "campers": [ { "is_adult": False, "meals": 50, "total": 250, "tuition": 200 }, { "is_adult": False, "meals": 50, "total": 250, "tuition": 200 }, { "is_adult": True, "meals": 300, "total": 700, "tuition": 400 }, { "is_adult": True, "meals": 300, "total": 700, "tuition": 400 }, ], "total": 1900, })
def test_calculate_registration(self): event = models.Event( organization=self.organization, name="Test Registration Event", pricing=self.pricing, registration_pricing_logic=self.registration_pricing_logic, camper_pricing_logic={}) registration = models.Registration(event=event, attributes={ "number_of_cabins": 3, "number_of_parking_passes": 2 }) price_components = pricing.calculate_price(registration, []) self.assertEqual(price_components, { "cabins": 300, "parking_passes": 100, "total": 400, })
def test_calculate_campers(self): event = models.Event(organization=self.organization, name="Test Camper Event", pricing=self.pricing, registration_pricing_logic={}, camper_pricing_logic=self.camper_pricing_logic) registration = models.Registration(event=event) campers = [ models.Camper(attributes={"age": 2}), models.Camper(attributes={"age": 17}), models.Camper(attributes={"age": 18}), models.Camper(attributes={"age": 42}) ] price_components = pricing.calculate_price(registration, campers) self.assertEqual(price_components, { "tuition": 1200, "meals": 700, "total": 1900, })
def test_dates(self): event = models.Event( organization=self.organization, name="Test Registration Event", start=datetime.datetime(2019, 2, 25, 17, 0, 5, tzinfo=timezone.utc), camper_schema={ "type": "object", "properties": { "birthdate": { "type": "string", "format": "date", }, }, }, pricing=self.pricing, registration_pricing_logic=[ { "var": "date_parts", "exp": [ { "var": "event.start.year" }, { "var": "event.start.month" }, { "var": "event.start.day" }, ], }, ], camper_pricing_logic=[ { "var": "birthdate_parts", "exp": [ { "var": "camper.birthdate.year" }, { "var": "camper.birthdate.month" }, { "var": "camper.birthdate.day" }, ], }, ], ) self.assertIsNotNone(event.start) registration = models.Registration(event=event, attributes={}) camper = models.Camper( registration=registration, attributes={ "birthdate": "2000-12-31", }, ) price_components = pricing.calculate_price(registration, [camper]) self.assertEqual( price_components, { "date_parts": [2019, 2, 25], "campers": [{ "birthdate_parts": [2000, 12, 31] }], })
def post(self, request, event_id=None, format=None): event = get_object_or_404(models.Event, id=event_id) form_data = request.data.get('formData') if form_data is None: raise ValidationError({'formData': 'This field is required.'}) client_reported_pricing = request.data.get('pricingResults') if client_reported_pricing is None: raise ValidationError( {'pricingResults': 'This field is required.'}) self.validate_form_data(event, form_data) registration, campers = self.deserialize_form_data(event, form_data) invitation = None try: invitation = self.find_invitation(request) except InvitationError as e: raise ValidationError(e.user_message) if invitation: registration.registration_type = invitation.registration_type server_pricing_results = pricing.calculate_price(registration, campers) registration.server_pricing_results = server_pricing_results registration.client_reported_pricing = client_reported_pricing registration.save() if invitation: invitation.registration = registration invitation.save() campers_template_value = [] for camper_index, camper in enumerate(campers): campers_template_value.append({ **camper.attributes, 'pricing_result': server_pricing_results['campers'][camper_index], }) camper.save() confirmation_email_body_text = chevron.render( event.confirmation_email_template, { 'registration': registration.attributes, 'campers': campers_template_value, 'pricing_results': server_pricing_results, }) confirmation_email_body_html = cmarkgfm.github_flavored_markdown_to_html( confirmation_email_body_text) email_error = None sent = False try: if '@dontsend.com' in registration.registrant_email: email_error = 'registration email contains @dontsend.com' logger.error(confirmation_email_body_html) else: msg = EmailMultiAlternatives(event.confirmation_email_subject, confirmation_email_body_text, event.confirmation_email_from, [registration.registrant_email]) msg.attach_alternative(confirmation_email_body_html, "text/html") sent = msg.send(fail_silently=False) if not sent: email_error = email_error or 'mail not sent' except SMTPException as e: email_error = str(e) if email_error: logger.error(f'error sending confirmation email: {email_error}') return Response({ 'confirmationPageTemplate': event.confirmation_page_template, 'serverPricingResults': server_pricing_results, 'emailError': bool(email_error), })