Thank you
I like this concept and will try to do it this way.
Thanks for the input.
/Carsten
On May 25, 12:05 pm, bruno desthuilliers
<bruno.desthuilli...@gmail.com> wrote:
> On May 24, 11:00 pm, Carsten Jantzen <cars...@jantzens.net> wrote:
>
>
>
> > Not sure what you mean by detecting skill change using properties, I
> > havn't looked into that.
> > Could you point to some documentation about it.
>
> Python has a strong support for computed attributes[1], and offers a
> builtin "property" type for the most common use cases[2]. This let you
> turn a plain attribute into a computed one (using a getter/setter
> pair) without breaking client code. What I had in ming was to turn
> your "skill" fields into computed attributes (using either properties
> or a custom descriptor) to track change.
>
> Now having looked at django-audit implementation (http://
> code.google.com/p/django-audit/), I think their approach using the
> __setattr__ hook[3] would be better here. Here's a very Q&D and naïve
> implementation example:
>
> import datetime
>
> class SkillChange(models.Model):
> player = models.ForeignKey(Player)
> skill = models.CharField()
> value = models.IntegerField()
> date = models.DatetimeField()
>
> class Player(models.Model):
> # your fields definitions here
> skill_x = models.IntegerField()
> skill_y = models.IntegerField()
>
> # which fields we want to track
> _tracked_fields = ['skill_x', 'skill_y']
>
> def __init__(self, *args, **kw):
> super(Player, self).__init__(*args, **kw)
> self._changed = {}
>
> def __setattr__(self, attr, value)
> if attr in self._tracked_fields:
> old_val = getattr(self, attr, None)
> if old_val != value:
> self._changed[attr] = value
> super(Player, self).__setattr__(attr, value)
>
> def save(self, *args, **kw):
> super(Player, self).save(*args, **kw)
> now = datetime.datetime.now()
> for skill, value in self._changed.items():
> SkillChange.objects.create(
> player=self,
> skill=skill,
> value=value,
> date=now
> )
> self._changed = {}
>
> Untested code, of course, it's just meant as a (possible) proof of
> concept. You'll probably have to deal with a few special cases like
> what happens when creating a new Player instance etc
>
> HTH
>
> [1]http://docs.python.org/release/2.6.7/reference/datamodel.html#impleme...
> [2]http://docs.python.org/release/2.6.7/library/functions.html#property
> [3]http://docs.python.org/release/2.6.7/reference/datamodel.html#object....
--
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 django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment