Statistics
| Branch: | Tag: | Revision:

root / peers / models.py @ 2a13d365

History | View | Annotate | Download (1.7 kB)

1
from django.db import models
2
from utils.whois import *
3
from django.contrib.auth.models import User
4

    
5
# Create your models here.
6
class PeerRange(models.Model):
7
    network = models.CharField(max_length=128)
8
    def __unicode__(self):
9
        return self.network
10
    class Meta:
11
        db_table = u'peer_range'
12

    
13
# Create your models here.
14
class Peer(models.Model):
15
    peer_id = models.IntegerField(primary_key=True)
16
    peer_name = models.CharField(max_length=128)
17
    peer_as = models.IntegerField()
18
    peer_tag = models.CharField(max_length=64)
19
    domain_name = models.CharField(max_length=128, null=True, blank=True)
20
    networks = models.ManyToManyField(PeerRange, null=True, blank=True)
21

    
22
    def __unicode__(self):
23
        return self.peer_name
24
    class Meta:
25
        db_table = u'peer'
26
        
27
    def fill_networks(self):
28
        network_range = []
29
        peer_as = "AS%s" %self.peer_as
30
        network_range = whois(peer_as)
31
        if network_range:
32
            for network_item in network_range:
33
                range, created = PeerRange.objects.get_or_create(network=network_item.compressed)
34
                if not range.network in self.networks.all():
35
                    self.networks.add(range)
36
            self.save()
37

    
38
    def techc(self):
39
        try:
40
            techcmails = PeerTechc.objects.get(peer_id=self).emails
41
        except:
42
            techcmails = None
43
        return techcmails
44

    
45
class PeerTechc(models.Model):
46
    peer_id = models.OneToOneField(Peer, primary_key=True,
47
            db_column="peer_id", parent_link=True)
48
    emails = models.CharField(max_length=352, db_column="emails")
49

    
50
    def __unicode__(self):
51
        return "%s %s" %(self.peer_id.peer_name, self.emails)
52

    
53
    class Meta:
54
        db_table="v_tech_c_peers"