def test_subblock(self): """Test that a block within a block works.""" result = render_block_to_string('test5.html', 'block1') self.assertEqual(result, u'block3 from test5') result = render_block_to_string('test5.html', 'block3') self.assertEqual(result, u'block3 from test5')
def test_block(self): """Test rendering an individual block.""" result = render_block_to_string('test1.html', 'block1') self.assertEqual(result, u'block1 from test1') # No reason this shouldn't work, but just in case. result = render_block_to_string('test1.html', 'block2') self.assertEqual(result, u'block2 from test1')
def test_different_backend(self): """ Ensure an exception is thrown if a different backed from the Django backend is used. """ with self.assertRaises(UnsupportedEngine) as exc: render_block_to_string('test1.html', 'noblock') self.assertExceptionMessageEquals(exc.exception, "Can only render blocks from the Django template backend.")
def test_subblock_no_parent(self): """ Test that a block within a block works if the parent block is only found in the base template. This is very similar to test_subblock, but the templates differ. In this test the sub-template does not replace the entire block from the parent template. """ result = render_block_to_string('test_sub.html', 'base') self.assertEqual(result, u'\n\nbar\n\n') result = render_block_to_string('test_sub.html', 'first') self.assertEqual(result, u'\nbar\n')
def _render_email(self, template_name, context, template_dir=None, file_extension=None): response = {} errors = {} render_context = Context(context, autoescape=False) file_extension = file_extension or self.template_suffix if file_extension.startswith('.'): file_extension = file_extension[1:] template_extension = '.%s' % file_extension if isinstance(template_name, (tuple, list, )): prefixed_templates = template_name else: prefixed_templates = [template_name] full_template_names = [] for one_prefixed_template in prefixed_templates: one_full_template_name = ''.join((template_dir or self.template_prefix, one_prefixed_template)) if not one_full_template_name.endswith(template_extension): one_full_template_name += template_extension full_template_names.append(one_full_template_name) for part in ['subject', 'html', 'plain']: try: response[part] = render_block_to_string(full_template_names, part, render_context) except BlockNotFound as error: errors[part] = error if response == {}: raise EmailRenderException("Couldn't render email parts. Errors: %s" % errors) return response
def test_inherit(self): """This block is inherited from test1.""" result = render_block_to_string('test2.html', 'block2') self.assertEqual(result, u'block2 from test1')
def test_override(self): """This block is overridden in test2.""" result = render_block_to_string('test2.html', 'block1') self.assertEqual(result, u'block1 from test2')
def test_inherit(self): """This block is inherited from test1.""" result = render_block_to_string("test2.html", "block2") self.assertEqual(result, "block2 from test1")
def test_context(self): """Test that a context is properly rendered in a template.""" data = "block2 from test5" result = render_block_to_string("test5.html", "block2", {"foo": data}) self.assertEqual(result, data)
def test_override(self): """This block is overridden in test2.""" result = render_block_to_string("test2.html", "block1") self.assertEqual(result, "block1 from test2")
def test_super(self): """Test that block.super works.""" result = render_block_to_string('test3_django.html', 'block2') self.assertEqual(result, u'block2 from test3 - block2 from test1')
def test_multi_super(self): result = render_block_to_string('test6_django.html', 'block2') self.assertEqual( result, 'block2 from test6 - block2 from test3 - block2 from test1')
def test_super(self): """Test that block.super works.""" result = render_block_to_string('test3_django.html', 'block2') self.assertEqual(result, 'block2 from test3 - block2 from test1')
def test_include(self): """Ensure that an include tag in a block still works.""" result = render_block_to_string('test3_django.html', 'block1') self.assertEqual(result, 'included template')
def test_no_block(self): """Check if there's no block available an exception is raised.""" with self.assertRaises(BlockNotFound) as exc: render_block_to_string('test1.html', 'noblock') self.assertExceptionMessageEquals( exc.exception, "block with name 'noblock' does not exist")
def test_super(self): """Test that super() works.""" result = render_block_to_string('test3_jinja2.html', 'block2') self.assertEqual(result, 'block2 from test3 - block2 from test1')
def detail_project(request, pk): """ Detailview page for a given proposal. Displays all information for the proposal. Used for students to choose a proposal from, and for staff to check. For staff it shows edit and up/downgrade buttons. For students it shows a apply or retract button. The proposal is cached after the create phase (phase>4). The apply/retract button is not cached but inserted using a .format(). Proposals are not cached for staff Private proposals don't have a apply/retract button, because the template doesn't have the {} in it then. :param request: :param pk: pk of the project :return: """ proj = get_cached_project(pk) # if student if not request.user.groups.exists(): # make apply / retract button. if request.user.personal_proposal.filter( TimeSlot=proj.TimeSlot).exists() or not proj.can_apply(): button = '' else: button = '<a href="{}" class="button {}">{}</a>' if get_all_applications( request.user, timeslot=proj.TimeSlot).filter(Proposal=proj).exists( ): # if user has applied to this proposal button = button.format( reverse('students:retractapplication', args=[ get_all_applications( request.user, timeslot=proj.TimeSlot).filter( Proposal=proj)[0].id ]), 'danger', 'Retract Application') else: # show apply button button = button.format( reverse('students:confirmapply', args=[proj.id]), 'primary', 'Apply') # get proposal from cache, or put in cache cdata = cache.get("proposaldetail{}".format(pk)) if cdata is None: data = { "proposal": proj, "project": proj, "user": request.user, 'cache_string_render': True } cdata = render_block_to_string("proposals/detail_project.html", 'body', data, request=request) cache.set('proposaldetail{}'.format(pk), cdata, settings.PROJECT_OBJECT_CACHE_DURATION) tracking_visit_project(proj, request.user) # always log visits from students # applications counter if proj.applications.exists(): applications_count = proj.applications.count() applications_count_txt = '{} student'.format(applications_count) if applications_count > 1: applications_count_txt += 's' else: applications_count_txt = 'No one applied to this project yet.' return render( request, "proposals/detail_project.html", { "bodyhtml": cdata.format(apply_buttons=button, applications_counter=applications_count_txt), 'project': proj, }) # send project for if statement in scripts. # if staff: else: data = { "proposal": proj, "project": proj, "Editlock": "Editing not possible" } if proj.Status == 4: # support staff can see applications and distributions always if get_grouptype("3") in request.user.groups.all(): data['applications'] = proj.applications.all() data['distributions'] = get_distributions( request.user, timeslot=proj.TimeSlot).filter(Proposal=proj) # other staff users can see old distributions and current in phase 4+ elif (get_grouptype('2u') not in request.user.groups.all()) and \ proj.prevyear() or (proj.curyear() and get_timephase_number() >= 4): data['distributions'] = get_distributions( request.user, timeslot=proj.TimeSlot).filter(Proposal=proj) allowed = can_edit_project_fn(request.user, proj, False) if allowed[0]: data['Editlock'] = False else: data['Editlock'] = allowed[1] data['cpv'] = cache.get('cpv_proj_{}'.format( proj.id)) # if cpv is not in cache, ignore return render(request, "proposals/detail_project.html", data)
def test_no_block(self): """Check if there's no block available an exception is raised.""" with self.assertRaises(BlockNotFound) as exc: render_block_to_string('test1.html', 'noblock') self.assertExceptionMessageEquals(exc.exception, "block with name 'noblock' does not exist")
def test_context(self): """Test that a context is properly rendered in a template.""" data = u'block2 from test5' result = render_block_to_string('test5.html', 'block2', {'foo': data}) self.assertEqual(result, data)
def test_include(self): """Ensure that an include tag in a block still works.""" result = render_block_to_string('test3_django.html', 'block1') self.assertEqual(result, u'included template')
def test_super(self): """Test that super() works.""" result = render_block_to_string("test3_jinja2.html", "block2") self.assertEqual(result, "block2 from test3 - block2 from test1")
def test_multi_super(self): result = render_block_to_string('test6_django.html', 'block2') self.assertEqual(result, u'block2 from test6 - block2 from test3 - block2 from test1')
def test_context(self): """Test that a context is properly rendered in a template.""" data = 'block2 from test5' result = render_block_to_string('test5.html', 'block2', {'foo': data}) self.assertEqual(result, data)
def test_multi_super(self): result = render_block_to_string("test6_django.html", "block2") self.assertEqual( result, "block2 from test6 - block2 from test3 - block2 from test1")
def test_context_autoescape_off(self): """Test that the user can disable autoescape by providing a Context instance.""" data = "&'" result = render_block_to_string( 'test5.html', 'block2', Context({'foo': data}, autoescape=False)) self.assertEqual(result, data)
def test_include(self): """Ensure that an include tag in a block still works.""" result = render_block_to_string("test3_jinja2.html", "block1") self.assertEqual(result, "included template")