Ejemplo n.º 1
0
    def __get__(self, instance, owner=None):
        """
        Tag getter. Returns an instance's tags if accessed on an instance, and
        all of a model's tags if called on a class. That is, this model::

           class Link(models.Model):
               ...
               tags = TagField()

        Lets you do both of these::

           >>> l = Link.objects.get(...)
           >>> l.tags
           'tag1 tag2 tag3'

           >>> Link.tags
           'tag1 tag2 tag3 tag4'

        """
        # Handle access on the model (i.e. Link.tags)
        if instance is None:
            return edit_string_for_tags(Tag.objects.usage_for_model(owner))

        tags = self._get_instance_tag_cache(instance)
        if tags is None:
            if instance.pk is None:
                self._set_instance_tag_cache(instance, '')
            else:
                self._set_instance_tag_cache(
                    instance, edit_string_for_tags(Tag.objects.get_for_object(instance)))
        return self._get_instance_tag_cache(instance)
Ejemplo n.º 2
0
	def render(self, name, value, attrs=None):
		list_view = reverse('taggit_autocomplete-list')
		if value is not None and not isinstance(value, basestring):
			value = edit_string_for_tags([o.tag for o in value.select_related("tag")])
		html = super(TagAutocomplete, self).render(name, value, attrs)
		js = u'<script type="text/javascript">jQuery().ready(function() { jQuery("#%s").autocomplete("%s", { multiple: true }); });</script>' % (attrs['id'], list_view)
		return mark_safe("\n".join([html, js]))
Ejemplo n.º 3
0
 def render(self, name, value, attrs=None):
     if attrs is not None:
         attrs = dict(self.attrs.items() + attrs.items())
 
     list_view = reverse('taggit-list')
     if value is not None and not isinstance(value, basestring):
         value = edit_string_for_tags(
           [o.tag for o in value.select_related("tag")]
         )
     html = super(TagAutocomplete, self).render(
         name+"_dummy",
         u'',
         attrs
     )
     allow_add = "false"
     if 'allow_add' in attrs and attrs['allow_add']:
         allow_add = "true"
     js_config = u"{startText: \"%s\", \
         preFill: \"%s\", \
         allowAdd: %s, \
         allowAddMessage: \"%s\"}" % (
             escapejs(_("Enter Tag Here")),
             escapejs(value) if value else u'',
             allow_add,
             escapejs(_("Please choose an existing tag")),
         )
     js = u"<script type=\"text/javascript\">jQuery = django.jQuery; \
         jQuery().ready(function() { jQuery(\"#%s\").autoSuggest(\"%s\", \
         %s); });</script>" % (
             attrs['id'],
             list_view,
             js_config
         )
     return mark_safe("\n".join([html, js]))
	def render(self, name, value, attrs=None):
		list_view = reverse('taggit_autocomplete-list')
		if value is not None and not isinstance(value, basestring):
			value = edit_string_for_tags([o.tag for o in value.select_related("tag")])
		html = super(TagAutocomplete, self).render(name, value, attrs)
		js = SCRIPT % (attrs['id'], list_view)
		return mark_safe("\n".join([html, js]))
Ejemplo n.º 5
0
	def render(self, name, value, attrs=None):
		list_view = reverse('taggit_autocomplete-list')
		if value is not None and not isinstance(value, basestring):
			value = edit_string_for_tags(
					[o.tag for o in value.select_related("tag")])
		html = super(TagAutocomplete, self).render(name, value, attrs)
		js = u"""
			<script type="text/javascript">
			(function($) {
				$(document).ready(function(){
					function split( val ) {
						return val.split( /,\s*/ );
					}
					function extractLast( term ) {
						return split( term ).pop();
					}
					
					$("#%(id)s")// don't navigate away from the field on tab when selecting an item
					.bind( "keydown", function( event ) {
						if ( event.keyCode === $.ui.keyCode.TAB &&
						$( this ).data( "autocomplete" ).menu.active ) {
						event.preventDefault();
					}
					})
					.autocomplete({
						source: function( request, response ) {
							$.getJSON( "%(source)s", {
							term: extractLast( request.term )
							}, response );
						},
						search: function() {
							// custom minLength
							var term = extractLast( this.value );
							if ( term.length < 1 ) {
							return false;
							}
						},
						focus: function() {
							// prevent value inserted on focus
							return false;
						},
						select: function( event, ui ) {
							var terms = split( this.value );
							// remove the current input
							terms.pop();
							// add the selected item
							terms.push( ui.item.value );
							// add placeholder to get the comma-and-space at the end
							terms.push( "" );
							this.value = terms.join( ", " );
							return false;
						}
					});
				}
			)})(django.jQuery);
			</script>
			""" % ({'id':attrs['id'], 'source':list_view})
			
		return mark_safe("\n".join([html, js]))
Ejemplo n.º 6
0
 def render(self, name, value, attrs=None):
     list_view = reverse('taggit_autocomplete-list')
     if value is not None and not isinstance(value, basestring):
         value = edit_string_for_tags(
             [o.tag for o in value.select_related("tag")])
     html = super(TagAutocomplete, self).render(name, value, attrs)
     js = u'<script type="text/javascript">jQuery().ready(function() { jQuery("#%s").autocomplete("%s", { multiple: true }); });</script>' % (
         attrs['id'], list_view)
     return mark_safe("\n".join([html, js]))
Ejemplo n.º 7
0
 def render(self, name, value, attrs=None):
     list_view = reverse('taggit-list')
     if value is not None and not isinstance(value, basestring):
         value = edit_string_for_tags(
             [o.tag for o in value.select_related("tag")])
     html = super(TagAutocomplete, self).render(name + "_dummy", u'', attrs)
     js = u'<script type="text/javascript">jQuery().ready(function() { jQuery("#%s").autoSuggest("%s", {startText: "Enter Tag Here", preFill: "%s"}); });</script>' % (
         attrs['id'], list_view, escapejs(value) if value else u'')
     return mark_safe("\n".join([html, js]))
Ejemplo n.º 8
0
 def render(self, name, value, attrs=None):
     list_view = reverse("taggit-list")
     if value is not None and not isinstance(value, basestring):
         value = edit_string_for_tags([o.tag for o in value.select_related("tag")])
     html = super(TagAutocomplete, self).render(name + "_dummy", u"", attrs)
     js = (
         u'<script type="text/javascript">jQuery().ready(function() { jQuery("#%s").autoSuggest("%s", {startText: "Enter Tag Here", preFill: "%s"}); });</script>'
         % (attrs["id"], list_view, escapejs(value) if value else u"")
     )
     return mark_safe("\n".join([html, js]))
Ejemplo n.º 9
0
	def render(self, name, value, attrs=None):
		list_view = reverse('tags_admin-list')
		if value is not None and not isinstance(value, basestring):
			value = edit_string_for_tags([o.tag for o in value.select_related("tag")])
		html = super(TagAutocomplete, self).render(name, value, attrs)
		js =  u'<script type="text/javascript">'
		js += u'jQuery().ready(function() {'
		js += u'	jQuery("#%s").tagsInput({ autocomplete_url: "%s" });' % (attrs['id'], list_view)
		js += u'});'
		js += u'</script>' 
		return mark_safe("\n".join([html, js]))
	def render(self, name, value, attrs=None):
		list_view = '/dynamic/autocomplete/1/taggable/'
		if value is not None and not isinstance(value, basestring):
			value = edit_string_for_tags([o.tag for o in value.select_related("tag")])
		attrs['class'] = 'vTextField'
		html = super(TagAutocomplete, self).render(name, value, attrs)
		js = u'''<script type="text/javascript">
			$(document).ready(function() { 
				$("#%s").autocomplete({ serviceUrl: "%s", delimiter: /(,|;)\s*/ });
			});
			</script>''' % (attrs['id'], list_view)
		return mark_safe("\n".join([html, js]))
    def render(self, name, value, attrs=None):
        list_view = '/dynamic/autocomplete/1/taggable/'
        if value is not None and not isinstance(value, basestring):
            value = edit_string_for_tags(
                [o.tag for o in value.select_related("tag")])
        attrs['class'] = 'vTextField'
        html = super(TagAutocomplete, self).render(name, value, attrs)
        js = u'''<script type="text/javascript">
			$(document).ready(function() { 
				$("#%s").autocomplete({ serviceUrl: "%s", delimiter: /(,|;)\s*/ });
			});
			</script>''' % (attrs['id'], list_view)
        return mark_safe("\n".join([html, js]))
Ejemplo n.º 12
0
    def render(self, name, value, attrs=None):
        if value is not None and not isinstance(value, basestring):
            value = edit_string_for_tags([o.tag for o in value.select_related("tag")])
        html = super(TagAutocomplete, self).render(name, value, attrs)
        tags = Tag.objects.values_list('name', flat=True)

        tagsJson = "[" + ",".join('"{0}"'.format(tag) for tag in tags) + "]"
        js = u'''<script type="text/javascript">
        var availableTags = %s;
            function split( val ) {
              return val.split( /,\\s*/ );
            }
            function extractLast( term ) {
              return split( term ).pop();
            }
        jQuery().ready(function() {
            jQuery("#%s")
              // don't navigate away from the field on tab when selecting an item
              .bind( "keydown", function( event ) {
                if ( event.keyCode === $.ui.keyCode.TAB &&
                    $( this ).autocomplete( "instance" ).menu.active ) {
                  event.preventDefault();
                }
              })
              .autocomplete({
                minLength: 0,
                source: function( request, response ) {
                  // delegate back to autocomplete, but extract the last term
                  response( $.ui.autocomplete.filter(
                    availableTags, extractLast( request.term ) ) );
                },
                focus: function() {
                  // prevent value inserted on focus
                  return false;
                },
                select: function( event, ui ) {
                  var terms = split( this.value );
                  // remove the current input
                  terms.pop();
                  // add the selected item
                  terms.push( ui.item.value );
                  // add placeholder to get the comma-and-space at the end
                  terms.push( "" );
                  this.value = terms.join( ", " );
                  return false;
                }
            });
        }); </script>''' % (tagsJson, attrs['id'])
        return mark_safe("\n".join([html, js]))
Ejemplo n.º 13
0
    def render(self, name, value, attrs=None):
        list_view = reverse('taggit_autocomplete-list')
        if value is not None and not isinstance(value, basestring):
            value = edit_string_for_tags(
                    [o.tag for o in value.select_related("tag")])
        html = super(TagAutocomplete, self).render(name, value, attrs)
        # change to use new jquery-ui autocomplete
        js = u"""
            <script type="text/javascript">
                (function($) {
                    $(document).ready(function() {
                        function split( val ) {
                            return val.split( /,\s*/ );
                        }
                        function extractLast( term ) {
                            return split( term ).pop();
                        }
                        function onitem(event, ui) {
                            // keep other entries for 'select'
                            // callbacks.
                            var terms = split( this.value );
                            // remove the current input
                            terms.pop();
                            // add the selected item
                            terms.push( ui.item.value );
                            // add placeholder to get the comma-and-space
                            // at the end
                            terms.push( "" );
                            this.value = terms.join( ", " );
                            return false;
                        }
                        function noop(event, item) {
                            // don't update for focus events.
                            return false;
                        }
                        function resize(event, item) {
                            // update the width of the textinput if we need to.
                            var size = parseInt($(this).attr('size')) - 5;
                            var chars = $(this).val().length;
                            if (chars >= size - 2) {
                                $(this).animate({width: 1.5 * chars / size *
                                $(this).width()}, 200);
                                $(this).attr('size', chars * 1.5);
                            }
                            return false;
                        }
                        // don't navigate away from the field on tab
                        // when selecting an item.
                        $("#%(id)s")
			                .bind( "keydown", function( event ) {
                            if ( event.keyCode === $.ui.keyCode.TAB &&
                                    $( this ).data( "autocomplete" ).menu.active ) {
                                event.preventDefault();
                            }
			            })
                        .autocomplete({
                            source: function( request, response ) {
                                $.getJSON( "%(source)s", {
                                    term: extractLast( request.term )
                                }, response );
                            },
                            create: resize,
                            select: onitem,
                            focus: noop,
                            close: resize
                        });

                        item = $("#%(id)s");
                        var size = parseInt(item.attr('size')) - 5;
                        chars = item.val().length;
                        if (chars >= size) {
                            item.animate({width: 1.5 * chars / size *
                                          item.width()}, 200);
                            item.attr('size', chars * 1.5);
                        };
                            
                    });
                })(django.jQuery);
            </script>
            """ % ({'id':attrs['id'], 'source':list_view})
        return mark_safe("\n".join([html, js]))
Ejemplo n.º 14
0
    def render(self, name, value, attrs=None):
        list_view = reverse("taggit_autocomplete-list")
        if value is not None and not isinstance(value, basestring):
            value = edit_string_for_tags([o.tag for o in value.select_related("tag")])
        html = super(TagAutocomplete, self).render(name, value, attrs)
        # change to use new jquery-ui autocomplete
        js = u"""
            <script type="text/javascript">
                (function($) {
                    $(document).ready(function() {
                        function split( val ) {
                            return val.split( /,\s*/ );
                        }
                        function extractLast( term ) {
                            return split( term ).pop();
                        }
                        function onitem(event, ui) {
                            // keep other entries for 'select'
                            // callbacks.
                            var terms = split( this.value );
                            // remove the current input
                            terms.pop();
                            // add the selected item
                            terms.push( ui.item.value );
                            // add placeholder to get the comma-and-space
                            // at the end
                            terms.push( "" );
                            this.value = terms.join( ", " );
                            return false;
                        }
                        function noop(event, item) {
                            // don't update for focus events.
                            return false;
                        }
                        function resize(event, item) {
                            // update the width of the textinput if we need to.
                            var size = parseInt($(this).attr('size')) - 5;
                            var chars = $(this).val().length;
                            if (chars >= size - 2) {
                                $(this).animate({width: 1.5 * chars / size *
                                $(this).width()}, 200);
                                $(this).attr('size', chars * 1.5);
                            }
                            return false;
                        }
                        // don't navigate away from the field on tab
                        // when selecting an item.
                        $("#%(id)s")
			                .bind( "keydown", function( event ) {
                            if ( event.keyCode === $.ui.keyCode.TAB &&
                                    $( this ).data( "autocomplete" ).menu.active ) {
                                event.preventDefault();
                            }
			            })
                        .autocomplete({
                            source: function( request, response ) {
                                $.getJSON( "%(source)s", {
                                    term: extractLast( request.term )
                                }, response );
                            },
                            create: resize,
                            select: onitem,
                            focus: noop,
                            close: resize
                        });

                        item = $("#%(id)s");
                        var size = parseInt(item.attr('size')) - 5;
                        chars = item.val().length;
                        if (chars >= size) {
                            item.animate({width: 1.5 * chars / size *
                                          item.width()}, 200);
                            item.attr('size', chars * 1.5);
                        };
                            
                    });
                })(django.jQuery);
            </script>
            """ % (
            {"id": attrs["id"], "source": list_view}
        )
        return mark_safe("\n".join([html, js]))