コード例 #1
0
ファイル: __init__.py プロジェクト: genghisu/eruditio
 def render(self, name, value, attrs = None):
     if value is None: 
         value = ''
     final_attrs = self.build_attrs(attrs, type = self.input_type, name = name)
     if value != '':
         final_attrs['value'] = force_unicode(value)
     else:
         final_attrs['value'] = ''
     
     target_id = generate_target_id(final_attrs['base_content_type'], final_attrs['base_object_id'])
     data_id = generate_data_id(final_attrs['base_content_type'], final_attrs['base_object_id'])
     init_id = generate_init_id(final_attrs['base_content_type'], final_attrs['base_object_id'])
     element_class = "%s %s" % (final_attrs.get('class', ''), getattr(settings, 'RELATEDCONTENT_FIELD_CLASS', ''))
     
     target_attrs = {}
     target_attrs['id'] = target_id
     target_attrs['class'] = element_class
     
     data_attrs = {}
     data_attrs['id'] = data_id
     data_attrs['type'] = 'hidden'
     data_attrs['class'] = element_class
     data_attrs['value'] = self.get_initial_data(final_attrs['initial_data'])
     data_attrs['name'] = final_attrs['name']
     
     init_attrs = {}
     init_attrs['id'] = init_id
     init_attrs['class'] = element_class
     
     relatedcontent_url = generate_relatedcontent_url(base_url = final_attrs['base_url'], 
                                               mode = getattr(settings, 'UPDATE_MODES')[0],
                                               target_id = target_id,
                                               data_id = data_id)
     target_url = generate_thickbox_url(relatedcontent_url, final_attrs['iframe_width'], final_attrs['iframe_height'])
     
     initial_target = self.get_initial_target(final_attrs['base_object'], 
                                              final_attrs['initial_data'], 
                                              target_id, 
                                              data_id)
     
     target = u"""<div%s>%s</div>""" % (flatatt(target_attrs), initial_target)
     data = u"""<input%s></input>""" % flatatt(data_attrs)
     init = u"""<a href='%s' %s>Select</a>""" % (target_url, flatatt(init_attrs))
     thickbox_script = generate_enable_thickbox(init_id)
     bind_click_script = ""
     
     return mark_safe(target + "\n" + data + "\n" + init + "\n" + thickbox_script + "\n" + bind_click_script)
コード例 #2
0
 def render(self,  context):
     base_content_type = resolve_variable(self.base_content_type, context, self.base_content_type)
     base_object_id = resolve_variable(self.base_object_id, context, self.base_object_id)
     usage = resolve_variable(self.usage, context, self.usage)
     update_mode = resolve_variable(self.update_mode, context, self.update_mode)
     associate_url = resolve_variable(self.associate_url, context, self.associate_url)
     iframe_height = resolve_variable(self.iframe_height, context, self.iframe_height)
     iframe_width = resolve_variable(self.iframe_width, context, self.iframe_width)
     resolved_associate_url = reverse(associate_url)
     
     base_content_type_object = ContentType.objects.get(id = int(base_content_type))
     base_content_model = base_content_type_object.model_class()
     if base_object_id > 0:
         base_object = base_content_model.objects.get(id = base_object_id)
     else:
         base_object = None
     
     target_id = generate_target_id(base_content_type, base_object_id)
     data_id = generate_data_id(base_content_type, base_object_id)
     init_id = generate_init_id(base_content_type, base_object_id)
     
     initial_objects = self.get_initial_data(base_object, usage)
     initial_target = render_initial_target(base_object, 
                                            initial_objects, 
                                            target_id, 
                                            data_id, 
                                            associate_url = resolved_associate_url,
                                            usage = usage)
     
     base_url = reverse('django-relatedcontent-select', args=[base_content_type, base_object_id, usage])
     relatedcontent_url = generate_relatedcontent_url(base_url = base_url, 
                                                      mode = update_mode,
                                                      target_id = target_id,
                                                      data_id = data_id,
                                                      ajax_associate_url = associate_url)
     target_url = generate_thickbox_url(relatedcontent_url, iframe_width, iframe_height)
     
     new_context = {}
     new_context['target_id'] = target_id
     new_context['data_id'] = data_id
     new_context['init_id'] = init_id
     new_context['target_url'] = target_url
     new_context['initial_target'] = initial_target
     context['select_link_id'] = init_id
     
     return render_to_string('django_relatedcontent/generate_ajax_select.html', new_context)