summaryrefslogtreecommitdiff
path: root/coip/apps/name/models.py
blob: ec8676924d948cc1de1270be5e0812bb51fa3088 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
'''
Created on Jun 24, 2010

@author: leifj
'''
from django.db import models
from django.contrib.auth.models import User
import re

class Attribute(models.Model):
    name = models.CharField(unique=True,max_length=255)
    description = models.TextField(blank=True)
    timecreated = models.DateTimeField(auto_now_add=True)
    lastupdated = models.DateTimeField(auto_now=True)
    
    def __unicode__(self):
        return self.name;

class Name(models.Model):
    '''
    A name-space/authorization/right/group/collaboration/thing
    '''
    type = models.ForeignKey(Attribute, blank=True, null=True,related_name='names')
    value = models.CharField(max_length=255)
    parent = models.ForeignKey('self', blank=True, null=True,related_name='children')
    partof = models.ForeignKey('self', blank=True, null=True,related_name='parts')
    acl = models.TextField(blank=True) # fully-qualified-name '#' rights
    description = models.TextField(blank=True)
    creator = models.ForeignKey(User)
    timecreated = models.DateTimeField(auto_now_add=True)
    lastupdated = models.DateTimeField(auto_now=True)
    
    def relative_name(self):
        if self.type:
            return "%s=%s" % (self.type.name,self.value)
        else:
            return self.value
    
    def __unicode__(self):
        n = self
        str = ""
        while n:
            sep = ""
            av = n.relative_name()
            
            if n.parent:
                if av.find("=") == -1:
                    sep = ':'
                else:
                    sep = ';'
                    
            str = sep+av+str
            n = n.parent
        
        return str
    
def walkto(root,nameparts):
    name = None
    for n in nameparts:
        (a,eq,v) = n.partition('=')
        if v:
            attribute = Attribute.objects.get(name=a)
            name = Name.objects.get(parent=root,type=attribute.id,value=v)
        else:
            name = Name.objects.get(parent=root,type=None,value=a)
    return name

def lookup(name):
    return walkto(None,nameparts=re.compile('[;:]').split(name))

def attribute(a):
    Attribute.objects.get_or_create(name=a)