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


# a change to an entity in our system is either the creation of that entity,
# a change to a column of it or its de/re-activation. ordering in time is maintained
# by sorting via the timestamp
class HistoryEntry(models.Model):
    timestamp = models.DateTimeField(
        auto_now_add=True,
        verbose_name=_('when')
    )

    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    entity = generic.GenericForeignKey()

    old_value = models.TextField(blank=True, verbose_name=_('old'))

    new_value = models.TextField(blank=True, verbose_name=_('new'))

    # verbose name of field that was changed
    field_name = models.CharField(blank=False,
                                  max_length=63,
                                  verbose_name=_('property'))

    # don't create related relation, since (as least not now)
    # we don't want to have all the changes show up in the user view
    who = models.ForeignKey('users.WinoseUser',
                            related_name='+',
                            null=True,
                            verbose_name=_('who'))

    @staticmethod
    def no_tracking():
        # do not track changes to those fields (else every login would produce a history entry!)
        return ['last_login', 'password', 'qualified']
