def test_template_edit_ui(self): template = self.create_emailtemplate( key=EmailTemplateType.NOTIFY_GUEST__BOOKING_CREATED) self.login("/emailtemplate/%d/edit" % template.id, self.admin) form_data = { 'type': EmailTemplateType.get( EmailTemplateType.NOTIFY_GUEST__BOOKING_CREATED).id, 'organizationalunit': self.unit.id, 'subject': 'Test subject', 'body': 'Test body' } fields = {'subject': None, 'body': None} for (key, value) in fields.items(): if type(value) != list: value = [value] for v in value: data = {} data.update(form_data) if v is None: del data[key] else: data[key] = v response = self.client.post( "/emailtemplate/%d/edit" % template.id, data) self.assertEquals(200, response.status_code) query = pq(response.content) errors = query("[name=\"%s\"]" % key) \ .closest("div.form-group").find("ul.errorlist li") self.assertEquals(1, len(errors)) response = self.client.post("/emailtemplate/%d/edit" % template.id, form_data) self.assertEquals(302, response.status_code) self.assertEquals("/emailtemplate", response['Location']) for (key, value) in form_data.items(): self.assertEquals( 1, EmailTemplate.objects.filter(**{ key: value }).count()) template = EmailTemplate.objects.get(**form_data) self.assertEquals( EmailTemplateType.get( EmailTemplateType.NOTIFY_GUEST__BOOKING_CREATED), template.type) self.assertEquals(self.unit, template.organizationalunit) self.assertEquals('Test subject', template.subject) self.assertEquals('Test body', template.body)
def __init__(self, *args, **kwargs): initial = kwargs.get('initial', {}) initial.update({'active': self.get_active_value(kwargs)}) kwargs['initial'] = initial super(VisitAutosendForm, self).__init__(*args, **kwargs) template_key = None if 'instance' in kwargs: template_key = kwargs['instance'].template_key elif 'initial' in kwargs: template_key = kwargs['initial']['template_key'] if template_key is not None: template_type = EmailTemplateType.get(template_key) if not template_type.enable_days: self.fields['days'].widget = forms.HiddenInput() elif template_key == \ EmailTemplateType.NOTITY_ALL__BOOKING_REMINDER: self.fields['days'].help_text = _(u'Notifikation vil blive ' u'afsendt dette antal dage ' u'før besøget') elif template_key == EmailTemplateType.NOTIFY_HOST__HOSTROLE_IDLE: self.fields['days'].help_text = _(u'Notifikation vil blive ' u'afsendt dette antal dage ' u'efter første booking ' u'er foretaget')
def create_emailtemplate(self, key=1, type=None, subject="", body="", unit=None): if key is not None and type is None: type = EmailTemplateType.get(key) template = EmailTemplate(key=key, type=type, subject=subject, body=body, organizationalunit=unit) template.save() return template
def test_template_list_ui(self): self.login("/emailtemplate", self.admin) self.create_emailtemplate( key=EmailTemplateType.NOTIFY_GUEST__BOOKING_CREATED) response = self.client.get("/emailtemplate") self.assertTemplateUsed(response, "email/list.html") query = pq(response.content) items = query("table tbody tr") found = [ tuple([cell.text.strip() for cell in query(i).find("td")]) for i in items ] self.assertEquals(1, len(found)) self.assertEquals( EmailTemplateType.get( EmailTemplateType.NOTIFY_GUEST__BOOKING_CREATED).name, found[0][0]) self.assertEquals("Grundskabelon", found[0][1])
def test_email_recipients(self): # setup products with users in different roles # test that get_recipients returns the correct users EmailTemplateType.set_defaults() ResourceType.create_defaults() UserRole.create_defaults() teacher = self.create_default_teacher(unit=self.unit) host = self.create_default_host(unit=self.unit) coordinator = self.create_default_coordinator(unit=self.unit) roomguy = self.create_default_roomresponsible(unit=self.unit) teacher_pool = self.create_resourcepool( ResourceType.RESOURCE_TYPE_TEACHER, self.unit, 'test_teacher_pool', teacher.userprofile.get_resource()) host_pool = self.create_resourcepool( ResourceType.RESOURCE_TYPE_HOST, self.unit, 'test_host_pool', teacher.userprofile.get_resource()) products = [] for product_type, label in Product.resource_type_choices: for time_mode in [ Product.TIME_MODE_NONE, Product.TIME_MODE_SPECIFIC, Product.TIME_MODE_GUEST_SUGGESTED, Product.TIME_MODE_NO_BOOKING ]: product = self.create_product(self.unit, time_mode=time_mode, potential_teachers=[teacher], potential_hosts=[host], state=Product.CREATED, product_type=product_type) product.tilbudsansvarlig = coordinator product.roomresponsible.add(roomguy) product.save() products.append(product) for time_mode in [ Product.TIME_MODE_RESOURCE_CONTROLLED, Product.TIME_MODE_RESOURCE_CONTROLLED_AUTOASSIGN ]: product = self.create_product(self.unit, time_mode=time_mode, state=Product.CREATED, product_type=product_type) product.tilbudsansvarlig = coordinator product.roomresponsible.add(roomguy) self.create_resourcerequirement(product, teacher_pool, 1) self.create_resourcerequirement(product, host_pool, 1) product.save() # products.append(product) expected = { EmailTemplateType.NOTIFY_EDITORS__BOOKING_CREATED: [(KUEmailRecipient.TYPE_PRODUCT_RESPONSIBLE, coordinator)], EmailTemplateType.NOTIFY_HOST__REQ_TEACHER_VOLUNTEER: [(KUEmailRecipient.TYPE_TEACHER, teacher)], EmailTemplateType.NOTIFY_HOST__REQ_HOST_VOLUNTEER: [(KUEmailRecipient.TYPE_HOST, host)], EmailTemplateType.NOTIFY_HOST__REQ_ROOM: [(KUEmailRecipient.TYPE_ROOM_RESPONSIBLE, roomguy)], EmailTemplateType.NOTIFY_ALL__BOOKING_COMPLETE: [ (KUEmailRecipient.TYPE_PRODUCT_RESPONSIBLE, coordinator), (KUEmailRecipient.TYPE_ROOM_RESPONSIBLE, roomguy) ], EmailTemplateType.NOTIFY_ALL__BOOKING_CANCELED: [ (KUEmailRecipient.TYPE_PRODUCT_RESPONSIBLE, coordinator), (KUEmailRecipient.TYPE_ROOM_RESPONSIBLE, roomguy) ], EmailTemplateType.NOTITY_ALL__BOOKING_REMINDER: [ (KUEmailRecipient.TYPE_PRODUCT_RESPONSIBLE, coordinator), (KUEmailRecipient.TYPE_ROOM_RESPONSIBLE, roomguy) ], EmailTemplateType.NOTIFY_HOST__HOSTROLE_IDLE: [ (KUEmailRecipient.TYPE_EDITOR, coordinator) ], EmailTemplateType.NOTIFY_EDITORS__SPOT_REJECTED: [ (KUEmailRecipient.TYPE_PRODUCT_RESPONSIBLE, coordinator), ], } expected.update({ template_type.key: [] for template_type in EmailTemplateType.objects.all() if template_type.key not in expected.keys() }) def unpack(item): if isinstance(item, User): return (item.get_full_name(), item.email) if isinstance(item, RoomResponsible): return (item.name, item.email) for product in products: for template_type_key, expected_recipients in expected.items(): actual_recipients = product.get_recipients( EmailTemplateType.get(template_type_key)) self.assertEquals( len(expected_recipients), len(actual_recipients), "Expected number of recipients not " "matching for template type %d" % template_type_key) expected_recipients = [(t, unpack(r)) for (t, r) in expected_recipients] for r in actual_recipients: self.assertTrue( (r.type, (r.name, r.email)) in expected_recipients, "did not expect (%d, (%s, %s)) " "for template type %d, expected %s" % (r.type, r.name, r.email, template_type_key, str(expected_recipients)))