def _create(self, **defaults): from models import Message, GroupedMessage URL_MAX_LENGTH = Message._meta.get_field_by_name('url')[0].max_length server_name = socket.gethostname() class_name = defaults.pop('class_name', None) data = defaults.pop('data', {}) or {} if defaults.get('url'): data['url'] = defaults['url'] defaults['url'] = defaults['url'][:URL_MAX_LENGTH] instance = Message( class_name=class_name, server_name=server_name, data=data, **defaults ) instance.checksum = construct_checksum(instance) if settings.THRASHING_TIMEOUT and settings.THRASHING_LIMIT: cache_key = 'dblog:%s:%s' % (instance.class_name, instance.checksum) added = cache.add(cache_key, 1, settings.THRASHING_TIMEOUT) if not added and cache.incr(cache_key) > settings.THRASHING_LIMIT: return try: instance.save() batch, created = GroupedMessage.objects.get_or_create( class_name = class_name, server_name = server_name, checksum = instance.checksum, defaults = defaults ) if not created: GroupedMessage.objects.filter(pk=batch.pk).update( times_seen=models.F('times_seen') + 1, status=0, last_seen=datetime.datetime.now(), ) except Exception, exc: try: logger.exception(u'Unable to process log entry: %s' % (exc,)) except Exception, exc: warnings.warn(u'Unable to process log entry: %s' % (exc,))
def save(self, *args, **kwargs): if not self.checksum: self.checksum = construct_checksum(self) super(Message, self).save(*args, **kwargs)