Hi there,

I followed some examples in the documentation regarding contenttype
and I wanted to write my own simple, generig tagging app. I know there
is tagging application but it's troublesome when you try to pass tag
names in GET (language specific chars). Here is my code - it doesn't
work - could anyone point me to the right direction? I need a
textfield that is attachable to any model and populating this field
will populate related TaggedItems table (and in future the Tag table):
Code paste:
http://dpaste.com/97545/

Code itself (in case pated code gets removed):

blog/models.py:

from django.db import models
from django.contrib.contenttypes import generic
from prymityw.tags.fields import *

class Post(models.Model):
  def __unicode__(self):
    return self.title
  title = models.CharField(max_length=200)
  author = models.CharField(max_length=100)
  pub_date = models.DateTimeField('date published')
  slug = models.CharField(max_length=200)
  body = models.TextField()
  tags = TagField(max_length=255)

tags/models.py:

from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from django.utils.translation import ugettext_lazy as _


class Tag(models.Model):
    """
    A tag.
    """
    name = models.SlugField()
    output_name = models.CharField(max_length=50, unique=True)

    class Meta:
        ordering = ('name',)
        verbose_name = _('tag')
        verbose_name_plural = _('tags')

    def __unicode__(self):
        return self.name

class TaggedItems(models.Model):
  """
  Table holding generic tag relationship
  """
  tag = models.SlugField()
  content_type = models.ForeignKey(ContentType)
  object_id = models.PositiveIntegerField()
  content_object = generic.GenericForeignKey('content_type',
'object_id')
  class Meta:
    ordering = ["tag"]


  def __unicode__(self):
    return self.tag
# Create your models here.

tags/fields.py:

# -*- coding: utf-8 -*-
from django.db import models
from tags.models import *

def _tag_split(string):
  list = string.split(",")
  i = 0
  for val in list:
    list[i] = val.strip()
    i = i + 1
  clear_list = [x for x in list if x]
  return clear_list

_transliteration_pl = (
    (u"�", r'a'),
    (u"\xc4\x85", r'a'),
    (u"�", r'c'),
    (u"\xc4\x87", r'c'),
    (u"�", r'e'),
    (u"\xc4\x99", r'e'),
    (u"�", r'l'),
    (u"\xc5\x82", r'l'),
    (u"�", r'n'),
    (u"\xc5\x84", r'n'),
    (u"Ăł", r'o'),
    (u"\xc3\xb3", r'o'),
    (u"�", r's'),
    (u"\xc5\x9b", r's'),
    (u"Ĺź", r'z'),
    (u"\xc5\xbc", r'z'),
    (u"Ĺş", r'z'),
    (u"\xc5\xba", r'z')
)

def _transliterate(value):
    """Replaces language specific chars with theis non-specific
equivalents"""
    for bad, good in _transliteration_pl:
        value = value.replace(bad, good)
    return value

class TagField(models.CharField):
  __metaclass__ = models.SubfieldBase

  def __init__(self, *args, **kwargs):
    super(TagField, self).__init__(*args, **kwargs)

  def to_python(self, value):
    if not value: return
    if isinstance(value, list):
        return value
    return _tag_split(value)

  def get_db_prep_value(self, value):
    if not value: return
    assert(isinstance(value, list) or isinstance(value, tuple))
    for tag in value:
      t = TaggedItems(content_object=self, tag=tag)
      t.save()
    return ",".join([unicode(s) for s in value])


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to