class Log(models.Model): ADD = 1 EDIT = 2 DELETE = 3 OPERATION_CHOICES = [[ADD, u'Cadastro'], [EDIT, u'Edição'], [DELETE, u'Exclusão']] content_type = models.ForeignKey(ContentType, verbose_name=u'Objeto', filter=True) operation = models.IntegerField(verbose_name=u'Operação', choices=OPERATION_CHOICES, filter=True) user = models.ForeignKey('admin.User', filter=True) date = models.DateTimeField(verbose_name=u'Data/Hora', auto_now=True, filter=True) object_id = models.IntegerField(verbose_name=u'Identificador', search=True) object_description = models.CharField(verbose_name=u'Descrição do Objeto') content = models.TextField(verbose_name=u'Conteúdo', null=True) fieldsets = ( (u'Dados Gerais', {'fields': ( ('content_type', 'operation'), ('user', 'date'), ('object_id', 'object_description'), 'get_tags')}), (u'Índices', {'relations': ('logindex_set',)}), ) objects = models.Manager() class Meta: verbose_name = u'Log' verbose_name_plural = u'Logs' icon = 'fa-history' list_per_page = 25 def __unicode__(self): return 'Log #%s' % self.pk def can_add(self): return False def can_edit(self): return False def can_delete(self): return False def get_action_description(self): return (u'adicionou', u'editou', u'removeu')[self.operation - 1] def get_style(self): return ('success', 'info', 'danger')[self.operation - 1] def get_icon(self): return ('plus', 'pencil', 'trash-o')[self.operation - 1] @meta(u'Tags', formatter='log_tags') def get_tags(self): return json.loads(self.content) def create_indexes(self, instance): for log_index in get_metadata(instance.__class__, 'logging', (), iterable=True): index_object = getattr2(instance, log_index) if index_object: index_content_type = ContentType.objects.get_for_model(index_object.__class__) LogIndex.objects.create(log=self, content_type=index_content_type, object_id=index_object.pk)
class Log(models.Model): ADD = 1 EDIT = 2 DELETE = 3 OPERATION_CHOICES = [[ADD, _('Add')], [EDIT, _('Edit')], [DELETE, _('Delete')]] content_type = models.ForeignKey(ContentType, verbose_name=_('Content Type'), filter=True) operation = models.IntegerField(verbose_name=_('Operation'), choices=OPERATION_CHOICES, filter=True) user = models.ForeignKey('admin.User', filter=True, verbose_name=_('User')) date = models.DateTimeField(verbose_name=_('Date/Time'), auto_now=True, filter=True) object_id = models.IntegerField(verbose_name=_('Identifier'), search=True) object_description = models.CharField(verbose_name=_('Object Description')) content = models.TextField(verbose_name=_('Content'), null=True) fieldsets = ( (_('General Data'), {'fields': ( ('content_type', 'operation'), ('user', 'date'), ('object_id', 'object_description'), 'get_tags')}), (_('Indexes'), {'relations': ('logindex_set',)}), ) objects = models.Manager() class Meta: verbose_name = _('Log') verbose_name_plural = _('Logs') icon = 'fa-history' list_per_page = 25 def __str__(self): return 'Log #{}'.format(self.pk) def can_add(self): return False def can_edit(self): return False def can_delete(self): return False def get_action_description(self): return (_('added'), _('edited'), _('deleted'))[self.operation - 1] def get_style(self): return ('success', 'info', 'danger')[self.operation - 1] def get_icon(self): return ('plus', 'pencil', 'trash-o')[self.operation - 1] @meta('Tags') def get_tags(self): return json.loads(self.content) def create_indexes(self, instance): for log_index in get_metadata(instance.__class__, 'logging', (), iterable=True): index_object = getattr2(instance, log_index) if index_object: index_content_type = ContentType.objects.get_for_model(index_object.__class__) LogIndex.objects.create(log=self, content_type=index_content_type, object_id=index_object.pk)
class LogIndex(models.Model): log = models.ForeignKey(Log, verbose_name=u'Log', composition=True) content_type = models.ForeignKey('contenttypes.ContentType', verbose_name=u'Dado') object_id = models.IntegerField(verbose_name=u'Identificador', search=True) class Meta: verbose_name = u'Index' verbose_name_plural = u'Indexes' def __unicode__(self): return u'Index #%s' % self.pk
class LogIndex(models.Model): log = models.ForeignKey(Log, verbose_name=_('Log'), composition=True) content_type = models.ForeignKey('contenttypes.ContentType', verbose_name=_('Content Type')) object_id = models.IntegerField(verbose_name=_('Identifier'), search=True) class Meta: verbose_name = _('Index') verbose_name_plural = _('Indexes') def __str__(self): return 'Index #{}'.format(self.pk)
class LogIndex(models.Model): log = models.ForeignKey(Log, verbose_name=_('Log'), composition=True) content_type = models.ForeignKey('contenttypes.ContentType', verbose_name=_('Content Type')) object_id = models.IntegerField(verbose_name=_('Identifier'), search=True) class Meta: verbose_name = _('Index') verbose_name_plural = _('Indexes') list_display = 'content_type', 'object_id', 'get_tags' def __str__(self): return 'Index #{}'.format(self.pk) @meta('Alterações') def get_tags(self): data = [] date = datetime.datetime(self.log.date.year, self.log.date.month, self.log.date.day, self.log.date.hour, self.log.date.minute, self.log.date.second) qs = Log.objects.filter(content_type=self.content_type, user=self.log.user, date__startswith=date, object_id=self.object_id) for log in qs: for attr, old, new in json.loads(log.content): data.append('{} : {} >> {}'.format(attr, old, new)) return ' | '.join(data)
class Log(models.Model): ADD = 1 EDIT = 2 DELETE = 3 OPERATION_CHOICES = [[ADD, _('Add')], [EDIT, _('Edit')], [DELETE, _('Delete')]] content_type = models.ForeignKey(ContentType, verbose_name=_('Content Type'), filter=True) operation = models.IntegerField(verbose_name=_('Operation'), choices=OPERATION_CHOICES, filter=True) user = models.ForeignKey('admin.User', filter=True, verbose_name=_('User')) date = models.DateTimeField(verbose_name=_('Date/Time'), auto_now=True, filter=True) object_id = models.IntegerField(verbose_name=_('Identifier'), search=True) object_description = models.CharField(verbose_name=_('Object Description')) content = models.TextField(verbose_name=_('Content'), null=True, search=True) fieldsets = ( (_('General Data'), {'fields': ( ('content_type', 'operation'), ('user', 'date'), ('object_id', 'object_description'), 'get_tags')}), (_('Indexes'), {'relations': ('logindex_set',), 'condition': 'has_index'}), ) objects = models.Manager() class Meta: verbose_name = _('Log') verbose_name_plural = _('Logs') icon = 'fa-history' list_per_page = 25 list_display = 'content_type', 'object_id', 'operation', 'user', 'date', 'get_tags' order_by = '-date' def __str__(self): return 'Log #{}'.format(self.pk) def can_add(self): return False def can_edit(self): return False def can_delete(self): return False def has_index(self): return self.logindex_set.exists() def get_action_description(self): return (_('added'), _('edited'), _('deleted'))[self.operation - 1] def get_style(self): return ('success', 'info', 'danger')[self.operation - 1] def get_icon(self): return ('plus', 'pencil', 'trash-o')[self.operation - 1] @meta('Alterações') def get_tags(self): data = [] for attr, old, new in json.loads(self.content): data.append('{} : {} >> {}'.format(attr, old, new)) for log_index in self.logindex_set.all(): date = datetime.datetime( log_index.log.date.year, log_index.log.date.month, log_index.log.date.day, log_index.log.date.hour, log_index.log.date.minute, log_index.log.date.second ) qs = Log.objects.filter( content_type=log_index.content_type, user=log_index.log.user, date__startswith=date, object_id=log_index.object_id ) for log in qs: for attr, old, new in json.loads(log.content): data.append('{} : {} >> {}'.format(attr, old, new)) return ' | '.join(data) def create_indexes(self, instance): for log_index in get_metadata(instance.__class__, 'logging', (), iterable=True): index_object = getattr2(instance, log_index) if index_object: index_content_type = ContentType.objects.get_for_model(index_object.__class__) LogIndex.objects.create(log=self, content_type=index_content_type, object_id=index_object.pk)