<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (C) 2011 <NAME>
#
# Author: <NAME> <<EMAIL>>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.db.models import permalink
from django.utils.translation import ugettext as _

from netadmin.permissions.models import ObjectPermission


class NetworkObject(models.Model):
    """
    Abstract model class for objects like host or network.
    Every object belongs to specified user
    """
    name = models.CharField(max_length=250)
    description = models.TextField(blank=True)
    user = models.ForeignKey(User, blank=False, null=False)
    
    def __unicode__(self):
        return self.name
    
    def short_description(self, word_limit=15, char_limit=150):
        words = self.description.split()
        if len(words) > word_limit:
            return '%s...' % ' '.join(words[:word_limit])
        elif len(self.description) > char_limit:
            return '%s...' % self.description[:char_limit]
        else:
            return self.description
    
    def sharing_users(self):
        ct = ContentType.objects.get_for_model(self.__class__)
        perms = ObjectPermission.objects.filter(content_type=ct,
                                                object_id=self.pk)
        return [(perm.user, perm.edit) for perm in perms]
    
    def events(self):
        return self.event_set.all().order_by('-timestamp')
    
    def latest_event(self):
        events = self.events().order_by('-timestamp')
        return events[0] if events else None
    
    def api_list(self):
        return {
            'id': self.pk,
            'name': self.name
        }
    
    class Meta:
        abstract = True

class Host(NetworkObject):
    """The single host in the network
    """
    ipv4 = models.IPAddressField(verbose_name=_("IPv4 address"))
    ipv6 = models.CharField(max_length=39, verbose_name=_("IPv6 address"), 
                            blank=True)
    
    def __unicode__(self):
        return "Host '%s'" % self.name
                            
    @permalink
    def get_absolute_url(self):
        return ('host_detail', [str(self.pk)])
    <commit_msg>Allowed IPv6 field in Host model class to be null<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (C) 2011 <NAME>
#
# Author: <NAME> <<EMAIL>>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.db.models import permalink
from django.utils.translation import ugettext as _

from netadmin.permissions.models import ObjectPermission


class NetworkObject(models.Model):
    """
    Abstract model class for objects like host or network.
    Every object belongs to specified user
    """
    name = models.CharField(max_length=250)
    description = models.TextField(blank=True)
    user = models.ForeignKey(User, blank=False, null=False)
    
    def __unicode__(self):
        return self.name
    
    def short_description(self, word_limit=15, char_limit=150):
        words = self.description.split()
        if len(words) > word_limit:
            return '%s...' % ' '.join(words[:word_limit])
        elif len(self.description) > char_limit:
            return '%s...' % self.description[:char_limit]
        else:
            return self.description
    
    def sharing_users(self):
        ct = ContentType.objects.get_for_model(self.__class__)
        perms = ObjectPermission.objects.filter(content_type=ct,
                                                object_id=self.pk)
        return [(perm.user, perm.edit) for perm in perms]
    
    def events(self):
        return self.event_set.all().order_by('-timestamp')
    
    def latest_event(self):
        events = self.events().order_by('-timestamp')
        return events[0] if events else None
    
    def api_list(self):
        return {
            'id': self.pk,
            'name': self.name
        }
    
    class Meta:
        abstract = True

class Host(NetworkObject):
    """The single host in the network
    """
    ipv4 = models.IPAddressField(verbose_name=_("IPv4 address"))
    ipv6 = models.CharField(max_length=39, verbose_name=_("IPv6 address"), 
                            blank=True, null=True)
    
    def __unicode__(self):
        return "Host '%s'" % self.name
                            
    @permalink
    def get_absolute_url(self):
        return ('host_detail', [str(self.pk)])
    
{'fraction_non_alphanumeric': 0.09708410584864319, 'fraction_numerical': 0.007247598179673015, 'mean_word_length': 2.6697588126159553, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '213123', 'n_tokens_mistral': 1662, 'n_tokens_neox': 1625, 'n_words': 528}